Skip to content

Commit 3908cf1

Browse files
MongoDB backup list, describe, restore-jobs (#307)
* Draft implementation mongodb backup list, describe, restore-jobs * Fix descriptions * Add expire date§ g * Add docs * Fix examples * Add restore status * Refactor getRestoreStatus * Sort restore jobs array * Add another date format to unit test§
1 parent c84e941 commit 3908cf1

File tree

15 files changed

+1512
-7
lines changed

15 files changed

+1512
-7
lines changed

docs/stackit_mongodbflex.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ stackit mongodbflex [flags]
2929
### SEE ALSO
3030

3131
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
32+
* [stackit mongodbflex backup](./stackit_mongodbflex_backup.md) - Provides functionality for MongoDB Flex instance backups
3233
* [stackit mongodbflex instance](./stackit_mongodbflex_instance.md) - Provides functionality for MongoDB Flex instances
3334
* [stackit mongodbflex options](./stackit_mongodbflex_options.md) - Lists MongoDB Flex options
3435
* [stackit mongodbflex user](./stackit_mongodbflex_user.md) - Provides functionality for MongoDB Flex users

docs/stackit_mongodbflex_backup.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## stackit mongodbflex backup
2+
3+
Provides functionality for MongoDB Flex instance backups
4+
5+
### Synopsis
6+
7+
Provides functionality for MongoDB Flex instance backups.
8+
9+
```
10+
stackit mongodbflex backup [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Help for "stackit mongodbflex backup"
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```
22+
-y, --assume-yes If set, skips all confirmation prompts
23+
--async If set, runs the command asynchronously
24+
-o, --output-format string Output format, one of ["json" "pretty" "none"]
25+
-p, --project-id string Project ID
26+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
27+
```
28+
29+
### SEE ALSO
30+
31+
* [stackit mongodbflex](./stackit_mongodbflex.md) - Provides functionality for MongoDB Flex
32+
* [stackit mongodbflex backup describe](./stackit_mongodbflex_backup_describe.md) - Shows details of a backup for a MongoDB Flex instance
33+
* [stackit mongodbflex backup list](./stackit_mongodbflex_backup_list.md) - Lists all backups which are available for a MongoDB Flex instance
34+
* [stackit mongodbflex backup restore-jobs](./stackit_mongodbflex_backup_restore-jobs.md) - Lists all restore jobs which have been run for a MongoDB Flex instance
35+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## stackit mongodbflex backup describe
2+
3+
Shows details of a backup for a MongoDB Flex instance
4+
5+
### Synopsis
6+
7+
Shows details of a backup for a MongoDB Flex instance.
8+
9+
```
10+
stackit mongodbflex backup describe BACKUP_ID [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Get details of a backup with ID "xxx" for a MongoDB Flex instance with ID "yyy"
17+
$ stackit mongodbflex backup describe xxx --instance-id yyy
18+
19+
Get details of a backup with ID "xxx" for a MongoDB Flex instance with ID "yyy" in JSON format
20+
$ stackit mongodbflex backup describe xxx --instance-id yyy --output-format json
21+
```
22+
23+
### Options
24+
25+
```
26+
-h, --help Help for "stackit mongodbflex backup describe"
27+
--instance-id string Instance ID
28+
```
29+
30+
### Options inherited from parent commands
31+
32+
```
33+
-y, --assume-yes If set, skips all confirmation prompts
34+
--async If set, runs the command asynchronously
35+
-o, --output-format string Output format, one of ["json" "pretty" "none"]
36+
-p, --project-id string Project ID
37+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
38+
```
39+
40+
### SEE ALSO
41+
42+
* [stackit mongodbflex backup](./stackit_mongodbflex_backup.md) - Provides functionality for MongoDB Flex instance backups
43+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## stackit mongodbflex backup list
2+
3+
Lists all backups which are available for a MongoDB Flex instance
4+
5+
### Synopsis
6+
7+
Lists all backups which are available for a MongoDB Flex instance.
8+
9+
```
10+
stackit mongodbflex backup list [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
List all backups of instance with ID "xxx"
17+
$ stackit mongodbflex backup list --instance-id xxx
18+
19+
List all backups of instance with ID "xxx" in JSON format
20+
$ stackit mongodbflex backup list --instance-id xxx --output-format json
21+
22+
List up to 10 backups of instance with ID "xxx"
23+
$ stackit mongodbflex backup list --instance-id xxx --limit 10
24+
```
25+
26+
### Options
27+
28+
```
29+
-h, --help Help for "stackit mongodbflex backup list"
30+
--instance-id string Instance ID
31+
--limit int Maximum number of entries to list
32+
```
33+
34+
### Options inherited from parent commands
35+
36+
```
37+
-y, --assume-yes If set, skips all confirmation prompts
38+
--async If set, runs the command asynchronously
39+
-o, --output-format string Output format, one of ["json" "pretty" "none"]
40+
-p, --project-id string Project ID
41+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
42+
```
43+
44+
### SEE ALSO
45+
46+
* [stackit mongodbflex backup](./stackit_mongodbflex_backup.md) - Provides functionality for MongoDB Flex instance backups
47+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## stackit mongodbflex backup restore-jobs
2+
3+
Lists all restore jobs which have been run for a MongoDB Flex instance
4+
5+
### Synopsis
6+
7+
Lists all restore jobs which have been run for a MongoDB Flex instance.
8+
9+
```
10+
stackit mongodbflex backup restore-jobs [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
List all restore jobs of instance with ID "xxx"
17+
$ stackit mongodbflex backup list --instance-id xxx
18+
19+
List all restore jobs of instance with ID "xxx" in JSON format
20+
$ stackit mongodbflex backup list --instance-id xxx --output-format json
21+
22+
List up to 10 restore jobs of instance with ID "xxx"
23+
$ stackit mongodbflex backup list --instance-id xxx --limit 10
24+
```
25+
26+
### Options
27+
28+
```
29+
-h, --help Help for "stackit mongodbflex backup restore-jobs"
30+
--instance-id string Instance ID
31+
--limit int Maximum number of entries to list
32+
```
33+
34+
### Options inherited from parent commands
35+
36+
```
37+
-y, --assume-yes If set, skips all confirmation prompts
38+
--async If set, runs the command asynchronously
39+
-o, --output-format string Output format, one of ["json" "pretty" "none"]
40+
-p, --project-id string Project ID
41+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
42+
```
43+
44+
### SEE ALSO
45+
46+
* [stackit mongodbflex backup](./stackit_mongodbflex_backup.md) - Provides functionality for MongoDB Flex instance backups
47+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package backup
2+
3+
import (
4+
"github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/backup/describe"
5+
"github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/backup/list"
6+
restorejobs "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/backup/restore-jobs"
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
func NewCmd(p *print.Printer) *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: "backup",
17+
Short: "Provides functionality for MongoDB Flex instance backups",
18+
Long: "Provides functionality for MongoDB Flex instance backups.",
19+
Args: args.NoArgs,
20+
Run: utils.CmdHelp,
21+
}
22+
addSubcommands(cmd, p)
23+
return cmd
24+
}
25+
26+
func addSubcommands(cmd *cobra.Command, p *print.Printer) {
27+
cmd.AddCommand(list.NewCmd(p))
28+
cmd.AddCommand(describe.NewCmd(p))
29+
cmd.AddCommand(restorejobs.NewCmd(p))
30+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)