diff --git a/cmd/scw/testdata/test-all-usage-rdb-instance-edit-settings-usage.golden b/cmd/scw/testdata/test-all-usage-rdb-instance-edit-settings-usage.golden new file mode 100644 index 000000000..55ec3ac7f --- /dev/null +++ b/cmd/scw/testdata/test-all-usage-rdb-instance-edit-settings-usage.golden @@ -0,0 +1,28 @@ +🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲 +πŸŸ₯πŸŸ₯πŸŸ₯ STDERR️️ πŸŸ₯πŸŸ₯πŸŸ₯️ +This command opens the current settings of your RDB instance in your $EDITOR. +You can modify the values and save the file to apply the new configuration. + +USAGE: + scw rdb instance edit-settings [arg=value ...] + +EXAMPLES: + Edit instance settings in YAML + scw rdb instance edit-settings 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=yaml + + Edit instance settings in JSON + scw rdb instance edit-settings 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=json + +ARGS: + instance-id ID of the instance + [mode=yaml] marshaling used when editing data (yaml | json) + [region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw) + +FLAGS: + -h, --help help for edit-settings + +GLOBAL FLAGS: + -c, --config string The path to the config file + -D, --debug Enable debug mode + -o, --output string Output format: json or human, see 'scw help output' for more info (default "human") + -p, --profile string The config profile to use diff --git a/cmd/scw/testdata/test-all-usage-rdb-setting-edit-usage.golden b/cmd/scw/testdata/test-all-usage-rdb-setting-edit-usage.golden new file mode 100644 index 000000000..590ce09cc --- /dev/null +++ b/cmd/scw/testdata/test-all-usage-rdb-setting-edit-usage.golden @@ -0,0 +1,28 @@ +🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲 +πŸŸ₯πŸŸ₯πŸŸ₯ STDERR️️ πŸŸ₯πŸŸ₯πŸŸ₯️ +This command opens the current settings of your RDB instance in your $EDITOR. +You can modify the values and save the file to apply the new configuration. + +USAGE: + scw rdb setting edit [arg=value ...] + +EXAMPLES: + Edit instance settings in YAML + scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=yaml + + Edit instance settings in JSON + scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=json + +ARGS: + instance-id ID of the instance + [mode=yaml] marshaling used when editing data (yaml | json) + [region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw) + +FLAGS: + -h, --help help for edit + +GLOBAL FLAGS: + -c, --config string The path to the config file + -D, --debug Enable debug mode + -o, --output string Output format: json or human, see 'scw help output' for more info (default "human") + -p, --profile string The config profile to use diff --git a/cmd/scw/testdata/test-all-usage-rdb-setting-usage.golden b/cmd/scw/testdata/test-all-usage-rdb-setting-usage.golden index 616a28479..35e20ae4e 100644 --- a/cmd/scw/testdata/test-all-usage-rdb-setting-usage.golden +++ b/cmd/scw/testdata/test-all-usage-rdb-setting-usage.golden @@ -12,6 +12,7 @@ USAGE: AVAILABLE COMMANDS: add Add Database Instance advanced settings delete Delete Database Instance advanced settings + edit Edit Database Instance settings in your default editor set Set Database Instance advanced settings FLAGS: diff --git a/docs/commands/rdb.md b/docs/commands/rdb.md index 319c25310..73bfca6b9 100644 --- a/docs/commands/rdb.md +++ b/docs/commands/rdb.md @@ -66,6 +66,7 @@ This API allows you to manage your Managed Databases for PostgreSQL and MySQL. - [Setting management](#setting-management) - [Add Database Instance advanced settings](#add-database-instance-advanced-settings) - [Delete Database Instance advanced settings](#delete-database-instance-advanced-settings) + - [Edit Database Instance settings in your default editor](#edit-database-instance-settings-in-your-default-editor) - [Set Database Instance advanced settings](#set-database-instance-advanced-settings) - [Block snapshot management](#block-snapshot-management) - [Create a Database Instance snapshot](#create-a-database-instance-snapshot) @@ -1398,6 +1399,43 @@ scw rdb setting delete [arg=value ...] +### Edit Database Instance settings in your default editor + +This command opens the current settings of your RDB instance in your $EDITOR. +You can modify the values and save the file to apply the new configuration. + +**Usage:** + +``` +scw rdb setting edit [arg=value ...] +``` + + +**Args:** + +| Name | | Description | +|------|---|-------------| +| instance-id | Required | ID of the instance | +| mode | Default: `yaml`
One of: `yaml`, `json` | marshaling used when editing data | +| region | Default: `fr-par`
One of: `fr-par`, `nl-ams`, `pl-waw` | Region to target. If none is passed will use default region from the config | + + +**Examples:** + + +Edit instance settings in YAML +``` +scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=yaml +``` + +Edit instance settings in JSON +``` +scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=json +``` + + + + ### Set Database Instance advanced settings Update an advanced setting for a Database Instance. Settings added upon database engine initialization can only be defined once, and cannot, therefore, be updated. diff --git a/internal/namespaces/rdb/v1/custom.go b/internal/namespaces/rdb/v1/custom.go index 6226f7dc9..e707c27c4 100644 --- a/internal/namespaces/rdb/v1/custom.go +++ b/internal/namespaces/rdb/v1/custom.go @@ -49,6 +49,7 @@ func GetCommands() *core.Commands { instanceConnectCommand(), instanceWaitCommand(), userGetURLCommand(), + instanceEditSettingsCommand(), )) cmds.MustFind("rdb", "acl", "add").Override(aclAddBuilder) cmds.MustFind("rdb", "acl", "delete").Override(aclDeleteBuilder) diff --git a/internal/namespaces/rdb/v1/custom_instance.go b/internal/namespaces/rdb/v1/custom_instance.go index 1174f9dd6..a255365d4 100644 --- a/internal/namespaces/rdb/v1/custom_instance.go +++ b/internal/namespaces/rdb/v1/custom_instance.go @@ -17,6 +17,7 @@ import ( "github.com/fatih/color" "github.com/scaleway/scaleway-cli/v2/core" "github.com/scaleway/scaleway-cli/v2/core/human" + "github.com/scaleway/scaleway-cli/v2/internal/editor" "github.com/scaleway/scaleway-cli/v2/internal/interactive" "github.com/scaleway/scaleway-cli/v2/internal/passwordgenerator" "github.com/scaleway/scaleway-cli/v2/internal/terminal" @@ -925,3 +926,89 @@ func instanceConnectCommand() *core.Command { }, } } + +func instanceEditSettingsCommand() *core.Command { + type editSettingsArgs struct { + InstanceID string `arg:"positional,required"` + Region scw.Region `arg:"required"` + Mode editor.MarshalMode + } + + return &core.Command{ + Namespace: "rdb", + Resource: "setting", + Verb: "edit", + Short: "Edit Database Instance settings in your default editor", + Long: `This command opens the current settings of your RDB instance in your $EDITOR. +You can modify the values and save the file to apply the new configuration.`, + ArgsType: reflect.TypeOf(editSettingsArgs{}), + ArgSpecs: core.ArgSpecs{ + { + Name: "instance-id", + Short: "ID of the instance", + Required: true, + Positional: true, + }, + editor.MarshalModeArgSpec(), // --mode=yaml|json + core.RegionArgSpec(scw.RegionFrPar, scw.RegionNlAms, scw.RegionPlWaw), + }, + Examples: []*core.Example{ + { + Short: "Edit instance settings in YAML", + Raw: "scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=yaml", + }, + { + Short: "Edit instance settings in JSON", + Raw: "scw rdb setting edit 12345678-1234-1234-1234-123456789abc --region=fr-par --mode=json", + }, + }, + Run: func(ctx context.Context, argsI interface{}) (interface{}, error) { + args := argsI.(*editSettingsArgs) + + client := core.ExtractClient(ctx) + api := rdbSDK.NewAPI(client) + + instance, err := api.GetInstance(&rdbSDK.GetInstanceRequest{ + InstanceID: args.InstanceID, + Region: args.Region, + }) + if err != nil { + return nil, err + } + + initialRequest := &rdbSDK.SetInstanceSettingsRequest{ + Region: args.Region, + InstanceID: args.InstanceID, + Settings: instance.Settings, + } + + editedRequestRaw, err := editor.UpdateResourceEditor( + initialRequest, + &rdbSDK.SetInstanceSettingsRequest{ + Region: args.Region, + InstanceID: args.InstanceID, + }, + &editor.Config{ + PutRequest: true, + MarshalMode: args.Mode, + }, + ) + if err != nil { + return nil, err + } + + editedRequest := editedRequestRaw.(*rdbSDK.SetInstanceSettingsRequest) + + if reflect.DeepEqual(initialRequest.Settings, editedRequest.Settings) { + return &core.SuccessResult{Message: "No changes detected."}, nil + } + + _, err = api.SetInstanceSettings(editedRequest) + if err != nil { + return nil, err + } + + return &core.SuccessResult{Message: "Settings successfully updated."}, nil + }, + } +}