Skip to content

Commit 0ce9f70

Browse files
hcsa73Henrique Santos
andauthored
Postgresflex user fixes (#70)
* Fix available roles * MAke username required * Add check for empty arg * Remove UUID check from user ID * Generate docs * Change --roles to --role, fix examples * Fix removed defaults * Update description * Simplify code * Add empty arg test case --------- Co-authored-by: Henrique Santos <[email protected]>
1 parent cbfcbed commit 0ce9f70

20 files changed

+83
-109
lines changed

docs/stackit_mongodbflex_user_create.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ stackit mongodbflex user create [flags]
1717

1818
```
1919
Create a MongoDB Flex user for instance with ID "xxx" and specify the username
20-
$ stackit mongodbflex user create --instance-id xxx --username johndoe --roles read --database default
20+
$ stackit mongodbflex user create --instance-id xxx --username johndoe --role read --database default
2121
2222
Create a MongoDB Flex user for instance with ID "xxx" with an automatically generated username
23-
$ stackit mongodbflex user create --instance-id xxx --roles read --database default
23+
$ stackit mongodbflex user create --instance-id xxx --role read --database default
2424
```
2525

2626
### Options
@@ -29,7 +29,7 @@ stackit mongodbflex user create [flags]
2929
--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
3030
-h, --help Help for "stackit mongodbflex user create"
3131
--instance-id string ID of the instance
32-
--roles strings Roles of the user, possible values are ["read" "readWrite"] (default [read])
32+
--role strings Roles of the user, possible values are ["read" "readWrite"] (default [read])
3333
--username string Username of the user. If not specified, a random username will be assigned
3434
```
3535

docs/stackit_mongodbflex_user_update.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ stackit mongodbflex user update USER_ID [flags]
1414

1515
```
1616
Update the roles of a MongoDB Flex user with ID "xxx" of instance with ID "yyy"
17-
$ stackit mongodbflex user update xxx --instance-id yyy --roles read
17+
$ stackit mongodbflex user update xxx --instance-id yyy --role read
1818
```
1919

2020
### Options
@@ -23,7 +23,7 @@ stackit mongodbflex user update USER_ID [flags]
2323
--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
2424
-h, --help Help for "stackit mongodbflex user update"
2525
--instance-id string ID of the instance
26-
--roles strings Roles of the user, possible values are ["read" "readWrite"] (default [])
26+
--role strings Roles of the user, possible values are ["read" "readWrite"] (default [])
2727
```
2828

2929
### Options inherited from parent commands

docs/stackit_postgresflex_user_create.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ stackit postgresflex user create [flags]
1616
### Examples
1717

1818
```
19-
Create a PostgreSQL Flex user for instance with ID "xxx" and specify the username
20-
$ stackit postgresflex user create --instance-id xxx --username johndoe --roles read
19+
Create a PostgreSQL Flex user for instance with ID "xxx"
20+
$ stackit postgresflex user create --instance-id xxx --username johndoe
2121
22-
Create a PostgreSQL Flex user for instance with ID "xxx" with an automatically generated username
23-
$ stackit postgresflex user create --instance-id xxx --roles read
22+
Create a PostgreSQL Flex user for instance with ID "xxx" and permission "createdb"
23+
$ stackit postgresflex user create --instance-id xxx --username johndoe --role createdb
2424
```
2525

2626
### Options
2727

2828
```
2929
-h, --help Help for "stackit postgresflex user create"
3030
--instance-id string ID of the instance
31-
--roles strings Roles of the user, possible values are ["read" "readWrite"] (default [read])
32-
--username string Username of the user. If not specified, a random username will be assigned
31+
--role strings Roles of the user, possible values are ["login" "createdb"] (default [login])
32+
--username string Username of the user
3333
```
3434

3535
### Options inherited from parent commands

docs/stackit_postgresflex_user_update.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ stackit postgresflex user update USER_ID [flags]
1414

1515
```
1616
Update the roles of a PostgreSQL Flex user with ID "xxx" of instance with ID "yyy"
17-
$ stackit postgresflex user update xxx --instance-id yyy --roles read
17+
$ stackit postgresflex user update xxx --instance-id yyy --role login
1818
```
1919

2020
### Options
2121

2222
```
2323
-h, --help Help for "stackit postgresflex user update"
2424
--instance-id string ID of the instance
25-
--roles strings Roles of the user, possible values are ["read" "readWrite"] (default [])
25+
--role strings Roles of the user, possible values are ["login" "createdb"] (default [])
2626
```
2727

2828
### Options inherited from parent commands

internal/cmd/mongodbflex/user/create/create.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
instanceIdFlag = "instance-id"
2222
usernameFlag = "username"
2323
databaseFlag = "database"
24-
rolesFlag = "roles"
24+
roleFlag = "role"
2525
)
2626

2727
var (
@@ -50,10 +50,10 @@ func NewCmd() *cobra.Command {
5050
Example: examples.Build(
5151
examples.NewExample(
5252
`Create a MongoDB Flex user for instance with ID "xxx" and specify the username`,
53-
"$ stackit mongodbflex user create --instance-id xxx --username johndoe --roles read --database default"),
53+
"$ stackit mongodbflex user create --instance-id xxx --username johndoe --role read --database default"),
5454
examples.NewExample(
5555
`Create a MongoDB Flex user for instance with ID "xxx" with an automatically generated username`,
56-
"$ stackit mongodbflex user create --instance-id xxx --roles read --database default"),
56+
"$ stackit mongodbflex user create --instance-id xxx --role read --database default"),
5757
),
5858
Args: args.NoArgs,
5959
RunE: func(cmd *cobra.Command, args []string) error {
@@ -108,12 +108,12 @@ func NewCmd() *cobra.Command {
108108
}
109109

110110
func configureFlags(cmd *cobra.Command) {
111-
rolesOptions := []string{"read", "readWrite"}
111+
roleOptions := []string{"read", "readWrite"}
112112

113113
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the instance")
114114
cmd.Flags().String(usernameFlag, "", "Username of the user. If not specified, a random username will be assigned")
115115
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")
116-
cmd.Flags().Var(flags.EnumSliceFlag(false, rolesDefault, rolesOptions...), rolesFlag, fmt.Sprintf("Roles of the user, possible values are %q", rolesOptions))
116+
cmd.Flags().Var(flags.EnumSliceFlag(false, rolesDefault, roleOptions...), roleFlag, fmt.Sprintf("Roles of the user, possible values are %q", roleOptions))
117117

118118
err := flags.MarkFlagsRequired(cmd, instanceIdFlag, databaseFlag)
119119
cobra.CheckErr(err)
@@ -130,7 +130,7 @@ func parseInput(cmd *cobra.Command) (*inputModel, error) {
130130
InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag),
131131
Username: flags.FlagToStringPointer(cmd, usernameFlag),
132132
Database: flags.FlagToStringPointer(cmd, databaseFlag),
133-
Roles: flags.FlagWithDefaultToStringSlicePointer(cmd, rolesFlag),
133+
Roles: flags.FlagWithDefaultToStringSlicePointer(cmd, roleFlag),
134134
}, nil
135135
}
136136

internal/cmd/mongodbflex/user/create/create_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
2929
instanceIdFlag: testInstanceId,
3030
usernameFlag: "johndoe",
3131
databaseFlag: "default",
32-
rolesFlag: "read",
32+
roleFlag: "read",
3333
}
3434
for _, mod := range mods {
3535
mod(flagValues)
@@ -141,7 +141,7 @@ func TestParseInput(t *testing.T) {
141141
{
142142
description: "roles missing",
143143
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
144-
delete(flagValues, rolesFlag)
144+
delete(flagValues, roleFlag)
145145
}),
146146
isValid: true,
147147
expectedModel: fixtureInputModel(func(model *inputModel) {
@@ -151,7 +151,7 @@ func TestParseInput(t *testing.T) {
151151
{
152152
description: "invalid role",
153153
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
154-
flagValues[rolesFlag] = "invalid-role"
154+
flagValues[roleFlag] = "invalid-role"
155155
}),
156156
isValid: false,
157157
},

internal/cmd/mongodbflex/user/update/update.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323

2424
instanceIdFlag = "instance-id"
2525
databaseFlag = "database"
26-
rolesFlag = "roles"
26+
roleFlag = "role"
2727
)
2828

2929
type inputModel struct {
@@ -43,7 +43,7 @@ func NewCmd() *cobra.Command {
4343
Example: examples.Build(
4444
examples.NewExample(
4545
`Update the roles of a MongoDB Flex user with ID "xxx" of instance with ID "yyy"`,
46-
"$ stackit mongodbflex user update xxx --instance-id yyy --roles read"),
46+
"$ stackit mongodbflex user update xxx --instance-id yyy --role read"),
4747
),
4848
Args: args.SingleArg(userIdArg, utils.ValidateUUID),
4949
RunE: func(cmd *cobra.Command, args []string) error {
@@ -94,11 +94,11 @@ func NewCmd() *cobra.Command {
9494
}
9595

9696
func configureFlags(cmd *cobra.Command) {
97-
rolesOptions := []string{"read", "readWrite"}
97+
roleOptions := []string{"read", "readWrite"}
9898

9999
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the instance")
100100
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")
101-
cmd.Flags().Var(flags.EnumSliceFlag(false, nil, rolesOptions...), rolesFlag, fmt.Sprintf("Roles of the user, possible values are %q", rolesOptions))
101+
cmd.Flags().Var(flags.EnumSliceFlag(false, nil, roleOptions...), roleFlag, fmt.Sprintf("Roles of the user, possible values are %q", roleOptions))
102102

103103
err := flags.MarkFlagsRequired(cmd, instanceIdFlag)
104104
cobra.CheckErr(err)
@@ -113,7 +113,7 @@ func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
113113
}
114114

115115
database := flags.FlagToStringPointer(cmd, databaseFlag)
116-
roles := flags.FlagToStringSlicePointer(cmd, rolesFlag)
116+
roles := flags.FlagToStringSlicePointer(cmd, roleFlag)
117117

118118
if database == nil && roles == nil {
119119
return nil, &errors.EmptyUpdateError{}

internal/cmd/mongodbflex/user/update/update_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestParseInput(t *testing.T) {
103103
description: "update roles",
104104
argValues: fixtureArgValues(),
105105
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
106-
flagValues[rolesFlag] = "read"
106+
flagValues[roleFlag] = "read"
107107
}),
108108
isValid: true,
109109
expectedModel: fixtureInputModel(func(model *inputModel) {
@@ -177,7 +177,7 @@ func TestParseInput(t *testing.T) {
177177
description: "invalid role",
178178
argValues: fixtureArgValues(),
179179
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
180-
flagValues[rolesFlag] = "invalid-role"
180+
flagValues[roleFlag] = "invalid-role"
181181
}),
182182
isValid: false,
183183
},
@@ -186,7 +186,7 @@ func TestParseInput(t *testing.T) {
186186
argValues: fixtureArgValues(),
187187
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
188188
delete(flagValues, databaseFlag)
189-
delete(flagValues, rolesFlag)
189+
delete(flagValues, roleFlag)
190190
}),
191191
isValid: false,
192192
},

internal/cmd/postgresflex/user/create/create.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import (
2020
const (
2121
instanceIdFlag = "instance-id"
2222
usernameFlag = "username"
23-
rolesFlag = "roles"
23+
roleFlag = "role"
2424
)
2525

2626
var (
27-
rolesDefault = []string{"read"}
27+
rolesDefault = []string{"login"}
2828
)
2929

3030
type inputModel struct {
@@ -47,11 +47,11 @@ func NewCmd() *cobra.Command {
4747
),
4848
Example: examples.Build(
4949
examples.NewExample(
50-
`Create a PostgreSQL Flex user for instance with ID "xxx" and specify the username`,
51-
"$ stackit postgresflex user create --instance-id xxx --username johndoe --roles read"),
50+
`Create a PostgreSQL Flex user for instance with ID "xxx"`,
51+
"$ stackit postgresflex user create --instance-id xxx --username johndoe"),
5252
examples.NewExample(
53-
`Create a PostgreSQL Flex user for instance with ID "xxx" with an automatically generated username`,
54-
"$ stackit postgresflex user create --instance-id xxx --roles read"),
53+
`Create a PostgreSQL Flex user for instance with ID "xxx" and permission "createdb"`,
54+
"$ stackit postgresflex user create --instance-id xxx --username johndoe --role createdb"),
5555
),
5656
Args: args.NoArgs,
5757
RunE: func(cmd *cobra.Command, args []string) error {
@@ -105,13 +105,13 @@ func NewCmd() *cobra.Command {
105105
}
106106

107107
func configureFlags(cmd *cobra.Command) {
108-
rolesOptions := []string{"read", "readWrite"}
108+
roleOptions := []string{"login", "createdb"}
109109

110110
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the instance")
111-
cmd.Flags().String(usernameFlag, "", "Username of the user. If not specified, a random username will be assigned")
112-
cmd.Flags().Var(flags.EnumSliceFlag(false, rolesDefault, rolesOptions...), rolesFlag, fmt.Sprintf("Roles of the user, possible values are %q", rolesOptions))
111+
cmd.Flags().String(usernameFlag, "", "Username of the user")
112+
cmd.Flags().Var(flags.EnumSliceFlag(false, rolesDefault, roleOptions...), roleFlag, fmt.Sprintf("Roles of the user, possible values are %q", roleOptions))
113113

114-
err := flags.MarkFlagsRequired(cmd, instanceIdFlag)
114+
err := flags.MarkFlagsRequired(cmd, instanceIdFlag, usernameFlag)
115115
cobra.CheckErr(err)
116116
}
117117

@@ -125,7 +125,7 @@ func parseInput(cmd *cobra.Command) (*inputModel, error) {
125125
GlobalFlagModel: globalFlags,
126126
InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag),
127127
Username: flags.FlagToStringPointer(cmd, usernameFlag),
128-
Roles: flags.FlagWithDefaultToStringSlicePointer(cmd, rolesFlag),
128+
Roles: flags.FlagWithDefaultToStringSlicePointer(cmd, roleFlag),
129129
}, nil
130130
}
131131

internal/cmd/postgresflex/user/create/create_test.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
2828
projectIdFlag: testProjectId,
2929
instanceIdFlag: testInstanceId,
3030
usernameFlag: "johndoe",
31-
rolesFlag: "read",
31+
roleFlag: "login",
3232
}
3333
for _, mod := range mods {
3434
mod(flagValues)
@@ -43,7 +43,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
4343
},
4444
InstanceId: testInstanceId,
4545
Username: utils.Ptr("johndoe"),
46-
Roles: utils.Ptr([]string{"read"}),
46+
Roles: utils.Ptr([]string{"login"}),
4747
}
4848
for _, mod := range mods {
4949
mod(model)
@@ -55,7 +55,7 @@ func fixtureRequest(mods ...func(request *postgresflex.ApiCreateUserRequest)) po
5555
request := testClient.CreateUser(testCtx, testProjectId, testInstanceId)
5656
request = request.CreateUserPayload(postgresflex.CreateUserPayload{
5757
Username: utils.Ptr("johndoe"),
58-
Roles: utils.Ptr([]string{"read"}),
58+
Roles: utils.Ptr([]string{"login"}),
5959
})
6060

6161
for _, mod := range mods {
@@ -78,16 +78,6 @@ func TestParseInput(t *testing.T) {
7878
isValid: true,
7979
expectedModel: fixtureInputModel(),
8080
},
81-
{
82-
description: "no username specified",
83-
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
84-
delete(flagValues, usernameFlag)
85-
}),
86-
isValid: true,
87-
expectedModel: fixtureInputModel(func(model *inputModel) {
88-
model.Username = nil
89-
}),
90-
},
9181
{
9282
description: "no values",
9383
flagValues: map[string]string{},
@@ -128,10 +118,17 @@ func TestParseInput(t *testing.T) {
128118
}),
129119
isValid: false,
130120
},
121+
{
122+
description: "username missing",
123+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
124+
delete(flagValues, usernameFlag)
125+
}),
126+
isValid: false,
127+
},
131128
{
132129
description: "roles missing",
133130
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
134-
delete(flagValues, rolesFlag)
131+
delete(flagValues, roleFlag)
135132
}),
136133
isValid: true,
137134
expectedModel: fixtureInputModel(func(model *inputModel) {
@@ -141,7 +138,7 @@ func TestParseInput(t *testing.T) {
141138
{
142139
description: "invalid role",
143140
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
144-
flagValues[rolesFlag] = "invalid-role"
141+
flagValues[roleFlag] = "invalid-role"
145142
}),
146143
isValid: false,
147144
},
@@ -211,7 +208,7 @@ func TestBuildRequest(t *testing.T) {
211208
model.Username = nil
212209
}),
213210
expectedRequest: fixtureRequest().CreateUserPayload(postgresflex.CreateUserPayload{
214-
Roles: utils.Ptr([]string{"read"}),
211+
Roles: utils.Ptr([]string{"login"}),
215212
}),
216213
},
217214
}

internal/cmd/postgresflex/user/delete/delete.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1313
"github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client"
1414
postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils"
15-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1615

1716
"github.com/spf13/cobra"
1817
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
@@ -45,7 +44,7 @@ func NewCmd() *cobra.Command {
4544
`Delete a PostgreSQL Flex user with ID "xxx" for instance with ID "yyy"`,
4645
"$ stackit postgresflex user delete xxx --instance-id yyy"),
4746
),
48-
Args: args.SingleArg(userIdArg, utils.ValidateUUID),
47+
Args: args.SingleArg(userIdArg, nil),
4948
RunE: func(cmd *cobra.Command, args []string) error {
5049
ctx := context.Background()
5150
model, err := parseInput(cmd, args)

0 commit comments

Comments
 (0)