Skip to content

Commit 40967df

Browse files
committed
- add nil pointer checks
- add tests for the outputResult functions within the network commands
1 parent 6213bb5 commit 40967df

File tree

8 files changed

+131
-9
lines changed

8 files changed

+131
-9
lines changed

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

Lines changed: 12 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

@@ -98,6 +99,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
9899
p.Debug(print.ErrorLevel, "get project name: %v", err)
99100
projectLabel = model.ProjectId
100101
}
102+
if projectLabel == "" {
103+
projectLabel = model.ProjectId
104+
}
101105

102106
if !model.AssumeYes {
103107
prompt := fmt.Sprintf("Are you sure you want to create a network for project %q?", projectLabel)
@@ -126,7 +130,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
126130
s.Stop()
127131
}
128132

129-
return outputResult(p, model, projectLabel, resp)
133+
return outputResult(p, model.OutputFormat, model.Async, projectLabel, resp)
130134
},
131135
}
132136
configureFlags(cmd)
@@ -234,8 +238,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
234238
return req.CreateNetworkPayload(payload)
235239
}
236240

237-
func outputResult(p *print.Printer, model *inputModel, projectLabel string, network *iaas.Network) error {
238-
switch model.OutputFormat {
241+
func outputResult(p *print.Printer, outputFormat string, async bool, projectLabel string, network *iaas.Network) error {
242+
if network == nil {
243+
return fmt.Errorf("network cannot be nil")
244+
}
245+
switch outputFormat {
239246
case print.JSONOutputFormat:
240247
details, err := json.MarshalIndent(network, "", " ")
241248
if err != nil {
@@ -254,10 +261,10 @@ func outputResult(p *print.Printer, model *inputModel, projectLabel string, netw
254261
return nil
255262
default:
256263
operationState := "Created"
257-
if model.Async {
264+
if async {
258265
operationState = "Triggered creation of"
259266
}
260-
p.Outputf("%s network for project %q.\nNetwork ID: %s\n", operationState, projectLabel, *network.NetworkId)
267+
p.Outputf("%s network for project %q.\nNetwork ID: %s\n", operationState, projectLabel, utils.PtrString(network.NetworkId))
261268
return nil
262269
}
263270
}

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
6161
p.Debug(print.ErrorLevel, "get network name: %v", err)
6262
networkLabel = model.NetworkId
6363
}
64+
if networkLabel == "" {
65+
networkLabel = model.NetworkId
66+
}
6467

6568
if !model.AssumeYes {
6669
prompt := fmt.Sprintf("Are you sure you want to delete network %q?", networkLabel)

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
7676
p.Debug(print.ErrorLevel, "get project name: %v", err)
7777
projectLabel = model.ProjectId
7878
}
79+
if projectLabel == "" {
80+
projectLabel = model.ProjectId
81+
}
7982
p.Info("No networks found for project %q\n", projectLabel)
8083
return nil
8184
}
@@ -155,10 +158,7 @@ func outputResult(p *print.Printer, outputFormat string, networks []iaas.Network
155158
table.SetHeader("ID", "NAME", "STATUS", "PUBLIC IP", "PREFIXES", "ROUTED")
156159

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

163163
routed := false
164164
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
8686
p.Debug(print.ErrorLevel, "get network name: %v", err)
8787
networkLabel = model.NetworkId
8888
}
89+
if networkLabel == "" {
90+
networkLabel = model.NetworkId
91+
}
8992

9093
if !model.AssumeYes {
9194
prompt := fmt.Sprintf("Are you sure you want to update network %q?", networkLabel)

0 commit comments

Comments
 (0)