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
21 changes: 13 additions & 8 deletions internal/cmd/observability/credentials/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("create credentials for Observability instance: %w", err)
}

return outputResult(p, model, instanceLabel, resp)
return outputResult(p, model.OutputFormat, instanceLabel, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -110,8 +110,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *observabili
return req
}

func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *observability.CreateCredentialsResponse) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat, instanceLabel string, resp *observability.CreateCredentialsResponse) error {
if resp == nil {
return fmt.Errorf("response is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand All @@ -130,13 +134,14 @@ func outputResult(p *print.Printer, model *inputModel, instanceLabel string, res
return nil
default:
p.Outputf("Created credentials for instance %q.\n\n", instanceLabel)
// The username field cannot be set by the user, so we only display it if it's not returned empty
username := *resp.Credentials.Username
if username != "" {
p.Outputf("Username: %s\n", username)
}

if resp.Credentials != nil {
// The username field cannot be set by the user, so we only display it if it's not returned empty
username := *resp.Credentials.Username
if username != "" {
p.Outputf("Username: %s\n", username)
}

p.Outputf("Password: %s\n", utils.PtrString(resp.Credentials.Password))
}
return nil
Expand Down
46 changes: 46 additions & 0 deletions internal/cmd/observability/credentials/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/observability"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -188,3 +190,47 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
instanceLabel string
resp *observability.CreateCredentialsResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty response",
args: args{
resp: &observability.CreateCredentialsResponse{},
},
wantErr: false,
},
{
name: "set response with credentials",
args: args{
resp: &observability.CreateCredentialsResponse{
Credentials: observability.NewCredentials(utils.Ptr("dummy-pw"), utils.Ptr("dummy-user")),
},
},
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.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
42 changes: 42 additions & 0 deletions internal/cmd/observability/credentials/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -205,3 +206,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
credentials []observability.ServiceKeysList
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "set empty credentials slice",
args: args{
credentials: []observability.ServiceKeysList{},
},
wantErr: false,
},
{
name: "set empty credential in credentials slice",
args: args{
credentials: []observability.ServiceKeysList{{}},
},
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.credentials); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
16 changes: 11 additions & 5 deletions internal/cmd/observability/grafana/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("get instance: %w", err)
}

return outputResult(p, model, grafanaConfigsResp, instanceResp)
return outputResult(p, model.OutputFormat, model.ShowPassword, grafanaConfigsResp, instanceResp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -124,8 +124,14 @@ func buildGetInstanceRequest(ctx context.Context, model *inputModel, apiClient *
return req
}

func outputResult(p *print.Printer, inputModel *inputModel, grafanaConfigs *observability.GrafanaConfigs, instance *observability.GetInstanceResponse) error {
switch inputModel.OutputFormat {
func outputResult(p *print.Printer, outputFormat string, showPassword bool, grafanaConfigs *observability.GrafanaConfigs, instance *observability.GetInstanceResponse) error {
if instance == nil || instance.Instance == nil {
return fmt.Errorf("instance or instance content is nil")
} else if grafanaConfigs == nil {
return fmt.Errorf("grafanaConfigs is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(grafanaConfigs, "", " ")
if err != nil {
Expand All @@ -143,8 +149,8 @@ func outputResult(p *print.Printer, inputModel *inputModel, grafanaConfigs *obse

return nil
default:
initialAdminPassword := *instance.Instance.GrafanaAdminPassword
if !inputModel.ShowPassword {
initialAdminPassword := utils.PtrString(instance.Instance.GrafanaAdminPassword)
if !showPassword {
initialAdminPassword = "<hidden>"
}

Expand Down
55 changes: 55 additions & 0 deletions internal/cmd/observability/grafana/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,58 @@ func TestBuildGetInstanceRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
showPassword bool
grafanaConfig *observability.GrafanaConfigs
instance *observability.GetInstanceResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set grafana configs but no instance",
args: args{
grafanaConfig: &observability.GrafanaConfigs{},
},
wantErr: true,
},
{
name: "set instance but no grafana config",
args: args{
instance: &observability.GetInstanceResponse{
Instance: &observability.InstanceSensitiveData{},
},
},
wantErr: true,
},
{
name: "set instance and grafana configs",
args: args{
grafanaConfig: &observability.GrafanaConfigs{},
instance: &observability.GetInstanceResponse{
Instance: &observability.InstanceSensitiveData{},
},
},
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.showPassword, tt.args.grafanaConfig, tt.args.instance); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
12 changes: 8 additions & 4 deletions internal/cmd/observability/instance/create/create.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, projectLabel, resp)
return outputResult(p, model.OutputFormat, model.Async, projectLabel, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -200,8 +200,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient observabilit
return req, nil
}

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

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand All @@ -220,7 +224,7 @@ 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 instance for project %q. Instance ID: %s\n", operationState, projectLabel, utils.PtrString(resp.InstanceId))
Expand Down
36 changes: 36 additions & 0 deletions internal/cmd/observability/instance/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,39 @@ func TestBuildRequest(t *testing.T) {
})
}
}

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

func outputResult(p *print.Printer, outputFormat string, instance *observability.GetInstanceResponse) error {
if instance == nil {
return fmt.Errorf("instance is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(instance, "", " ")
Expand Down
34 changes: 34 additions & 0 deletions internal/cmd/observability/instance/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
instance *observability.GetInstanceResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "empty instance",
args: args{
instance: &observability.GetInstanceResponse{},
},
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.instance); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading
Loading