Skip to content
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
16 changes: 11 additions & 5 deletions internal/cmd/beta/network/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"

Expand Down Expand Up @@ -97,6 +98,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 Down Expand Up @@ -126,7 +129,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
s.Stop()
}

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

func outputResult(p *print.Printer, model *inputModel, projectLabel string, network *iaas.Network) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat string, async bool, projectLabel string, network *iaas.Network) error {
if network == nil {
return fmt.Errorf("network cannot be nil")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(network, "", " ")
if err != nil {
Expand All @@ -254,10 +260,10 @@ func outputResult(p *print.Printer, model *inputModel, projectLabel string, netw
return nil
default:
operationState := "Created"
if model.Async {
if async {
operationState = "Triggered creation of"
}
p.Outputf("%s network for project %q.\nNetwork ID: %s\n", operationState, projectLabel, *network.NetworkId)
p.Outputf("%s network for project %q.\nNetwork ID: %s\n", operationState, projectLabel, utils.PtrString(network.NetworkId))
return nil
}
}
36 changes: 36 additions & 0 deletions internal/cmd/beta/network/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,39 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
projectLabel string
network *iaas.Network
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty network",
args: args{
network: &iaas.Network{},
},
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.async, tt.args.projectLabel, tt.args.network); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2 changes: 2 additions & 0 deletions internal/cmd/beta/network/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.ErrorLevel, "get network name: %v", err)
networkLabel = model.NetworkId
} else if networkLabel == "" {
networkLabel = model.NetworkId
}

if !model.AssumeYes {
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/beta/network/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
}

func outputResult(p *print.Printer, outputFormat string, network *iaas.Network) error {
if network == nil {
return fmt.Errorf("network cannot be nil")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(network, "", " ")
Expand Down
34 changes: 34 additions & 0 deletions internal/cmd/beta/network/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
network *iaas.Network
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty network",
args: args{
network: &iaas.Network{},
},
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.network); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
7 changes: 3 additions & 4 deletions internal/cmd/beta/network/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,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 networks found for project %q\n", projectLabel)
return nil
Expand Down Expand Up @@ -155,10 +157,7 @@ func outputResult(p *print.Printer, outputFormat string, networks []iaas.Network
table.SetHeader("ID", "NAME", "STATUS", "PUBLIC IP", "PREFIXES", "ROUTED")

for _, network := range networks {
publicIp := ""
if network.PublicIp != nil {
publicIp = *network.PublicIp
}
publicIp := utils.PtrString(network.PublicIp)

routed := false
if network.Routed != nil {
Expand Down
36 changes: 36 additions & 0 deletions internal/cmd/beta/network/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,39 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
networks []iaas.Network
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "set empty network",
args: args{
networks: []iaas.Network{
{},
},
},
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.networks); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2 changes: 2 additions & 0 deletions internal/cmd/beta/network/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.ErrorLevel, "get network name: %v", err)
networkLabel = model.NetworkId
} else if networkLabel == "" {
networkLabel = model.NetworkId
}

if !model.AssumeYes {
Expand Down