Skip to content

Commit 4c06b4b

Browse files
authored
CLOUDP-205313: patch dbuser update to support external DB (#2378)
1 parent 811c672 commit 4c06b4b

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

docs/atlascli/command/atlas-dbusers-update.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Options
5353
- Type
5454
- Required
5555
- Description
56+
* - --authDB
57+
- string
58+
- false
59+
- Authentication database name. If the user authenticates with AWS IAM, x.509, or LDAP, this value should be $external. If the user authenticates with SCRAM-SHA, this value should be admin.
5660
* - -h, --help
5761
-
5862
- false

docs/mongocli/command/mongocli-atlas-dbusers-update.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Options
5353
- Type
5454
- Required
5555
- Description
56+
* - --authDB
57+
- string
58+
- false
59+
- Authentication database name. If the user authenticates with AWS IAM, x.509, or LDAP, this value should be $external. If the user authenticates with SCRAM-SHA, this value should be admin.
5660
* - -h, --help
5761
-
5862
- false

internal/cli/atlas/dbusers/update.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/mongodb/mongodb-atlas-cli/internal/pointer"
2727
"github.com/mongodb/mongodb-atlas-cli/internal/store"
2828
"github.com/mongodb/mongodb-atlas-cli/internal/usage"
29+
"github.com/mongodb/mongodb-atlas-cli/internal/validate"
2930
"github.com/spf13/cobra"
3031
"go.mongodb.org/atlas-sdk/v20230201008/admin"
3132
)
@@ -38,6 +39,7 @@ type UpdateOpts struct {
3839
username string
3940
currentUsername string
4041
password string
42+
authDB string
4143
roles []string
4244
scopes []string
4345
store store.DatabaseUserUpdater
@@ -79,13 +81,23 @@ func (opts *UpdateOpts) update(out *admin.CloudDatabaseUser) {
7981
if opts.password != "" {
8082
out.Password = pointer.GetStringPointerIfNotEmpty(opts.password)
8183
}
82-
8384
out.Scopes = convert.BuildAtlasScopes(opts.scopes)
8485
out.Roles = convert.BuildAtlasRoles(opts.roles)
85-
out.DatabaseName = convert.GetAuthDB(out)
86+
out.DatabaseName = opts.authDB
87+
if opts.authDB == "" {
88+
out.DatabaseName = convert.GetAuthDB(out)
89+
}
90+
}
91+
92+
func (opts *UpdateOpts) validateAuthDB() error {
93+
if opts.authDB == "" {
94+
return nil
95+
}
96+
validAuthDBs := []string{convert.AdminDB, convert.ExternalAuthDB}
97+
return validate.FlagInSlice(opts.authDB, flag.AuthDB, validAuthDBs)
8698
}
8799

88-
// atlas dbuser(s) update <username> [--password password] [--role roleName@dbName] [--projectId projectId].
100+
// atlas dbuser(s) update <username> [--password password] [--role roleName@dbName] [--projectId projectId] [--authDB authDB].
89101
func UpdateBuilder() *cobra.Command {
90102
opts := &UpdateOpts{}
91103
cmd := &cobra.Command{
@@ -106,6 +118,7 @@ func UpdateBuilder() *cobra.Command {
106118
PreRunE: func(cmd *cobra.Command, args []string) error {
107119
return opts.PreRunE(
108120
opts.ValidateProjectID,
121+
opts.validateAuthDB,
109122
opts.initStore(cmd.Context()),
110123
opts.InitOutput(cmd.OutOrStdout(), updateTemplate),
111124
)
@@ -118,6 +131,7 @@ func UpdateBuilder() *cobra.Command {
118131

119132
cmd.Flags().StringVarP(&opts.username, flag.Username, flag.UsernameShort, "", usage.DBUsername)
120133
cmd.Flags().StringVarP(&opts.password, flag.Password, flag.PasswordShort, "", usage.DBUserPassword)
134+
cmd.Flags().StringVar(&opts.authDB, flag.AuthDB, "", usage.AtlasAuthDB)
121135
cmd.Flags().StringSliceVar(&opts.roles, flag.Role, []string{}, usage.Roles+usage.UpdateWarning)
122136
cmd.Flags().StringSliceVar(&opts.scopes, flag.Scope, []string{}, usage.Scopes+usage.UpdateWarning)
123137

internal/cli/atlas/dbusers/update_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
"github.com/golang/mock/gomock"
23+
"github.com/mongodb/mongodb-atlas-cli/internal/convert"
2324
"github.com/mongodb/mongodb-atlas-cli/internal/mocks"
2425
"go.mongodb.org/atlas-sdk/v20230201008/admin"
2526
)
@@ -57,3 +58,50 @@ func TestDBUserUpdate_Run(t *testing.T) {
5758
t.Fatalf("Run() unexpected error: %v", err)
5859
}
5960
}
61+
62+
func TestDBUserUpdate_validateAuthDB(t *testing.T) {
63+
type fields struct {
64+
authDB string
65+
}
66+
tests := []struct {
67+
name string
68+
fields fields
69+
wantErr bool
70+
}{
71+
{
72+
name: "invalid authDB",
73+
fields: fields{
74+
authDB: "fakeAuthDB",
75+
},
76+
wantErr: true,
77+
},
78+
{
79+
name: "valid adminDB",
80+
fields: fields{
81+
authDB: convert.AdminDB,
82+
},
83+
wantErr: false,
84+
},
85+
{
86+
name: "valid externalAuthDB",
87+
fields: fields{
88+
authDB: convert.ExternalAuthDB,
89+
},
90+
wantErr: false,
91+
},
92+
}
93+
for _, tt := range tests {
94+
fields := tt.fields
95+
wantErr := tt.wantErr
96+
t.Run(tt.name, func(t *testing.T) {
97+
t.Parallel()
98+
updateOpts := &UpdateOpts{
99+
authDB: fields.authDB,
100+
}
101+
102+
if err := updateOpts.validateAuthDB(); (err != nil) != wantErr {
103+
t.Errorf("validate() error = %v, wantErr %v", err, wantErr)
104+
}
105+
})
106+
}
107+
}

0 commit comments

Comments
 (0)