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
20 changes: 16 additions & 4 deletions internal/cmd/redis/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 Redis credentials: %w", err)
}

return outputResult(p, model, instanceLabel, resp)
return outputResult(p, *model, instanceLabel, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -123,8 +123,20 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *redis.APICl
return req
}

func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *redis.CredentialsResponse) error {
func outputResult(p *print.Printer, model inputModel, instanceLabel string, resp *redis.CredentialsResponse) error {
if model.GlobalFlagModel == nil {
return fmt.Errorf("no global flags defined")
}
if resp == nil {
return fmt.Errorf("no response defined")
}
if !model.ShowPassword {
if resp.Raw == nil {
resp.Raw = &redis.RawCredentials{}
}
if resp.Raw.Credentials == nil {
resp.Raw.Credentials = &redis.Credentials{}
}
resp.Raw.Credentials.Password = utils.Ptr("hidden")
}

Expand All @@ -146,11 +158,11 @@ func outputResult(p *print.Printer, model *inputModel, instanceLabel string, res

return nil
default:
p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id)
p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, utils.PtrString(resp.Id))
// The username field cannot be set by the user, so we only display it if it's not returned empty
if resp.HasRaw() && resp.Raw.Credentials != nil {
if username := resp.Raw.Credentials.Username; username != nil && *username != "" {
p.Outputf("Username: %s\n", *username)
p.Outputf("Username: %s\n", utils.PtrString(username))
}
if !model.ShowPassword {
p.Outputf("Password: <hidden>\n")
Expand Down
38 changes: 35 additions & 3 deletions internal/cmd/redis/credentials/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"testing"

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

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-sdk-go/services/redis"
)

Expand Down Expand Up @@ -200,3 +199,36 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
model inputModel
instanceLabel string
resp *redis.CredentialsResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{model: inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{}}, resp: &redis.CredentialsResponse{}},
wantErr: false,
},
{
name: "nil response",
args: args{model: inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{}}},
wantErr: true,
},
}
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.model, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
5 changes: 4 additions & 1 deletion internal/cmd/redis/credentials/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *redis.APICl
}

func outputResult(p *print.Printer, outputFormat string, credentials *redis.CredentialsResponse) error {
if credentials == nil {
return fmt.Errorf("no credentials passed")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(credentials, "", " ")
Expand All @@ -131,7 +134,7 @@ func outputResult(p *print.Printer, outputFormat string, credentials *redis.Cred
return nil
default:
table := tables.NewTable()
table.AddRow("ID", *credentials.Id)
table.AddRow("ID", utils.PtrString(credentials.Id))
table.AddSeparator()
// The username field cannot be set by the user so we only display it if it's not returned empty
if credentials.HasRaw() && credentials.Raw.Credentials != nil {
Expand Down
39 changes: 36 additions & 3 deletions internal/cmd/redis/credentials/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"testing"

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

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-sdk-go/services/redis"
)

Expand Down Expand Up @@ -243,3 +242,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
credentials *redis.CredentialsResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{
credentials: &redis.CredentialsResponse{},
},
wantErr: false,
},
{
name: "nil response",
args: args{},
wantErr: true,
},
}
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)
}
})
}
}
44 changes: 40 additions & 4 deletions internal/cmd/redis/credentials/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"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"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"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/redis"
)

Expand Down Expand Up @@ -207,3 +206,40 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
credentials []redis.CredentialsListItem
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "slice with empty element",
args: args{
outputFormat: "",
credentials: []redis.CredentialsListItem{
{},
},
},
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)
}
})
}
}
9 changes: 9 additions & 0 deletions internal/cmd/redis/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient redisClient)
}

func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId string, resp *redis.CreateInstanceResponse) error {
if model == nil {
return fmt.Errorf("no model passed")
}
if model.GlobalFlagModel == nil {
return fmt.Errorf("no global flags passed")
}
if resp == nil {
return fmt.Errorf("no response defined")
}
switch model.OutputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
Expand Down
50 changes: 46 additions & 4 deletions internal/cmd/redis/instance/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"fmt"
"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"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"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/redis"
)

Expand Down Expand Up @@ -463,3 +462,46 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
model *inputModel
projectLabel string
instanceId string
resp *redis.CreateInstanceResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{
model: &inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{}},
projectLabel: "",
instanceId: testMonitoringInstanceId,
resp: &redis.CreateInstanceResponse{},
},
wantErr: false,
},
{
name: "nil response",
args: args{
model: &inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{}},
projectLabel: "",
instanceId: testMonitoringInstanceId,
},
wantErr: true,
},
}
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.model, tt.args.projectLabel, tt.args.instanceId, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
29 changes: 18 additions & 11 deletions internal/cmd/redis/instance/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *redis.APICl
}

func outputResult(p *print.Printer, outputFormat string, instance *redis.Instance) error {
if instance == nil {
return fmt.Errorf("no instance passed")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(instance, "", " ")
Expand All @@ -123,18 +126,22 @@ func outputResult(p *print.Printer, outputFormat string, instance *redis.Instanc
table.AddSeparator()
table.AddRow("NAME", utils.PtrString(instance.Name))
table.AddSeparator()
table.AddRow("LAST OPERATION TYPE", utils.PtrString(instance.LastOperation.Type))
table.AddSeparator()
table.AddRow("LAST OPERATION STATE", utils.PtrString(instance.LastOperation.State))
table.AddSeparator()
if lastOperation := instance.LastOperation; lastOperation != nil {
table.AddRow("LAST OPERATION TYPE", utils.PtrString(instance.LastOperation.Type))
table.AddSeparator()
table.AddRow("LAST OPERATION STATE", utils.PtrString(instance.LastOperation.State))
table.AddSeparator()
}
table.AddRow("PLAN ID", utils.PtrString(instance.PlanId))
// Only show ACL if it's present and not empty
acl := (*instance.Parameters)[aclParameterKey]
aclStr, ok := acl.(string)
if ok {
if aclStr != "" {
table.AddSeparator()
table.AddRow("ACL", aclStr)
if parameters := instance.Parameters; parameters != nil {
// Only show ACL if it's present and not empty
acl := (*parameters)[aclParameterKey]
aclStr, ok := acl.(string)
if ok {
if aclStr != "" {
table.AddSeparator()
table.AddRow("ACL", aclStr)
}
}
}
err := table.Display(p)
Expand Down
Loading