Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgresflex user fixes #70

Merged
merged 11 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/stackit_mongodbflex_instance_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ stackit mongodbflex instance update INSTANCE_ID [flags]
--ram int Amount of RAM (in GB)
--storage-class string Storage class
--storage-size int Storage size (in GB)
--type string Instance type, one of ["Single" "Replica" "Sharded"]
--type string Instance type, one of ["Sharded" "Single" "Replica"]
--version string Version
```

Expand Down
6 changes: 3 additions & 3 deletions docs/stackit_mongodbflex_user_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, can be specified multiple times. Possible values are ["read" "readWrite"] (default [])
--username string Username of the user. If not specified, a random username will be assigned
```

Expand Down
4 changes: 2 additions & 2 deletions docs/stackit_mongodbflex_user_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, can be specified multiple times. Possible values are ["read" "readWrite"] (default [])
```

### Options inherited from parent commands
Expand Down
12 changes: 6 additions & 6 deletions docs/stackit_postgresflex_user_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ 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

```
-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, can be specified multiple times. Possible values are ["login" "createdb"] (default [])
--username string Username of the user
```

### Options inherited from parent commands
Expand Down
4 changes: 2 additions & 2 deletions docs/stackit_postgresflex_user_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ 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

```
-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, can be specified multiple times. Possible values are ["login" "createdb"] (default [])
```

### Options inherited from parent commands
Expand Down
12 changes: 6 additions & 6 deletions internal/cmd/mongodbflex/user/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
instanceIdFlag = "instance-id"
usernameFlag = "username"
databaseFlag = "database"
rolesFlag = "roles"
roleFlag = "role"
)

var (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, nil, roleOptions...), roleFlag, fmt.Sprintf("Roles of the user, can be specified multiple times. Possible values are %q", roleOptions))
hcsa73 marked this conversation as resolved.
Show resolved Hide resolved

err := flags.MarkFlagsRequired(cmd, instanceIdFlag, databaseFlag)
cobra.CheckErr(err)
Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/mongodbflex/user/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
},
Expand Down
10 changes: 5 additions & 5 deletions internal/cmd/mongodbflex/user/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (

instanceIdFlag = "instance-id"
databaseFlag = "database"
rolesFlag = "roles"
roleFlag = "role"
)

type inputModel struct {
Expand All @@ -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 {
Expand Down Expand Up @@ -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, can be specified multiple times. Possible values are %q", roleOptions))

err := flags.MarkFlagsRequired(cmd, instanceIdFlag)
cobra.CheckErr(err)
Expand All @@ -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{}
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/mongodbflex/user/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down
22 changes: 11 additions & 11 deletions internal/cmd/postgresflex/user/create/create.go
hcsa73 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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, nil, roleOptions...), roleFlag, fmt.Sprintf("Roles of the user, can be specified multiple times. Possible values are %q", roleOptions))

err := flags.MarkFlagsRequired(cmd, instanceIdFlag)
err := flags.MarkFlagsRequired(cmd, instanceIdFlag, usernameFlag)
cobra.CheckErr(err)
}

Expand All @@ -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
}

Expand Down
29 changes: 13 additions & 16 deletions internal/cmd/postgresflex/user/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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{},
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
},
Expand Down Expand Up @@ -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"}),
}),
},
}
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/postgresflex/user/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
hcsa73 marked this conversation as resolved.
Show resolved Hide resolved
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(cmd, args)
Expand Down
Loading
Loading