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
14 changes: 9 additions & 5 deletions internal/cmd/mariadb/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 MariaDB credentials: %w", err)
}

return outputResult(p, model, instanceLabel, resp)
return outputResult(p, model.OutputFormat, model.ShowPassword, instanceLabel, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -123,11 +123,15 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API
return req
}

func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *mariadb.CredentialsResponse) error {
if !model.ShowPassword {
func outputResult(p *print.Printer, outputFormat string, showPassword bool, instanceLabel string, resp *mariadb.CredentialsResponse) error {
if resp == nil {
return fmt.Errorf("response is nil")
}

if !showPassword && resp.HasRaw() && resp.Raw.Credentials != nil {
resp.Raw.Credentials.Password = utils.Ptr("hidden")
}
switch model.OutputFormat {
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand All @@ -151,7 +155,7 @@ func outputResult(p *print.Printer, model *inputModel, instanceLabel string, res
if username := resp.Raw.Credentials.Username; username != nil && *username != "" {
p.Outputf("Username: %s\n", *username)
}
if !model.ShowPassword {
if !showPassword {
p.Outputf("Password: <hidden>\n")
} else {
p.Outputf("Password: %s\n", utils.PtrString(resp.Raw.Credentials.Password))
Expand Down
37 changes: 37 additions & 0 deletions internal/cmd/mariadb/credentials/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,40 @@ func TestBuildRequest(t *testing.T) {
})
}
}

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

func outputResult(p *print.Printer, outputFormat string, credentials *mariadb.CredentialsResponse) error {
if credentials == nil {
return fmt.Errorf("credentials is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(credentials, "", " ")
Expand All @@ -131,7 +135,7 @@ func outputResult(p *print.Printer, outputFormat string, credentials *mariadb.Cr
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
35 changes: 35 additions & 0 deletions internal/cmd/mariadb/credentials/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,38 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
credentials *mariadb.CredentialsResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty credentials",
args: args{
credentials: &mariadb.CredentialsResponse{},
},
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)
}
})
}
}
42 changes: 42 additions & 0 deletions internal/cmd/mariadb/credentials/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,45 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
credentials []mariadb.CredentialsListItem
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "set empty credentials slice",
args: args{
credentials: []mariadb.CredentialsListItem{},
},
wantErr: false,
},
{
name: "set empty credential in credentials slice",
args: args{
credentials: []mariadb.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)
}
})
}
}
12 changes: 8 additions & 4 deletions internal/cmd/mariadb/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,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 @@ -251,8 +251,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient mariaDBClien
return req, nil
}

func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *mariadb.CreateInstanceResponse) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat string, async bool, projectLabel string, resp *mariadb.CreateInstanceResponse) 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 @@ -271,7 +275,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
37 changes: 37 additions & 0 deletions internal/cmd/mariadb/instance/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,40 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
projectLabel string
resp *mariadb.CreateInstanceResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty response",
args: args{
resp: &mariadb.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)
}
})
}
}
28 changes: 18 additions & 10 deletions internal/cmd/mariadb/instance/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API
}

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

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(instance, "", " ")
Expand All @@ -123,18 +127,22 @@ func outputResult(p *print.Printer, outputFormat string, instance *mariadb.Insta
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 instance.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 instance.Parameters != nil {
acl := (*instance.Parameters)[aclParameterKey]
aclStr, ok := acl.(string)
if ok {
if aclStr != "" {
table.AddSeparator()
table.AddRow("ACL", aclStr)
}
}
}
err := table.Display(p)
Expand Down
35 changes: 35 additions & 0 deletions internal/cmd/mariadb/instance/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,38 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
instance *mariadb.Instance
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty instance",
args: args{
instance: &mariadb.Instance{},
},
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)
}
})
}
}
11 changes: 9 additions & 2 deletions internal/cmd/mariadb/instance/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,18 @@ func outputResult(p *print.Printer, outputFormat string, instances []mariadb.Ins
table.SetHeader("ID", "NAME", "LAST OPERATION TYPE", "LAST OPERATION STATE")
for i := range instances {
instance := instances[i]

lastOperationType, lastOperationState := "", ""
if instance.LastOperation != nil {
lastOperationType = utils.PtrString(instance.LastOperation.Type)
lastOperationState = utils.PtrString(instance.LastOperation.State)
}

table.AddRow(
utils.PtrString(instance.InstanceId),
utils.PtrString(instance.Name),
utils.PtrString(instance.LastOperation.Type),
utils.PtrString(instance.LastOperation.State),
lastOperationType,
lastOperationState,
)
}
err := table.Display(p)
Expand Down
Loading
Loading