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

feat(primary-ip): allow setting auto-delete on create #860

Merged
merged 1 commit into from
Sep 12, 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
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