Skip to content

Commit b3fb7b1

Browse files
authored
fix: stackit beta network commands - add nil pointer checks and tests for the outputResult functions (#607)
* add nil pointer checks * add tests for the outputResult functions within the network command
1 parent 4e8d53f commit b3fb7b1

File tree

8 files changed

+127
-9
lines changed

8 files changed

+127
-9
lines changed

internal/cmd/beta/network/create/create.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
1616
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
1717
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
18+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1819
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1920
"github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"
2021

@@ -97,6 +98,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
9798
if err != nil {
9899
p.Debug(print.ErrorLevel, "get project name: %v", err)
99100
projectLabel = model.ProjectId
101+
} else if projectLabel == "" {
102+
projectLabel = model.ProjectId
100103
}
101104

102105
if !model.AssumeYes {
@@ -126,7 +129,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
126129
s.Stop()
127130
}
128131

129-
return outputResult(p, model, projectLabel, resp)
132+
return outputResult(p, model.OutputFormat, model.Async, projectLabel, resp)
130133
},
131134
}
132135
configureFlags(cmd)
@@ -234,8 +237,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
234237
return req.CreateNetworkPayload(payload)
235238
}
236239

237-
func outputResult(p *print.Printer, model *inputModel, projectLabel string, network *iaas.Network) error {
238-
switch model.OutputFormat {
240+
func outputResult(p *print.Printer, outputFormat string, async bool, projectLabel string, network *iaas.Network) error {
241+
if network == nil {
242+
return fmt.Errorf("network cannot be nil")
243+
}
244+
switch outputFormat {
239245
case print.JSONOutputFormat:
240246
details, err := json.MarshalIndent(network, "", " ")
241247
if err != nil {
@@ -254,10 +260,10 @@ func outputResult(p *print.Printer, model *inputModel, projectLabel string, netw
254260
return nil
255261
default:
256262
operationState := "Created"
257-
if model.Async {
263+
if async {
258264
operationState = "Triggered creation of"
259265
}
260-
p.Outputf("%s network for project %q.\nNetwork ID: %s\n", operationState, projectLabel, *network.NetworkId)
266+
p.Outputf("%s network for project %q.\nNetwork ID: %s\n", operationState, projectLabel, utils.PtrString(network.NetworkId))
261267
return nil
262268
}
263269
}

internal/cmd/beta/network/create/create_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,39 @@ func TestBuildRequest(t *testing.T) {
423423
})
424424
}
425425
}
426+
427+
func TestOutputResult(t *testing.T) {
428+
type args struct {
429+
outputFormat string
430+
async bool
431+
projectLabel string
432+
network *iaas.Network
433+
}
434+
tests := []struct {
435+
name string
436+
args args
437+
wantErr bool
438+
}{
439+
{
440+
name: "empty",
441+
args: args{},
442+
wantErr: true,
443+
},
444+
{
445+
name: "set empty network",
446+
args: args{
447+
network: &iaas.Network{},
448+
},
449+
wantErr: false,
450+
},
451+
}
452+
p := print.NewPrinter()
453+
p.Cmd = NewCmd(p)
454+
for _, tt := range tests {
455+
t.Run(tt.name, func(t *testing.T) {
456+
if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.projectLabel, tt.args.network); (err != nil) != tt.wantErr {
457+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
458+
}
459+
})
460+
}
461+
}

internal/cmd/beta/network/delete/delete.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
6060
if err != nil {
6161
p.Debug(print.ErrorLevel, "get network name: %v", err)
6262
networkLabel = model.NetworkId
63+
} else if networkLabel == "" {
64+
networkLabel = model.NetworkId
6365
}
6466

6567
if !model.AssumeYes {

internal/cmd/beta/network/describe/describe.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
101101
}
102102

103103
func outputResult(p *print.Printer, outputFormat string, network *iaas.Network) error {
104+
if network == nil {
105+
return fmt.Errorf("network cannot be nil")
106+
}
104107
switch outputFormat {
105108
case print.JSONOutputFormat:
106109
details, err := json.MarshalIndent(network, "", " ")

internal/cmd/beta/network/describe/describe_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,37 @@ func TestBuildRequest(t *testing.T) {
216216
})
217217
}
218218
}
219+
220+
func TestOutputResult(t *testing.T) {
221+
type args struct {
222+
outputFormat string
223+
network *iaas.Network
224+
}
225+
tests := []struct {
226+
name string
227+
args args
228+
wantErr bool
229+
}{
230+
{
231+
name: "empty",
232+
args: args{},
233+
wantErr: true,
234+
},
235+
{
236+
name: "set empty network",
237+
args: args{
238+
network: &iaas.Network{},
239+
},
240+
wantErr: false,
241+
},
242+
}
243+
p := print.NewPrinter()
244+
p.Cmd = NewCmd(p)
245+
for _, tt := range tests {
246+
t.Run(tt.name, func(t *testing.T) {
247+
if err := outputResult(p, tt.args.outputFormat, tt.args.network); (err != nil) != tt.wantErr {
248+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
249+
}
250+
})
251+
}
252+
}

internal/cmd/beta/network/list/list.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
7575
if err != nil {
7676
p.Debug(print.ErrorLevel, "get project name: %v", err)
7777
projectLabel = model.ProjectId
78+
} else if projectLabel == "" {
79+
projectLabel = model.ProjectId
7880
}
7981
p.Info("No networks found for project %q\n", projectLabel)
8082
return nil
@@ -155,10 +157,7 @@ func outputResult(p *print.Printer, outputFormat string, networks []iaas.Network
155157
table.SetHeader("ID", "NAME", "STATUS", "PUBLIC IP", "PREFIXES", "ROUTED")
156158

157159
for _, network := range networks {
158-
publicIp := ""
159-
if network.PublicIp != nil {
160-
publicIp = *network.PublicIp
161-
}
160+
publicIp := utils.PtrString(network.PublicIp)
162161

163162
routed := false
164163
if network.Routed != nil {

internal/cmd/beta/network/list/list_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,39 @@ func TestBuildRequest(t *testing.T) {
188188
})
189189
}
190190
}
191+
192+
func TestOutputResult(t *testing.T) {
193+
type args struct {
194+
outputFormat string
195+
networks []iaas.Network
196+
}
197+
tests := []struct {
198+
name string
199+
args args
200+
wantErr bool
201+
}{
202+
{
203+
name: "empty",
204+
args: args{},
205+
wantErr: false,
206+
},
207+
{
208+
name: "set empty network",
209+
args: args{
210+
networks: []iaas.Network{
211+
{},
212+
},
213+
},
214+
wantErr: false,
215+
},
216+
}
217+
p := print.NewPrinter()
218+
p.Cmd = NewCmd(p)
219+
for _, tt := range tests {
220+
t.Run(tt.name, func(t *testing.T) {
221+
if err := outputResult(p, tt.args.outputFormat, tt.args.networks); (err != nil) != tt.wantErr {
222+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
223+
}
224+
})
225+
}
226+
}

internal/cmd/beta/network/update/update.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
8585
if err != nil {
8686
p.Debug(print.ErrorLevel, "get network name: %v", err)
8787
networkLabel = model.NetworkId
88+
} else if networkLabel == "" {
89+
networkLabel = model.NetworkId
8890
}
8991

9092
if !model.AssumeYes {

0 commit comments

Comments
 (0)