Skip to content

feat(mongodb): improve ux for instance create #4601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ USAGE:
ARGS:
[project-id] Project ID to use. If none is passed the default project ID will be used
[name=<generated>] 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)

Expand Down
8 changes: 4 additions & 4 deletions docs/commands/mongodb.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<generated>` | Name of the Database Instance |
| version | Required | Version of the MongoDB® engine |
| version | Required<br />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<br />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`<br />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`<br />One of: `fr-par` | Region to target. If none is passed will use default region from the config |

Expand Down
2 changes: 2 additions & 0 deletions internal/namespaces/mongodb/v1alpha1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
61 changes: 61 additions & 0 deletions internal/namespaces/mongodb/v1alpha1/custom_instance.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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
}
Loading