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 1 commit
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
3 changes: 3 additions & 0 deletions internal/cmd/beta/public-ip/associate/associate.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
p.Debug(print.ErrorLevel, "get public IP: %v", err)
publicIpLabel = model.PublicIpId
}
if publicIpLabel == "" {
publicIpLabel = model.PublicIpId
}

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to associate public IP %q with resource %v?", publicIpLabel, *model.AssociatedResourceId)
Expand Down
9 changes: 6 additions & 3 deletions internal/cmd/beta/public-ip/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
p.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
}
if projectLabel == "" {
projectLabel = model.ProjectId
}

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to create a public IP for project %q?", projectLabel)
Expand All @@ -84,7 +87,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 +143,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)
}
})
}
}
5 changes: 4 additions & 1 deletion internal/cmd/beta/public-ip/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
p.Debug(print.ErrorLevel, "get public IP: %v", err)
publicIpLabel = model.PublicIpId
}
if publicIpLabel == "" {
publicIpLabel = model.PublicIpId
}

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to delete public IP %q? (This cannot be undone)", publicIpLabel)
Expand All @@ -74,7 +77,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)
}
})
}
}
3 changes: 3 additions & 0 deletions internal/cmd/beta/public-ip/disassociate/disassociate.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
p.Debug(print.ErrorLevel, "get public IP: %v", err)
publicIpLabel = model.PublicIpId
}
if publicIpLabel == "" {
publicIpLabel = model.PublicIpId
}

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to disassociate public IP %q from the associated resource %q?", publicIpLabel, associatedResourceId)
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/beta/public-ip/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
p.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
}
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)
}
})
}
}
Loading