Skip to content

Commit

Permalink
CLOUDP-234532: fix atlas customDbRoles update command (#2687)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibulca authored Mar 1, 2024
1 parent c994778 commit 63207dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
13 changes: 5 additions & 8 deletions internal/cli/atlas/customdbroles/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/mongodb/mongodb-atlas-cli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/internal/convert"
"github.com/mongodb/mongodb-atlas-cli/internal/flag"
"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/spf13/cobra"
Expand Down Expand Up @@ -69,20 +68,18 @@ func (opts *UpdateOpts) Run() error {
}

func (opts *UpdateOpts) newCustomDBRole(existingRole *atlasv2.UserCustomDBRole) *atlasv2.UserCustomDBRole {
out := &atlasv2.UserCustomDBRole{
InheritedRoles: pointer.Get(convert.BuildAtlasInheritedRoles(opts.inheritedRoles)),
}
inheritedRoles := convert.BuildAtlasInheritedRoles(opts.inheritedRoles)
actions := joinActions(convert.BuildAtlasActions(opts.action))
inheritedRoles := []atlasv2.DatabaseInheritedRole{}

if opts.append {
actions = appendActions(existingRole.GetActions(), actions)
inheritedRoles = append(inheritedRoles, existingRole.GetInheritedRoles()...)
}
out.SetActions(actions)
out.SetInheritedRoles(inheritedRoles)

return out
return &atlasv2.UserCustomDBRole{
Actions: &actions,
InheritedRoles: &inheritedRoles,
}
}

func (opts *UpdateOpts) validate() error {
Expand Down
25 changes: 16 additions & 9 deletions test/e2e/atlas/custom_db_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ import (
)

const (
createPrivilege = "UPDATE"
updatePrivilege = "LIST_SESSIONS"
inheritedRole = "enableSharding@admin"
enableSharding = "enableSharding"
createPrivilege = "UPDATE"
updatePrivilege = "LIST_SESSIONS"
enableShardingRole = "enableSharding"
enableShardingInheritedRole = "enableSharding@admin"
readRole = "read"
readInheritedRole = "read@mydb"
)

func TestDBRoles(t *testing.T) {
Expand All @@ -50,7 +52,7 @@ func TestDBRoles(t *testing.T) {
"create",
roleName,
"--privilege", fmt.Sprintf("%[email protected]", createPrivilege),
"--inheritedRole", inheritedRole,
"--inheritedRole", enableShardingInheritedRole,
"-o=json",
)
cmd.Env = os.Environ()
Expand All @@ -65,7 +67,7 @@ func TestDBRoles(t *testing.T) {
a.Len(role.GetActions(), 1)
a.Equal(createPrivilege, role.GetActions()[0].Action)
a.Len(role.GetInheritedRoles(), 1)
a.Equal(enableSharding, role.GetInheritedRoles()[0].Role)
a.Equal(enableShardingRole, role.GetInheritedRoles()[0].Role)
})

t.Run("List", func(t *testing.T) {
Expand Down Expand Up @@ -101,14 +103,15 @@ func TestDBRoles(t *testing.T) {
a.Len(role.GetActions(), 1)
a.Equal(createPrivilege, role.GetActions()[0].Action)
a.Len(role.GetInheritedRoles(), 1)
a.Equal(enableSharding, role.GetInheritedRoles()[0].Role)
a.Equal(enableShardingRole, role.GetInheritedRoles()[0].Role)
})

t.Run("Update with append", func(t *testing.T) {
cmd := exec.Command(cliPath,
customDBRoleEntity,
"update",
roleName,
"--inheritedRole", readInheritedRole,
"--privilege", updatePrivilege,
"--privilege", fmt.Sprintf("%[email protected]", createPrivilege),
"--append",
Expand All @@ -126,15 +129,17 @@ func TestDBRoles(t *testing.T) {
a.ElementsMatch(
[]string{role.GetActions()[0].Action, role.GetActions()[1].Action},
[]string{updatePrivilege, createPrivilege})
a.Len(role.GetInheritedRoles(), 1)
a.Equal(enableSharding, role.GetInheritedRoles()[0].Role)
a.ElementsMatch(
[]string{enableShardingRole, readRole},
[]string{role.GetInheritedRoles()[0].Role, role.GetInheritedRoles()[1].Role})
})

t.Run("Update", func(t *testing.T) {
cmd := exec.Command(cliPath,
customDBRoleEntity,
"update",
roleName,
"--inheritedRole", enableShardingInheritedRole,
"--privilege", updatePrivilege,
"-o=json")
cmd.Env = os.Environ()
Expand All @@ -148,6 +153,8 @@ func TestDBRoles(t *testing.T) {
a.Equal(roleName, role.RoleName)
a.Len(role.GetActions(), 1)
a.Equal(updatePrivilege, role.GetActions()[0].Action)
a.Len(role.GetInheritedRoles(), 1)
a.Equal(enableShardingRole, role.GetInheritedRoles()[0].Role)
})

t.Run("Delete", func(t *testing.T) {
Expand Down

0 comments on commit 63207dd

Please sign in to comment.