Skip to content

Commit f0dc059

Browse files
authored
fix(rdb): add nodetype autocompletion (#2731)
1 parent 9a54a90 commit f0dc059

File tree

7 files changed

+297
-134
lines changed

7 files changed

+297
-134
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ARGS:
1111
engine Database engine of the database (PostgreSQL, MySQL, ...)
1212
user-name Name of the user created when the instance is created
1313
password Password of the user
14-
node-type=DB-DEV-S Type of node to use for the instance (DB-DEV-S | DB-DEV-M | DB-DEV-L | DB-DEV-XL | DB-GP-XS | DB-GP-S | DB-GP-M | DB-GP-L | DB-GP-XL)
14+
node-type=DB-DEV-S Type of node to use for the instance
1515
[is-ha-cluster] Whether or not High-Availability is enabled
1616
[disable-backup] Whether or not backups are disabled
1717
[tags.{index}] Tags to apply to the instance

cmd/scw/testdata/test-all-usage-rdb-instance-upgrade-usage.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ USAGE:
77

88
ARGS:
99
instance-id UUID of the instance you want to upgrade
10-
[node-type] Node type of the instance you want to upgrade to (DB-DEV-S | DB-DEV-M | DB-DEV-L | DB-DEV-XL | DB-GP-XS | DB-GP-S | DB-GP-M | DB-GP-L | DB-GP-XL)
10+
[node-type] Node type of the instance you want to upgrade to
1111
[enable-ha] Set to true to enable high availability on your instance
1212
[volume-size] Increase your block storage volume size
1313
[volume-type] Change your instance storage type (lssd | bssd)

docs/commands/rdb.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ scw rdb instance create [arg=value ...]
555555
| engine | Required | Database engine of the database (PostgreSQL, MySQL, ...) |
556556
| user-name | Required | Name of the user created when the instance is created |
557557
| password | Required | Password of the user |
558-
| node-type | Required<br />Default: `DB-DEV-S`<br />One of: `DB-DEV-S`, `DB-DEV-M`, `DB-DEV-L`, `DB-DEV-XL`, `DB-GP-XS`, `DB-GP-S`, `DB-GP-M`, `DB-GP-L`, `DB-GP-XL` | Type of node to use for the instance |
558+
| node-type | Required<br />Default: `DB-DEV-S` | Type of node to use for the instance |
559559
| is-ha-cluster | | Whether or not High-Availability is enabled |
560560
| disable-backup | | Whether or not backups are disabled |
561561
| tags.{index} | | Tags to apply to the instance |
@@ -761,7 +761,7 @@ scw rdb instance upgrade <instance-id ...> [arg=value ...]
761761
| Name | | Description |
762762
|------|---|-------------|
763763
| instance-id | Required | UUID of the instance you want to upgrade |
764-
| node-type | One of: `DB-DEV-S`, `DB-DEV-M`, `DB-DEV-L`, `DB-DEV-XL`, `DB-GP-XS`, `DB-GP-S`, `DB-GP-M`, `DB-GP-L`, `DB-GP-XL` | Node type of the instance you want to upgrade to |
764+
| node-type | | Node type of the instance you want to upgrade to |
765765
| enable-ha | | Set to true to enable high availability on your instance |
766766
| volume-size | | Increase your block storage volume size |
767767
| volume-type | One of: `lssd`, `bssd` | Change your instance storage type |

internal/namespaces/rdb/v1/custom.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ import (
66
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
77
)
88

9-
var nodeTypes = []string{
10-
"DB-DEV-S",
11-
"DB-DEV-M",
12-
"DB-DEV-L",
13-
"DB-DEV-XL",
14-
"DB-GP-XS",
15-
"DB-GP-S",
16-
"DB-GP-M",
17-
"DB-GP-L",
18-
"DB-GP-XL",
19-
}
20-
219
func GetCommands() *core.Commands {
2210
cmds := GetGeneratedCommands()
2311

internal/namespaces/rdb/v1/custom_instance.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,35 @@ func instanceCloneBuilder(c *core.Command) *core.Command {
123123
return c
124124
}
125125

126+
// Caching ListNodeType response for shell completion
127+
var completeListNodeTypeCache *rdb.ListNodeTypesResponse
128+
129+
func autoCompleteNodeType(ctx context.Context, prefix string) core.AutocompleteSuggestions {
130+
suggestions := core.AutocompleteSuggestions(nil)
131+
132+
client := core.ExtractClient(ctx)
133+
api := rdb.NewAPI(client)
134+
135+
if completeListNodeTypeCache == nil {
136+
res, err := api.ListNodeTypes(&rdb.ListNodeTypesRequest{}, scw.WithAllPages())
137+
if err != nil {
138+
return nil
139+
}
140+
completeListNodeTypeCache = res
141+
}
142+
143+
for _, nodeType := range completeListNodeTypeCache.NodeTypes {
144+
if strings.HasPrefix(nodeType.Name, prefix) {
145+
suggestions = append(suggestions, nodeType.Name)
146+
}
147+
}
148+
149+
return suggestions
150+
}
151+
126152
func instanceCreateBuilder(c *core.Command) *core.Command {
127153
c.ArgSpecs.GetByName("node-type").Default = core.DefaultValueSetter("DB-DEV-S")
128-
c.ArgSpecs.GetByName("node-type").EnumValues = nodeTypes
154+
c.ArgSpecs.GetByName("node-type").AutoCompleteFunc = autoCompleteNodeType
129155

130156
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
131157
api := rdb.NewAPI(core.ExtractClient(ctx))
@@ -148,7 +174,7 @@ func instanceCreateBuilder(c *core.Command) *core.Command {
148174
}
149175

150176
func instanceUpgradeBuilder(c *core.Command) *core.Command {
151-
c.ArgSpecs.GetByName("node-type").EnumValues = nodeTypes
177+
c.ArgSpecs.GetByName("node-type").AutoCompleteFunc = autoCompleteNodeType
152178

153179
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
154180
api := rdb.NewAPI(core.ExtractClient(ctx))

0 commit comments

Comments
 (0)