Skip to content

Commit b080773

Browse files
authored
CLOUDP-81828: Add support for collections when creating a dbuser (#602)
1 parent 40ae0f3 commit b080773

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

internal/convert/database_user.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
ExternalAuthDB = "$external"
2727
roleSep = "@"
2828
scopeSep = ":"
29+
collectionSep = "."
2930
defaultUserDatabase = "admin"
3031
defaultResourceType = "CLUSTER"
3132
)
@@ -35,32 +36,38 @@ const (
3536
func BuildAtlasRoles(r []string) []atlas.Role {
3637
roles := make([]atlas.Role, len(r))
3738
for i, roleP := range r {
38-
role := strings.Split(roleP, roleSep)
39-
roleName := role[0]
40-
databaseName := defaultUserDatabase
41-
if len(role) > 1 {
42-
databaseName = role[1]
39+
roleName, databaseName := splitRoleAndDBName(roleP)
40+
var collectionName string
41+
dbCollection := strings.Split(databaseName, collectionSep)
42+
databaseName = dbCollection[0]
43+
if len(dbCollection) > 1 {
44+
collectionName = strings.Join(dbCollection[1:], ".")
4345
}
44-
4546
roles[i] = atlas.Role{
46-
RoleName: roleName,
47-
DatabaseName: databaseName,
47+
RoleName: roleName,
48+
DatabaseName: databaseName,
49+
CollectionName: collectionName,
4850
}
4951
}
5052
return roles
5153
}
5254

55+
func splitRoleAndDBName(roleAndDBNAme string) (role, dbName string) {
56+
rd := strings.Split(roleAndDBNAme, roleSep)
57+
dbName = defaultUserDatabase
58+
role = rd[0]
59+
if len(rd) > 1 {
60+
dbName = rd[1]
61+
}
62+
return
63+
}
64+
5365
// BuildOMRoles converts the roles inside the array of string in an array of opsmngr.Role structs.
5466
// r contains roles in the format roleName@dbName
5567
func BuildOMRoles(r []string) []*opsmngr.Role {
5668
roles := make([]*opsmngr.Role, len(r))
5769
for i, roleP := range r {
58-
role := strings.Split(roleP, roleSep)
59-
roleName := role[0]
60-
databaseName := defaultUserDatabase
61-
if len(role) > 1 {
62-
databaseName = role[1]
63-
}
70+
roleName, databaseName := splitRoleAndDBName(roleP)
6471

6572
roles[i] = &opsmngr.Role{
6673
Role: roleName,

internal/convert/database_user_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ func TestBuildAtlasRoles(t *testing.T) {
6262
},
6363
},
6464
},
65+
{
66+
input: []string{"[email protected]"},
67+
want: []mongodbatlas.Role{
68+
{
69+
RoleName: "admin",
70+
DatabaseName: "db",
71+
CollectionName: "collection",
72+
},
73+
},
74+
},
75+
{
76+
input: []string{"[email protected]"},
77+
want: []mongodbatlas.Role{
78+
{
79+
RoleName: "admin",
80+
DatabaseName: "db",
81+
CollectionName: "collection.name",
82+
},
83+
},
84+
},
6585
}
6686

6787
for _, tc := range tests {

0 commit comments

Comments
 (0)