Skip to content

Commit f7d710b

Browse files
committed
apply review suggestions
1 parent 14fcb3a commit f7d710b

10 files changed

Lines changed: 181 additions & 75 deletions

File tree

‎internal/cmd/alpha/vpc/region/create/create.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7272
return err
7373
}
7474

75-
req := buildRequest(ctx, model, apiClient)
75+
req := buildRequest(ctx, model, apiClient.DefaultAPI)
7676

7777
resp, err := req.Execute()
7878
if err != nil {
@@ -130,7 +130,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient iaas.Default
130130
}
131131
}
132132

133-
return apiClient.DefaultAPI.CreateVPCRegion(ctx, model.ProjectId, model.VpcId, model.Region).CreateVPCRegionPayload(payload)
133+
return apiClient.CreateVPCRegion(ctx, model.ProjectId, model.VpcId, model.Region).CreateVPCRegionPayload(payload)
134134
}
135135

136136
func outputResult(p *print.Printer, outputFormat string, async bool, region, vpcLabel string, resp *iaas.RegionalVPC) error {

‎internal/cmd/alpha/vpc/region/create/create_test.go‎

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2alpha1api"
1212

1313
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1415
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
1516
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1617
)
@@ -113,33 +114,78 @@ func TestParseInput(t *testing.T) {
113114
}
114115

115116
func TestBuildRequest(t *testing.T) {
116-
ctx := context.Background()
117-
client := &iaas.APIClient{
118-
DefaultAPI: &iaas.DefaultAPIService{},
119-
}
120-
121-
for _, nameservers := range [][]string{nil, {}, {"8.8.8.8", "8.8.4.4"}} {
117+
buildRequestInputModelDefault := func(opts ...func(m *inputModel)) *inputModel {
122118
model := &inputModel{
123119
GlobalFlagModel: &globalflags.GlobalFlagModel{
124120
ProjectId: testProjectId,
125121
Region: "eu02",
126122
},
127123
VpcId: testVpcId,
128-
IPv4DefaultNameservers: nameservers,
124+
IPv4DefaultNameservers: []string{"8.8.8.8", "8.8.4.4"},
129125
}
130126

131-
payload := iaas.CreateVPCRegionPayload{}
132-
if nameservers != nil {
133-
payload.Ipv4 = &iaas.RegionalVPCIPv4{
134-
DefaultNameservers: nameservers,
135-
}
127+
for _, opt := range opts {
128+
opt(model)
129+
}
130+
131+
return model
132+
}
133+
134+
buildRequestPayloadDefault := func(opts ...func(p *iaas.CreateVPCRegionPayload)) iaas.CreateVPCRegionPayload {
135+
payload := iaas.CreateVPCRegionPayload{
136+
Ipv4: &iaas.RegionalVPCIPv4{
137+
DefaultNameservers: []string{"8.8.8.8", "8.8.4.4"},
138+
},
136139
}
137140

138-
want := client.DefaultAPI.CreateVPCRegion(ctx, testProjectId, testVpcId, "eu02").CreateVPCRegionPayload(payload)
139-
got := buildRequest(ctx, model, client)
140-
if diff := cmp.Diff(want, got, cmp.AllowUnexported(want), cmpopts.EquateComparable(ctx, iaas.DefaultAPIService{})); diff != "" {
141-
t.Fatalf("request mismatch (-want +got): %s", diff)
141+
for _, opt := range opts {
142+
opt(&payload)
142143
}
144+
145+
return payload
146+
}
147+
148+
tests := []struct {
149+
description string
150+
inputModel *inputModel
151+
wantPayload iaas.CreateVPCRegionPayload
152+
}{
153+
{
154+
description: "default",
155+
inputModel: buildRequestInputModelDefault(),
156+
wantPayload: buildRequestPayloadDefault(),
157+
},
158+
{
159+
description: "nameservers is empty",
160+
inputModel: buildRequestInputModelDefault(func(m *inputModel) {
161+
m.IPv4DefaultNameservers = []string{}
162+
}),
163+
wantPayload: buildRequestPayloadDefault(func(p *iaas.CreateVPCRegionPayload) {
164+
p.Ipv4.DefaultNameservers = []string{}
165+
}),
166+
},
167+
{
168+
description: "nameservers is nil",
169+
inputModel: buildRequestInputModelDefault(func(m *inputModel) {
170+
m.IPv4DefaultNameservers = nil
171+
}),
172+
wantPayload: buildRequestPayloadDefault(func(p *iaas.CreateVPCRegionPayload) {
173+
p.Ipv4 = nil
174+
}),
175+
},
176+
}
177+
178+
for _, tt := range tests {
179+
t.Run(tt.description, func(t *testing.T) {
180+
ctx := context.Background()
181+
client := &iaas.DefaultAPIServiceMock{}
182+
183+
want := client.CreateVPCRegion(ctx, testProjectId, testVpcId, "eu02").CreateVPCRegionPayload(tt.wantPayload)
184+
got := buildRequest(ctx, tt.inputModel, client)
185+
if diff := cmp.Diff(want, got, cmp.AllowUnexported(want), cmpopts.EquateComparable(ctx, iaas.DefaultAPIService{})); diff != "" {
186+
t.Fatalf("request mismatch (-want +got): %s", diff)
187+
}
188+
})
143189
}
144190
}
145191

‎internal/cmd/alpha/vpc/region/delete/delete.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6868
return err
6969
}
7070

71-
req := buildRequest(ctx, model, apiClient)
71+
req := buildRequest(ctx, model, apiClient.DefaultAPI)
7272

7373
if err := req.Execute(); err != nil {
7474
return fmt.Errorf("delete vpc region: %w", err)
@@ -124,5 +124,5 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
124124
}
125125

126126
func buildRequest(ctx context.Context, model *inputModel, apiClient iaas.DefaultAPI) iaas.ApiDeleteVPCRegionRequest {
127-
return apiClient.DefaultAPI.DeleteVPCRegion(ctx, model.ProjectId, model.VpcId, model.Region)
127+
return apiClient.DeleteVPCRegion(ctx, model.ProjectId, model.VpcId, model.Region)
128128
}

‎internal/cmd/alpha/vpc/region/delete/delete_test.go‎

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

111111
want := client.DefaultAPI.DeleteVPCRegion(ctx, testProjectId, testVpcId, "eu02")
112-
got := buildRequest(ctx, model, client)
112+
got := buildRequest(ctx, model, client.DefaultAPI)
113113
if diff := cmp.Diff(want, got, cmp.AllowUnexported(want), cmpopts.EquateComparable(ctx, iaas.DefaultAPIService{})); diff != "" {
114114
t.Fatalf("request mismatch (-want +got): %s", diff)
115115
}

‎internal/cmd/alpha/vpc/region/describe/describe.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6262
vpcName = ""
6363
}
6464

65-
req := buildRequest(ctx, model, apiClient)
65+
req := buildRequest(ctx, model, apiClient.DefaultAPI)
6666

6767
resp, err := req.Execute()
6868
if err != nil {
@@ -102,7 +102,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
102102
}
103103

104104
func buildRequest(ctx context.Context, model *inputModel, apiClient iaas.DefaultAPI) iaas.ApiGetVPCRegionRequest {
105-
return apiClient.DefaultAPI.GetVPCRegion(ctx, model.ProjectId, model.VpcId, model.Region)
105+
return apiClient.GetVPCRegion(ctx, model.ProjectId, model.VpcId, model.Region)
106106
}
107107

108108
func outputResult(p *print.Printer, outputFormat, region, vpcId, vpcName string, resp *iaas.RegionalVPC) error {

‎internal/cmd/alpha/vpc/region/describe/describe_test.go‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2alpha1api"
1212

1313
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1415
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
1516
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1617
)
@@ -99,21 +100,33 @@ func TestParseInput(t *testing.T) {
99100

100101
func TestBuildRequest(t *testing.T) {
101102
ctx := context.Background()
102-
client := &iaas.APIClient{
103-
DefaultAPI: &iaas.DefaultAPIService{},
104-
}
105-
model := &inputModel{
106-
GlobalFlagModel: &globalflags.GlobalFlagModel{
107-
ProjectId: testProjectId,
108-
Region: "eu02",
103+
client := &iaas.DefaultAPIServiceMock{}
104+
105+
tests := []struct {
106+
description string
107+
inputModel *inputModel
108+
want iaas.ApiGetVPCRegionRequest
109+
}{
110+
{
111+
description: "default",
112+
inputModel: &inputModel{
113+
GlobalFlagModel: &globalflags.GlobalFlagModel{
114+
ProjectId: testProjectId,
115+
Region: "eu02",
116+
},
117+
VpcId: testVpcId,
118+
},
119+
want: client.GetVPCRegion(ctx, testProjectId, testVpcId, "eu02"),
109120
},
110-
VpcId: testVpcId,
111121
}
112122

113-
want := client.DefaultAPI.GetVPCRegion(ctx, testProjectId, testVpcId, "eu02")
114-
got := buildRequest(ctx, model, client)
115-
if diff := cmp.Diff(want, got, cmp.AllowUnexported(want), cmpopts.EquateComparable(ctx, iaas.DefaultAPIService{})); diff != "" {
116-
t.Fatalf("request mismatch (-want +got): %s", diff)
123+
for _, tt := range tests {
124+
t.Run(tt.description, func(t *testing.T) {
125+
got := buildRequest(ctx, tt.inputModel, client)
126+
if diff := cmp.Diff(tt.want, got, cmp.AllowUnexported(tt.want), cmpopts.EquateComparable(ctx, iaas.DefaultAPIService{})); diff != "" {
127+
t.Fatalf("request mismatch (-want +got): %s", diff)
128+
}
129+
})
117130
}
118131
}
119132

@@ -150,13 +163,13 @@ func TestOutputResult(t *testing.T) {
150163
},
151164
{
152165
name: "json",
153-
format: "json",
166+
format: print.JSONOutputFormat,
154167
resp: resp,
155168
want: []string{`"status": "CREATED"`, `"defaultNameservers"`},
156169
},
157170
{
158171
name: "yaml",
159-
format: "yaml",
172+
format: print.YAMLOutputFormat,
160173
resp: resp,
161174
want: []string{"status: CREATED", "defaultNameservers:"},
162175
},

‎internal/cmd/alpha/vpc/region/list/list.go‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package list
33
import (
44
"context"
55
"fmt"
6+
"maps"
67
"slices"
78
"strings"
89

@@ -65,7 +66,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6566
vpcLabel = model.VpcId
6667
}
6768

68-
req := buildRequest(ctx, model, apiClient)
69+
req := buildRequest(ctx, model, apiClient.DefaultAPI)
6970

7071
resp, err := req.Execute()
7172
if err != nil {
@@ -102,7 +103,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
102103
}
103104

104105
func buildRequest(ctx context.Context, model *inputModel, apiClient iaas.DefaultAPI) iaas.ApiListVPCRegionsRequest {
105-
return apiClient.DefaultAPI.ListVPCRegions(ctx, model.ProjectId, model.VpcId)
106+
return apiClient.ListVPCRegions(ctx, model.ProjectId, model.VpcId)
106107
}
107108

108109
func outputResult(p *print.Printer, outputFormat, vpcLabel string, resp *iaas.RegionalVPCList) error {
@@ -117,15 +118,10 @@ func outputResult(p *print.Printer, outputFormat, vpcLabel string, resp *iaas.Re
117118
}
118119

119120
regionIds := slices.Sorted(maps.Keys(resp.Regions))
120-
for region := range resp.Regions {
121-
regions = append(regions, region)
122-
}
123-
124-
slices.Sort(regions)
125121

126122
table := tables.NewTable()
127123
table.SetHeader("REGION", "STATUS", "DNS NAME SERVERS")
128-
for _, region := range regions {
124+
for _, region := range regionIds {
129125
regionConfig := resp.Regions[region]
130126
var dnsNames string
131127
if ipv4 := regionConfig.Ipv4; ipv4 != nil {

‎internal/cmd/alpha/vpc/region/list/list_test.go‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2alpha1api"
1212

1313
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1415
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
1516
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1617
)
@@ -92,20 +93,32 @@ func TestParseInput(t *testing.T) {
9293

9394
func TestBuildRequest(t *testing.T) {
9495
ctx := context.Background()
95-
client := &iaas.APIClient{
96-
DefaultAPI: &iaas.DefaultAPIService{},
97-
}
98-
model := &inputModel{
99-
GlobalFlagModel: &globalflags.GlobalFlagModel{
100-
ProjectId: testProjectId,
96+
client := &iaas.DefaultAPIServiceMock{}
97+
98+
tests := []struct {
99+
description string
100+
inputModel *inputModel
101+
want iaas.ApiListVPCRegionsRequest
102+
}{
103+
{
104+
description: "default",
105+
inputModel: &inputModel{
106+
GlobalFlagModel: &globalflags.GlobalFlagModel{
107+
ProjectId: testProjectId,
108+
},
109+
VpcId: testVpcId,
110+
},
111+
want: client.ListVPCRegions(ctx, testProjectId, testVpcId),
101112
},
102-
VpcId: testVpcId,
103113
}
104114

105-
want := client.DefaultAPI.ListVPCRegions(ctx, testProjectId, testVpcId)
106-
got := buildRequest(ctx, model, client)
107-
if diff := cmp.Diff(want, got, cmp.AllowUnexported(want), cmpopts.EquateComparable(ctx, iaas.DefaultAPIService{})); diff != "" {
108-
t.Fatalf("request mismatch (-want +got): %s", diff)
115+
for _, tt := range tests {
116+
t.Run(tt.description, func(t *testing.T) {
117+
got := buildRequest(ctx, tt.inputModel, client)
118+
if diff := cmp.Diff(tt.want, got, cmp.AllowUnexported(tt.want), cmpopts.EquateComparable(ctx, iaas.DefaultAPIService{})); diff != "" {
119+
t.Fatalf("request mismatch (-want +got): %s", diff)
120+
}
121+
})
109122
}
110123
}
111124

@@ -145,19 +158,19 @@ func TestOutputResult(t *testing.T) {
145158
},
146159
{
147160
name: "json",
148-
format: "json",
161+
format: print.JSONOutputFormat,
149162
resp: resp,
150163
want: []string{`"regions"`, `"eu02"`, `"status": "CREATED"`},
151164
},
152165
{
153166
name: "yaml",
154-
format: "yaml",
167+
format: print.YAMLOutputFormat,
155168
resp: resp,
156169
want: []string{"regions:", "eu02:", "status: CREATED"},
157170
},
158171
{
159172
name: "empty json",
160-
format: "json",
173+
format: print.JSONOutputFormat,
161174
resp: &iaas.RegionalVPCList{
162175
Regions: map[string]iaas.RegionalVPC{},
163176
},

‎internal/cmd/alpha/vpc/region/update/update.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7070
return err
7171
}
7272

73-
req := buildRequest(ctx, model, apiClient)
73+
req := buildRequest(ctx, model, apiClient.DefaultAPI)
7474

7575
resp, err := req.Execute()
7676
if err != nil {
@@ -118,7 +118,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient iaas.Default
118118
},
119119
}
120120

121-
return apiClient.DefaultAPI.UpdateVPCRegion(ctx, model.ProjectId, model.VpcId, model.Region).UpdateVPCRegionPayload(payload)
121+
return apiClient.UpdateVPCRegion(ctx, model.ProjectId, model.VpcId, model.Region).UpdateVPCRegionPayload(payload)
122122
}
123123

124124
func outputResult(p *print.Printer, outputFormat, region, vpcLabel string, resp *iaas.RegionalVPC) error {

0 commit comments

Comments
 (0)