Skip to content

Commit

Permalink
refactor: extract protection level parsing and changing (#566)
Browse files Browse the repository at this point in the history
In preparation for the changes requested in #488
  • Loading branch information
phm07 authored Oct 18, 2023
1 parent b11e223 commit 09f1a4d
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 324 deletions.
26 changes: 2 additions & 24 deletions internal/cmd/floatingip/disable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
},
}
66 changes: 45 additions & 21 deletions internal/cmd/floatingip/enable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
},
}
28 changes: 3 additions & 25 deletions internal/cmd/image/disable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
},
}
66 changes: 45 additions & 21 deletions internal/cmd/image/enable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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)
},
}
26 changes: 2 additions & 24 deletions internal/cmd/loadbalancer/disable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
},
}
Loading

0 comments on commit 09f1a4d

Please sign in to comment.