From b6aecb31893016e05070856c291704403b76a296 Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Thu, 12 Sep 2024 10:11:32 +0200 Subject: [PATCH] feat(primary-ip): allow setting auto-delete on create (#860) Closes #855 --- internal/cmd/primaryip/create.go | 6 ++++++ internal/cmd/primaryip/create_test.go | 13 ++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/internal/cmd/primaryip/create.go b/internal/cmd/primaryip/create.go index 459f9d71..04e4408a 100644 --- a/internal/cmd/primaryip/create.go +++ b/internal/cmd/primaryip/create.go @@ -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) { @@ -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 { @@ -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 { diff --git a/internal/cmd/primaryip/create_test.go b/internal/cmd/primaryip/create_test.go index 11af3c7c..ce12279c 100644 --- a/internal/cmd/primaryip/create_test.go +++ b/internal/cmd/primaryip/create_test.go @@ -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}, }, @@ -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 @@ -77,6 +79,7 @@ func TestCreateJSON(t *testing.T) { Type: "ipv4", Datacenter: "fsn1-dc14", AssigneeType: "server", + AutoDelete: hcloud.Ptr(true), }, ). Return( @@ -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"