Skip to content

Commit 05d3e8f

Browse files
jremy42remyleone
andauthored
feat(mongodb): improve ux for instance create (#4601)
Co-authored-by: Rémy Léone <[email protected]>
1 parent 4b3ba7b commit 05d3e8f

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed

cmd/scw/testdata/test-all-usage-mongodb-instance-create-usage.golden

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ USAGE:
88
ARGS:
99
[project-id] Project ID to use. If none is passed the default project ID will be used
1010
[name=<generated>] Name of the Database Instance
11-
version Version of the MongoDB® engine
11+
version= Version of the MongoDB® engine
1212
[tags.{index}] Tags to apply to the Database Instance
13-
node-number Number of node to use for the Database Instance
13+
node-number=1 Number of node to use for the Database Instance
1414
node-type Type of node to use for the Database Instance
1515
user-name Username created when the Database Instance is created
1616
password Password of the initial user
17-
[volume.volume-size] Volume size
18-
[volume.volume-type] Type of volume where data is stored (unknown_type | sbs_5k | sbs_15k)
17+
[volume.volume-size=5GB] Volume size
18+
[volume.volume-type=sbs_5k] Type of volume where data is stored (unknown_type | sbs_5k | sbs_15k)
1919
[endpoints.{index}.private-network.private-network-id] UUID of the Private Network
2020
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par)
2121

docs/commands/mongodb.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ scw mongodb instance create [arg=value ...]
6363
|------|---|-------------|
6464
| project-id | | Project ID to use. If none is passed the default project ID will be used |
6565
| name | Default: `<generated>` | Name of the Database Instance |
66-
| version | Required | Version of the MongoDB® engine |
66+
| version | Required<br />Default: `` | Version of the MongoDB® engine |
6767
| tags.{index} | | Tags to apply to the Database Instance |
68-
| node-number | Required | Number of node to use for the Database Instance |
68+
| node-number | Required<br />Default: `1` | Number of node to use for the Database Instance |
6969
| node-type | Required | Type of node to use for the Database Instance |
7070
| user-name | Required | Username created when the Database Instance is created |
7171
| password | Required | Password of the initial user |
72-
| volume.volume-size | | Volume size |
73-
| volume.volume-type | One of: `unknown_type`, `sbs_5k`, `sbs_15k` | Type of volume where data is stored |
72+
| volume.volume-size | Default: `5GB` | Volume size |
73+
| volume.volume-type | Default: `sbs_5k`<br />One of: `unknown_type`, `sbs_5k`, `sbs_15k` | Type of volume where data is stored |
7474
| endpoints.{index}.private-network.private-network-id | | UUID of the Private Network |
7575
| region | Default: `fr-par`<br />One of: `fr-par` | Region to target. If none is passed will use default region from the config |
7676

internal/namespaces/mongodb/v1alpha1/custom.go

+2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ func GetCommands() *core.Commands {
1313
human.RegisterMarshalerFunc(mongodb.InstanceStatus(""), human.EnumMarshalFunc(instanceStatusMarshalSpecs))
1414
human.RegisterMarshalerFunc(mongodb.NodeTypeStock(""), human.EnumMarshalFunc(nodeTypeStockMarshalSpecs))
1515

16+
cmds.MustFind("mongodb", "instance", "create").Override(instanceCreateBuilder)
17+
1618
return cmds
1719
}

internal/namespaces/mongodb/v1alpha1/custom_instance.go

+61
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package mongodb
22

33
import (
4+
"context"
5+
"strings"
6+
47
"github.com/fatih/color"
8+
"github.com/scaleway/scaleway-cli/v2/core"
59
"github.com/scaleway/scaleway-cli/v2/core/human"
610
mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
11+
"github.com/scaleway/scaleway-sdk-go/scw"
712
)
813

914
var instanceStatusMarshalSpecs = human.EnumMarshalSpecs{
@@ -16,3 +21,59 @@ var instanceStatusMarshalSpecs = human.EnumMarshalSpecs{
1621
mongodb.InstanceStatusReady: &human.EnumMarshalSpec{Attribute: color.FgGreen, Value: "ready"},
1722
mongodb.InstanceStatusSnapshotting: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "snapshotting"},
1823
}
24+
25+
func instanceCreateBuilder(c *core.Command) *core.Command {
26+
c.ArgSpecs.GetByName("node-number").Default = core.DefaultValueSetter("1")
27+
c.ArgSpecs.GetByName("version").Default = fetchLatestEngine
28+
c.ArgSpecs.GetByName("volume.volume-size").Default = core.DefaultValueSetter("5GB")
29+
c.ArgSpecs.GetByName("volume.volume-type").Default = core.DefaultValueSetter("sbs_5k")
30+
c.ArgSpecs.GetByName("node-type").AutoCompleteFunc = autoCompleteNodeType
31+
32+
return c
33+
}
34+
35+
func fetchLatestEngine(ctx context.Context) (string, string) {
36+
client := core.ExtractClient(ctx)
37+
api := mongodb.NewAPI(client)
38+
latestValueVersion, err := api.FetchLatestEngineVersion()
39+
if err != nil {
40+
return "", ""
41+
}
42+
43+
return latestValueVersion.Version, latestValueVersion.Version
44+
}
45+
46+
var completeListNodeTypeCache *mongodb.ListNodeTypesResponse
47+
48+
func autoCompleteNodeType(ctx context.Context, prefix string, request any) core.AutocompleteSuggestions {
49+
region := scw.Region("")
50+
switch req := request.(type) {
51+
case *mongodb.CreateInstanceRequest:
52+
region = req.Region
53+
case *mongodb.UpgradeInstanceRequest:
54+
region = req.Region
55+
}
56+
57+
suggestions := core.AutocompleteSuggestions(nil)
58+
59+
client := core.ExtractClient(ctx)
60+
api := mongodb.NewAPI(client)
61+
62+
if completeListNodeTypeCache == nil {
63+
res, err := api.ListNodeTypes(&mongodb.ListNodeTypesRequest{
64+
Region: region,
65+
}, scw.WithAllPages())
66+
if err != nil {
67+
return nil
68+
}
69+
completeListNodeTypeCache = res
70+
}
71+
72+
for _, nodeType := range completeListNodeTypeCache.NodeTypes {
73+
if strings.HasPrefix(nodeType.Name, prefix) {
74+
suggestions = append(suggestions, nodeType.Name)
75+
}
76+
}
77+
78+
return suggestions
79+
}

0 commit comments

Comments
 (0)