1
1
package mongodb
2
2
3
3
import (
4
+ "context"
5
+ "strings"
6
+
4
7
"github.com/fatih/color"
8
+ "github.com/scaleway/scaleway-cli/v2/core"
5
9
"github.com/scaleway/scaleway-cli/v2/core/human"
6
10
mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
11
+ "github.com/scaleway/scaleway-sdk-go/scw"
7
12
)
8
13
9
14
var instanceStatusMarshalSpecs = human.EnumMarshalSpecs {
@@ -16,3 +21,59 @@ var instanceStatusMarshalSpecs = human.EnumMarshalSpecs{
16
21
mongodb .InstanceStatusReady : & human.EnumMarshalSpec {Attribute : color .FgGreen , Value : "ready" },
17
22
mongodb .InstanceStatusSnapshotting : & human.EnumMarshalSpec {Attribute : color .FgBlue , Value : "snapshotting" },
18
23
}
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