From 09f1a4ddd41832fea8e02fe5bc1fb460e4fe8a3d Mon Sep 17 00:00:00 2001 From: pauhull Date: Wed, 18 Oct 2023 11:59:17 +0200 Subject: [PATCH] refactor: extract protection level parsing and changing (#566) In preparation for the changes requested in #488 --- internal/cmd/floatingip/disable_protection.go | 26 +------ internal/cmd/floatingip/enable_protection.go | 66 +++++++++++------ internal/cmd/image/disable_protection.go | 28 +------- internal/cmd/image/enable_protection.go | 66 +++++++++++------ .../cmd/loadbalancer/disable_protection.go | 26 +------ .../cmd/loadbalancer/enable_protection.go | 70 +++++++++++++------ internal/cmd/network/disable_protection.go | 26 +------ internal/cmd/network/enable_protection.go | 66 +++++++++++------ .../cmd/primaryip/disable_protecion_test.go | 2 +- internal/cmd/primaryip/disable_protection.go | 26 +------ .../cmd/primaryip/enable_protecion_test.go | 2 +- internal/cmd/primaryip/enable_protection.go | 64 +++++++++++------ internal/cmd/server/disable_protection.go | 28 +------- internal/cmd/server/enable_protection.go | 70 +++++++++++++------ internal/cmd/volume/disable_protection.go | 26 +------ internal/cmd/volume/enable_protection.go | 66 +++++++++++------ 16 files changed, 334 insertions(+), 324 deletions(-) diff --git a/internal/cmd/floatingip/disable_protection.go b/internal/cmd/floatingip/disable_protection.go index 7485d565..127ca9c3 100644 --- a/internal/cmd/floatingip/disable_protection.go +++ b/internal/cmd/floatingip/disable_protection.go @@ -3,13 +3,10 @@ package floatingip import ( "context" "fmt" - "strings" - "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/v2/hcloud" "github.com/spf13/cobra" ) @@ -37,30 +34,11 @@ var DisableProtectionCommand = base.Cmd{ return fmt.Errorf("Floating IP not found: %v", idOrName) } - var unknown []string - opts := hcloud.FloatingIPChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(false) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.FloatingIP().ChangeProtection(ctx, floatingIP, opts) + opts, err := getChangeProtectionOpts(false, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection disabled for Floating IP %d\n", floatingIP.ID) - return nil + return changeProtection(ctx, client, waiter, floatingIP, false, opts) }, } diff --git a/internal/cmd/floatingip/enable_protection.go b/internal/cmd/floatingip/enable_protection.go index 9dde6179..dc8b7c62 100644 --- a/internal/cmd/floatingip/enable_protection.go +++ b/internal/cmd/floatingip/enable_protection.go @@ -13,6 +13,49 @@ import ( "github.com/spf13/cobra" ) +func getChangeProtectionOpts(enable bool, flags []string) (hcloud.FloatingIPChangeProtectionOpts, error) { + + opts := hcloud.FloatingIPChangeProtectionOpts{} + + var unknown []string + for _, arg := range flags { + switch strings.ToLower(arg) { + case "delete": + opts.Delete = hcloud.Ptr(enable) + default: + unknown = append(unknown, arg) + } + } + if len(unknown) > 0 { + return opts, fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) + } + + return opts, nil +} + +func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, floatingIP *hcloud.FloatingIP, enable bool, opts hcloud.FloatingIPChangeProtectionOpts) error { + + if opts.Delete == nil { + return nil + } + + action, _, err := client.FloatingIP().ChangeProtection(ctx, floatingIP, opts) + if err != nil { + return err + } + + if err := waiter.ActionProgress(ctx, action); err != nil { + return err + } + + if enable { + fmt.Printf("Resource protection enabled for floating IP %d\n", floatingIP.ID) + } else { + fmt.Printf("Resource protection disabled for floating IP %d\n", floatingIP.ID) + } + return nil +} + var EnableProtectionCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ @@ -38,30 +81,11 @@ var EnableProtectionCommand = base.Cmd{ return fmt.Errorf("Floating IP not found: %v", idOrName) } - var unknown []string - opts := hcloud.FloatingIPChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(true) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.FloatingIP().ChangeProtection(ctx, floatingIP, opts) + opts, err := getChangeProtectionOpts(true, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection enabled for Floating IP %d\n", floatingIP.ID) - return nil + return changeProtection(ctx, client, waiter, floatingIP, true, opts) }, } diff --git a/internal/cmd/image/disable_protection.go b/internal/cmd/image/disable_protection.go index f8b144bb..e54b2cf5 100644 --- a/internal/cmd/image/disable_protection.go +++ b/internal/cmd/image/disable_protection.go @@ -3,16 +3,13 @@ package image import ( "context" "errors" - "fmt" - "strconv" - "strings" - "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/v2/hcloud" "github.com/spf13/cobra" + "strconv" ) var DisableProtectionCommand = base.Cmd{ @@ -36,30 +33,11 @@ var DisableProtectionCommand = base.Cmd{ } image := &hcloud.Image{ID: imageID} - var unknown []string - opts := hcloud.ImageChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(false) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.Image().ChangeProtection(ctx, image, opts) + opts, err := getChangeProtectionOpts(false, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection disabled for image %d\n", image.ID) - return nil + return changeProtection(ctx, client, waiter, image, false, opts) }, } diff --git a/internal/cmd/image/enable_protection.go b/internal/cmd/image/enable_protection.go index 36fe86ff..cfa50284 100644 --- a/internal/cmd/image/enable_protection.go +++ b/internal/cmd/image/enable_protection.go @@ -15,6 +15,49 @@ import ( "github.com/spf13/cobra" ) +func getChangeProtectionOpts(enable bool, flags []string) (hcloud.ImageChangeProtectionOpts, error) { + + opts := hcloud.ImageChangeProtectionOpts{} + + var unknown []string + for _, arg := range flags { + switch strings.ToLower(arg) { + case "delete": + opts.Delete = hcloud.Ptr(enable) + default: + unknown = append(unknown, arg) + } + } + if len(unknown) > 0 { + return opts, fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) + } + + return opts, nil +} + +func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, image *hcloud.Image, enable bool, opts hcloud.ImageChangeProtectionOpts) error { + + if opts.Delete == nil { + return nil + } + + action, _, err := client.Image().ChangeProtection(ctx, image, opts) + if err != nil { + return err + } + + if err := waiter.ActionProgress(ctx, action); err != nil { + return err + } + + if enable { + fmt.Printf("Resource protection enabled for image %d\n", image.ID) + } else { + fmt.Printf("Resource protection disabled for image %d\n", image.ID) + } + return nil +} + var EnableProtectionCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { @@ -37,30 +80,11 @@ var EnableProtectionCommand = base.Cmd{ } image := &hcloud.Image{ID: imageID} - var unknown []string - opts := hcloud.ImageChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(true) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.Image().ChangeProtection(ctx, image, opts) + opts, err := getChangeProtectionOpts(true, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection enabled for image %d\n", image.ID) - return nil + return changeProtection(ctx, client, waiter, image, true, opts) }, } diff --git a/internal/cmd/loadbalancer/disable_protection.go b/internal/cmd/loadbalancer/disable_protection.go index 8566d5b3..f41284b3 100644 --- a/internal/cmd/loadbalancer/disable_protection.go +++ b/internal/cmd/loadbalancer/disable_protection.go @@ -3,13 +3,10 @@ package loadbalancer import ( "context" "fmt" - "strings" - "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/v2/hcloud" "github.com/spf13/cobra" ) @@ -37,30 +34,11 @@ var DisableProtectionCommand = base.Cmd{ return fmt.Errorf("Load Balancer not found: %s", idOrName) } - var unknown []string - opts := hcloud.LoadBalancerChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(false) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.LoadBalancer().ChangeProtection(ctx, loadBalancer, opts) + opts, err := getChangeProtectionOpts(false, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection disabled for Load Balancer %d\n", loadBalancer.ID) - return nil + return changeProtection(ctx, client, waiter, loadBalancer, false, opts) }, } diff --git a/internal/cmd/loadbalancer/enable_protection.go b/internal/cmd/loadbalancer/enable_protection.go index 478e851f..4e9fedbe 100644 --- a/internal/cmd/loadbalancer/enable_protection.go +++ b/internal/cmd/loadbalancer/enable_protection.go @@ -13,6 +13,49 @@ import ( "github.com/spf13/cobra" ) +func getChangeProtectionOpts(enable bool, flags []string) (hcloud.LoadBalancerChangeProtectionOpts, error) { + + opts := hcloud.LoadBalancerChangeProtectionOpts{} + + var unknown []string + for _, arg := range flags { + switch strings.ToLower(arg) { + case "delete": + opts.Delete = hcloud.Ptr(enable) + default: + unknown = append(unknown, arg) + } + } + if len(unknown) > 0 { + return opts, fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) + } + + return opts, nil +} + +func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, loadBalancer *hcloud.LoadBalancer, enable bool, opts hcloud.LoadBalancerChangeProtectionOpts) error { + + if opts.Delete == nil { + return nil + } + + action, _, err := client.LoadBalancer().ChangeProtection(ctx, loadBalancer, opts) + if err != nil { + return err + } + + if err := waiter.ActionProgress(ctx, action); err != nil { + return err + } + + if enable { + fmt.Printf("Resource protection enabled for Load Balancer %d\n", loadBalancer.ID) + } else { + fmt.Printf("Resource protection disabled for Load Balancer %d\n", loadBalancer.ID) + } + return nil +} + var EnableProtectionCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ @@ -29,38 +72,19 @@ var EnableProtectionCommand = base.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) + loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) if err != nil { return err } - if LoadBalancer == nil { + if loadBalancer == nil { return fmt.Errorf("Load Balancer not found: %s", idOrName) } - var unknown []string - opts := hcloud.LoadBalancerChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(true) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.LoadBalancer().ChangeProtection(ctx, LoadBalancer, opts) + opts, err := getChangeProtectionOpts(true, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection enabled Load Balancer %d\n", LoadBalancer.ID) - return nil + return changeProtection(ctx, client, waiter, loadBalancer, true, opts) }, } diff --git a/internal/cmd/network/disable_protection.go b/internal/cmd/network/disable_protection.go index 0da4201e..26c5717f 100644 --- a/internal/cmd/network/disable_protection.go +++ b/internal/cmd/network/disable_protection.go @@ -3,13 +3,10 @@ package network import ( "context" "fmt" - "strings" - "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/v2/hcloud" "github.com/spf13/cobra" ) @@ -37,30 +34,11 @@ var DisableProtectionCommand = base.Cmd{ return fmt.Errorf("network not found: %s", idOrName) } - var unknown []string - opts := hcloud.NetworkChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(false) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.Network().ChangeProtection(ctx, network, opts) + opts, err := getChangeProtectionOpts(false, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection disabled for network %d\n", network.ID) - return nil + return changeProtection(ctx, client, waiter, network, false, opts) }, } diff --git a/internal/cmd/network/enable_protection.go b/internal/cmd/network/enable_protection.go index 13be3f9d..50aba125 100644 --- a/internal/cmd/network/enable_protection.go +++ b/internal/cmd/network/enable_protection.go @@ -13,6 +13,49 @@ import ( "github.com/spf13/cobra" ) +func getChangeProtectionOpts(enable bool, flags []string) (hcloud.NetworkChangeProtectionOpts, error) { + + opts := hcloud.NetworkChangeProtectionOpts{} + + var unknown []string + for _, arg := range flags { + switch strings.ToLower(arg) { + case "delete": + opts.Delete = hcloud.Ptr(enable) + default: + unknown = append(unknown, arg) + } + } + if len(unknown) > 0 { + return opts, fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) + } + + return opts, nil +} + +func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, network *hcloud.Network, enable bool, opts hcloud.NetworkChangeProtectionOpts) error { + + if opts.Delete == nil { + return nil + } + + action, _, err := client.Network().ChangeProtection(ctx, network, opts) + if err != nil { + return err + } + + if err := waiter.ActionProgress(ctx, action); err != nil { + return err + } + + if enable { + fmt.Printf("Resource protection enabled for network %d\n", network.ID) + } else { + fmt.Printf("Resource protection disabled for network %d\n", network.ID) + } + return nil +} + var EnableProtectionCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ @@ -37,30 +80,11 @@ var EnableProtectionCommand = base.Cmd{ return fmt.Errorf("network not found: %s", idOrName) } - var unknown []string - opts := hcloud.NetworkChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(true) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.Network().ChangeProtection(ctx, network, opts) + opts, err := getChangeProtectionOpts(true, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection enabled for network %d\n", network.ID) - return nil + return changeProtection(ctx, client, waiter, network, true, opts) }, } diff --git a/internal/cmd/primaryip/disable_protecion_test.go b/internal/cmd/primaryip/disable_protecion_test.go index 9e8df143..5ca38bf2 100644 --- a/internal/cmd/primaryip/disable_protecion_test.go +++ b/internal/cmd/primaryip/disable_protecion_test.go @@ -45,7 +45,7 @@ func TestEnable(t *testing.T) { fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), action).Return(nil) out, err := fx.Run(cmd, []string{"13"}) - expOut := "Primary IP 13 protection disabled" + expOut := "Resource protection disabled for primary IP 13\n" assert.NoError(t, err) assert.Equal(t, expOut, out) diff --git a/internal/cmd/primaryip/disable_protection.go b/internal/cmd/primaryip/disable_protection.go index 1faee7f8..b7c06f4f 100644 --- a/internal/cmd/primaryip/disable_protection.go +++ b/internal/cmd/primaryip/disable_protection.go @@ -3,14 +3,11 @@ package primaryip import ( "context" "fmt" - "strings" - "github.com/hetznercloud/cli/internal/cmd/cmpl" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" - "github.com/hetznercloud/hcloud-go/v2/hcloud" "github.com/spf13/cobra" ) @@ -45,30 +42,11 @@ var DisableProtectionCmd = base.Cmd{ args = append(args, "delete") } - var unknown []string - opts := hcloud.PrimaryIPChangeProtectionOpts{ID: primaryIP.ID} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = false - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.PrimaryIP().ChangeProtection(ctx, opts) + opts, err := getChangeProtectionOpts(false, args[1:]) if err != nil { return err } - if err := actionWaiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Primary IP %d protection disabled", opts.ID) - return nil + return changeProtection(ctx, client, actionWaiter, primaryIP, false, opts) }, } diff --git a/internal/cmd/primaryip/enable_protecion_test.go b/internal/cmd/primaryip/enable_protecion_test.go index 973ec9e9..21ce887e 100644 --- a/internal/cmd/primaryip/enable_protecion_test.go +++ b/internal/cmd/primaryip/enable_protecion_test.go @@ -45,7 +45,7 @@ func TestEnableProtection(t *testing.T) { fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), action).Return(nil) out, err := fx.Run(cmd, []string{"13"}) - expOut := "Primary IP 13 protection enabled" + expOut := "Resource protection enabled for primary IP 13\n" assert.NoError(t, err) assert.Equal(t, expOut, out) diff --git a/internal/cmd/primaryip/enable_protection.go b/internal/cmd/primaryip/enable_protection.go index deb0b10c..422a5600 100644 --- a/internal/cmd/primaryip/enable_protection.go +++ b/internal/cmd/primaryip/enable_protection.go @@ -14,6 +14,47 @@ import ( "github.com/spf13/cobra" ) +func getChangeProtectionOpts(enable bool, flags []string) (hcloud.PrimaryIPChangeProtectionOpts, error) { + + opts := hcloud.PrimaryIPChangeProtectionOpts{} + + var unknown []string + for _, arg := range flags { + switch strings.ToLower(arg) { + case "delete": + opts.Delete = enable + default: + unknown = append(unknown, arg) + } + } + if len(unknown) > 0 { + return opts, fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) + } + + return opts, nil +} + +func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, primaryIp *hcloud.PrimaryIP, enable bool, opts hcloud.PrimaryIPChangeProtectionOpts) error { + + opts.ID = primaryIp.ID + + action, _, err := client.PrimaryIP().ChangeProtection(ctx, opts) + if err != nil { + return err + } + + if err := waiter.ActionProgress(ctx, action); err != nil { + return err + } + + if enable { + fmt.Printf("Resource protection enabled for primary IP %d\n", opts.ID) + } else { + fmt.Printf("Resource protection disabled for primary IP %d\n", opts.ID) + } + return nil +} + var EnableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ @@ -45,30 +86,11 @@ var EnableProtectionCmd = base.Cmd{ args = append(args, "delete") } - var unknown []string - opts := hcloud.PrimaryIPChangeProtectionOpts{ID: primaryIP.ID} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = true - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.PrimaryIP().ChangeProtection(ctx, opts) + opts, err := getChangeProtectionOpts(true, args[1:]) if err != nil { return err } - if err := actionWaiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Primary IP %d protection enabled", opts.ID) - return nil + return changeProtection(ctx, client, actionWaiter, primaryIP, true, opts) }, } diff --git a/internal/cmd/server/disable_protection.go b/internal/cmd/server/disable_protection.go index c737c49d..5fa950e1 100644 --- a/internal/cmd/server/disable_protection.go +++ b/internal/cmd/server/disable_protection.go @@ -3,13 +3,10 @@ package server import ( "context" "fmt" - "strings" - "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/v2/hcloud" "github.com/spf13/cobra" ) @@ -37,32 +34,11 @@ var DisableProtectionCommand = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - var unknown []string - opts := hcloud.ServerChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(false) - case "rebuild": - opts.Rebuild = hcloud.Bool(false) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.Server().ChangeProtection(ctx, server, opts) + opts, err := getChangeProtectionOpts(false, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection disabled for server %d\n", server.ID) - return nil + return changeProtection(ctx, client, waiter, server, false, opts) }, } diff --git a/internal/cmd/server/enable_protection.go b/internal/cmd/server/enable_protection.go index 7353e91a..3aec14a0 100644 --- a/internal/cmd/server/enable_protection.go +++ b/internal/cmd/server/enable_protection.go @@ -13,6 +13,51 @@ import ( "github.com/spf13/cobra" ) +func getChangeProtectionOpts(enable bool, flags []string) (hcloud.ServerChangeProtectionOpts, error) { + + opts := hcloud.ServerChangeProtectionOpts{} + + var unknown []string + for _, arg := range flags { + switch strings.ToLower(arg) { + case "delete": + opts.Delete = hcloud.Ptr(enable) + case "rebuild": + opts.Rebuild = hcloud.Ptr(enable) + default: + unknown = append(unknown, arg) + } + } + if len(unknown) > 0 { + return opts, fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) + } + + return opts, nil +} + +func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, server *hcloud.Server, enable bool, opts hcloud.ServerChangeProtectionOpts) error { + + if opts.Delete == nil && opts.Rebuild == nil { + return nil + } + + action, _, err := client.Server().ChangeProtection(ctx, server, opts) + if err != nil { + return err + } + + if err := waiter.ActionProgress(ctx, action); err != nil { + return err + } + + if enable { + fmt.Printf("Resource protection enabled for server %d\n", server.ID) + } else { + fmt.Printf("Resource protection disabled for server %d\n", server.ID) + } + return nil +} + var EnableProtectionCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ @@ -37,32 +82,11 @@ var EnableProtectionCommand = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - var unknown []string - opts := hcloud.ServerChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(true) - case "rebuild": - opts.Rebuild = hcloud.Bool(true) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.Server().ChangeProtection(ctx, server, opts) + opts, err := getChangeProtectionOpts(true, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection enabled for server %d\n", server.ID) - return nil + return changeProtection(ctx, client, waiter, server, true, opts) }, } diff --git a/internal/cmd/volume/disable_protection.go b/internal/cmd/volume/disable_protection.go index 4105d18f..9a75945c 100644 --- a/internal/cmd/volume/disable_protection.go +++ b/internal/cmd/volume/disable_protection.go @@ -3,13 +3,10 @@ package volume import ( "context" "fmt" - "strings" - "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/v2/hcloud" "github.com/spf13/cobra" ) @@ -36,30 +33,11 @@ var DisableProtectionCommand = base.Cmd{ return fmt.Errorf("volume not found: %s", args[0]) } - var unknown []string - opts := hcloud.VolumeChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(false) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.Volume().ChangeProtection(ctx, volume, opts) + opts, err := getChangeProtectionOpts(false, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection disabled for volume %d\n", volume.ID) - return nil + return changeProtection(ctx, client, waiter, volume, false, opts) }, } diff --git a/internal/cmd/volume/enable_protection.go b/internal/cmd/volume/enable_protection.go index 93224515..4d7daf35 100644 --- a/internal/cmd/volume/enable_protection.go +++ b/internal/cmd/volume/enable_protection.go @@ -13,6 +13,49 @@ import ( "github.com/spf13/cobra" ) +func getChangeProtectionOpts(enable bool, flags []string) (hcloud.VolumeChangeProtectionOpts, error) { + + opts := hcloud.VolumeChangeProtectionOpts{} + + var unknown []string + for _, arg := range flags { + switch strings.ToLower(arg) { + case "delete": + opts.Delete = hcloud.Ptr(enable) + default: + unknown = append(unknown, arg) + } + } + if len(unknown) > 0 { + return opts, fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) + } + + return opts, nil +} + +func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, volume *hcloud.Volume, enable bool, opts hcloud.VolumeChangeProtectionOpts) error { + + if opts.Delete == nil { + return nil + } + + action, _, err := client.Volume().ChangeProtection(ctx, volume, opts) + if err != nil { + return err + } + + if err := waiter.ActionProgress(ctx, action); err != nil { + return err + } + + if enable { + fmt.Printf("Resource protection enabled for volume %d\n", volume.ID) + } else { + fmt.Printf("Resource protection disabled for volume %d\n", volume.ID) + } + return nil +} + var EnableProtectionCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ @@ -36,30 +79,11 @@ var EnableProtectionCommand = base.Cmd{ return fmt.Errorf("volume not found: %s", args[0]) } - var unknown []string - opts := hcloud.VolumeChangeProtectionOpts{} - for _, arg := range args[1:] { - switch strings.ToLower(arg) { - case "delete": - opts.Delete = hcloud.Bool(true) - default: - unknown = append(unknown, arg) - } - } - if len(unknown) > 0 { - return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) - } - - action, _, err := client.Volume().ChangeProtection(ctx, volume, opts) + opts, err := getChangeProtectionOpts(true, args[1:]) if err != nil { return err } - if err := waiter.ActionProgress(ctx, action); err != nil { - return err - } - - fmt.Printf("Resource protection enabled for volume %d\n", volume.ID) - return nil + return changeProtection(ctx, client, waiter, volume, true, opts) }, }