11package mongodb
22
33import (
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
914var 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