Skip to content

Commit

Permalink
chore: replace hcapi.LocationClient usage with hcapi2 (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
samcday authored Feb 17, 2023
1 parent 5c39ae0 commit 2c5b4a7
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 336 deletions.
131 changes: 63 additions & 68 deletions internal/cmd/floatingip/create.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,91 @@
package floatingip

import (
"context"
"errors"
"fmt"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)

func newCreateCommand(cli *state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "create FLAGS",
Short: "Create a Floating IP",
Args: cobra.NoArgs,
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: util.ChainRunE(validateCreate, cli.EnsureToken),
RunE: cli.Wrap(runCreate),
}
cmd.Flags().String("type", "", "Type (ipv4 or ipv6) (required)")
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("ipv4", "ipv6"))
cmd.MarkFlagRequired("type")

cmd.Flags().String("description", "", "Description")
var CreateCommand = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "create FLAGS",
Short: "Create a Floating IP",
Args: cobra.NoArgs,
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
cmd.Flags().String("type", "", "Type (ipv4 or ipv6) (required)")
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("ipv4", "ipv6"))
cmd.MarkFlagRequired("type")

cmd.Flags().String("name", "", "Name")
cmd.Flags().String("description", "", "Description")

cmd.Flags().String("home-location", "", "Home location")
cmd.RegisterFlagCompletionFunc("home-location", cmpl.SuggestCandidatesF(cli.LocationNames))
cmd.Flags().String("name", "", "Name")

cmd.Flags().String("server", "", "Server to assign Floating IP to")
cmd.RegisterFlagCompletionFunc("server", cmpl.SuggestCandidatesF(cli.ServerNames))
cmd.Flags().String("home-location", "", "Home location")
cmd.RegisterFlagCompletionFunc("home-location", cmpl.SuggestCandidatesF(client.Location().Names))

cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)")
cmd.Flags().String("server", "", "Server to assign Floating IP to")
cmd.RegisterFlagCompletionFunc("server", cmpl.SuggestCandidatesF(client.Server().Names))

return cmd
}
cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)")

func validateCreate(cmd *cobra.Command, args []string) error {
typ, _ := cmd.Flags().GetString("type")
if typ == "" {
return errors.New("type is required")
}
return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
typ, _ := cmd.Flags().GetString("type")
if typ == "" {
return errors.New("type is required")
}

homeLocation, _ := cmd.Flags().GetString("home-location")
server, _ := cmd.Flags().GetString("server")
if homeLocation == "" && server == "" {
return errors.New("one of --home-location or --server is required")
}
homeLocation, _ := cmd.Flags().GetString("home-location")
server, _ := cmd.Flags().GetString("server")
if homeLocation == "" && server == "" {
return errors.New("one of --home-location or --server is required")
}

return nil
}
name, _ := cmd.Flags().GetString("name")
description, _ := cmd.Flags().GetString("description")
serverNameOrID, _ := cmd.Flags().GetString("server")
labels, _ := cmd.Flags().GetStringToString("label")

func runCreate(cli *state.State, cmd *cobra.Command, args []string) error {
typ, _ := cmd.Flags().GetString("type")
name, _ := cmd.Flags().GetString("name")
description, _ := cmd.Flags().GetString("description")
homeLocation, _ := cmd.Flags().GetString("home-location")
serverNameOrID, _ := cmd.Flags().GetString("server")
labels, _ := cmd.Flags().GetStringToString("label")
opts := hcloud.FloatingIPCreateOpts{
Type: hcloud.FloatingIPType(typ),
Description: &description,
Labels: labels,
}
if name != "" {
opts.Name = &name
}
if homeLocation != "" {
opts.HomeLocation = &hcloud.Location{Name: homeLocation}
}
if serverNameOrID != "" {
server, _, err := client.Server().Get(ctx, serverNameOrID)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("server not found: %s", serverNameOrID)
}
opts.Server = server
}

opts := hcloud.FloatingIPCreateOpts{
Type: hcloud.FloatingIPType(typ),
Description: &description,
Labels: labels,
}
if name != "" {
opts.Name = &name
}
if homeLocation != "" {
opts.HomeLocation = &hcloud.Location{Name: homeLocation}
}
if serverNameOrID != "" {
server, _, err := cli.Client().Server.Get(cli.Context, serverNameOrID)
result, _, err := client.FloatingIP().Create(ctx, opts)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("server not found: %s", serverNameOrID)
}
opts.Server = server
}

result, _, err := cli.Client().FloatingIP.Create(cli.Context, opts)
if err != nil {
return err
}

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

return nil
return nil
},
}
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
cmd.AddCommand(
updateCmd.CobraCommand(cli.Context, client, cli),
listCmd.CobraCommand(cli.Context, client, cli),
newCreateCommand(cli),
CreateCommand.CobraCommand(cli.Context, client, cli, cli),
describeCmd.CobraCommand(cli.Context, client, cli),
newAssignCommand(cli),
newUnassignCommand(cli),
Expand Down
134 changes: 68 additions & 66 deletions internal/cmd/loadbalancer/create.go
Original file line number Diff line number Diff line change
@@ -1,86 +1,88 @@
package loadbalancer

import (
"context"
"fmt"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)

func newCreateCommand(cli *state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "create [FLAGS]",
Short: "Create a Load Balancer",
Args: cobra.NoArgs,
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: cli.EnsureToken,
RunE: cli.Wrap(runCreate),
}
var CreateCommand = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "create [FLAGS]",
Short: "Create a Load Balancer",
Args: cobra.NoArgs,
TraverseChildren: true,
DisableFlagsInUseLine: true,
}

cmd.Flags().String("name", "", "Load Balancer name (required)")
cmd.MarkFlagRequired("name")
cmd.Flags().String("name", "", "Load Balancer name (required)")
cmd.MarkFlagRequired("name")

cmd.Flags().String("type", "", "Load Balancer type (ID or name) (required)")
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidatesF(cli.LoadBalancerTypeNames))
cmd.MarkFlagRequired("type")
cmd.Flags().String("type", "", "Load Balancer type (ID or name) (required)")
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidatesF(client.LoadBalancerType().Names))
cmd.MarkFlagRequired("type")

cmd.Flags().String("algorithm-type", "", "Algorithm Type name (round_robin or least_connections)")
cmd.RegisterFlagCompletionFunc("algorithm-type", cmpl.SuggestCandidates(
string(hcloud.LoadBalancerAlgorithmTypeLeastConnections),
string(hcloud.LoadBalancerAlgorithmTypeRoundRobin),
))
cmd.Flags().String("location", "", "Location (ID or name)")
cmd.RegisterFlagCompletionFunc("location", cmpl.SuggestCandidatesF(cli.LocationNames))
cmd.Flags().String("algorithm-type", "", "Algorithm Type name (round_robin or least_connections)")
cmd.RegisterFlagCompletionFunc("algorithm-type", cmpl.SuggestCandidates(
string(hcloud.LoadBalancerAlgorithmTypeLeastConnections),
string(hcloud.LoadBalancerAlgorithmTypeRoundRobin),
))
cmd.Flags().String("location", "", "Location (ID or name)")
cmd.RegisterFlagCompletionFunc("location", cmpl.SuggestCandidatesF(client.Location().Names))

cmd.Flags().String("network-zone", "", "Network Zone")
cmd.RegisterFlagCompletionFunc("network-zone", cmpl.SuggestCandidatesF(cli.NetworkZoneNames))
cmd.Flags().String("network-zone", "", "Network Zone")
cmd.RegisterFlagCompletionFunc("network-zone", cmpl.SuggestCandidatesF(client.Location().NetworkZones))

cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)")
cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)")

return cmd
}

func runCreate(cli *state.State, cmd *cobra.Command, args []string) error {
name, _ := cmd.Flags().GetString("name")
serverType, _ := cmd.Flags().GetString("type")
algorithmType, _ := cmd.Flags().GetString("algorithm-type")
location, _ := cmd.Flags().GetString("location")
networkZone, _ := cmd.Flags().GetString("network-zone")
labels, _ := cmd.Flags().GetStringToString("label")
return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
name, _ := cmd.Flags().GetString("name")
serverType, _ := cmd.Flags().GetString("type")
algorithmType, _ := cmd.Flags().GetString("algorithm-type")
location, _ := cmd.Flags().GetString("location")
networkZone, _ := cmd.Flags().GetString("network-zone")
labels, _ := cmd.Flags().GetStringToString("label")

opts := hcloud.LoadBalancerCreateOpts{
Name: name,
LoadBalancerType: &hcloud.LoadBalancerType{
Name: serverType,
},
Labels: labels,
}
if algorithmType != "" {
opts.Algorithm = &hcloud.LoadBalancerAlgorithm{Type: hcloud.LoadBalancerAlgorithmType(algorithmType)}
}
if networkZone != "" {
opts.NetworkZone = hcloud.NetworkZone(networkZone)
}
if location != "" {
opts.Location = &hcloud.Location{Name: location}
}
result, _, err := cli.Client().LoadBalancer.Create(cli.Context, opts)
if err != nil {
return err
}
opts := hcloud.LoadBalancerCreateOpts{
Name: name,
LoadBalancerType: &hcloud.LoadBalancerType{
Name: serverType,
},
Labels: labels,
}
if algorithmType != "" {
opts.Algorithm = &hcloud.LoadBalancerAlgorithm{Type: hcloud.LoadBalancerAlgorithmType(algorithmType)}
}
if networkZone != "" {
opts.NetworkZone = hcloud.NetworkZone(networkZone)
}
if location != "" {
opts.Location = &hcloud.Location{Name: location}
}
result, _, err := client.LoadBalancer().Create(ctx, opts)
if err != nil {
return err
}

if err := cli.ActionProgress(cli.Context, result.Action); err != nil {
return err
}
loadBalancer, _, err := cli.Client().LoadBalancer.GetByID(cli.Context, result.LoadBalancer.ID)
if err != nil {
return err
}
fmt.Printf("LoadBalancer %d created\n", loadBalancer.ID)
fmt.Printf("IPv4: %s\n", loadBalancer.PublicNet.IPv4.IP.String())
fmt.Printf("IPv6: %s\n", loadBalancer.PublicNet.IPv6.IP.String())
return nil
if err := waiter.ActionProgress(ctx, result.Action); err != nil {
return err
}
loadBalancer, _, err := client.LoadBalancer().GetByID(ctx, result.LoadBalancer.ID)
if err != nil {
return err
}
fmt.Printf("LoadBalancer %d created\n", loadBalancer.ID)
fmt.Printf("IPv4: %s\n", loadBalancer.PublicNet.IPv4.IP.String())
fmt.Printf("IPv6: %s\n", loadBalancer.PublicNet.IPv6.IP.String())
return nil
},
}
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
DisableFlagsInUseLine: true,
}
cmd.AddCommand(
newCreateCommand(cli),
CreateCommand.CobraCommand(cli.Context, client, cli, cli),
ListCmd.CobraCommand(cli.Context, client, cli),
DescribeCmd.CobraCommand(cli.Context, client, cli),
deleteCmd.CobraCommand(cli.Context, client, cli),
Expand Down
Loading

0 comments on commit 2c5b4a7

Please sign in to comment.