Skip to content

Commit

Permalink
feat(primary-ip): enable/disable-protection accept levels as arguments (
Browse files Browse the repository at this point in the history
#564)

This PR changes how enable/disable-protection works on primary IPs. You
can now pass the protection level as an optional argument. This is to
make it consistent with other enable/disable-protection commands.
  • Loading branch information
phm07 authored Oct 18, 2023
1 parent 0882560 commit b11e223
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
27 changes: 22 additions & 5 deletions internal/cmd/primaryip/disable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package primaryip
import (
"context"
"fmt"
"strings"

"github.com/hetznercloud/cli/internal/cmd/cmpl"

Expand All @@ -16,11 +17,12 @@ import (
var DisableProtectionCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "disable-protection PRIMARYIP",
Use: "disable-protection PRIMARYIP [PROTECTIONLEVEL...]",
Short: "Disable Protection for a Primary IP",
Args: cobra.ExactArgs(1),
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: cmpl.SuggestArgs(
cmpl.SuggestCandidatesF(client.PrimaryIP().Names),
cmpl.SuggestCandidates("delete"),
),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand All @@ -37,9 +39,24 @@ var DisableProtectionCmd = base.Cmd{
return fmt.Errorf("Primary IP not found: %v", idOrName)
}

opts := hcloud.PrimaryIPChangeProtectionOpts{
ID: primaryIP.ID,
Delete: false,
// This command used to have the "delete" protection level as the default.
// To avoid a breaking change, we now add it if no level is defined.
if len(args) < 2 {
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)
Expand Down
27 changes: 22 additions & 5 deletions internal/cmd/primaryip/enable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package primaryip
import (
"context"
"fmt"
"strings"

"github.com/hetznercloud/cli/internal/cmd/cmpl"

Expand All @@ -16,11 +17,12 @@ import (
var EnableProtectionCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "enable-protection PRIMARYIP",
Use: "enable-protection PRIMARYIP [PROTECTIONLEVEL...]",
Short: "Enable Protection for a Primary IP",
Args: cobra.ExactArgs(1),
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: cmpl.SuggestArgs(
cmpl.SuggestCandidatesF(client.PrimaryIP().Names),
cmpl.SuggestCandidates("delete"),
),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand All @@ -37,9 +39,24 @@ var EnableProtectionCmd = base.Cmd{
return fmt.Errorf("Primary IP not found: %v", idOrName)
}

opts := hcloud.PrimaryIPChangeProtectionOpts{
ID: primaryIP.ID,
Delete: true,
// This command used to have the "delete" protection level as the default.
// To avoid a breaking change, we now add it if no level is defined.
if len(args) < 2 {
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)
Expand Down

0 comments on commit b11e223

Please sign in to comment.