Skip to content

Commit

Permalink
CLOUDP-237474: AtlasCLI fails to support collection names with dots f…
Browse files Browse the repository at this point in the history
…or custom roles (#2767)
  • Loading branch information
gssbzn committed Mar 13, 2024
1 parent f52ef94 commit 1181f26
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
7 changes: 4 additions & 3 deletions internal/cli/atlas/customdbroles/create.go
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -62,10 +61,12 @@ func (opts *CreateOpts) Run() error {
}

func (opts *CreateOpts) newCustomDBRole() *atlasv2.UserCustomDBRole {
actions := convert.BuildAtlasActions(opts.action)
inheritedRoles := convert.BuildAtlasInheritedRoles(opts.inheritedRoles)
return &atlasv2.UserCustomDBRole{
RoleName: opts.roleName,
Actions: pointer.Get((convert.BuildAtlasActions(opts.action))),
InheritedRoles: pointer.Get(convert.BuildAtlasInheritedRoles(opts.inheritedRoles)),
Actions: &actions,
InheritedRoles: &inheritedRoles,
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/convert/custom_db_role.go
Expand Up @@ -56,7 +56,7 @@ func BuildAtlasActions(a []string) []atlasv2.DatabasePrivilegeAction {
resource := strings.Split(action[1], resourceSep)
resourceStruct.Db = resource[0]
if len(resource) > 1 {
resourceStruct.Collection = resource[1]
resourceStruct.Collection = strings.Join(resource[1:], resourceSep)
}
} else {
resourceStruct.Cluster = true
Expand Down
64 changes: 36 additions & 28 deletions internal/convert/custom_db_role_test.go
Expand Up @@ -24,12 +24,10 @@ import (
)

func TestBuildAtlasInheritedRoles(t *testing.T) {
type test struct {
tests := []struct {
input []string
want []atlasv2.DatabaseInheritedRole
}

tests := []test{
}{
{
input: []string{"admin"},
want: []atlasv2.DatabaseInheritedRole{
Expand Down Expand Up @@ -69,84 +67,94 @@ func TestBuildAtlasInheritedRoles(t *testing.T) {
t.Run("", func(t *testing.T) {
t.Parallel()
got := BuildAtlasInheritedRoles(input)
if err := deep.Equal(want, got); err != nil {
t.Fatalf("expected: %v, got: %v", want, got)
if diff := deep.Equal(want, got); diff != nil {
t.Error(diff)
}
})
}
}

func TestBuildAtlasActions(t *testing.T) {
type test struct {
tests := []struct {
name string
input []string
want []atlasv2.DatabasePrivilegeAction
}

cluster := true

testdb := "testdb"
collection := "collection"
datalake := "DATA_LAKE"

tests := []test{
}{
{
name: "role",
input: []string{"clusterName"},
want: []atlasv2.DatabasePrivilegeAction{
{
Action: "clusterName",
Resources: &[]atlasv2.DatabasePermittedNamespaceResource{
{
Cluster: cluster,
Cluster: true,
},
},
},
},
},
{
name: "role and fqn",
input: []string{"clusterName@testdb.collection"},
want: []atlasv2.DatabasePrivilegeAction{
{
Action: "clusterName",
Resources: &[]atlasv2.DatabasePermittedNamespaceResource{
{
Db: testdb,
Collection: collection,
Db: "testdb",
Collection: "collection",
},
},
},
},
},
{
name: "role and fqn",
input: []string{"clusterName@testdb.collection.with.dots"},
want: []atlasv2.DatabasePrivilegeAction{
{
Action: "clusterName",
Resources: &[]atlasv2.DatabasePermittedNamespaceResource{
{
Db: "testdb",
Collection: "collection.with.dots",
},
},
},
},
},
{
name: "role and fqn",
input: []string{"clusterName", "name@DATA_LAKE"},
want: []atlasv2.DatabasePrivilegeAction{
{
Action: "clusterName",
Resources: &[]atlasv2.DatabasePermittedNamespaceResource{
{
Cluster: cluster,
Cluster: true,
},
},
},
{
Action: "name",
Resources: &[]atlasv2.DatabasePermittedNamespaceResource{
{
Db: datalake,
Db: "DATA_LAKE",
},
},
},
},
},
}

for _, tc := range tests {
input := tc.input
want := tc.want
t.Run("", func(t *testing.T) {
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := BuildAtlasActions(input)
if err := deep.Equal(want, got); err != nil {
t.Fatalf("expected: %v, got: %v", want, got)
got := BuildAtlasActions(tc.input)
if diff := deep.Equal(tc.want, got); diff != nil {
t.Error(diff)
}
})
}
Expand Down

0 comments on commit 1181f26

Please sign in to comment.