Skip to content

Commit 553953d

Browse files
committed
feat(mongodbflex): refactor implementation
relates to STACKITCLI-441
1 parent 5d87a6f commit 553953d

23 files changed

Lines changed: 1319 additions & 916 deletions

File tree

docs/stackit_mongodbflex.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ stackit mongodbflex [flags]
3131

3232
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
3333
* [stackit mongodbflex backup](./stackit_mongodbflex_backup.md) - Provides functionality for MongoDB Flex instance backups
34+
* [stackit mongodbflex flavor](./stackit_mongodbflex_flavor.md) - Provides functionality for MongoDB Flex flavors
3435
* [stackit mongodbflex instance](./stackit_mongodbflex_instance.md) - Provides functionality for MongoDB Flex instances
35-
* [stackit mongodbflex options](./stackit_mongodbflex_options.md) - Lists MongoDB Flex options
36+
* [stackit mongodbflex storage](./stackit_mongodbflex_storage.md) - Provides functionality for MongoDB Flex storages for a certain flavor
3637
* [stackit mongodbflex user](./stackit_mongodbflex_user.md) - Provides functionality for MongoDB Flex users
38+
* [stackit mongodbflex version](./stackit_mongodbflex_version.md) - Provides functionality for MongoDB Flex versions
3739

docs/stackit_mongodbflex_instance_create.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,25 @@ stackit mongodbflex instance create [flags]
1313
### Examples
1414

1515
```
16-
Create a MongoDB Flex instance with name "my-instance", ACL 0.0.0.0/0 (open access) and specify flavor by CPU and RAM. Other parameters are set to default values
17-
$ stackit mongodbflex instance create --name my-instance --cpu 1 --ram 4 --acl 0.0.0.0/0
16+
Create a MongoDB Flex instance with name "my-instance", ACL 0.0.0.0/0 (open access).
17+
$ stackit mongodbflex instance create --name my-instance --flavor-id xxx --acl 0.0.0.0/0 --type Replica --storage-size 20 --version 8.0 --backup-schedule "6 6 * * *" --storage-size 10 --storage-class premium-perf2-mongodb
1818
19-
Create a MongoDB Flex instance with name "my-instance", ACL 0.0.0.0/0 (open access) and specify flavor by ID. Other parameters are set to default values
20-
$ stackit mongodbflex instance create --name my-instance --flavor-id xxx --acl 0.0.0.0/0
21-
22-
Create a MongoDB Flex instance with name "my-instance", allow access to a specific range of IP addresses, specify flavor by CPU and RAM and set storage size to 20 GB. Other parameters are set to default values
23-
$ stackit mongodbflex instance create --name my-instance --cpu 1 --ram 4 --acl 1.2.3.0/24 --storage-size 20
19+
Create a MongoDB Flex instance with name "my-instance", allow access to a specific range of IP addresses.
20+
$ stackit mongodbflex instance create --name my-instance --flavor-id xxx --acl 1.2.3.0/24 --type Replica --storage-size 20 --version 8.0 --backup-schedule "6 6 * * *" --storage-size 10 --storage-class premium-perf2-mongodb
2421
```
2522

2623
### Options
2724

2825
```
2926
--acl strings The access control list (ACL). Must contain at least one valid subnet, for instance '0.0.0.0/0' for open access (discouraged), '1.2.3.0/24 for a public IP range of an organization, '1.2.3.4/32' for a single IP range, etc. (default [])
30-
--backup-schedule string Backup schedule (default "0 0/6 * * *")
31-
--cpu int32 Number of CPUs
32-
--flavor-id string ID of the flavor
27+
--backup-schedule string Backup schedule. This flag will be required after 2027-03-07. (default "0 0/6 * * *")
28+
--flavor-id string ID of the flavor. This flag will be required after 2027-03-07.
3329
-h, --help Help for "stackit mongodbflex instance create"
3430
-n, --name string Instance name
35-
--ram int32 Amount of RAM (in GB)
36-
--storage-class string Storage class (default "premium-perf2-mongodb")
37-
--storage-size int Storage size (in GB) (default 10)
31+
--storage-class string Storage class. This flag will be required after 2027-03-07. (default "premium-perf2-mongodb")
32+
--storage-size int Storage size (in GB). This flag will be required after 2027-03-07. (default 10)
3833
--type string Instance type, (one of: [Replica, Sharded, Single]) (default "Replica")
39-
--version string MongoDB version. Defaults to the latest version available
34+
--version string MongoDB version. Defaults to the latest version available. This flag will be required after 2027-03-07.
4035
```
4136

4237
### Options inherited from parent commands

docs/stackit_mongodbflex_instance_update.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@ stackit mongodbflex instance update INSTANCE_ID [flags]
1717
$ stackit mongodbflex instance update xxx --name my-new-name
1818
1919
Update the version of a MongoDB Flex instance
20-
$ stackit mongodbflex instance update xxx --version 6.0
20+
$ stackit mongodbflex instance update xxx --version 8.0
2121
```
2222

2323
### Options
2424

2525
```
2626
--acl strings Lists of IP networks in CIDR notation which are allowed to access this instance (default [])
2727
--backup-schedule string Backup schedule
28-
--cpu int32 Number of CPUs
2928
--flavor-id string ID of the flavor
3029
-h, --help Help for "stackit mongodbflex instance update"
3130
-n, --name string Instance name
32-
--ram int32 Amount of RAM (in GB)
3331
--storage-class string Storage class
3432
--storage-size int Storage size (in GB)
3533
--type string Instance type, (one of: [Replica, Sharded, Single])

docs/stackit_mongodbflex_options.md

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package flavor
2+
3+
import (
4+
"github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/flavor/list"
5+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
6+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
8+
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func NewCmd(params *types.CmdParams) *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "flavor",
15+
Short: "Provides functionality for MongoDB Flex flavors",
16+
Long: "Provides functionality for MongoDB Flex flavors.",
17+
Args: args.NoArgs,
18+
Run: utils.CmdHelp,
19+
}
20+
addSubcommands(cmd, params)
21+
return cmd
22+
}
23+
24+
func addSubcommands(cmd *cobra.Command, params *types.CmdParams) {
25+
cmd.AddCommand(list.NewCmd(params))
26+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package list
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
9+
10+
"github.com/spf13/cobra"
11+
mongodbflex "github.com/stackitcloud/stackit-sdk-go/services/mongodbflex/v2api"
12+
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
14+
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
16+
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
17+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
18+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
19+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client"
20+
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
21+
)
22+
23+
const (
24+
limitFlag = "limit"
25+
)
26+
27+
type inputModel struct {
28+
*globalflags.GlobalFlagModel
29+
Limit *int64
30+
}
31+
32+
func NewCmd(params *types.CmdParams) *cobra.Command {
33+
cmd := &cobra.Command{
34+
Use: "list",
35+
Short: "Lists MongoDB Flex flavors",
36+
Long: "Lists MongoDB Flex flavors.",
37+
Args: args.NoArgs,
38+
Example: examples.Build(
39+
examples.NewExample(
40+
`List MongoDB Flex flavor`,
41+
"$ stackit mongodbflex flavor list"),
42+
),
43+
RunE: func(cmd *cobra.Command, args []string) error {
44+
ctx := context.Background()
45+
model, err := parseInput(params.Printer, cmd, args)
46+
if err != nil {
47+
return err
48+
}
49+
50+
// Configure API client
51+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
52+
if err != nil {
53+
return err
54+
}
55+
56+
// Call API
57+
flavors, err := buildRequest(ctx, model, apiClient.DefaultAPI).Execute()
58+
if err != nil {
59+
return fmt.Errorf("get MongoDB Flex flavors: %w", err)
60+
}
61+
62+
return outputResult(params.Printer, model.OutputFormat, flavors.Flavors)
63+
},
64+
}
65+
66+
configureFlags(cmd)
67+
return cmd
68+
}
69+
70+
func configureFlags(cmd *cobra.Command) {
71+
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")
72+
}
73+
74+
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
75+
globalFlags := globalflags.Parse(p, cmd)
76+
if globalFlags.ProjectId == "" {
77+
return nil, &cliErr.ProjectIdError{}
78+
}
79+
80+
limit := flags.FlagToInt64Pointer(p, cmd, limitFlag)
81+
if limit != nil && *limit < 1 {
82+
return nil, &cliErr.FlagValidationError{
83+
Flag: limitFlag,
84+
Details: "must be greater than 0",
85+
}
86+
}
87+
88+
model := inputModel{
89+
GlobalFlagModel: globalFlags,
90+
Limit: limit,
91+
}
92+
93+
p.DebugInputModel(model)
94+
return &model, nil
95+
}
96+
97+
func buildRequest(ctx context.Context, model *inputModel, apiClient mongodbflex.DefaultAPI) mongodbflex.ApiListFlavorsRequest {
98+
return apiClient.ListFlavors(ctx, model.ProjectId, model.Region)
99+
}
100+
101+
func outputResult(p *print.Printer, outputFormat string, flavors []mongodbflex.InstanceFlavor) error {
102+
return p.OutputResult(outputFormat, flavors, func() error {
103+
if len(flavors) == 0 {
104+
p.Outputf("No MongoDB flavors found.")
105+
return nil
106+
}
107+
108+
table := tables.NewTable()
109+
table.SetTitle("Flavors")
110+
table.SetHeader("ID", "CPU", "MEMORY", "DESCRIPTION", "VALID INSTANCE TYPES")
111+
for _, f := range flavors {
112+
table.AddRow(
113+
utils.PtrString(f.Id),
114+
utils.PtrString(f.Cpu),
115+
utils.PtrString(f.Memory),
116+
utils.PtrString(f.Description),
117+
f.Categories,
118+
)
119+
table.AddSeparator()
120+
}
121+
122+
return table.Display(p)
123+
})
124+
}

0 commit comments

Comments
 (0)