diff --git a/cmd/scw/testdata/test-all-usage-mongodb-instance-create-usage.golden b/cmd/scw/testdata/test-all-usage-mongodb-instance-create-usage.golden index 85cfdf1c77..a7516842b3 100644 --- a/cmd/scw/testdata/test-all-usage-mongodb-instance-create-usage.golden +++ b/cmd/scw/testdata/test-all-usage-mongodb-instance-create-usage.golden @@ -8,14 +8,14 @@ USAGE: ARGS: [project-id] Project ID to use. If none is passed the default project ID will be used [name=] Name of the Database Instance - version Version of the MongoDB® engine + version= Version of the MongoDB® engine [tags.{index}] Tags to apply to the Database Instance - node-number Number of node to use for the Database Instance + node-number=1 Number of node to use for the Database Instance node-type Type of node to use for the Database Instance user-name Username created when the Database Instance is created password Password of the initial user - [volume.volume-size] Volume size - [volume.volume-type] Type of volume where data is stored (unknown_type | sbs_5k | sbs_15k) + [volume.volume-size=5GB] Volume size + [volume.volume-type=sbs_5k] Type of volume where data is stored (unknown_type | sbs_5k | sbs_15k) [endpoints.{index}.private-network.private-network-id] UUID of the Private Network [region=fr-par] Region to target. If none is passed will use default region from the config (fr-par) diff --git a/docs/commands/mongodb.md b/docs/commands/mongodb.md index 897b55f5f1..b4fbcc29f6 100644 --- a/docs/commands/mongodb.md +++ b/docs/commands/mongodb.md @@ -63,14 +63,14 @@ scw mongodb instance create [arg=value ...] |------|---|-------------| | project-id | | Project ID to use. If none is passed the default project ID will be used | | name | Default: `` | Name of the Database Instance | -| version | Required | Version of the MongoDB® engine | +| version | Required
Default: `` | Version of the MongoDB® engine | | tags.{index} | | Tags to apply to the Database Instance | -| node-number | Required | Number of node to use for the Database Instance | +| node-number | Required
Default: `1` | Number of node to use for the Database Instance | | node-type | Required | Type of node to use for the Database Instance | | user-name | Required | Username created when the Database Instance is created | | password | Required | Password of the initial user | -| volume.volume-size | | Volume size | -| volume.volume-type | One of: `unknown_type`, `sbs_5k`, `sbs_15k` | Type of volume where data is stored | +| volume.volume-size | Default: `5GB` | Volume size | +| volume.volume-type | Default: `sbs_5k`
One of: `unknown_type`, `sbs_5k`, `sbs_15k` | Type of volume where data is stored | | endpoints.{index}.private-network.private-network-id | | UUID of the Private Network | | region | Default: `fr-par`
One of: `fr-par` | Region to target. If none is passed will use default region from the config | diff --git a/internal/namespaces/mongodb/v1alpha1/custom.go b/internal/namespaces/mongodb/v1alpha1/custom.go index c90f0688e0..2507b891de 100644 --- a/internal/namespaces/mongodb/v1alpha1/custom.go +++ b/internal/namespaces/mongodb/v1alpha1/custom.go @@ -13,5 +13,7 @@ func GetCommands() *core.Commands { human.RegisterMarshalerFunc(mongodb.InstanceStatus(""), human.EnumMarshalFunc(instanceStatusMarshalSpecs)) human.RegisterMarshalerFunc(mongodb.NodeTypeStock(""), human.EnumMarshalFunc(nodeTypeStockMarshalSpecs)) + cmds.MustFind("mongodb", "instance", "create").Override(instanceCreateBuilder) + return cmds } diff --git a/internal/namespaces/mongodb/v1alpha1/custom_instance.go b/internal/namespaces/mongodb/v1alpha1/custom_instance.go index 68d2fead9d..7cae144386 100644 --- a/internal/namespaces/mongodb/v1alpha1/custom_instance.go +++ b/internal/namespaces/mongodb/v1alpha1/custom_instance.go @@ -1,9 +1,14 @@ package mongodb import ( + "context" + "strings" + "github.com/fatih/color" + "github.com/scaleway/scaleway-cli/v2/core" "github.com/scaleway/scaleway-cli/v2/core/human" mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1" + "github.com/scaleway/scaleway-sdk-go/scw" ) var instanceStatusMarshalSpecs = human.EnumMarshalSpecs{ @@ -16,3 +21,59 @@ var instanceStatusMarshalSpecs = human.EnumMarshalSpecs{ mongodb.InstanceStatusReady: &human.EnumMarshalSpec{Attribute: color.FgGreen, Value: "ready"}, mongodb.InstanceStatusSnapshotting: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "snapshotting"}, } + +func instanceCreateBuilder(c *core.Command) *core.Command { + c.ArgSpecs.GetByName("node-number").Default = core.DefaultValueSetter("1") + c.ArgSpecs.GetByName("version").Default = fetchLatestEngine + c.ArgSpecs.GetByName("volume.volume-size").Default = core.DefaultValueSetter("5GB") + c.ArgSpecs.GetByName("volume.volume-type").Default = core.DefaultValueSetter("sbs_5k") + c.ArgSpecs.GetByName("node-type").AutoCompleteFunc = autoCompleteNodeType + + return c +} + +func fetchLatestEngine(ctx context.Context) (string, string) { + client := core.ExtractClient(ctx) + api := mongodb.NewAPI(client) + latestValueVersion, err := api.FetchLatestEngineVersion() + if err != nil { + return "", "" + } + + return latestValueVersion.Version, latestValueVersion.Version +} + +var completeListNodeTypeCache *mongodb.ListNodeTypesResponse + +func autoCompleteNodeType(ctx context.Context, prefix string, request any) core.AutocompleteSuggestions { + region := scw.Region("") + switch req := request.(type) { + case *mongodb.CreateInstanceRequest: + region = req.Region + case *mongodb.UpgradeInstanceRequest: + region = req.Region + } + + suggestions := core.AutocompleteSuggestions(nil) + + client := core.ExtractClient(ctx) + api := mongodb.NewAPI(client) + + if completeListNodeTypeCache == nil { + res, err := api.ListNodeTypes(&mongodb.ListNodeTypesRequest{ + Region: region, + }, scw.WithAllPages()) + if err != nil { + return nil + } + completeListNodeTypeCache = res + } + + for _, nodeType := range completeListNodeTypeCache.NodeTypes { + if strings.HasPrefix(nodeType.Name, prefix) { + suggestions = append(suggestions, nodeType.Name) + } + } + + return suggestions +}