Skip to content

fix: stackit beta public-ip commands - add nil pointer checks and tests for the outputResult functions #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/cmd/beta/public-ip/associate/associate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.ErrorLevel, "get public IP: %v", err)
publicIpLabel = model.PublicIpId
} else if publicIpLabel == "" {
publicIpLabel = model.PublicIpId
}

if !model.AssumeYes {
Expand Down
8 changes: 5 additions & 3 deletions internal/cmd/beta/public-ip/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
} else if projectLabel == "" {
projectLabel = model.ProjectId
}

if !model.AssumeYes {
Expand All @@ -84,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("create public IP: %w", err)
}

return outputResult(p, model, projectLabel, resp)
return outputResult(p, model.OutputFormat, projectLabel, *resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -140,8 +142,8 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return req.CreatePublicIPPayload(payload)
}

func outputResult(p *print.Printer, model *inputModel, projectLabel string, publicIp *iaas.PublicIp) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat, projectLabel string, publicIp iaas.PublicIp) error {
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(publicIp, "", " ")
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions internal/cmd/beta/public-ip/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,31 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
projectLabel string
publicIp iaas.PublicIp
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.publicIp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
4 changes: 3 additions & 1 deletion internal/cmd/beta/public-ip/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.ErrorLevel, "get public IP: %v", err)
publicIpLabel = model.PublicIpId
} else if publicIpLabel == "" {
publicIpLabel = model.PublicIpId
}

if !model.AssumeYes {
Expand All @@ -74,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("delete public IP: %w", err)
}

p.Info("Deleted public IP %q\n", model.PublicIpId)
p.Info("Deleted public IP %q\n", publicIpLabel)
return nil
},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/beta/public-ip/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("read public IP: %w", err)
}

return outputResult(p, model.OutputFormat, resp)
return outputResult(p, model.OutputFormat, *resp)
},
}
return cmd
Expand Down Expand Up @@ -101,7 +101,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return apiClient.GetPublicIP(ctx, model.ProjectId, model.PublicIpId)
}

func outputResult(p *print.Printer, outputFormat string, publicIp *iaas.PublicIp) error {
func outputResult(p *print.Printer, outputFormat string, publicIp iaas.PublicIp) error {
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(publicIp, "", " ")
Expand Down
27 changes: 27 additions & 0 deletions internal/cmd/beta/public-ip/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,30 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
publicIp iaas.PublicIp
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.publicIp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2 changes: 2 additions & 0 deletions internal/cmd/beta/public-ip/disassociate/disassociate.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.ErrorLevel, "get public IP: %v", err)
publicIpLabel = model.PublicIpId
} else if publicIpLabel == "" {
publicIpLabel = model.PublicIpId
}

if !model.AssumeYes {
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/beta/public-ip/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
} else if projectLabel == "" {
projectLabel = model.ProjectId
}
p.Info("No public IPs found for project %q\n", projectLabel)
return nil
Expand Down
27 changes: 27 additions & 0 deletions internal/cmd/beta/public-ip/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,30 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
publicIps []iaas.PublicIp
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.publicIps); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}