Skip to content

CLOUDP-58790: Take database name from a flag when handling dbusers, Atlas #53

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

Merged
merged 11 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 e2e/atlas_dbusers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestAtlasDBUsers(t *testing.T) {
})

t.Run("Delete", func(t *testing.T) {
cmd := exec.Command(cliPath, atlasEntity, dbusersEntity, "delete", username, "--force")
cmd := exec.Command(cliPath, atlasEntity, dbusersEntity, "delete", username, "--force", "--authDB", "admin")
cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()

Expand Down
5 changes: 3 additions & 2 deletions internal/cli/atlas_alert_config_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func TestAtlasAlertConfigsDelete_Run(t *testing.T) {
deleteOpts := &atlasAlertConfigDeleteOpts{
globalOpts: newGlobalOpts(),
deleteOpts: &deleteOpts{
confirm: true,
entry: "test",
confirm: true,
entry: "test",
successMessage: "Alert config '%s' deleted\n",
},
store: mockStore,
}
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/atlas_clusters_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func TestAtlasClustersDelete_Run(t *testing.T) {
deleteOpts := &atlasClustersDeleteOpts{
globalOpts: newGlobalOpts(),
deleteOpts: &deleteOpts{
confirm: true,
entry: "test",
confirm: true,
entry: "test",
successMessage: "Cluster '%s' deleted\n",
},
store: mockStore,
}
Expand Down
6 changes: 4 additions & 2 deletions internal/cli/atlas_dbusers_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
type atlasDBUsersDeleteOpts struct {
*globalOpts
*deleteOpts
store store.DatabaseUserDeleter
authDB string
store store.DatabaseUserDeleter
}

func (opts *atlasDBUsersDeleteOpts) init() error {
Expand All @@ -38,7 +39,7 @@ func (opts *atlasDBUsersDeleteOpts) init() error {
}

func (opts *atlasDBUsersDeleteOpts) Run() error {
return opts.DeleteFromProject(opts.store.DeleteDatabaseUser, opts.ProjectID())
return opts.DeleterFromProjectAuthDB(opts.store.DeleteDatabaseUser, opts.authDB, opts.ProjectID())
}

// mongocli atlas dbuser(s) delete <username> --force
Expand Down Expand Up @@ -70,6 +71,7 @@ func AtlasDBUsersDeleteBuilder() *cobra.Command {
cmd.Flags().BoolVar(&opts.confirm, flags.Force, false, usage.Force)

cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
cmd.Flags().StringVar(&opts.authDB, flags.AuthDB, "admin", usage.AuthDB)

return cmd
}
10 changes: 6 additions & 4 deletions internal/cli/atlas_dbusers_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ func TestAtlasDBUsersDelete_Run(t *testing.T) {
deleteOpts := &atlasDBUsersDeleteOpts{
globalOpts: newGlobalOpts(),
deleteOpts: &deleteOpts{
confirm: true,
entry: "test",
confirm: true,
entry: "test",
successMessage: "DB user '%s' deleted\n",
},
store: mockStore,
authDB: "admin",
store: mockStore,
}

mockStore.
EXPECT().
DeleteDatabaseUser(deleteOpts.projectID, deleteOpts.entry).
DeleteDatabaseUser(deleteOpts.authDB, deleteOpts.projectID, deleteOpts.entry).
Return(nil).
Times(1)

Expand Down
5 changes: 3 additions & 2 deletions internal/cli/atlas_whitelist_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func TestAtlasWhitelistDelete_Run(t *testing.T) {
deleteOpts := &atlasWhitelistDeleteOpts{
globalOpts: newGlobalOpts(),
deleteOpts: &deleteOpts{
confirm: true,
entry: "test",
confirm: true,
entry: "test",
successMessage: "Project whitelist entry '%s' deleted\n",
},
store: mockStore,
}
Expand Down
21 changes: 21 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type deleteOpts struct {
// DeleterFromProject a function to delete from the store.
type DeleterFromProject func(projectID string, entry string) error
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about why we are doing all of this before calling the delete function in the store


// DeleterFromProjectAuthDB a function to delete from the store.
type DeleterFromProjectAuthDB func(authDB string, projectID string, entry string) error

// DeleteFromProject deletes a resource from a project, it expects a callback
// that should perform the deletion from the store.
func (opts *deleteOpts) DeleteFromProject(d DeleterFromProject, projectID string) error {
Expand All @@ -83,6 +86,24 @@ func (opts *deleteOpts) DeleteFromProject(d DeleterFromProject, projectID string
return nil
}

// DeleterFromProjectAuthDB deletes a resource from a project, it expects a callback
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not why after this change every time I run a test regarding any delete I get === RUN TestAtlasDBUsersDelete_Run %!(EXTRA string=test)--- PASS: TestAtlasDBUsersDelete_Run (0.00s) PASS.

There is this extra string (EXTRA string=test), and I do not know where it came from. I also tried to revert all of my changes but I still get the same result 🤕

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the issue by adding the successMessage to the test. The problem was caused by fmt.Printf(opts.successMessage, opts.entry) inside the function DeleterFromProject which was executed without the successMessage

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we improve on the deleteOpts api then? let's do this in another PR but this was probably an overlook from my side, we could improve this by having a fallback message and check the value we are using, like a said separate PR to not complicate this too much

// that should perform the deletion from the store.
func (opts *deleteOpts) DeleterFromProjectAuthDB(d DeleterFromProjectAuthDB, authDB, projectID string) error {
if !opts.confirm {
fmt.Println(opts.failMessage)
return nil
}
err := d(authDB, projectID, opts.entry)

if err != nil {
return err
}

fmt.Printf(opts.successMessage, opts.entry)

return nil
}

// Deleter a function to delete from the store.
type Deleter func(entry string) error

Expand Down
2 changes: 1 addition & 1 deletion internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
ProfileShort = "p" // ProfileShort flag to use a profile
OrgID = "orgId" // OrgID flag to use an Organization ID
ProjectID = "projectId" // ProjectID flag to use a project ID
AuthDB = "authDB" // AuthDB flag
Provider = "provider" // Provider flag to set the cloud provider
Region = "region" // Region flag
RegionShort = "r" // RegionShort flag
Expand All @@ -31,7 +32,6 @@ const (
Backup = "backup" // Backup flag
Username = "username" // Username flag
Password = "password" // Password flag
AuthDB = "authDB" // AuthDB flag
Email = "email" // Email flag
FirstName = "firstName" // FirstName flag
LastName = "lastName" // LastName flag
Expand Down
32 changes: 16 additions & 16 deletions internal/mocks/mock_database_users.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions internal/store/database_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ type DatabaseUserCreator interface {
}

type DatabaseUserDeleter interface {
DeleteDatabaseUser(string, string) error
DeleteDatabaseUser(string, string, string) error
}

type DatabaseUserUpdater interface {
UpdateDatabaseUser(*atlas.DatabaseUser) (*atlas.DatabaseUser, error)
}

type DatabaseUserDescriber interface {
DatabaseUser(string, string) (*atlas.DatabaseUser, error)
DatabaseUser(string, string, string) (*atlas.DatabaseUser, error)
}

type DatabaseUserStore interface {
Expand All @@ -60,11 +60,10 @@ func (s *Store) CreateDatabaseUser(user *atlas.DatabaseUser) (*atlas.DatabaseUse
}
}

func (s *Store) DeleteDatabaseUser(groupID, username string) error {
dbName := "admin"
func (s *Store) DeleteDatabaseUser(authDB, groupID, username string) error {
switch s.service {
case config.CloudService:
_, err := s.client.(*atlas.Client).DatabaseUsers.Delete(context.Background(), dbName, groupID, username)
_, err := s.client.(*atlas.Client).DatabaseUsers.Delete(context.Background(), authDB, groupID, username)
return err
default:
return fmt.Errorf("unsupported service: %s", s.service)
Expand All @@ -91,11 +90,10 @@ func (s *Store) UpdateDatabaseUser(user *atlas.DatabaseUser) (*atlas.DatabaseUse
}
}

func (s *Store) DatabaseUser(groupID string, username string) (*atlas.DatabaseUser, error) {
dbName := "admin"
func (s *Store) DatabaseUser(authDB string, groupID string, username string) (*atlas.DatabaseUser, error) {
switch s.service {
case config.CloudService:
result, _, err := s.client.(*atlas.Client).DatabaseUsers.Get(context.Background(), dbName, groupID, username)
result, _, err := s.client.(*atlas.Client).DatabaseUsers.Get(context.Background(), authDB, groupID, username)
return result, err
default:
return nil, fmt.Errorf("unsupported service: %s", s.service)
Expand Down
2 changes: 1 addition & 1 deletion internal/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ const (
DiskSizeGB = "Capacity, in gigabytes, of the host’s root volume."
Backup = "If true, uses Atlas Continuous Backups to back up cluster data."
MDBVersion = "MongoDB version of the cluster to deploy."
AuthDB = "Authentication database name."
Page = "Page number."
Limit = "Number of items per page."
Username = "Username for authenticating to MongoDB."
Password = "User’s password."
AuthDB = "Authentication database name."
Roles = "User's roles and the databases or collections on which the roles apply."
Comment = "Optional description of the whitelist entry."
Force = "Don't ask for confirmation."
Expand Down