-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: replace hcapi.LocationClient usage with hcapi2 (#446)
- Loading branch information
Showing
13 changed files
with
302 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.