|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/inhies/go-bytesize" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 19 | + "github.com/stackitcloud/stackit-sdk-go/services/mongodbflex" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + backupIdArg = "BACKUP_ID" |
| 24 | + |
| 25 | + instanceIdFlag = "instance-id" |
| 26 | +) |
| 27 | + |
| 28 | +type inputModel struct { |
| 29 | + *globalflags.GlobalFlagModel |
| 30 | + |
| 31 | + InstanceId string |
| 32 | + BackupId string |
| 33 | +} |
| 34 | + |
| 35 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 36 | + cmd := &cobra.Command{ |
| 37 | + Use: fmt.Sprintf("describe %s", backupIdArg), |
| 38 | + Short: "Shows details of a backup for a MongoDB Flex instance", |
| 39 | + Long: "Shows details of a backup for a MongoDB Flex instance.", |
| 40 | + Example: examples.Build( |
| 41 | + examples.NewExample( |
| 42 | + `Get details of a backup with ID "xxx" for a MongoDB Flex instance with ID "yyy"`, |
| 43 | + "$ stackit mongodbflex backup describe xxx --instance-id yyy"), |
| 44 | + examples.NewExample( |
| 45 | + `Get details of a backup with ID "xxx" for a MongoDB Flex instance with ID "yyy" in JSON format`, |
| 46 | + "$ stackit mongodbflex backup describe xxx --instance-id yyy --output-format json"), |
| 47 | + ), |
| 48 | + Args: args.SingleArg(backupIdArg, nil), |
| 49 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 50 | + ctx := context.Background() |
| 51 | + model, err := parseInput(p, cmd, args) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + // Configure API client |
| 57 | + apiClient, err := client.ConfigureClient(p) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + instanceLabel, err := utils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) |
| 63 | + if err != nil { |
| 64 | + p.Debug(print.ErrorLevel, "get instance name: %v", err) |
| 65 | + instanceLabel = model.InstanceId |
| 66 | + } |
| 67 | + |
| 68 | + // Call API |
| 69 | + req := buildRequest(ctx, model, apiClient) |
| 70 | + resp, err := req.Execute() |
| 71 | + |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("describe backup for MongoDB Flex instance: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + restoreJobs, err := apiClient.ListRestoreJobs(ctx, model.ProjectId, model.InstanceId).Execute() |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("get restore jobs for MongoDB Flex instance %q: %w", instanceLabel, err) |
| 79 | + } |
| 80 | + |
| 81 | + restoreJobState := utils.GetRestoreStatus(model.BackupId, restoreJobs) |
| 82 | + return outputResult(p, cmd, model.OutputFormat, restoreJobState, *resp.Item) |
| 83 | + }, |
| 84 | + } |
| 85 | + configureFlags(cmd) |
| 86 | + return cmd |
| 87 | +} |
| 88 | + |
| 89 | +func configureFlags(cmd *cobra.Command) { |
| 90 | + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID") |
| 91 | + |
| 92 | + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) |
| 93 | + cobra.CheckErr(err) |
| 94 | +} |
| 95 | + |
| 96 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 97 | + backupId := inputArgs[0] |
| 98 | + |
| 99 | + globalFlags := globalflags.Parse(p, cmd) |
| 100 | + if globalFlags.ProjectId == "" { |
| 101 | + return nil, &errors.ProjectIdError{} |
| 102 | + } |
| 103 | + |
| 104 | + model := inputModel{ |
| 105 | + GlobalFlagModel: globalFlags, |
| 106 | + InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), |
| 107 | + BackupId: backupId, |
| 108 | + } |
| 109 | + |
| 110 | + if p.IsVerbosityDebug() { |
| 111 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 112 | + if err != nil { |
| 113 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 114 | + } else { |
| 115 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return &model, nil |
| 120 | +} |
| 121 | + |
| 122 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex.APIClient) mongodbflex.ApiGetBackupRequest { |
| 123 | + req := apiClient.GetBackup(ctx, model.ProjectId, model.InstanceId, model.BackupId) |
| 124 | + return req |
| 125 | +} |
| 126 | + |
| 127 | +func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat, restoreStatus string, backup mongodbflex.Backup) error { |
| 128 | + switch outputFormat { |
| 129 | + case print.JSONOutputFormat: |
| 130 | + details, err := json.MarshalIndent(backup, "", " ") |
| 131 | + if err != nil { |
| 132 | + return fmt.Errorf("marshal backup for MongoDB Flex backup: %w", err) |
| 133 | + } |
| 134 | + cmd.Println(string(details)) |
| 135 | + |
| 136 | + return nil |
| 137 | + default: |
| 138 | + table := tables.NewTable() |
| 139 | + table.AddRow("ID", *backup.Id) |
| 140 | + table.AddSeparator() |
| 141 | + table.AddRow("CREATED AT", *backup.StartTime) |
| 142 | + table.AddSeparator() |
| 143 | + table.AddRow("EXPIRES AT", *backup.EndTime) |
| 144 | + table.AddSeparator() |
| 145 | + table.AddRow("BACKUP SIZE", bytesize.New(float64(*backup.Size))) |
| 146 | + table.AddSeparator() |
| 147 | + table.AddRow("RESTORE STATUS", restoreStatus) |
| 148 | + |
| 149 | + err := table.Display(p) |
| 150 | + if err != nil { |
| 151 | + return fmt.Errorf("render table: %w", err) |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | + } |
| 156 | +} |
0 commit comments