Skip to content

Commit

Permalink
fix create command YAML output
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Dec 19, 2023
1 parent ee4c51e commit e0abef5
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 73 deletions.
22 changes: 5 additions & 17 deletions internal/cmd/base/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package base

import (
"context"
"encoding/json"
"io"
"os"

"github.com/spf13/cobra"
Expand All @@ -12,14 +10,15 @@ import (
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

// CreateCmd allows defining commands for resource creation
type CreateCmd struct {
BaseCobraCommand func(hcapi2.Client) *cobra.Command
Run func(context.Context, hcapi2.Client, state.ActionWaiter, *cobra.Command, []string) (*hcloud.Response, any, error)
PrintResource func(context.Context, hcapi2.Client, *cobra.Command, any)
// Run is the function that will be called when the command is executed.
// It should return the created resource, the schema of the resource and an error.
Run func(context.Context, hcapi2.Client, state.ActionWaiter, *cobra.Command, []string) (any, any, error)
PrintResource func(context.Context, hcapi2.Client, *cobra.Command, any)
}

// CobraCommand creates a command that can be registered with cobra.
Expand Down Expand Up @@ -53,23 +52,12 @@ func (cc *CreateCmd) CobraCommand(
cmd.SetOut(os.Stdout)
}

response, resource, err := cc.Run(ctx, client, actionWaiter, cmd, args)
resource, schema, err := cc.Run(ctx, client, actionWaiter, cmd, args)
if err != nil {
return err
}

if isSchema {
bytes, _ := io.ReadAll(response.Body)

var schema map[string]any
if err := json.Unmarshal(bytes, &schema); err != nil {
return err
}

delete(schema, "action")
delete(schema, "actions")
delete(schema, "next_actions")

if outputFlags.IsSet("json") {
return util.DescribeJSON(schema)
} else {
Expand Down
28 changes: 17 additions & 11 deletions internal/cmd/certificate/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,32 @@ var CreateCmd = base.CreateCmd{

return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, strings []string) (*hcloud.Response, any, error) {
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, strings []string) (any, any, error) {
certType, err := cmd.Flags().GetString("type")
if err != nil {
return nil, nil, err
}
switch hcloud.CertificateType(certType) {
case hcloud.CertificateTypeManaged:
response, err := createManaged(ctx, client, waiter, cmd)
return response, nil, err
cert, err := createManaged(ctx, client, waiter, cmd)
if err != nil {
return nil, nil, err
}
return cert, hcloud.SchemaFromCertificate(cert), err
default: // Uploaded
response, err := createUploaded(ctx, client, cmd)
return response, nil, err
cert, err := createUploaded(ctx, client, cmd)
if err != nil {
return nil, nil, err
}
return cert, hcloud.SchemaFromCertificate(cert), err
}
},
PrintResource: func(_ context.Context, _ hcapi2.Client, _ *cobra.Command, _ any) {
// no-op
},
}

func createUploaded(ctx context.Context, client hcapi2.Client, cmd *cobra.Command) (*hcloud.Response, error) {
func createUploaded(ctx context.Context, client hcapi2.Client, cmd *cobra.Command) (*hcloud.Certificate, error) {
var (
name string
certFile, keyFile string
Expand Down Expand Up @@ -96,15 +102,15 @@ func createUploaded(ctx context.Context, client hcapi2.Client, cmd *cobra.Comman
Certificate: string(certPEM),
PrivateKey: string(keyPEM),
}
cert, response, err := client.Certificate().Create(ctx, createOpts)
cert, _, err = client.Certificate().Create(ctx, createOpts)
if err != nil {
return nil, err
}
cmd.Printf("Certificate %d created\n", cert.ID)
return response, nil
return cert, nil
}

func createManaged(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command) (*hcloud.Response, error) {
func createManaged(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command) (*hcloud.Certificate, error) {
var (
name string
domains []string
Expand All @@ -127,13 +133,13 @@ func createManaged(ctx context.Context, client hcapi2.Client, waiter state.Actio
Type: hcloud.CertificateTypeManaged,
DomainNames: domains,
}
res, response, err := client.Certificate().CreateCertificate(ctx, createOpts)
res, _, err = client.Certificate().CreateCertificate(ctx, createOpts)
if err != nil {
return nil, err
}
if err := waiter.ActionProgress(ctx, res.Action); err != nil {
return nil, err
}
cmd.Printf("Certificate %d created\n", res.Certificate.ID)
return response, nil
return res.Certificate, nil
}
10 changes: 5 additions & 5 deletions internal/cmd/firewall/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var CreateCmd = base.CreateCmd{
cmd.Flags().String("rules-file", "", "JSON file containing your routes (use - to read from stdin). The structure of the file needs to be the same as within the API: https://docs.hetzner.cloud/#firewalls-get-a-firewall ")
return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, strings []string) (*hcloud.Response, any, error) {
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, strings []string) (any, any, error) {
name, _ := cmd.Flags().GetString("name")
labels, _ := cmd.Flags().GetStringToString("label")

Expand Down Expand Up @@ -78,17 +78,17 @@ var CreateCmd = base.CreateCmd{
}
}

result, response, err := client.Firewall().Create(ctx, opts)
res, _, err := client.Firewall().Create(ctx, opts)
if err != nil {
return nil, nil, err
}

if err := waiter.WaitForActions(ctx, result.Actions); err != nil {
if err := waiter.WaitForActions(ctx, res.Actions); err != nil {
return nil, nil, err
}

cmd.Printf("Firewall %d created\n", result.Firewall.ID)
cmd.Printf("Firewall %d created\n", res.Firewall.ID)

return response, nil, err
return res.Firewall, hcloud.SchemaFromFirewall(res.Firewall), err
},
}
14 changes: 7 additions & 7 deletions internal/cmd/floatingip/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var CreateCmd = base.CreateCmd{

return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (*hcloud.Response, any, error) {
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) {
typ, _ := cmd.Flags().GetString("type")
if typ == "" {
return nil, nil, errors.New("type is required")
Expand Down Expand Up @@ -89,24 +89,24 @@ var CreateCmd = base.CreateCmd{
createOpts.Server = server
}

result, response, err := client.FloatingIP().Create(ctx, createOpts)
res, _, err := client.FloatingIP().Create(ctx, createOpts)
if err != nil {
return nil, nil, err
}

if result.Action != nil {
if err := waiter.ActionProgress(ctx, result.Action); err != nil {
if res.Action != nil {
if err := waiter.ActionProgress(ctx, res.Action); err != nil {
return nil, nil, err
}
}

cmd.Printf("Floating IP %d created\n", result.FloatingIP.ID)
cmd.Printf("Floating IP %d created\n", res.FloatingIP.ID)

if err := changeProtection(ctx, client, waiter, cmd, result.FloatingIP, true, protectionOps); err != nil {
if err := changeProtection(ctx, client, waiter, cmd, res.FloatingIP, true, protectionOps); err != nil {
return nil, nil, err
}

return response, result.FloatingIP, nil
return res.FloatingIP, hcloud.SchemaFromFloatingIP(res.FloatingIP), nil
},

PrintResource: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource any) {
Expand Down
10 changes: 5 additions & 5 deletions internal/cmd/loadbalancer/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var CreateCmd = base.CreateCmd{

return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (*hcloud.Response, any, error) {
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) {
name, _ := cmd.Flags().GetString("name")
serverType, _ := cmd.Flags().GetString("type")
algorithmType, _ := cmd.Flags().GetString("algorithm-type")
Expand Down Expand Up @@ -77,15 +77,15 @@ var CreateCmd = base.CreateCmd{
if location != "" {
createOpts.Location = &hcloud.Location{Name: location}
}
result, response, err := client.LoadBalancer().Create(ctx, createOpts)
res, _, err := client.LoadBalancer().Create(ctx, createOpts)
if err != nil {
return nil, nil, err
}

if err := waiter.ActionProgress(ctx, result.Action); err != nil {
if err := waiter.ActionProgress(ctx, res.Action); err != nil {
return nil, nil, err
}
loadBalancer, _, err := client.LoadBalancer().GetByID(ctx, result.LoadBalancer.ID)
loadBalancer, _, err := client.LoadBalancer().GetByID(ctx, res.LoadBalancer.ID)
if err != nil {
return nil, nil, err
}
Expand All @@ -95,7 +95,7 @@ var CreateCmd = base.CreateCmd{
return nil, nil, err
}

return response, loadBalancer, nil
return res.LoadBalancer, hcloud.SchemaFromLoadBalancer(res.LoadBalancer), nil
},

PrintResource: func(_ context.Context, _ hcapi2.Client, cmd *cobra.Command, resource any) {
Expand Down
10 changes: 7 additions & 3 deletions internal/cmd/network/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var CreateCmd = base.CreateCmd{
cmd.RegisterFlagCompletionFunc("enable-protection", cmpl.SuggestCandidates("delete"))
return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (*hcloud.Response, any, error) {
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) {
name, _ := cmd.Flags().GetString("name")
ipRange, _ := cmd.Flags().GetIPNet("ip-range")
labels, _ := cmd.Flags().GetStringToString("label")
Expand All @@ -54,14 +54,18 @@ var CreateCmd = base.CreateCmd{
ExposeRoutesToVSwitch: exposeRoutesToVSwitch,
}

network, response, err := client.Network().Create(ctx, createOpts)
network, _, err := client.Network().Create(ctx, createOpts)
if err != nil {
return nil, nil, err
}

cmd.Printf("Network %d created\n", network.ID)

return response, nil, changeProtection(ctx, client, waiter, cmd, network, true, protectionOpts)
if err := changeProtection(ctx, client, waiter, cmd, network, true, protectionOpts); err != nil {
return nil, nil, err
}

return network, hcloud.SchemaFromNetwork(network), nil
},
PrintResource: func(_ context.Context, _ hcapi2.Client, _ *cobra.Command, _ any) {
// no-op
Expand Down
12 changes: 6 additions & 6 deletions internal/cmd/placementgroup/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var CreateCmd = base.CreateCmd{
cmd.MarkFlagRequired("type")
return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (*hcloud.Response, any, error) {
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) {
name, _ := cmd.Flags().GetString("name")
labels, _ := cmd.Flags().GetStringToString("label")
placementGroupType, _ := cmd.Flags().GetString("type")
Expand All @@ -37,20 +37,20 @@ var CreateCmd = base.CreateCmd{
Type: hcloud.PlacementGroupType(placementGroupType),
}

result, response, err := client.PlacementGroup().Create(ctx, opts)
res, _, err := client.PlacementGroup().Create(ctx, opts)
if err != nil {
return nil, nil, err
}

if result.Action != nil {
if err := waiter.ActionProgress(ctx, result.Action); err != nil {
if res.Action != nil {
if err := waiter.ActionProgress(ctx, res.Action); err != nil {
return nil, nil, err
}
}

cmd.Printf("Placement group %d created\n", result.PlacementGroup.ID)
cmd.Printf("Placement group %d created\n", res.PlacementGroup.ID)

return response, nil, nil
return res.PlacementGroup, hcloud.SchemaFromPlacementGroup(res.PlacementGroup), nil
},
PrintResource: func(_ context.Context, _ hcapi2.Client, _ *cobra.Command, _ any) {
// no-op
Expand Down
14 changes: 7 additions & 7 deletions internal/cmd/primaryip/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var CreateCmd = base.CreateCmd{

return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (*hcloud.Response, any, error) {
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) {
typ, _ := cmd.Flags().GetString("type")
name, _ := cmd.Flags().GetString("name")
assigneeID, _ := cmd.Flags().GetInt64("assignee-id")
Expand All @@ -62,26 +62,26 @@ var CreateCmd = base.CreateCmd{
createOpts.AssigneeID = &assigneeID
}

result, response, err := client.PrimaryIP().Create(ctx, createOpts)
res, _, err := client.PrimaryIP().Create(ctx, createOpts)
if err != nil {
return nil, nil, err
}

if result.Action != nil {
if err := waiter.ActionProgress(ctx, result.Action); err != nil {
if res.Action != nil {
if err := waiter.ActionProgress(ctx, res.Action); err != nil {
return nil, nil, err
}
}

cmd.Printf("Primary IP %d created\n", result.PrimaryIP.ID)
cmd.Printf("Primary IP %d created\n", res.PrimaryIP.ID)

if len(protection) > 0 {
if err := changeProtection(ctx, client, waiter, cmd, result.PrimaryIP, true, protectionOpts); err != nil {
if err := changeProtection(ctx, client, waiter, cmd, res.PrimaryIP, true, protectionOpts); err != nil {
return nil, nil, err
}
}

return response, result.PrimaryIP, nil
return res.PrimaryIP, hcloud.SchemaFromPrimaryIP(res.PrimaryIP), nil
},
PrintResource: func(_ context.Context, _ hcapi2.Client, cmd *cobra.Command, resource any) {
primaryIP := resource.(*hcloud.PrimaryIP)
Expand Down
13 changes: 10 additions & 3 deletions internal/cmd/server/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import (
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

type createResult struct {
Server *hcloud.Server
RootPassword string
}

type createResultSchema struct {
Server schema.Server `json:"server"`
RootPassword string `json:"root_password"`
}

// CreateCmd defines a command for creating a server.
var CreateCmd = base.CreateCmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
Expand Down Expand Up @@ -91,13 +97,13 @@ var CreateCmd = base.CreateCmd{
return cmd
},

Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) (*hcloud.Response, any, error) {
Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) {
createOpts, protectionOpts, err := createOptsFromFlags(ctx, client, cmd)
if err != nil {
return nil, nil, err
}

result, response, err := client.Server().Create(ctx, createOpts)
result, _, err := client.Server().Create(ctx, createOpts)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -134,7 +140,8 @@ var CreateCmd = base.CreateCmd{
cmd.Printf("Backups enabled for server %d\n", server.ID)
}

return response, createResult{Server: server, RootPassword: result.RootPassword}, nil
return createResult{Server: server, RootPassword: result.RootPassword},
createResultSchema{Server: hcloud.SchemaFromServer(server), RootPassword: result.RootPassword}, nil
},

PrintResource: func(_ context.Context, client hcapi2.Client, cmd *cobra.Command, resource any) {
Expand Down
Loading

0 comments on commit e0abef5

Please sign in to comment.