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

fix(primary-ip): change protection commands do not allow protection levels #851

Merged
merged 1 commit into from
Aug 30, 2024
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
2 changes: 2 additions & 0 deletions internal/cmd/primaryip/disable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"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"
)
Expand All @@ -15,6 +16,7 @@ var DisableProtectionCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "disable-protection <primary-ip> [<protection-level>...]", // optional because of backwards compatibility
Args: util.ValidateLenient,
Short: "Disable Protection for a Primary IP",
ValidArgsFunction: cmpl.SuggestArgs(
cmpl.SuggestCandidatesF(client.PrimaryIP().Names),
Expand Down
32 changes: 31 additions & 1 deletion internal/cmd/primaryip/disable_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestEnable(t *testing.T) {
func TestDisableProtection(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

Expand Down Expand Up @@ -52,3 +52,33 @@ func TestEnable(t *testing.T) {
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}

func TestDisableDeleteProtection(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := primaryip.DisableProtectionCmd.CobraCommand(fx.State())
action := &hcloud.Action{ID: 1}
ip := &hcloud.PrimaryIP{ID: 13}

fx.ExpectEnsureToken()
fx.Client.PrimaryIPClient.EXPECT().
Get(gomock.Any(), "13").
Return(ip, nil, nil)
fx.Client.PrimaryIPClient.EXPECT().
ChangeProtection(gomock.Any(), hcloud.PrimaryIPChangeProtectionOpts{
ID: 13,
Delete: false,
}).
Return(action, nil, nil)

fx.ActionWaiter.EXPECT().WaitForActions(gomock.Any(), gomock.Any(), action).Return(nil)

out, errOut, err := fx.Run(cmd, []string{"13", "delete"})

expOut := "Resource protection disabled for primary IP 13\n"

assert.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}
2 changes: 2 additions & 0 deletions internal/cmd/primaryip/enable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"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/v2/hcloud"
Expand Down Expand Up @@ -57,6 +58,7 @@ var EnableProtectionCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "enable-protection <primary-ip> [<protection-level>...]", // optional because of backwards compatibility
Args: util.ValidateLenient,
Short: "Enable Protection for a Primary IP",
ValidArgsFunction: cmpl.SuggestArgs(
cmpl.SuggestCandidatesF(client.PrimaryIP().Names),
Expand Down
29 changes: 29 additions & 0 deletions internal/cmd/primaryip/enable_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,32 @@ func TestEnableProtection(t *testing.T) {
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}

func TestEnableDeleteProtection(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := primaryip.EnableProtectionCmd.CobraCommand(fx.State())
action := &hcloud.Action{ID: 1}
ip := &hcloud.PrimaryIP{ID: 13}

fx.ExpectEnsureToken()
fx.Client.PrimaryIPClient.EXPECT().
Get(gomock.Any(), "13").
Return(ip, nil, nil)
fx.Client.PrimaryIPClient.EXPECT().
ChangeProtection(gomock.Any(), hcloud.PrimaryIPChangeProtectionOpts{
ID: 13,
Delete: true,
}).
Return(action, nil, nil)
fx.ActionWaiter.EXPECT().WaitForActions(gomock.Any(), gomock.Any(), action).Return(nil)

out, errOut, err := fx.Run(cmd, []string{"13", "delete"})

expOut := "Resource protection enabled for primary IP 13\n"

assert.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}