Skip to content

Commit 4ad153a

Browse files
authored
feat(rdb): add command for instance settings (#4677)
1 parent ddc327a commit 4ad153a

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
3+
This command opens the current settings of your RDB instance in your $EDITOR.
4+
You can modify the values and save the file to apply the new configuration.
5+
6+
USAGE:
7+
scw rdb setting edit <instance-id ...> [arg=value ...]
8+
9+
EXAMPLES:
10+
Edit instance settings in YAML
11+
scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=yaml
12+
13+
Edit instance settings in JSON
14+
scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=json
15+
16+
ARGS:
17+
instance-id ID of the instance
18+
[mode=yaml] marshaling used when editing data (yaml | json)
19+
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)
20+
21+
FLAGS:
22+
-h, --help help for edit
23+
24+
GLOBAL FLAGS:
25+
-c, --config string The path to the config file
26+
-D, --debug Enable debug mode
27+
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
28+
-p, --profile string The config profile to use

cmd/scw/testdata/test-all-usage-rdb-setting-usage.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ USAGE:
1212
AVAILABLE COMMANDS:
1313
add Add Database Instance advanced settings
1414
delete Delete Database Instance advanced settings
15+
edit Edit Database Instance settings in your default editor
1516
set Set Database Instance advanced settings
1617

1718
FLAGS:

docs/commands/rdb.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ This API allows you to manage your Managed Databases for PostgreSQL and MySQL.
6666
- [Setting management](#setting-management)
6767
- [Add Database Instance advanced settings](#add-database-instance-advanced-settings)
6868
- [Delete Database Instance advanced settings](#delete-database-instance-advanced-settings)
69+
- [Edit Database Instance settings in your default editor](#edit-database-instance-settings-in-your-default-editor)
6970
- [Set Database Instance advanced settings](#set-database-instance-advanced-settings)
7071
- [Block snapshot management](#block-snapshot-management)
7172
- [Create a Database Instance snapshot](#create-a-database-instance-snapshot)
@@ -1398,6 +1399,43 @@ scw rdb setting delete [arg=value ...]
13981399

13991400

14001401

1402+
### Edit Database Instance settings in your default editor
1403+
1404+
This command opens the current settings of your RDB instance in your $EDITOR.
1405+
You can modify the values and save the file to apply the new configuration.
1406+
1407+
**Usage:**
1408+
1409+
```
1410+
scw rdb setting edit <instance-id ...> [arg=value ...]
1411+
```
1412+
1413+
1414+
**Args:**
1415+
1416+
| Name | | Description |
1417+
|------|---|-------------|
1418+
| instance-id | Required | ID of the instance |
1419+
| mode | Default: `yaml`<br />One of: `yaml`, `json` | marshaling used when editing data |
1420+
| region | Default: `fr-par`<br />One of: `fr-par`, `nl-ams`, `pl-waw` | Region to target. If none is passed will use default region from the config |
1421+
1422+
1423+
**Examples:**
1424+
1425+
1426+
Edit instance settings in YAML
1427+
```
1428+
scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=yaml
1429+
```
1430+
1431+
Edit instance settings in JSON
1432+
```
1433+
scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=json
1434+
```
1435+
1436+
1437+
1438+
14011439
### Set Database Instance advanced settings
14021440

14031441
Update an advanced setting for a Database Instance. Settings added upon database engine initialization can only be defined once, and cannot, therefore, be updated.

internal/namespaces/rdb/v1/custom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func GetCommands() *core.Commands {
4949
instanceConnectCommand(),
5050
instanceWaitCommand(),
5151
userGetURLCommand(),
52+
instanceEditSettingsCommand(),
5253
))
5354
cmds.MustFind("rdb", "acl", "add").Override(aclAddBuilder)
5455
cmds.MustFind("rdb", "acl", "delete").Override(aclDeleteBuilder)

internal/namespaces/rdb/v1/custom_instance.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/fatih/color"
1818
"github.com/scaleway/scaleway-cli/v2/core"
1919
"github.com/scaleway/scaleway-cli/v2/core/human"
20+
"github.com/scaleway/scaleway-cli/v2/internal/editor"
2021
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
2122
"github.com/scaleway/scaleway-cli/v2/internal/passwordgenerator"
2223
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
@@ -925,3 +926,89 @@ func instanceConnectCommand() *core.Command {
925926
},
926927
}
927928
}
929+
930+
func instanceEditSettingsCommand() *core.Command {
931+
type editSettingsArgs struct {
932+
InstanceID string `arg:"positional,required"`
933+
Region scw.Region `arg:"required"`
934+
Mode editor.MarshalMode
935+
}
936+
937+
return &core.Command{
938+
Namespace: "rdb",
939+
Resource: "setting",
940+
Verb: "edit",
941+
Short: "Edit Database Instance settings in your default editor",
942+
Long: `This command opens the current settings of your RDB instance in your $EDITOR.
943+
You can modify the values and save the file to apply the new configuration.`,
944+
ArgsType: reflect.TypeOf(editSettingsArgs{}),
945+
ArgSpecs: core.ArgSpecs{
946+
{
947+
Name: "instance-id",
948+
Short: "ID of the instance",
949+
Required: true,
950+
Positional: true,
951+
},
952+
editor.MarshalModeArgSpec(), // --mode=yaml|json
953+
core.RegionArgSpec(scw.RegionFrPar, scw.RegionNlAms, scw.RegionPlWaw),
954+
},
955+
Examples: []*core.Example{
956+
{
957+
Short: "Edit instance settings in YAML",
958+
Raw: "scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=yaml",
959+
},
960+
{
961+
Short: "Edit instance settings in JSON",
962+
Raw: "scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=json",
963+
},
964+
},
965+
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) {
966+
args := argsI.(*editSettingsArgs)
967+
968+
client := core.ExtractClient(ctx)
969+
api := rdbSDK.NewAPI(client)
970+
971+
instance, err := api.GetInstance(&rdbSDK.GetInstanceRequest{
972+
InstanceID: args.InstanceID,
973+
Region: args.Region,
974+
})
975+
if err != nil {
976+
return nil, err
977+
}
978+
979+
initialRequest := &rdbSDK.SetInstanceSettingsRequest{
980+
Region: args.Region,
981+
InstanceID: args.InstanceID,
982+
Settings: instance.Settings,
983+
}
984+
985+
editedRequestRaw, err := editor.UpdateResourceEditor(
986+
initialRequest,
987+
&rdbSDK.SetInstanceSettingsRequest{
988+
Region: args.Region,
989+
InstanceID: args.InstanceID,
990+
},
991+
&editor.Config{
992+
PutRequest: true,
993+
MarshalMode: args.Mode,
994+
},
995+
)
996+
if err != nil {
997+
return nil, err
998+
}
999+
1000+
editedRequest := editedRequestRaw.(*rdbSDK.SetInstanceSettingsRequest)
1001+
1002+
if reflect.DeepEqual(initialRequest.Settings, editedRequest.Settings) {
1003+
return &core.SuccessResult{Message: "No changes detected."}, nil
1004+
}
1005+
1006+
_, err = api.SetInstanceSettings(editedRequest)
1007+
if err != nil {
1008+
return nil, err
1009+
}
1010+
1011+
return &core.SuccessResult{Message: "Settings successfully updated."}, nil
1012+
},
1013+
}
1014+
}

0 commit comments

Comments
 (0)