diff --git a/cmd/scw/testdata/test-all-usage-mongodb-user-delete-usage.golden b/cmd/scw/testdata/test-all-usage-mongodb-user-delete-usage.golden new file mode 100644 index 0000000000..53b2e6b06f --- /dev/null +++ b/cmd/scw/testdata/test-all-usage-mongodb-user-delete-usage.golden @@ -0,0 +1,20 @@ +🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲 +🟥🟥🟥 STDERR️️ 🟥🟥🟥️ +Delete an existing user on a Database Instance. + +USAGE: + scw mongodb user delete [arg=value ...] + +ARGS: + instance-id UUID of the Database Instance the user belongs to + name Name of the database user + [region=fr-par] Region to target. If none is passed will use default region from the config (fr-par) + +FLAGS: + -h, --help help for delete + +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-mongodb-user-set-role-usage.golden b/cmd/scw/testdata/test-all-usage-mongodb-user-set-role-usage.golden new file mode 100644 index 0000000000..776ab02c22 --- /dev/null +++ b/cmd/scw/testdata/test-all-usage-mongodb-user-set-role-usage.golden @@ -0,0 +1,23 @@ +🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲 +🟥🟥🟥 STDERR️️ 🟥🟥🟥️ +Apply preset roles for a user in a Database Instance. + +USAGE: + scw mongodb user set-role [arg=value ...] + +ARGS: + instance-id UUID of the Database Instance the user belongs to + [user-name] Name of the database user + [roles.{index}.role] Name of the preset role (unknown_role | read | read_write | db_admin | sync) + [roles.{index}.database] Name of the database on which the preset role will be used + [roles.{index}.any-database] Flag to enable the preset role in all databases + [region=fr-par] Region to target. If none is passed will use default region from the config (fr-par) + +FLAGS: + -h, --help help for set-role + +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-mongodb-user-usage.golden b/cmd/scw/testdata/test-all-usage-mongodb-user-usage.golden index f87da38028..dfe8dad643 100644 --- a/cmd/scw/testdata/test-all-usage-mongodb-user-usage.golden +++ b/cmd/scw/testdata/test-all-usage-mongodb-user-usage.golden @@ -6,7 +6,9 @@ USAGE: scw mongodb user AVAILABLE COMMANDS: + delete Delete a user on a Database Instance list List users of a Database Instance + set-role Apply user roles update Update a user on a Database Instance FLAGS: diff --git a/docs/commands/mongodb.md b/docs/commands/mongodb.md index b4fbcc29f6..40560ee20c 100644 --- a/docs/commands/mongodb.md +++ b/docs/commands/mongodb.md @@ -21,7 +21,9 @@ This API allows you to manage your Managed Databases for MongoDB®. - [Restore a Database Instance snapshot](#restore-a-database-instance-snapshot) - [Update a Database Instance snapshot](#update-a-database-instance-snapshot) - [User management commands](#user-management-commands) + - [Delete a user on a Database Instance](#delete-a-user-on-a-database-instance) - [List users of a Database Instance](#list-users-of-a-database-instance) + - [Apply user roles](#apply-user-roles) - [Update a user on a Database Instance](#update-a-user-on-a-database-instance) - [MongoDB® version management commands](#mongodb®-version-management-commands) - [List available MongoDB® versions](#list-available-mongodb®-versions) @@ -373,6 +375,27 @@ scw mongodb snapshot update [arg=value ...] Users are profiles to which you can attribute database-level permissions. They allow you to define permissions specific to each type of database usage. +### Delete a user on a Database Instance + +Delete an existing user on a Database Instance. + +**Usage:** + +``` +scw mongodb user delete [arg=value ...] +``` + + +**Args:** + +| Name | | Description | +|------|---|-------------| +| instance-id | Required | UUID of the Database Instance the user belongs to | +| name | Required | Name of the database user | +| region | Default: `fr-par`
One of: `fr-par` | Region to target. If none is passed will use default region from the config | + + + ### List users of a Database Instance List all users of a given Database Instance. @@ -395,6 +418,30 @@ scw mongodb user list [arg=value ...] +### Apply user roles + +Apply preset roles for a user in a Database Instance. + +**Usage:** + +``` +scw mongodb user set-role [arg=value ...] +``` + + +**Args:** + +| Name | | Description | +|------|---|-------------| +| instance-id | Required | UUID of the Database Instance the user belongs to | +| user-name | | Name of the database user | +| roles.{index}.role | One of: `unknown_role`, `read`, `read_write`, `db_admin`, `sync` | Name of the preset role | +| roles.{index}.database | | Name of the database on which the preset role will be used | +| roles.{index}.any-database | | Flag to enable the preset role in all databases | +| region | Default: `fr-par`
One of: `fr-par` | Region to target. If none is passed will use default region from the config | + + + ### Update a user on a Database Instance Update the parameters of a user on a Database Instance. You can update the `password` parameter, but you cannot change the name of the user. diff --git a/internal/namespaces/mongodb/v1alpha1/mongodb_cli.go b/internal/namespaces/mongodb/v1alpha1/mongodb_cli.go index f382b0d606..3571ddf1e6 100644 --- a/internal/namespaces/mongodb/v1alpha1/mongodb_cli.go +++ b/internal/namespaces/mongodb/v1alpha1/mongodb_cli.go @@ -43,6 +43,8 @@ func GetGeneratedCommands() *core.Commands { mongodbSnapshotDelete(), mongodbUserList(), mongodbUserUpdate(), + mongodbUserDelete(), + mongodbUserSetRole(), ) } @@ -935,3 +937,112 @@ func mongodbUserUpdate() *core.Command { }, } } + +func mongodbUserDelete() *core.Command { + return &core.Command{ + Short: `Delete a user on a Database Instance`, + Long: `Delete an existing user on a Database Instance.`, + Namespace: "mongodb", + Resource: "user", + Verb: "delete", + // Deprecated: false, + ArgsType: reflect.TypeOf(mongodb.DeleteUserRequest{}), + ArgSpecs: core.ArgSpecs{ + { + Name: "instance-id", + Short: `UUID of the Database Instance the user belongs to`, + Required: true, + Deprecated: false, + Positional: false, + }, + { + Name: "name", + Short: `Name of the database user`, + Required: true, + Deprecated: false, + Positional: false, + }, + core.RegionArgSpec(scw.RegionFrPar), + }, + Run: func(ctx context.Context, args interface{}) (i interface{}, e error) { + request := args.(*mongodb.DeleteUserRequest) + + client := core.ExtractClient(ctx) + api := mongodb.NewAPI(client) + e = api.DeleteUser(request) + if e != nil { + return nil, e + } + + return &core.SuccessResult{ + Resource: "user", + Verb: "delete", + }, nil + }, + } +} + +func mongodbUserSetRole() *core.Command { + return &core.Command{ + Short: `Apply user roles`, + Long: `Apply preset roles for a user in a Database Instance.`, + Namespace: "mongodb", + Resource: "user", + Verb: "set-role", + // Deprecated: false, + ArgsType: reflect.TypeOf(mongodb.SetUserRoleRequest{}), + ArgSpecs: core.ArgSpecs{ + { + Name: "instance-id", + Short: `UUID of the Database Instance the user belongs to`, + Required: true, + Deprecated: false, + Positional: false, + }, + { + Name: "user-name", + Short: `Name of the database user`, + Required: false, + Deprecated: false, + Positional: false, + }, + { + Name: "roles.{index}.role", + Short: `Name of the preset role`, + Required: false, + Deprecated: false, + Positional: false, + EnumValues: []string{ + "unknown_role", + "read", + "read_write", + "db_admin", + "sync", + }, + }, + { + Name: "roles.{index}.database", + Short: `Name of the database on which the preset role will be used`, + Required: false, + Deprecated: false, + Positional: false, + }, + { + Name: "roles.{index}.any-database", + Short: `Flag to enable the preset role in all databases`, + Required: false, + Deprecated: false, + Positional: false, + }, + core.RegionArgSpec(scw.RegionFrPar), + }, + Run: func(ctx context.Context, args interface{}) (i interface{}, e error) { + request := args.(*mongodb.SetUserRoleRequest) + + client := core.ExtractClient(ctx) + api := mongodb.NewAPI(client) + + return api.SetUserRole(request) + }, + } +}