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/secrets-manager/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ If you want to retry configuring the ACLs, you can do it via:
}
}

return outputResult(p, model, projectLabel, instanceId, resp)
return outputResult(p, model.OutputFormat, projectLabel, instanceId, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -157,18 +157,22 @@ func buildUpdateACLsRequest(ctx context.Context, model *inputModel, instanceId s
return req
}

func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId string, resp *secretsmanager.Instance) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat, projectLabel, instanceId string, instance *secretsmanager.Instance) error {
if instance == nil {
return fmt.Errorf("instance is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
details, err := json.MarshalIndent(instance, "", " ")
if err != nil {
return fmt.Errorf("marshal Secrets Manager instance: %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(instance, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal Secrets Manager instance: %w", err)
}
Expand Down
36 changes: 36 additions & 0 deletions internal/cmd/secrets-manager/instance/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,39 @@ func TestBuildCreateACLRequests(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
projectLabel string
instanceId string
instance *secretsmanager.Instance
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "empty instance",
args: args{
instance: &secretsmanager.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.projectLabel, tt.args.instanceId, tt.args.instance); (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 @@ -111,6 +111,12 @@ func buildListACLsRequest(ctx context.Context, model *inputModel, apiClient *sec
}

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

output := struct {
*secretsmanager.Instance
*secretsmanager.ListACLsResponse
Expand Down Expand Up @@ -148,7 +154,7 @@ func outputResult(p *print.Printer, outputFormat string, instance *secretsmanage
table.AddRow("CREATION DATE", utils.PtrString(instance.CreationStartDate))
table.AddSeparator()
// Only show ACL if it's present and not empty
if aclList != nil && aclList.Acls != nil && len(*aclList.Acls) > 0 {
if aclList.Acls != nil && len(*aclList.Acls) > 0 {
var cidrs []string

for _, acl := range *aclList.Acls {
Expand Down
50 changes: 50 additions & 0 deletions internal/cmd/secrets-manager/instance/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,53 @@ func TestBuildGetACLsRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
instance *secretsmanager.Instance
aclList *secretsmanager.ListACLsResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "missing acl",
args: args{
aclList: &secretsmanager.ListACLsResponse{},
},
wantErr: true,
},
{
name: "missing instance",
args: args{
instance: &secretsmanager.Instance{},
},
wantErr: true,
},
{
name: "empty instance and empty acl",
args: args{
instance: &secretsmanager.Instance{},
aclList: &secretsmanager.ListACLsResponse{},
},
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, tt.args.aclList); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
41 changes: 41 additions & 0 deletions internal/cmd/secrets-manager/instance/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
instances []secretsmanager.Instance
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "empty instances slice",
args: args{
instances: []secretsmanager.Instance{},
},
wantErr: false,
},
{
name: "empty instance in instances slice",
args: args{
instances: []secretsmanager.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.instances); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
24 changes: 14 additions & 10 deletions internal/cmd/secrets-manager/user/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("create Secrets Manager user: %w", err)
}

return outputResult(p, model, instanceLabel, resp)
return outputResult(p, model.OutputFormat, instanceLabel, resp)
},
}

Expand Down Expand Up @@ -137,30 +137,34 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *secretsmana
return req
}

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

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
details, err := json.MarshalIndent(user, "", " ")
if err != nil {
return fmt.Errorf("marshal Secrets Manager user: %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(user, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal Secrets Manager user: %w", err)
}
p.Outputln(string(details))

return nil
default:
p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, utils.PtrString(resp.Id))
p.Outputf("Username: %s\n", utils.PtrString(resp.Username))
p.Outputf("Password: %s\n", utils.PtrString(resp.Password))
p.Outputf("Description: %s\n", utils.PtrString(resp.Description))
p.Outputf("Write Access: %s\n", utils.PtrString(resp.Write))
p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, utils.PtrString(user.Id))
p.Outputf("Username: %s\n", utils.PtrString(user.Username))
p.Outputf("Password: %s\n", utils.PtrString(user.Password))
p.Outputf("Description: %s\n", utils.PtrString(user.Description))
p.Outputf("Write Access: %s\n", utils.PtrString(user.Write))

return nil
}
Expand Down
35 changes: 35 additions & 0 deletions internal/cmd/secrets-manager/user/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,38 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
instanceLabel string
user *secretsmanager.User
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "empty user",
args: args{
user: &secretsmanager.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.user); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
34 changes: 34 additions & 0 deletions internal/cmd/secrets-manager/user/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
user secretsmanager.User
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "empty user",
args: args{
user: secretsmanager.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.user); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
41 changes: 41 additions & 0 deletions internal/cmd/secrets-manager/user/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
users []secretsmanager.User
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "empty users slice",
args: args{
users: []secretsmanager.User{},
},
wantErr: false,
},
{
name: "empty user in users slice",
args: args{
users: []secretsmanager.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.users); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading