diff --git a/docs/stackit_mongodbflex_user_create.md b/docs/stackit_mongodbflex_user_create.md index 40a6366ae..c9f3438ab 100644 --- a/docs/stackit_mongodbflex_user_create.md +++ b/docs/stackit_mongodbflex_user_create.md @@ -17,10 +17,10 @@ stackit mongodbflex user create [flags] ``` Create a MongoDB Flex user for instance with ID "xxx" and specify the username - $ stackit mongodbflex user create --instance-id xxx --username johndoe --roles read --database default + $ stackit mongodbflex user create --instance-id xxx --username johndoe --role read --database default Create a MongoDB Flex user for instance with ID "xxx" with an automatically generated username - $ stackit mongodbflex user create --instance-id xxx --roles read --database default + $ stackit mongodbflex user create --instance-id xxx --role read --database default ``` ### Options @@ -29,7 +29,7 @@ stackit mongodbflex user create [flags] --database string The database inside the MongoDB instance that the user has access to. If it does not exist, it will be created once the user writes to it -h, --help Help for "stackit mongodbflex user create" --instance-id string ID of the instance - --roles strings Roles of the user, possible values are ["read" "readWrite"] (default [read]) + --role strings Roles of the user, possible values are ["read" "readWrite"] (default [read]) --username string Username of the user. If not specified, a random username will be assigned ``` diff --git a/docs/stackit_mongodbflex_user_update.md b/docs/stackit_mongodbflex_user_update.md index 91dff7db8..b97404cc5 100644 --- a/docs/stackit_mongodbflex_user_update.md +++ b/docs/stackit_mongodbflex_user_update.md @@ -14,7 +14,7 @@ stackit mongodbflex user update USER_ID [flags] ``` Update the roles of a MongoDB Flex user with ID "xxx" of instance with ID "yyy" - $ stackit mongodbflex user update xxx --instance-id yyy --roles read + $ stackit mongodbflex user update xxx --instance-id yyy --role read ``` ### Options @@ -23,7 +23,7 @@ stackit mongodbflex user update USER_ID [flags] --database string The database inside the MongoDB instance that the user has access to. If it does not exist, it will be created once the user writes to it -h, --help Help for "stackit mongodbflex user update" --instance-id string ID of the instance - --roles strings Roles of the user, possible values are ["read" "readWrite"] (default []) + --role strings Roles of the user, possible values are ["read" "readWrite"] (default []) ``` ### Options inherited from parent commands diff --git a/docs/stackit_postgresflex_user_create.md b/docs/stackit_postgresflex_user_create.md index 9ff69c806..b47e65efa 100644 --- a/docs/stackit_postgresflex_user_create.md +++ b/docs/stackit_postgresflex_user_create.md @@ -16,11 +16,11 @@ stackit postgresflex user create [flags] ### Examples ``` - Create a PostgreSQL Flex user for instance with ID "xxx" and specify the username - $ stackit postgresflex user create --instance-id xxx --username johndoe --roles read + Create a PostgreSQL Flex user for instance with ID "xxx" + $ stackit postgresflex user create --instance-id xxx --username johndoe - Create a PostgreSQL Flex user for instance with ID "xxx" with an automatically generated username - $ stackit postgresflex user create --instance-id xxx --roles read + Create a PostgreSQL Flex user for instance with ID "xxx" and permission "createdb" + $ stackit postgresflex user create --instance-id xxx --username johndoe --role createdb ``` ### Options @@ -28,8 +28,8 @@ stackit postgresflex user create [flags] ``` -h, --help Help for "stackit postgresflex user create" --instance-id string ID of the instance - --roles strings Roles of the user, possible values are ["read" "readWrite"] (default [read]) - --username string Username of the user. If not specified, a random username will be assigned + --role strings Roles of the user, possible values are ["login" "createdb"] (default [login]) + --username string Username of the user ``` ### Options inherited from parent commands diff --git a/docs/stackit_postgresflex_user_update.md b/docs/stackit_postgresflex_user_update.md index d23f17020..5f323978d 100644 --- a/docs/stackit_postgresflex_user_update.md +++ b/docs/stackit_postgresflex_user_update.md @@ -14,7 +14,7 @@ stackit postgresflex user update USER_ID [flags] ``` Update the roles of a PostgreSQL Flex user with ID "xxx" of instance with ID "yyy" - $ stackit postgresflex user update xxx --instance-id yyy --roles read + $ stackit postgresflex user update xxx --instance-id yyy --role login ``` ### Options @@ -22,7 +22,7 @@ stackit postgresflex user update USER_ID [flags] ``` -h, --help Help for "stackit postgresflex user update" --instance-id string ID of the instance - --roles strings Roles of the user, possible values are ["read" "readWrite"] (default []) + --role strings Roles of the user, possible values are ["login" "createdb"] (default []) ``` ### Options inherited from parent commands diff --git a/internal/cmd/mongodbflex/user/create/create.go b/internal/cmd/mongodbflex/user/create/create.go index bdf4e591a..000f9ddf8 100644 --- a/internal/cmd/mongodbflex/user/create/create.go +++ b/internal/cmd/mongodbflex/user/create/create.go @@ -21,7 +21,7 @@ const ( instanceIdFlag = "instance-id" usernameFlag = "username" databaseFlag = "database" - rolesFlag = "roles" + roleFlag = "role" ) var ( @@ -50,10 +50,10 @@ func NewCmd() *cobra.Command { Example: examples.Build( examples.NewExample( `Create a MongoDB Flex user for instance with ID "xxx" and specify the username`, - "$ stackit mongodbflex user create --instance-id xxx --username johndoe --roles read --database default"), + "$ stackit mongodbflex user create --instance-id xxx --username johndoe --role read --database default"), examples.NewExample( `Create a MongoDB Flex user for instance with ID "xxx" with an automatically generated username`, - "$ stackit mongodbflex user create --instance-id xxx --roles read --database default"), + "$ stackit mongodbflex user create --instance-id xxx --role read --database default"), ), Args: args.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { @@ -108,12 +108,12 @@ func NewCmd() *cobra.Command { } func configureFlags(cmd *cobra.Command) { - rolesOptions := []string{"read", "readWrite"} + roleOptions := []string{"read", "readWrite"} cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the instance") cmd.Flags().String(usernameFlag, "", "Username of the user. If not specified, a random username will be assigned") cmd.Flags().String(databaseFlag, "", "The database inside the MongoDB instance that the user has access to. If it does not exist, it will be created once the user writes to it") - cmd.Flags().Var(flags.EnumSliceFlag(false, rolesDefault, rolesOptions...), rolesFlag, fmt.Sprintf("Roles of the user, possible values are %q", rolesOptions)) + cmd.Flags().Var(flags.EnumSliceFlag(false, rolesDefault, roleOptions...), roleFlag, fmt.Sprintf("Roles of the user, possible values are %q", roleOptions)) err := flags.MarkFlagsRequired(cmd, instanceIdFlag, databaseFlag) cobra.CheckErr(err) @@ -130,7 +130,7 @@ func parseInput(cmd *cobra.Command) (*inputModel, error) { InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), Username: flags.FlagToStringPointer(cmd, usernameFlag), Database: flags.FlagToStringPointer(cmd, databaseFlag), - Roles: flags.FlagWithDefaultToStringSlicePointer(cmd, rolesFlag), + Roles: flags.FlagWithDefaultToStringSlicePointer(cmd, roleFlag), }, nil } diff --git a/internal/cmd/mongodbflex/user/create/create_test.go b/internal/cmd/mongodbflex/user/create/create_test.go index c11d878c5..feb2dc175 100644 --- a/internal/cmd/mongodbflex/user/create/create_test.go +++ b/internal/cmd/mongodbflex/user/create/create_test.go @@ -29,7 +29,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st instanceIdFlag: testInstanceId, usernameFlag: "johndoe", databaseFlag: "default", - rolesFlag: "read", + roleFlag: "read", } for _, mod := range mods { mod(flagValues) @@ -141,7 +141,7 @@ func TestParseInput(t *testing.T) { { description: "roles missing", flagValues: fixtureFlagValues(func(flagValues map[string]string) { - delete(flagValues, rolesFlag) + delete(flagValues, roleFlag) }), isValid: true, expectedModel: fixtureInputModel(func(model *inputModel) { @@ -151,7 +151,7 @@ func TestParseInput(t *testing.T) { { description: "invalid role", flagValues: fixtureFlagValues(func(flagValues map[string]string) { - flagValues[rolesFlag] = "invalid-role" + flagValues[roleFlag] = "invalid-role" }), isValid: false, }, diff --git a/internal/cmd/mongodbflex/user/update/update.go b/internal/cmd/mongodbflex/user/update/update.go index d76537c2c..7339e4955 100644 --- a/internal/cmd/mongodbflex/user/update/update.go +++ b/internal/cmd/mongodbflex/user/update/update.go @@ -23,7 +23,7 @@ const ( instanceIdFlag = "instance-id" databaseFlag = "database" - rolesFlag = "roles" + roleFlag = "role" ) type inputModel struct { @@ -43,7 +43,7 @@ func NewCmd() *cobra.Command { Example: examples.Build( examples.NewExample( `Update the roles of a MongoDB Flex user with ID "xxx" of instance with ID "yyy"`, - "$ stackit mongodbflex user update xxx --instance-id yyy --roles read"), + "$ stackit mongodbflex user update xxx --instance-id yyy --role read"), ), Args: args.SingleArg(userIdArg, utils.ValidateUUID), RunE: func(cmd *cobra.Command, args []string) error { @@ -94,11 +94,11 @@ func NewCmd() *cobra.Command { } func configureFlags(cmd *cobra.Command) { - rolesOptions := []string{"read", "readWrite"} + roleOptions := []string{"read", "readWrite"} cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the instance") cmd.Flags().String(databaseFlag, "", "The database inside the MongoDB instance that the user has access to. If it does not exist, it will be created once the user writes to it") - cmd.Flags().Var(flags.EnumSliceFlag(false, nil, rolesOptions...), rolesFlag, fmt.Sprintf("Roles of the user, possible values are %q", rolesOptions)) + cmd.Flags().Var(flags.EnumSliceFlag(false, nil, roleOptions...), roleFlag, fmt.Sprintf("Roles of the user, possible values are %q", roleOptions)) err := flags.MarkFlagsRequired(cmd, instanceIdFlag) cobra.CheckErr(err) @@ -113,7 +113,7 @@ func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { } database := flags.FlagToStringPointer(cmd, databaseFlag) - roles := flags.FlagToStringSlicePointer(cmd, rolesFlag) + roles := flags.FlagToStringSlicePointer(cmd, roleFlag) if database == nil && roles == nil { return nil, &errors.EmptyUpdateError{} diff --git a/internal/cmd/mongodbflex/user/update/update_test.go b/internal/cmd/mongodbflex/user/update/update_test.go index 90f6da2b9..223325c57 100644 --- a/internal/cmd/mongodbflex/user/update/update_test.go +++ b/internal/cmd/mongodbflex/user/update/update_test.go @@ -103,7 +103,7 @@ func TestParseInput(t *testing.T) { description: "update roles", argValues: fixtureArgValues(), flagValues: fixtureFlagValues(func(flagValues map[string]string) { - flagValues[rolesFlag] = "read" + flagValues[roleFlag] = "read" }), isValid: true, expectedModel: fixtureInputModel(func(model *inputModel) { @@ -177,7 +177,7 @@ func TestParseInput(t *testing.T) { description: "invalid role", argValues: fixtureArgValues(), flagValues: fixtureFlagValues(func(flagValues map[string]string) { - flagValues[rolesFlag] = "invalid-role" + flagValues[roleFlag] = "invalid-role" }), isValid: false, }, @@ -186,7 +186,7 @@ func TestParseInput(t *testing.T) { argValues: fixtureArgValues(), flagValues: fixtureFlagValues(func(flagValues map[string]string) { delete(flagValues, databaseFlag) - delete(flagValues, rolesFlag) + delete(flagValues, roleFlag) }), isValid: false, }, diff --git a/internal/cmd/postgresflex/user/create/create.go b/internal/cmd/postgresflex/user/create/create.go index f544200c1..172c51bf9 100644 --- a/internal/cmd/postgresflex/user/create/create.go +++ b/internal/cmd/postgresflex/user/create/create.go @@ -20,11 +20,11 @@ import ( const ( instanceIdFlag = "instance-id" usernameFlag = "username" - rolesFlag = "roles" + roleFlag = "role" ) var ( - rolesDefault = []string{"read"} + rolesDefault = []string{"login"} ) type inputModel struct { @@ -47,11 +47,11 @@ func NewCmd() *cobra.Command { ), Example: examples.Build( examples.NewExample( - `Create a PostgreSQL Flex user for instance with ID "xxx" and specify the username`, - "$ stackit postgresflex user create --instance-id xxx --username johndoe --roles read"), + `Create a PostgreSQL Flex user for instance with ID "xxx"`, + "$ stackit postgresflex user create --instance-id xxx --username johndoe"), examples.NewExample( - `Create a PostgreSQL Flex user for instance with ID "xxx" with an automatically generated username`, - "$ stackit postgresflex user create --instance-id xxx --roles read"), + `Create a PostgreSQL Flex user for instance with ID "xxx" and permission "createdb"`, + "$ stackit postgresflex user create --instance-id xxx --username johndoe --role createdb"), ), Args: args.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { @@ -105,13 +105,13 @@ func NewCmd() *cobra.Command { } func configureFlags(cmd *cobra.Command) { - rolesOptions := []string{"read", "readWrite"} + roleOptions := []string{"login", "createdb"} cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the instance") - cmd.Flags().String(usernameFlag, "", "Username of the user. If not specified, a random username will be assigned") - cmd.Flags().Var(flags.EnumSliceFlag(false, rolesDefault, rolesOptions...), rolesFlag, fmt.Sprintf("Roles of the user, possible values are %q", rolesOptions)) + cmd.Flags().String(usernameFlag, "", "Username of the user") + cmd.Flags().Var(flags.EnumSliceFlag(false, rolesDefault, roleOptions...), roleFlag, fmt.Sprintf("Roles of the user, possible values are %q", roleOptions)) - err := flags.MarkFlagsRequired(cmd, instanceIdFlag) + err := flags.MarkFlagsRequired(cmd, instanceIdFlag, usernameFlag) cobra.CheckErr(err) } @@ -125,7 +125,7 @@ func parseInput(cmd *cobra.Command) (*inputModel, error) { GlobalFlagModel: globalFlags, InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), Username: flags.FlagToStringPointer(cmd, usernameFlag), - Roles: flags.FlagWithDefaultToStringSlicePointer(cmd, rolesFlag), + Roles: flags.FlagWithDefaultToStringSlicePointer(cmd, roleFlag), }, nil } diff --git a/internal/cmd/postgresflex/user/create/create_test.go b/internal/cmd/postgresflex/user/create/create_test.go index c178302ad..106dff903 100644 --- a/internal/cmd/postgresflex/user/create/create_test.go +++ b/internal/cmd/postgresflex/user/create/create_test.go @@ -28,7 +28,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st projectIdFlag: testProjectId, instanceIdFlag: testInstanceId, usernameFlag: "johndoe", - rolesFlag: "read", + roleFlag: "login", } for _, mod := range mods { mod(flagValues) @@ -43,7 +43,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { }, InstanceId: testInstanceId, Username: utils.Ptr("johndoe"), - Roles: utils.Ptr([]string{"read"}), + Roles: utils.Ptr([]string{"login"}), } for _, mod := range mods { mod(model) @@ -55,7 +55,7 @@ func fixtureRequest(mods ...func(request *postgresflex.ApiCreateUserRequest)) po request := testClient.CreateUser(testCtx, testProjectId, testInstanceId) request = request.CreateUserPayload(postgresflex.CreateUserPayload{ Username: utils.Ptr("johndoe"), - Roles: utils.Ptr([]string{"read"}), + Roles: utils.Ptr([]string{"login"}), }) for _, mod := range mods { @@ -78,16 +78,6 @@ func TestParseInput(t *testing.T) { isValid: true, expectedModel: fixtureInputModel(), }, - { - description: "no username specified", - flagValues: fixtureFlagValues(func(flagValues map[string]string) { - delete(flagValues, usernameFlag) - }), - isValid: true, - expectedModel: fixtureInputModel(func(model *inputModel) { - model.Username = nil - }), - }, { description: "no values", flagValues: map[string]string{}, @@ -128,10 +118,17 @@ func TestParseInput(t *testing.T) { }), isValid: false, }, + { + description: "username missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, usernameFlag) + }), + isValid: false, + }, { description: "roles missing", flagValues: fixtureFlagValues(func(flagValues map[string]string) { - delete(flagValues, rolesFlag) + delete(flagValues, roleFlag) }), isValid: true, expectedModel: fixtureInputModel(func(model *inputModel) { @@ -141,7 +138,7 @@ func TestParseInput(t *testing.T) { { description: "invalid role", flagValues: fixtureFlagValues(func(flagValues map[string]string) { - flagValues[rolesFlag] = "invalid-role" + flagValues[roleFlag] = "invalid-role" }), isValid: false, }, @@ -211,7 +208,7 @@ func TestBuildRequest(t *testing.T) { model.Username = nil }), expectedRequest: fixtureRequest().CreateUserPayload(postgresflex.CreateUserPayload{ - Roles: utils.Ptr([]string{"read"}), + Roles: utils.Ptr([]string{"login"}), }), }, } diff --git a/internal/cmd/postgresflex/user/delete/delete.go b/internal/cmd/postgresflex/user/delete/delete.go index f79bdf5db..03c18ec49 100644 --- a/internal/cmd/postgresflex/user/delete/delete.go +++ b/internal/cmd/postgresflex/user/delete/delete.go @@ -12,7 +12,6 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" - "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" "github.com/stackitcloud/stackit-sdk-go/services/postgresflex" @@ -45,7 +44,7 @@ func NewCmd() *cobra.Command { `Delete a PostgreSQL Flex user with ID "xxx" for instance with ID "yyy"`, "$ stackit postgresflex user delete xxx --instance-id yyy"), ), - Args: args.SingleArg(userIdArg, utils.ValidateUUID), + Args: args.SingleArg(userIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() model, err := parseInput(cmd, args) diff --git a/internal/cmd/postgresflex/user/delete/delete_test.go b/internal/cmd/postgresflex/user/delete/delete_test.go index 4fe8ecf1f..47a1240ef 100644 --- a/internal/cmd/postgresflex/user/delete/delete_test.go +++ b/internal/cmd/postgresflex/user/delete/delete_test.go @@ -20,7 +20,7 @@ var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") var testClient = &postgresflex.APIClient{} var testProjectId = uuid.NewString() var testInstanceId = uuid.NewString() -var testUserId = uuid.NewString() +var testUserId = "12345" func fixtureArgValues(mods ...func(argValues []string)) []string { argValues := []string{ @@ -147,17 +147,11 @@ func TestParseInput(t *testing.T) { isValid: false, }, { - description: "user id invalid 1", + description: "user id invalid", argValues: []string{""}, flagValues: fixtureFlagValues(), isValid: false, }, - { - description: "user id invalid 2", - argValues: []string{"invalid-uuid"}, - flagValues: fixtureFlagValues(), - isValid: false, - }, } for _, tt := range tests { diff --git a/internal/cmd/postgresflex/user/describe/describe.go b/internal/cmd/postgresflex/user/describe/describe.go index 0fbbcd01b..a24e432db 100644 --- a/internal/cmd/postgresflex/user/describe/describe.go +++ b/internal/cmd/postgresflex/user/describe/describe.go @@ -12,7 +12,6 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" - "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" "github.com/stackitcloud/stackit-sdk-go/services/postgresflex" @@ -48,7 +47,7 @@ func NewCmd() *cobra.Command { `Get details of a PostgreSQL Flex user with ID "xxx" of instance with ID "yyy" in table format`, "$ stackit postgresflex user list xxx --instance-id yyy --output-format pretty"), ), - Args: args.SingleArg(userIdArg, utils.ValidateUUID), + Args: args.SingleArg(userIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() model, err := parseInput(cmd, args) diff --git a/internal/cmd/postgresflex/user/describe/describe_test.go b/internal/cmd/postgresflex/user/describe/describe_test.go index dfa7a73c7..1826289dd 100644 --- a/internal/cmd/postgresflex/user/describe/describe_test.go +++ b/internal/cmd/postgresflex/user/describe/describe_test.go @@ -20,7 +20,7 @@ var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") var testClient = &postgresflex.APIClient{} var testProjectId = uuid.NewString() var testInstanceId = uuid.NewString() -var testUserId = uuid.NewString() +var testUserId = "12345" func fixtureArgValues(mods ...func(argValues []string)) []string { argValues := []string{ @@ -147,17 +147,11 @@ func TestParseInput(t *testing.T) { isValid: false, }, { - description: "user id invalid 1", + description: "user id invalid", argValues: []string{""}, flagValues: fixtureFlagValues(), isValid: false, }, - { - description: "user id invalid 2", - argValues: []string{"invalid-uuid"}, - flagValues: fixtureFlagValues(), - isValid: false, - }, } for _, tt := range tests { diff --git a/internal/cmd/postgresflex/user/reset-password/reset_password.go b/internal/cmd/postgresflex/user/reset-password/reset_password.go index 05033a9ee..3e3b550ab 100644 --- a/internal/cmd/postgresflex/user/reset-password/reset_password.go +++ b/internal/cmd/postgresflex/user/reset-password/reset_password.go @@ -12,7 +12,6 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" - "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" "github.com/stackitcloud/stackit-sdk-go/services/postgresflex" @@ -41,7 +40,7 @@ func NewCmd() *cobra.Command { `Reset the password of a PostgreSQL Flex user with ID "xxx" of instance with ID "yyy"`, "$ stackit postgresflex user reset-password xxx --instance-id yyy"), ), - Args: args.SingleArg(userIdArg, utils.ValidateUUID), + Args: args.SingleArg(userIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() model, err := parseInput(cmd, args) diff --git a/internal/cmd/postgresflex/user/reset-password/reset_password_test.go b/internal/cmd/postgresflex/user/reset-password/reset_password_test.go index 88a2e75e6..0898d2b7b 100644 --- a/internal/cmd/postgresflex/user/reset-password/reset_password_test.go +++ b/internal/cmd/postgresflex/user/reset-password/reset_password_test.go @@ -20,7 +20,7 @@ var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") var testClient = &postgresflex.APIClient{} var testProjectId = uuid.NewString() var testInstanceId = uuid.NewString() -var testUserId = uuid.NewString() +var testUserId = "12345" func fixtureArgValues(mods ...func(argValues []string)) []string { argValues := []string{ @@ -147,17 +147,11 @@ func TestParseInput(t *testing.T) { isValid: false, }, { - description: "user id invalid 1", + description: "user id invalid", argValues: []string{""}, flagValues: fixtureFlagValues(), isValid: false, }, - { - description: "user id invalid 2", - argValues: []string{"invalid-uuid"}, - flagValues: fixtureFlagValues(), - isValid: false, - }, } for _, tt := range tests { diff --git a/internal/cmd/postgresflex/user/update/update.go b/internal/cmd/postgresflex/user/update/update.go index d65f844c3..07da890db 100644 --- a/internal/cmd/postgresflex/user/update/update.go +++ b/internal/cmd/postgresflex/user/update/update.go @@ -12,7 +12,6 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" - "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" "github.com/stackitcloud/stackit-sdk-go/services/postgresflex" @@ -22,7 +21,7 @@ const ( userIdArg = "USER_ID" instanceIdFlag = "instance-id" - rolesFlag = "roles" + roleFlag = "role" ) type inputModel struct { @@ -41,9 +40,9 @@ func NewCmd() *cobra.Command { Example: examples.Build( examples.NewExample( `Update the roles of a PostgreSQL Flex user with ID "xxx" of instance with ID "yyy"`, - "$ stackit postgresflex user update xxx --instance-id yyy --roles read"), + "$ stackit postgresflex user update xxx --instance-id yyy --role login"), ), - Args: args.SingleArg(userIdArg, utils.ValidateUUID), + Args: args.SingleArg(userIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() model, err := parseInput(cmd, args) @@ -92,10 +91,10 @@ func NewCmd() *cobra.Command { } func configureFlags(cmd *cobra.Command) { - rolesOptions := []string{"read", "readWrite"} + roleOptions := []string{"login", "createdb"} cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the instance") - cmd.Flags().Var(flags.EnumSliceFlag(false, nil, rolesOptions...), rolesFlag, fmt.Sprintf("Roles of the user, possible values are %q", rolesOptions)) + cmd.Flags().Var(flags.EnumSliceFlag(false, nil, roleOptions...), roleFlag, fmt.Sprintf("Roles of the user, possible values are %q", roleOptions)) err := flags.MarkFlagsRequired(cmd, instanceIdFlag) cobra.CheckErr(err) @@ -109,7 +108,7 @@ func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { return nil, &errors.ProjectIdError{} } - roles := flags.FlagToStringSlicePointer(cmd, rolesFlag) + roles := flags.FlagToStringSlicePointer(cmd, roleFlag) if roles == nil { return nil, &errors.EmptyUpdateError{} } diff --git a/internal/cmd/postgresflex/user/update/update_test.go b/internal/cmd/postgresflex/user/update/update_test.go index edf2931a4..e73ddff46 100644 --- a/internal/cmd/postgresflex/user/update/update_test.go +++ b/internal/cmd/postgresflex/user/update/update_test.go @@ -21,7 +21,7 @@ var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") var testClient = &postgresflex.APIClient{} var testProjectId = uuid.NewString() var testInstanceId = uuid.NewString() -var testUserId = uuid.NewString() +var testUserId = "12345" func fixtureArgValues(mods ...func(argValues []string)) []string { argValues := []string{ @@ -37,7 +37,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st flagValues := map[string]string{ projectIdFlag: testProjectId, instanceIdFlag: testInstanceId, - rolesFlag: "read", + roleFlag: "login", } for _, mod := range mods { mod(flagValues) @@ -52,7 +52,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { }, InstanceId: testInstanceId, UserId: testUserId, - Roles: utils.Ptr([]string{"read"}), + Roles: utils.Ptr([]string{"login"}), } for _, mod := range mods { mod(model) @@ -63,7 +63,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { func fixtureRequest(mods ...func(request *postgresflex.ApiPartialUpdateUserRequest)) postgresflex.ApiPartialUpdateUserRequest { request := testClient.PartialUpdateUser(testCtx, testProjectId, testInstanceId, testUserId) request = request.PartialUpdateUserPayload(postgresflex.PartialUpdateUserPayload{ - Roles: utils.Ptr([]string{"read"}), + Roles: utils.Ptr([]string{"login"}), }) for _, mod := range mods { mod(&request) @@ -140,22 +140,16 @@ func TestParseInput(t *testing.T) { isValid: false, }, { - description: "user id invalid 1", + description: "user id invalid", argValues: []string{""}, flagValues: fixtureFlagValues(), isValid: false, }, - { - description: "user id invalid 2", - argValues: []string{"invalid-uuid"}, - flagValues: fixtureFlagValues(), - isValid: false, - }, { description: "invalid role", argValues: fixtureArgValues(), flagValues: fixtureFlagValues(func(flagValues map[string]string) { - flagValues[rolesFlag] = "invalid-role" + flagValues[roleFlag] = "invalid-role" }), isValid: false, }, @@ -163,7 +157,7 @@ func TestParseInput(t *testing.T) { description: "empty update", argValues: fixtureArgValues(), flagValues: fixtureFlagValues(func(flagValues map[string]string) { - delete(flagValues, rolesFlag) + delete(flagValues, roleFlag) }), isValid: false, }, diff --git a/internal/pkg/args/args.go b/internal/pkg/args/args.go index 2180f2e81..2307ed342 100644 --- a/internal/pkg/args/args.go +++ b/internal/pkg/args/args.go @@ -17,13 +17,13 @@ func NoArgs(cmd *cobra.Command, args []string) error { } } -// SingleArg checks if only one argument was provided and validates it +// SingleArg checks if only one non-empty argument was provided and validates it // using the validate function. It returns an error if none or multiple arguments // are provided, or if the argument is invalid. // For no validation, you can pass a nil validate function func SingleArg(argName string, validate func(value string) error) cobra.PositionalArgs { return func(cmd *cobra.Command, args []string) error { - if len(args) != 1 { + if len(args) != 1 || args[0] == "" { return &errors.SingleArgExpectedError{ Cmd: cmd, Expected: argName, diff --git a/internal/pkg/args/args_test.go b/internal/pkg/args/args_test.go index 951a19311..ad477e50e 100644 --- a/internal/pkg/args/args_test.go +++ b/internal/pkg/args/args_test.go @@ -68,6 +68,11 @@ func TestSingleArg(t *testing.T) { args: []string{"arg", "arg2"}, isValid: false, }, + { + description: "empty_arg", + args: []string{""}, + isValid: false, + }, { description: "invalid_arg", args: []string{"arg"},