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 20, 2023
1 parent ee4c51e commit bf40c7c
Show file tree
Hide file tree
Showing 11 changed files with 118 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
30 changes: 19 additions & 11 deletions internal/cmd/certificate/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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"
)

var CreateCmd = base.CreateCmd{
Expand Down Expand Up @@ -41,26 +42,33 @@ 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
}
var cert *hcloud.Certificate
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)
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, struct {
Certificate schema.Certificate `json:"certificate"`
}{
Certificate: hcloud.SchemaFromCertificate(cert),
}, nil
},
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 +104,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 +135,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
}
14 changes: 9 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,21 @@ 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, struct {
Firewall schema.Firewall `json:"firewall"`
}{
Firewall: hcloud.SchemaFromFirewall(res.Firewall),
}, err
},
}
19 changes: 12 additions & 7 deletions internal/cmd/floatingip/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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"
)

var CreateCmd = base.CreateCmd{
Expand Down Expand Up @@ -44,7 +45,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 +90,28 @@ 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, struct {
FloatingIP schema.FloatingIP `json:"floating_ip"`
}{
FloatingIP: hcloud.SchemaFromFloatingIP(res.FloatingIP),
}, nil
},

PrintResource: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource any) {
Expand Down
15 changes: 10 additions & 5 deletions internal/cmd/loadbalancer/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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"
)

var CreateCmd = base.CreateCmd{
Expand Down Expand Up @@ -47,7 +48,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 +78,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 +96,11 @@ var CreateCmd = base.CreateCmd{
return nil, nil, err
}

return response, loadBalancer, nil
return res.LoadBalancer, struct {
LoadBalancer schema.LoadBalancer `json:"load_balancer"`
}{
LoadBalancer: hcloud.SchemaFromLoadBalancer(loadBalancer),
}, nil
},

PrintResource: func(_ context.Context, _ hcapi2.Client, cmd *cobra.Command, resource any) {
Expand Down
15 changes: 12 additions & 3 deletions internal/cmd/network/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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"
)

var CreateCmd = base.CreateCmd{
Expand All @@ -35,7 +36,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 +55,22 @@ 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, struct {
Network schema.Network `json:"network"`
}{
Network: hcloud.SchemaFromNetwork(network),
}, nil
},
PrintResource: func(_ context.Context, _ hcapi2.Client, _ *cobra.Command, _ any) {
// no-op
Expand Down
17 changes: 11 additions & 6 deletions internal/cmd/placementgroup/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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"
)

var CreateCmd = base.CreateCmd{
Expand All @@ -26,7 +27,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 +38,24 @@ 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, struct {
PlacementGroup schema.PlacementGroup `json:"placement_group"`
}{
PlacementGroup: hcloud.SchemaFromPlacementGroup(res.PlacementGroup),
}, nil
},
PrintResource: func(_ context.Context, _ hcapi2.Client, _ *cobra.Command, _ any) {
// no-op
Expand Down
Loading

0 comments on commit bf40c7c

Please sign in to comment.