Skip to content

Commit

Permalink
CLOUDP-205313: patch dbuser update to support external DB
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-condon committed Oct 19, 2023
1 parent 2e5cd75 commit c0b7db8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/atlascli/command/atlas-dbusers-update.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Options
- Type
- Required
- Description
* - --authDB
- string
- false
- 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.
* - -h, --help
-
- false
Expand Down
4 changes: 4 additions & 0 deletions docs/mongocli/command/mongocli-atlas-dbusers-update.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Options
- Type
- Required
- Description
* - --authDB
- string
- false
- 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.
* - -h, --help
-
- false
Expand Down
23 changes: 20 additions & 3 deletions internal/cli/atlas/dbusers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/internal/usage"
"github.com/mongodb/mongodb-atlas-cli/internal/validate"
"github.com/spf13/cobra"
"go.mongodb.org/atlas-sdk/v20230201004/admin"
)
Expand All @@ -38,6 +39,7 @@ type UpdateOpts struct {
username string
currentUsername string
password string
authDB string
roles []string
scopes []string
store store.DatabaseUserUpdater
Expand Down Expand Up @@ -79,13 +81,26 @@ func (opts *UpdateOpts) update(out *admin.CloudDatabaseUser) {
if opts.password != "" {
out.Password = pointer.GetStringPointerIfNotEmpty(opts.password)
}

out.Scopes = convert.BuildAtlasScopes(opts.scopes)
out.Roles = convert.BuildAtlasRoles(opts.roles)
out.DatabaseName = convert.GetAuthDB(out)
out.DatabaseName = opts.authDB
if opts.authDB == "" {
out.DatabaseName = convert.GetAuthDB(out)
}
}

func (opts *UpdateOpts) validateAuthDB() error {
if opts.authDB == "" {
return nil
}
validAuthDBs := []string{convert.AdminDB, convert.ExternalAuthDB}
if err := validate.FlagInSlice(opts.authDB, flag.AuthDB, validAuthDBs); err != nil {

Check warning on line 97 in internal/cli/atlas/dbusers/update.go

View workflow job for this annotation

GitHub Actions / lint

if-return: redundant if ...; err != nil check, just return error instead. (revive)
return err
}
return nil
}

// atlas dbuser(s) update <username> [--password password] [--role roleName@dbName] [--projectId projectId].
// atlas dbuser(s) update <username> [--password password] [--role roleName@dbName] [--projectId projectId] [--authDB authDB]

Check failure on line 103 in internal/cli/atlas/dbusers/update.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
func UpdateBuilder() *cobra.Command {
opts := &UpdateOpts{}
cmd := &cobra.Command{
Expand All @@ -106,6 +121,7 @@ func UpdateBuilder() *cobra.Command {
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(
opts.ValidateProjectID,
opts.validateAuthDB,
opts.initStore(cmd.Context()),
opts.InitOutput(cmd.OutOrStdout(), updateTemplate),
)
Expand All @@ -118,6 +134,7 @@ func UpdateBuilder() *cobra.Command {

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

Expand Down
48 changes: 48 additions & 0 deletions internal/cli/atlas/dbusers/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/internal/convert"
"github.com/mongodb/mongodb-atlas-cli/internal/mocks"
"go.mongodb.org/atlas-sdk/v20230201004/admin"
)
Expand Down Expand Up @@ -57,3 +58,50 @@ func TestDBUserUpdate_Run(t *testing.T) {
t.Fatalf("Run() unexpected error: %v", err)
}
}

func TestDBUserUpdate_validateAuthDB(t *testing.T) {
type fields struct {
authDB string
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "invalid authDB",
fields: fields{
authDB: "fakeAuthDB",
},
wantErr: true,
},
{
name: "valid adminDB",
fields: fields{
authDB: convert.AdminDB,
},
wantErr: false,
},
{
name: "valid externalAuthDB",
fields: fields{
authDB: convert.ExternalAuthDB,
},
wantErr: false,
},
}
for _, tt := range tests {
fields := tt.fields
wantErr := tt.wantErr
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
updateOpts := &UpdateOpts{
authDB: fields.authDB,
}

if err := updateOpts.validateAuthDB(); (err != nil) != wantErr {
t.Errorf("validate() error = %v, wantErr %v", err, wantErr)
}
})
}
}

0 comments on commit c0b7db8

Please sign in to comment.