Skip to content

Commit 9c65e6f

Browse files
committed
review feedback
1 parent a59d4f8 commit 9c65e6f

10 files changed

Lines changed: 108 additions & 40 deletions

File tree

‎internal/cmd/alpha/vpc/network-range/create/create.go‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/spf13/cobra"
88
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2alpha1api"
9+
"github.com/stackitcloud/stackit-sdk-go/services/iaas/v2alpha1api/wait"
910

1011
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -15,6 +16,7 @@ import (
1516
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1617
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaasalpha/client"
1718
iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaasalpha/utils"
19+
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
1820
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1921
)
2022

@@ -92,7 +94,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
9294
}
9395

9496
// Call API
95-
req, err := buildRequest(ctx, model, apiClient)
97+
req, err := buildRequest(ctx, model, apiClient.DefaultAPI)
9698
if err != nil {
9799
return err
98100
}
@@ -101,7 +103,28 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
101103
return fmt.Errorf("create network range: %w", err)
102104
}
103105

104-
return outputResult(params.Printer, model.OutputFormat, vpcLabel, resp)
106+
if !model.Async {
107+
var networkRangeId string
108+
if ipv4 := resp.VPCNetworkRangeIPv4; ipv4 != nil && ipv4.Id != nil {
109+
networkRangeId = *ipv4.Id
110+
} else if ipv6 := resp.VPCNetworkRangeIPv6; ipv6 != nil && ipv6.Id != nil {
111+
networkRangeId = *ipv6.Id
112+
} else {
113+
return fmt.Errorf("network range ID is missing in API response")
114+
}
115+
116+
err = spinner.Run(params.Printer, "Creating VPC Network range", func() error {
117+
resp, err = wait.CreateVPCNetworkRangeWaitHandler(ctx, apiClient.DefaultAPI, model.ProjectId, model.VpcId, model.Region, networkRangeId).WaitWithContext(ctx)
118+
return err
119+
})
120+
if err != nil {
121+
if err != nil {
122+
return fmt.Errorf("wait for network range %q creation: %w", networkRangeId, err)
123+
}
124+
}
125+
}
126+
127+
return outputResult(params.Printer, model.OutputFormat, model.Async, vpcLabel, resp)
105128
},
106129
}
107130
configureFlags(cmd)
@@ -146,7 +169,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
146169
return &model, nil
147170
}
148171

149-
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) (iaas.ApiCreateVPCNetworkRangeRequest, error) {
172+
func buildRequest(ctx context.Context, model *inputModel, apiClient iaas.DefaultAPI) (iaas.ApiCreateVPCNetworkRangeRequest, error) {
150173
if model.IpVersion != "ipv4" {
151174
return iaas.ApiCreateVPCNetworkRangeRequest{}, fmt.Errorf("unsupported IP version: %q", model.IpVersion)
152175
}
@@ -163,10 +186,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
163186
Labels: model.Labels,
164187
},
165188
}
166-
return apiClient.DefaultAPI.CreateVPCNetworkRange(ctx, model.ProjectId, model.VpcId, model.Region).CreateVPCNetworkRangePayload(payload), nil
189+
return apiClient.CreateVPCNetworkRange(ctx, model.ProjectId, model.VpcId, model.Region).CreateVPCNetworkRangePayload(payload), nil
167190
}
168191

169-
func outputResult(p *print.Printer, outputFormat, vpcLabel string, networkRange *iaas.VPCNetworkRange) error {
192+
func outputResult(p *print.Printer, outputFormat string, async bool, vpcLabel string, networkRange *iaas.VPCNetworkRange) error {
170193
if networkRange == nil {
171194
return fmt.Errorf("create vpc network range response is nil")
172195
}
@@ -180,7 +203,12 @@ func outputResult(p *print.Printer, outputFormat, vpcLabel string, networkRange
180203
return fmt.Errorf("network range ID is missing in API response")
181204
}
182205

183-
p.Outputf("Created network range for VPC %q.\nNetwork range ID: %s\n", vpcLabel, networkRangeId)
206+
operationState := "Created"
207+
if async {
208+
operationState = "Triggered creation of"
209+
}
210+
211+
p.Outputf("%s network range for VPC %q.\nNetwork range ID: %s\n", operationState, vpcLabel, networkRangeId)
184212
return nil
185213
})
186214
}

‎internal/cmd/alpha/vpc/network-range/create/create_test.go‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package create
22

33
import (
44
"context"
5+
"maps"
6+
"slices"
57
"strconv"
68
"strings"
79
"testing"
@@ -77,8 +79,8 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
7779
DefaultPrefixLength: new(testDefaultPrefixLength),
7880
MaxPrefixLength: new(testMaxPrefixLength),
7981
MinPrefixLength: new(testMinPrefixLength),
80-
Nameservers: testNameservers,
81-
Labels: testLabel,
82+
Nameservers: slices.Clone(testNameservers),
83+
Labels: maps.Clone(testLabel),
8284
}
8385
for _, mod := range mods {
8486
mod(model)
@@ -101,10 +103,10 @@ func fixturePayload(mods ...func(payload *iaas.CreateVPCNetworkRangePayload)) ia
101103
DefaultPrefixLen: new(testDefaultPrefixLength),
102104
Description: new(testDescription),
103105
IpVersion: testIpVersion,
104-
Labels: testLabel,
106+
Labels: maps.Clone(testLabel),
105107
MaxPrefixLen: new(testMaxPrefixLength),
106108
MinPrefixLen: new(testMinPrefixLength),
107-
Nameservers: testNameservers,
109+
Nameservers: slices.Clone(testNameservers),
108110
Prefix: testPrefix,
109111
},
110112
}
@@ -207,7 +209,7 @@ func TestBuildRequest(t *testing.T) {
207209

208210
for _, tt := range tests {
209211
t.Run(tt.description, func(t *testing.T) {
210-
request, err := buildRequest(testCtx, tt.model, testClient)
212+
request, err := buildRequest(testCtx, tt.model, testClient.DefaultAPI)
211213
if err != nil {
212214
t.Fatalf("test failed: %v", err)
213215
}
@@ -228,6 +230,7 @@ func TestOutputResult(t *testing.T) {
228230

229231
type args struct {
230232
outputFormat string
233+
async bool
231234
vpcLabel string
232235
networkRange *iaas.VPCNetworkRange
233236
}
@@ -292,7 +295,7 @@ func TestOutputResult(t *testing.T) {
292295
params := testparams.NewTestParams()
293296
for _, tt := range tests {
294297
t.Run(tt.name, func(t *testing.T) {
295-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.vpcLabel, tt.args.networkRange); (err != nil) != tt.wantErr {
298+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.async, tt.args.vpcLabel, tt.args.networkRange); (err != nil) != tt.wantErr {
296299
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
297300
}
298301
})

‎internal/cmd/alpha/vpc/network-range/delete/delete.go‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
8-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
9-
107
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2alpha1api"
8+
"github.com/stackitcloud/stackit-sdk-go/services/iaas/v2alpha1api/wait"
119

1210
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1312
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
1413
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1514
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1615
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1716
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaasalpha/client"
1817
iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaasalpha/utils"
18+
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
19+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1920
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
2021

2122
"github.com/spf13/cobra"
@@ -79,14 +80,29 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7980
}
8081

8182
// Call API
82-
req := buildRequest(ctx, model, apiClient)
83+
req := buildRequest(ctx, model, apiClient.DefaultAPI)
8384
err = req.Execute()
8485
if err != nil {
8586
return fmt.Errorf("delete vpc network range: %w", err)
8687
}
8788

88-
params.Printer.Outputf("Deleted network range %q on VPC %q\n", networkRangeLabel, vpcLabel)
89+
// Wait for async operation, if async mode not enabled
90+
if !model.Async {
91+
err := spinner.Run(params.Printer, "Deleting VPC network range", func() error {
92+
_, err = wait.DeleteVPCNetworkRangeWaitHandler(ctx, apiClient.DefaultAPI, model.ProjectId, model.VpcId, model.Region, model.NetworkRangeId).WaitWithContext(ctx)
93+
return err
94+
})
95+
if err != nil {
96+
return fmt.Errorf("wait for VPC network range deletion: %w", err)
97+
}
98+
}
99+
100+
operationState := "Deleted"
101+
if model.Async {
102+
operationState = "Triggered deletion of"
103+
}
89104

105+
params.Printer.Outputf("%s network range %q on VPC %q\n", operationState, networkRangeLabel, vpcLabel)
90106
return nil
91107
},
92108
}
@@ -117,6 +133,6 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
117133
return &model, nil
118134
}
119135

120-
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiDeleteVPCNetworkRangeRequest {
121-
return apiClient.DefaultAPI.DeleteVPCNetworkRange(ctx, model.ProjectId, model.VpcId, model.Region, model.NetworkRangeId)
136+
func buildRequest(ctx context.Context, model *inputModel, apiClient iaas.DefaultAPI) iaas.ApiDeleteVPCNetworkRangeRequest {
137+
return apiClient.DeleteVPCNetworkRange(ctx, model.ProjectId, model.VpcId, model.Region, model.NetworkRangeId)
122138
}

‎internal/cmd/alpha/vpc/network-range/delete/delete_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func TestBuildRequest(t *testing.T) {
234234

235235
for _, tt := range tests {
236236
t.Run(tt.description, func(t *testing.T) {
237-
request := buildRequest(testCtx, tt.model, testClient)
237+
request := buildRequest(testCtx, tt.model, testClient.DefaultAPI)
238238

239239
diff := cmp.Diff(request, tt.expectedRequest,
240240
cmp.AllowUnexported(tt.expectedRequest),

‎internal/cmd/alpha/vpc/network-range/describe/describe.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6060
}
6161

6262
// Call API
63-
req := buildRequest(ctx, model, apiClient)
63+
req := buildRequest(ctx, model, apiClient.DefaultAPI)
6464
resp, err := req.Execute()
6565
if err != nil {
6666
return fmt.Errorf("describe network range: %w", err)
@@ -97,8 +97,8 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
9797
return &model, nil
9898
}
9999

100-
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiGetVPCNetworkRangeRequest {
101-
return apiClient.DefaultAPI.GetVPCNetworkRange(ctx, model.ProjectId, model.VpcId, model.Region, model.NetworkRangeId)
100+
func buildRequest(ctx context.Context, model *inputModel, apiClient iaas.DefaultAPI) iaas.ApiGetVPCNetworkRangeRequest {
101+
return apiClient.GetVPCNetworkRange(ctx, model.ProjectId, model.VpcId, model.Region, model.NetworkRangeId)
102102
}
103103

104104
func outputResult(p *print.Printer, outputFormat string, networkRange *iaas.VPCNetworkRange) error {

‎internal/cmd/alpha/vpc/network-range/describe/describe_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func TestBuildRequest(t *testing.T) {
188188

189189
for _, tt := range tests {
190190
t.Run(tt.description, func(t *testing.T) {
191-
request := buildRequest(testCtx, tt.model, testClient)
191+
request := buildRequest(testCtx, tt.model, testClient.DefaultAPI)
192192

193193
diff := cmp.Diff(request, tt.expectedRequest,
194194
cmp.AllowUnexported(tt.expectedRequest),

‎internal/cmd/alpha/vpc/network-range/list/list.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6767
}
6868

6969
// Call API
70-
req := buildRequest(ctx, model, apiClient)
70+
req := buildRequest(ctx, model, apiClient.DefaultAPI)
7171
resp, err := req.Execute()
7272
if err != nil {
7373
return fmt.Errorf("list network ranges: %w", err)
@@ -125,8 +125,8 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
125125
return &model, nil
126126
}
127127

128-
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiListVPCNetworkRangesRequest {
129-
return apiClient.DefaultAPI.ListVPCNetworkRanges(ctx, model.ProjectId, model.VpcId, model.Region)
128+
func buildRequest(ctx context.Context, model *inputModel, apiClient iaas.DefaultAPI) iaas.ApiListVPCNetworkRangesRequest {
129+
return apiClient.ListVPCNetworkRanges(ctx, model.ProjectId, model.VpcId, model.Region)
130130
}
131131

132132
func outputResult(p *print.Printer, outputFormat, vpcLabel string, networkRanges []iaas.VPCNetworkRange) error {

‎internal/cmd/alpha/vpc/network-range/list/list_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func TestBuildRequest(t *testing.T) {
172172

173173
for _, tt := range tests {
174174
t.Run(tt.description, func(t *testing.T) {
175-
request := buildRequest(testCtx, tt.model, testClient)
175+
request := buildRequest(testCtx, tt.model, testClient.DefaultAPI)
176176

177177
diff := cmp.Diff(request, tt.expectedRequest,
178178
cmp.AllowUnexported(tt.expectedRequest),

‎internal/cmd/alpha/vpc/network-range/update/update.go‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/spf13/cobra"
88
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2alpha1api"
9+
"github.com/stackitcloud/stackit-sdk-go/services/iaas/v2alpha1api/wait"
910

1011
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -15,6 +16,7 @@ import (
1516
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1617
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaasalpha/client"
1718
iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaasalpha/utils"
19+
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
1820
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1921
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
2022
)
@@ -103,7 +105,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
103105
}
104106

105107
// Call API
106-
req, err := buildRequest(ctx, model, apiClient)
108+
req, err := buildRequest(ctx, model, apiClient.DefaultAPI)
107109
if err != nil {
108110
return err
109111
}
@@ -112,7 +114,18 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
112114
return fmt.Errorf("update network range: %w", err)
113115
}
114116

115-
return outputResult(params.Printer, model.OutputFormat, vpcLabel, resp)
117+
// Wait for async operation, if async mode not enabled
118+
if !model.Async {
119+
err := spinner.Run(params.Printer, "Updating VPC network range", func() error {
120+
_, err = wait.UpdateVPCNetworkRangeWaitHandler(ctx, apiClient.DefaultAPI, model.ProjectId, model.VpcId, model.Region, model.NetworkRangeId).WaitWithContext(ctx)
121+
return err
122+
})
123+
if err != nil {
124+
return fmt.Errorf("wait for VPC network range update: %w", err)
125+
}
126+
}
127+
128+
return outputResult(params.Printer, model.OutputFormat, model.Async, vpcLabel, resp)
116129
},
117130
}
118131
configureFlags(cmd)
@@ -165,7 +178,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
165178
return &model, nil
166179
}
167180

168-
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) (iaas.ApiUpdateVPCNetworkRangeRequest, error) {
181+
func buildRequest(ctx context.Context, model *inputModel, apiClient iaas.DefaultAPI) (iaas.ApiUpdateVPCNetworkRangeRequest, error) {
169182
if model.IpVersion != "ipv4" {
170183
return iaas.ApiUpdateVPCNetworkRangeRequest{}, fmt.Errorf("unsupported IP version: %q", model.IpVersion)
171184
}
@@ -181,10 +194,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
181194
Labels: model.Labels,
182195
},
183196
}
184-
return apiClient.DefaultAPI.UpdateVPCNetworkRange(ctx, model.ProjectId, model.VpcId, model.Region, model.NetworkRangeId).UpdateVPCNetworkRangePayload(payload), nil
197+
return apiClient.UpdateVPCNetworkRange(ctx, model.ProjectId, model.VpcId, model.Region, model.NetworkRangeId).UpdateVPCNetworkRangePayload(payload), nil
185198
}
186199

187-
func outputResult(p *print.Printer, outputFormat, vpcLabel string, networkRange *iaas.VPCNetworkRange) error {
200+
func outputResult(p *print.Printer, outputFormat string, async bool, vpcLabel string, networkRange *iaas.VPCNetworkRange) error {
188201
if networkRange == nil {
189202
return fmt.Errorf("update vpc network range response is nil")
190203
}
@@ -198,7 +211,12 @@ func outputResult(p *print.Printer, outputFormat, vpcLabel string, networkRange
198211
return fmt.Errorf("network range ID is missing in API response")
199212
}
200213

201-
p.Outputf("Updated network range for VPC %q.\nNetwork range ID: %s\n", vpcLabel, networkRangeId)
214+
operationState := "Updated"
215+
if async {
216+
operationState = "Triggered update of"
217+
}
218+
219+
p.Outputf("%s network range for VPC %q.\nNetwork range ID: %s\n", operationState, vpcLabel, networkRangeId)
202220
return nil
203221
})
204222
}

0 commit comments

Comments
 (0)