Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate hcapi.NetworkClient usages to hcapi2 #453

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 51 additions & 49 deletions internal/cmd/loadbalancer/attach_to_network.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
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 newAttachToNetworkCommand(cli *state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "attach-to-network [FLAGS] LOADBALANCER",
Short: "Attach a Load Balancer to a Network",
Args: cobra.ExactArgs(1),
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: cli.EnsureToken,
RunE: cli.Wrap(runAttachToNetwork),
}
var AttachToNetworkCommand = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "attach-to-network [FLAGS] LOADBALANCER",
Short: "Attach a Load Balancer to a Network",
Args: cobra.ExactArgs(1),
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.LoadBalancer().Names)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
}

cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)")
cmd.RegisterFlagCompletionFunc("network", cmpl.SuggestCandidatesF(cli.NetworkNames))
cmd.MarkFlagRequired("network")
cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)")
cmd.RegisterFlagCompletionFunc("network", cmpl.SuggestCandidatesF(client.Network().Names))
cmd.MarkFlagRequired("network")

cmd.Flags().IP("ip", nil, "IP address to assign to the Load Balancer (auto-assigned if omitted)")
cmd.Flags().IP("ip", nil, "IP address to assign to the Load Balancer (auto-assigned if omitted)")

return cmd
}

func runAttachToNetwork(cli *state.State, cmd *cobra.Command, args []string) error {
idOrName := args[0]
loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName)
if err != nil {
return err
}
if loadBalancer == nil {
return fmt.Errorf("Load Balancer not found: %s", idOrName)
}
return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
idOrName := args[0]
loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName)
if err != nil {
return err
}
if loadBalancer == nil {
return fmt.Errorf("Load Balancer not found: %s", idOrName)
}

networkIDOrName, _ := cmd.Flags().GetString("network")
network, _, err := cli.Client().Network.Get(cli.Context, networkIDOrName)
if err != nil {
return err
}
if network == nil {
return fmt.Errorf("network not found: %s", networkIDOrName)
}
networkIDOrName, _ := cmd.Flags().GetString("network")
network, _, err := client.Network().Get(ctx, networkIDOrName)
if err != nil {
return err
}
if network == nil {
return fmt.Errorf("network not found: %s", networkIDOrName)
}

ip, _ := cmd.Flags().GetIP("ip")
ip, _ := cmd.Flags().GetIP("ip")

opts := hcloud.LoadBalancerAttachToNetworkOpts{
Network: network,
IP: ip,
}
action, _, err := cli.Client().LoadBalancer.AttachToNetwork(cli.Context, loadBalancer, opts)
opts := hcloud.LoadBalancerAttachToNetworkOpts{
Network: network,
IP: ip,
}
action, _, err := client.LoadBalancer().AttachToNetwork(ctx, loadBalancer, opts)

if err != nil {
return err
}
if err != nil {
return err
}

if err := cli.ActionProgress(cli.Context, action); err != nil {
return err
}
if err := waiter.ActionProgress(ctx, action); err != nil {
return err
}

fmt.Printf("Load Balancer %d attached to network %d\n", loadBalancer.ID, network.ID)
return nil
fmt.Printf("Load Balancer %d attached to network %d\n", loadBalancer.ID, network.ID)
return nil
},
}
94 changes: 48 additions & 46 deletions internal/cmd/loadbalancer/detach_from_network.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,64 @@
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 newDetachFromNetworkCommand(cli *state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "detach-from-network [FLAGS] LOADBALANCER",
Short: "Detach a Load Balancer from a Network",
Args: cobra.ExactArgs(1),
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: cli.EnsureToken,
RunE: cli.Wrap(runDetachFromNetwork),
}
cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)")
cmd.RegisterFlagCompletionFunc("network", cmpl.SuggestCandidatesF(cli.NetworkNames))
cmd.MarkFlagRequired("network")
return cmd
}

func runDetachFromNetwork(cli *state.State, cmd *cobra.Command, args []string) error {
idOrName := args[0]
loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName)
if err != nil {
return err
}
if loadBalancer == nil {
return fmt.Errorf("Load Balancer not found: %s", idOrName)
}
networkIDOrName, _ := cmd.Flags().GetString("network")
network, _, err := cli.Client().Network.Get(cli.Context, networkIDOrName)
if err != nil {
return err
}
if network == nil {
return fmt.Errorf("network not found: %s", networkIDOrName)
}
var DetachFromNetworkCommand = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "detach-from-network [FLAGS] LOADBALANCER",
Short: "Detach a Load Balancer from a Network",
Args: cobra.ExactArgs(1),
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.LoadBalancer().Names)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)")
cmd.RegisterFlagCompletionFunc("network", cmpl.SuggestCandidatesF(client.Network().Names))
cmd.MarkFlagRequired("network")
return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
idOrName := args[0]
loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName)
if err != nil {
return err
}
if loadBalancer == nil {
return fmt.Errorf("Load Balancer not found: %s", idOrName)
}
networkIDOrName, _ := cmd.Flags().GetString("network")
network, _, err := client.Network().Get(ctx, networkIDOrName)
if err != nil {
return err
}
if network == nil {
return fmt.Errorf("network not found: %s", networkIDOrName)
}

opts := hcloud.LoadBalancerDetachFromNetworkOpts{
Network: network,
}
action, _, err := cli.Client().LoadBalancer.DetachFromNetwork(cli.Context, loadBalancer, opts)
if err != nil {
return err
}
opts := hcloud.LoadBalancerDetachFromNetworkOpts{
Network: network,
}
action, _, err := client.LoadBalancer().DetachFromNetwork(ctx, loadBalancer, opts)
if err != nil {
return err
}

if err := cli.ActionProgress(cli.Context, action); err != nil {
return err
}
if err := waiter.ActionProgress(ctx, action); err != nil {
return err
}

fmt.Printf("Load Balancer %d detached from Network %d\n", loadBalancer.ID, network.ID)
return nil
fmt.Printf("Load Balancer %d detached from Network %d\n", loadBalancer.ID, network.ID)
return nil
},
}
4 changes: 2 additions & 2 deletions internal/cmd/loadbalancer/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
newAddServiceCommand(cli),
newEnableProtectionCommand(cli),
newDisableProtectionCommand(cli),
newAttachToNetworkCommand(cli),
newDetachFromNetworkCommand(cli),
AttachToNetworkCommand.CobraCommand(cli.Context, client, cli, cli),
DetachFromNetworkCommand.CobraCommand(cli.Context, client, cli, cli),
newEnablePublicInterfaceCommand(cli),
newDisablePublicInterfaceCommand(cli),
newChangeTypeCommand(cli),
Expand Down
100 changes: 51 additions & 49 deletions internal/cmd/network/add_route.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
package network

import (
"context"
"fmt"
"net"

"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 newAddRouteCommand(cli *state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "add-route NETWORK FLAGS",
Short: "Add a route to a network",
Args: cobra.ExactArgs(1),
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: cli.EnsureToken,
RunE: cli.Wrap(runAddRoute),
}

cmd.Flags().IPNet("destination", net.IPNet{}, "Destination network or host (required)")
cmd.MarkFlagRequired("destination")

cmd.Flags().IP("gateway", net.IP{}, "Gateway IP address (required)")
cmd.MarkFlagRequired("gateway")

return cmd
}
var AddRouteCommand = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "add-route NETWORK FLAGS",
Short: "Add a route to a network",
Args: cobra.ExactArgs(1),
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Network().Names)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
}

cmd.Flags().IPNet("destination", net.IPNet{}, "Destination network or host (required)")
cmd.MarkFlagRequired("destination")

cmd.Flags().IP("gateway", net.IP{}, "Gateway IP address (required)")
cmd.MarkFlagRequired("gateway")

return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
gateway, _ := cmd.Flags().GetIP("gateway")
destination, _ := cmd.Flags().GetIPNet("destination")
idOrName := args[0]

network, _, err := client.Network().Get(ctx, idOrName)
if err != nil {
return err
}
if network == nil {
return fmt.Errorf("network not found: %s", idOrName)
}

opts := hcloud.NetworkAddRouteOpts{
Route: hcloud.NetworkRoute{
Gateway: gateway,
Destination: &destination,
},
}
action, _, err := client.Network().AddRoute(ctx, network, opts)
if err != nil {
return err
}
if err := waiter.ActionProgress(ctx, action); err != nil {
return err
}
fmt.Printf("Route added to network %d\n", network.ID)

func runAddRoute(cli *state.State, cmd *cobra.Command, args []string) error {
gateway, _ := cmd.Flags().GetIP("gateway")
destination, _ := cmd.Flags().GetIPNet("destination")
idOrName := args[0]

network, _, err := cli.Client().Network.Get(cli.Context, idOrName)
if err != nil {
return err
}
if network == nil {
return fmt.Errorf("network not found: %s", idOrName)
}

opts := hcloud.NetworkAddRouteOpts{
Route: hcloud.NetworkRoute{
Gateway: gateway,
Destination: &destination,
},
}
action, _, err := cli.Client().Network.AddRoute(cli.Context, network, opts)
if err != nil {
return err
}
if err := cli.ActionProgress(cli.Context, action); err != nil {
return err
}
fmt.Printf("Route added to network %d\n", network.ID)

return nil
return nil
},
}
Loading