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
6 changes: 3 additions & 3 deletions internal/cmd/mongodbflex/backup/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
}

restoreJobState := mongoUtils.GetRestoreStatus(model.BackupId, restoreJobs)
return outputResult(p, cmd, model.OutputFormat, restoreJobState, *resp.Item)
return outputResult(p, model.OutputFormat, restoreJobState, *resp.Item)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -125,14 +125,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex
return req
}

func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat, restoreStatus string, backup mongodbflex.Backup) error {
func outputResult(p *print.Printer, outputFormat, restoreStatus string, backup mongodbflex.Backup) error {
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(backup, "", " ")
if err != nil {
return fmt.Errorf("marshal MongoDB Flex backup: %w", err)
}
cmd.Println(string(details))
p.Outputln(string(details))

return nil
case print.YAMLOutputFormat:
Expand Down
35 changes: 35 additions & 0 deletions internal/cmd/mongodbflex/backup/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,38 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
backup mongodbflex.Backup
restoreStatus string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "set backup",
args: args{
backup: mongodbflex.Backup{},
},
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.restoreStatus, tt.args.backup); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
4 changes: 4 additions & 0 deletions internal/cmd/mongodbflex/backup/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex
}

func outputResult(p *print.Printer, outputFormat string, backups []mongodbflex.Backup, restoreJobs *mongodbflex.ListRestoreJobsResponse) error {
if restoreJobs == nil {
return fmt.Errorf("restore jobs is empty")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(backups, "", " ")
Expand Down
50 changes: 50 additions & 0 deletions internal/cmd/mongodbflex/backup/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,53 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
backups []mongodbflex.Backup
restoreJobs *mongodbflex.ListRestoreJobsResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty backups",
args: args{
backups: []mongodbflex.Backup{},
},
wantErr: true,
},
{
name: "set restore jobs",
args: args{
restoreJobs: &mongodbflex.ListRestoreJobsResponse{},
},
wantErr: false,
},
{
name: "set restore jobs and empty backups",
args: args{
backups: []mongodbflex.Backup{},
restoreJobs: &mongodbflex.ListRestoreJobsResponse{},
},
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.backups, tt.args.restoreJobs); (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 @@ -207,3 +207,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
restoreJobs []mongodbflex.RestoreInstanceStatus
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "set empty restore jobs",
args: args{
restoreJobs: []mongodbflex.RestoreInstanceStatus{},
},
wantErr: false,
},
{
name: "set empty restore job",
args: args{
restoreJobs: []mongodbflex.RestoreInstanceStatus{{}},
},
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.restoreJobs); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
43 changes: 24 additions & 19 deletions internal/cmd/mongodbflex/backup/schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex
}

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

output := struct {
BackupSchedule string `json:"backup_schedule"`
DailySnaphotRetentionDays string `json:"daily_snapshot_retention_days"`
Expand All @@ -112,12 +116,14 @@ func outputResult(p *print.Printer, outputFormat string, instance *mongodbflex.I
SnapshotRetentionDays string `json:"snapshot_retention_days"`
WeeklySnapshotRetentionWeeks string `json:"weekly_snapshot_retention_weeks"`
}{
BackupSchedule: *instance.BackupSchedule,
DailySnaphotRetentionDays: (*instance.Options)["dailySnapshotRetentionDays"],
MonthlySnapshotRetentionMonths: (*instance.Options)["monthlySnapshotRetentionDays"],
PointInTimeWindowHours: (*instance.Options)["pointInTimeWindowHours"],
SnapshotRetentionDays: (*instance.Options)["snapshotRetentionDays"],
WeeklySnapshotRetentionWeeks: (*instance.Options)["weeklySnapshotRetentionWeeks"],
BackupSchedule: utils.PtrString(instance.BackupSchedule),
}
if instance.Options != nil {
output.DailySnaphotRetentionDays = (*instance.Options)["dailySnapshotRetentionDays"]
output.MonthlySnapshotRetentionMonths = (*instance.Options)["monthlySnapshotRetentionDays"]
output.PointInTimeWindowHours = (*instance.Options)["pointInTimeWindowHours"]
output.SnapshotRetentionDays = (*instance.Options)["snapshotRetentionDays"]
output.WeeklySnapshotRetentionWeeks = (*instance.Options)["weeklySnapshotRetentionWeeks"]
}

switch outputFormat {
Expand All @@ -139,20 +145,19 @@ func outputResult(p *print.Printer, outputFormat string, instance *mongodbflex.I
return nil
default:
table := tables.NewTable()
table.AddRow("BACKUP SCHEDULE (UTC)", utils.PtrString(instance.BackupSchedule))
table.AddRow("BACKUP SCHEDULE (UTC)", output.BackupSchedule)
table.AddSeparator()
if instance.Options != nil {
table.AddRow("DAILY SNAPSHOT RETENTION (DAYS)", (*instance.Options)["dailySnapshotRetentionDays"])
table.AddSeparator()
table.AddRow("MONTHLY SNAPSHOT RETENTION (MONTHS)", (*instance.Options)["monthlySnapshotRetentionMonths"])
table.AddSeparator()
table.AddRow("POINT IN TIME WINDOW (HOURS)", (*instance.Options)["pointInTimeWindowHours"])
table.AddSeparator()
table.AddRow("SNAPSHOT RETENTION (DAYS)", (*instance.Options)["snapshotRetentionDays"])
table.AddSeparator()
table.AddRow("WEEKLY SNAPSHOT RETENTION (WEEKS)", (*instance.Options)["weeklySnapshotRetentionWeeks"])
table.AddSeparator()
}
table.AddRow("DAILY SNAPSHOT RETENTION (DAYS)", output.DailySnaphotRetentionDays)
table.AddSeparator()
table.AddRow("MONTHLY SNAPSHOT RETENTION (MONTHS)", output.MonthlySnapshotRetentionMonths)
table.AddSeparator()
table.AddRow("POINT IN TIME WINDOW (HOURS)", output.PointInTimeWindowHours)
table.AddSeparator()
table.AddRow("SNAPSHOT RETENTION (DAYS)", output.SnapshotRetentionDays)
table.AddSeparator()
table.AddRow("WEEKLY SNAPSHOT RETENTION (WEEKS)", output.WeeklySnapshotRetentionWeeks)
table.AddSeparator()

err := table.Display(p)
if err != nil {
return fmt.Errorf("render table: %w", err)
Expand Down
34 changes: 34 additions & 0 deletions internal/cmd/mongodbflex/backup/schedule/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
instance *mongodbflex.Instance
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty instance",
args: args{
instance: &mongodbflex.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)
}
})
}
}
15 changes: 9 additions & 6 deletions internal/cmd/mongodbflex/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
Expand All @@ -18,8 +19,6 @@ import (
mongodbflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils"
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/mongodbflex"
"github.com/stackitcloud/stackit-sdk-go/services/mongodbflex/wait"
)
Expand Down Expand Up @@ -133,7 +132,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 @@ -273,8 +272,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient MongoDBFlexC
return req, nil
}

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

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand All @@ -293,7 +296,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.Id))
Expand Down
36 changes: 36 additions & 0 deletions internal/cmd/mongodbflex/instance/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,39 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
projectLabel string
createInstanceResponse *mongodbflex.CreateInstanceResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty create instance response",
args: args{
createInstanceResponse: &mongodbflex.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.createInstanceResponse); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading
Loading