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
18 changes: 11 additions & 7 deletions internal/cmd/ske/cluster/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,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 @@ -197,18 +197,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie
return req
}

func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *ske.Cluster) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat string, async bool, projectLabel string, cluster *ske.Cluster) error {
if cluster == nil {
return fmt.Errorf("cluster is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
details, err := json.MarshalIndent(cluster, "", " ")
if err != nil {
return fmt.Errorf("marshal SKE cluster: %w", err)
}
p.Outputln(string(details))

return nil
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
details, err := yaml.MarshalWithOptions(cluster, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal SKE cluster: %w", err)
}
Expand All @@ -217,10 +221,10 @@ func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp
return nil
default:
operationState := "Created"
if model.Async {
if async {
operationState = "Triggered creation of"
}
p.Outputf("%s cluster for project %q. Cluster name: %s\n", operationState, projectLabel, utils.PtrString(resp.Name))
p.Outputf("%s cluster for project %q. Cluster name: %s\n", operationState, projectLabel, utils.PtrString(cluster.Name))
return nil
}
}
36 changes: 36 additions & 0 deletions internal/cmd/ske/cluster/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,39 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
projectLabel string
cluster *ske.Cluster
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "empty cluster",
args: args{
cluster: &ske.Cluster{},
},
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.cluster); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
16 changes: 12 additions & 4 deletions internal/cmd/ske/cluster/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie
}

func outputResult(p *print.Printer, outputFormat string, cluster *ske.Cluster) error {
if cluster == nil {
return fmt.Errorf("cluster is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(cluster, "", " ")
Expand All @@ -123,10 +127,14 @@ func outputResult(p *print.Printer, outputFormat string, cluster *ske.Cluster) e
table := tables.NewTable()
table.AddRow("NAME", utils.PtrString(cluster.Name))
table.AddSeparator()
table.AddRow("STATE", utils.PtrString(cluster.Status.Aggregated))
table.AddSeparator()
table.AddRow("VERSION", utils.PtrString(cluster.Kubernetes.Version))
table.AddSeparator()
if cluster.HasStatus() {
table.AddRow("STATE", utils.PtrString(cluster.Status.Aggregated))
table.AddSeparator()
}
if cluster.Kubernetes != nil {
table.AddRow("VERSION", utils.PtrString(cluster.Kubernetes.Version))
table.AddSeparator()
}
table.AddRow("ACL", acl)
err := table.Display(p)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions internal/cmd/ske/cluster/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
cluster *ske.Cluster
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "empty cluster",
args: args{
cluster: &ske.Cluster{},
},
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.cluster); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie
}

func outputResult(p *print.Printer, filePath *string, payload *ske.CreateOrUpdateClusterPayload) error {
if payload == nil {
return fmt.Errorf("payload is nil")
}

payloadBytes, err := json.MarshalIndent(*payload, "", " ")
if err != nil {
return fmt.Errorf("marshal payload: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,45 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
filePath *string
payload *ske.CreateOrUpdateClusterPayload
}
filePathDummy := "/dummy.txt"
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "missing payload",
args: args{
filePath: &filePathDummy,
},
wantErr: true,
},
{
name: "missing file path",
args: args{
payload: &ske.CreateOrUpdateClusterPayload{},
},
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.filePath, tt.args.payload); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
17 changes: 14 additions & 3 deletions internal/cmd/ske/cluster/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,22 @@ func outputResult(p *print.Printer, outputFormat string, clusters []ske.Cluster)
if c.Extensions != nil && c.Extensions.Argus != nil && *c.Extensions.Argus.Enabled {
monitoring = "Enabled"
}
statusAggregated, kubernetesVersion := "", ""
if c.HasStatus() {
statusAggregated = utils.PtrString(c.Status.Aggregated)
}
if c.Kubernetes != nil {
kubernetesVersion = utils.PtrString(c.Kubernetes.Version)
}
countNodepools := 0
if c.Nodepools != nil {
countNodepools = len(*c.Nodepools)
}
table.AddRow(
utils.PtrString(c.Name),
utils.PtrString(c.Status.Aggregated),
utils.PtrString(c.Kubernetes.Version),
len(*c.Nodepools),
statusAggregated,
kubernetesVersion,
countNodepools,
monitoring,
)
}
Expand Down
41 changes: 41 additions & 0 deletions internal/cmd/ske/cluster/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
clusters []ske.Cluster
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "empty clusters slice",
args: args{
clusters: []ske.Cluster{},
},
wantErr: false,
},
{
name: "empty cluster in clusters slice",
args: args{
clusters: []ske.Cluster{{}},
},
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.clusters); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
18 changes: 11 additions & 7 deletions internal/cmd/ske/cluster/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
s.Stop()
}

return outputResult(p, model, resp)
return outputResult(p, model.OutputFormat, model.Async, model.ClusterName, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -159,18 +159,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie
return req
}

func outputResult(p *print.Printer, model *inputModel, resp *ske.Cluster) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat string, async bool, clusterName string, cluster *ske.Cluster) error {
if cluster == nil {
return fmt.Errorf("cluster is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
details, err := json.MarshalIndent(cluster, "", " ")
if err != nil {
return fmt.Errorf("marshal SKE cluster: %w", err)
}
p.Outputln(string(details))

return nil
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
details, err := yaml.MarshalWithOptions(cluster, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal SKE cluster: %w", err)
}
Expand All @@ -179,10 +183,10 @@ func outputResult(p *print.Printer, model *inputModel, resp *ske.Cluster) error
return nil
default:
operationState := "Updated"
if model.Async {
if async {
operationState = "Triggered update of"
}
p.Info("%s cluster %q\n", operationState, model.ClusterName)
p.Info("%s cluster %q\n", operationState, clusterName)
return nil
}
}
36 changes: 36 additions & 0 deletions internal/cmd/ske/cluster/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,39 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
clusterName string
cluster *ske.Cluster
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "empty cluster",
args: args{
cluster: &ske.Cluster{},
},
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.clusterName, tt.args.cluster); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
8 changes: 7 additions & 1 deletion internal/cmd/ske/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceenab
}

func outputResult(p *print.Printer, outputFormat string, project *serviceenablement.ServiceStatus, projectId string) error {
if project == nil {
return fmt.Errorf("project is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(project, "", " ")
Expand All @@ -108,7 +112,9 @@ func outputResult(p *print.Printer, outputFormat string, project *serviceenablem
table := tables.NewTable()
table.AddRow("ID", projectId)
table.AddSeparator()
table.AddRow("STATE", utils.PtrString(project.State))
if project.HasState() {
table.AddRow("STATE", utils.PtrString(project.State))
}
err := table.Display(p)
if err != nil {
return fmt.Errorf("render table: %w", err)
Expand Down
Loading
Loading