Skip to content

Commit

Permalink
fix: "create" actions were not awaited in some cases (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Oct 31, 2023
1 parent 89a25c3 commit 5377e50
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions internal/cmd/firewall/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ var CreateCmd = base.Cmd{
return err
}

if err := waiter.WaitForActions(ctx, result.Actions); err != nil {
return err
}

fmt.Printf("Firewall %d created\n", result.Firewall.ID)

return nil
Expand Down
6 changes: 6 additions & 0 deletions internal/cmd/floatingip/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ var CreateCmd = base.Cmd{
return err
}

if result.Action != nil {
if err := waiter.ActionProgress(ctx, result.Action); err != nil {
return err
}
}

fmt.Printf("Floating IP %d created\n", result.FloatingIP.ID)

return changeProtection(ctx, client, waiter, result.FloatingIP, true, protectionOps)
Expand Down
8 changes: 7 additions & 1 deletion internal/cmd/placementgroup/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var CreateCmd = base.Cmd{
cmd.MarkFlagRequired("type")
return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
name, _ := cmd.Flags().GetString("name")
labels, _ := cmd.Flags().GetStringToString("label")
placementGroupType, _ := cmd.Flags().GetString("type")
Expand All @@ -43,6 +43,12 @@ var CreateCmd = base.Cmd{
return err
}

if result.Action != nil {
if err := waiter.ActionProgress(ctx, result.Action); err != nil {
return err
}
}

fmt.Printf("Placement group %d created\n", result.PlacementGroup.ID)

return nil
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/placementgroup/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func TestCreate(t *testing.T) {

fx.Client.PlacementGroupClient.EXPECT().
Create(gomock.Any(), opts).
Return(hcloud.PlacementGroupCreateResult{PlacementGroup: &placementGroup, Action: nil}, nil, nil)
Return(hcloud.PlacementGroupCreateResult{PlacementGroup: &placementGroup, Action: &hcloud.Action{ID: 321}}, nil, nil)

fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), &hcloud.Action{ID: 321})

out, err := fx.Run(cmd, []string{"--name", placementGroup.Name, "--type", string(placementGroup.Type)})

Expand Down
10 changes: 8 additions & 2 deletions internal/cmd/primaryip/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var CreateCmd = base.Cmd{

return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
typ, _ := cmd.Flags().GetString("type")
name, _ := cmd.Flags().GetString("name")
assigneeID, _ := cmd.Flags().GetInt64("assignee-id")
Expand All @@ -68,10 +68,16 @@ var CreateCmd = base.Cmd{
return err
}

if result.Action != nil {
if err := waiter.ActionProgress(ctx, result.Action); err != nil {
return err
}
}

fmt.Printf("Primary IP %d created\n", result.PrimaryIP.ID)

if len(protection) > 0 {
if err := changeProtection(ctx, client, actionWaiter, result.PrimaryIP, true, protectionOpts); err != nil {
if err := changeProtection(ctx, client, waiter, result.PrimaryIP, true, protectionOpts); err != nil {
return err
}
}
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/primaryip/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ func TestCreate(t *testing.T) {
Return(
&hcloud.PrimaryIPCreateResult{
PrimaryIP: &hcloud.PrimaryIP{ID: 1},
Action: &hcloud.Action{ID: 321},
},
&hcloud.Response{},
nil,
)

fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), &hcloud.Action{ID: 321})

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

expOut := "Primary IP 1 created\n"
Expand Down

0 comments on commit 5377e50

Please sign in to comment.