Skip to content

Commit

Permalink
feat(primary-ip): allow setting auto-delete on create (#860)
Browse files Browse the repository at this point in the history
Closes #855
  • Loading branch information
phm07 authored Sep 12, 2024
1 parent ea46641 commit b6aecb3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 6 additions & 0 deletions internal/cmd/primaryip/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var CreateCmd = base.CreateCmd{
cmd.Flags().StringSlice("enable-protection", []string{}, "Enable protection (delete) (default: none)")
_ = cmd.RegisterFlagCompletionFunc("enable-protection", cmpl.SuggestCandidates("delete"))

cmd.Flags().Bool("auto-delete", false, "Delete Primary IP if assigned resource is deleted")

return cmd
},
Run: func(s state.State, cmd *cobra.Command, _ []string) (any, any, error) {
Expand All @@ -44,6 +46,7 @@ var CreateCmd = base.CreateCmd{
assigneeID, _ := cmd.Flags().GetInt64("assignee-id")
datacenter, _ := cmd.Flags().GetString("datacenter")
protection, _ := cmd.Flags().GetStringSlice("enable-protection")
autoDelete, _ := cmd.Flags().GetBool("auto-delete")

protectionOpts, err := getChangeProtectionOpts(true, protection)
if err != nil {
Expand All @@ -59,6 +62,9 @@ var CreateCmd = base.CreateCmd{
if assigneeID != 0 {
createOpts.AssigneeID = &assigneeID
}
if cmd.Flags().Changed("auto-delete") {
createOpts.AutoDelete = &autoDelete
}

result, _, err := s.Client().PrimaryIP().Create(s, createOpts)
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions internal/cmd/primaryip/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ func TestCreate(t *testing.T) {
Type: "ipv4",
Datacenter: "fsn1-dc14",
AssigneeType: "server",
AutoDelete: hcloud.Ptr(true),
},
).
Return(
&hcloud.PrimaryIPCreateResult{
PrimaryIP: &hcloud.PrimaryIP{
ID: 1,
IP: net.ParseIP("192.168.2.1"),
Type: hcloud.PrimaryIPTypeIPv4,
ID: 1,
IP: net.ParseIP("192.168.2.1"),
Type: hcloud.PrimaryIPTypeIPv4,
AutoDelete: true,
},
Action: &hcloud.Action{ID: 321},
},
Expand All @@ -49,7 +51,7 @@ func TestCreate(t *testing.T) {
fx.ActionWaiter.EXPECT().
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 321})

out, errOut, err := fx.Run(cmd, []string{"--name=my-ip", "--type=ipv4", "--datacenter=fsn1-dc14"})
out, errOut, err := fx.Run(cmd, []string{"--name=my-ip", "--type=ipv4", "--datacenter=fsn1-dc14", "--auto-delete"})

expOut := `Primary IP 1 created
IPv4: 192.168.2.1
Expand Down Expand Up @@ -77,6 +79,7 @@ func TestCreateJSON(t *testing.T) {
Type: "ipv4",
Datacenter: "fsn1-dc14",
AssigneeType: "server",
AutoDelete: hcloud.Ptr(true),
},
).
Return(
Expand Down Expand Up @@ -104,7 +107,7 @@ func TestCreateJSON(t *testing.T) {
fx.ActionWaiter.EXPECT().
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 321})

jsonOut, out, err := fx.Run(cmd, []string{"-o=json", "--name=my-ip", "--type=ipv4", "--datacenter=fsn1-dc14"})
jsonOut, out, err := fx.Run(cmd, []string{"-o=json", "--name=my-ip", "--type=ipv4", "--datacenter=fsn1-dc14", "--auto-delete"})

expOut := "Primary IP 1 created\n"

Expand Down

0 comments on commit b6aecb3

Please sign in to comment.