@@ -8,6 +8,16 @@ import (
88
99const dbUsersBasePath = "groups/%s/databaseUsers"
1010
11+ var adminX509Type = map [string ]struct {}{
12+ "MANAGED" : {},
13+ "CUSTOMER" : {},
14+ }
15+
16+ var awsIAMType = map [string ]struct {}{
17+ "USER" : {},
18+ "ROLE" : {},
19+ }
20+
1121// DatabaseUsersService is an interface for interfacing with the Database Users
1222// endpoints of the MongoDB Atlas API.
1323// See more: https://docs.atlas.mongodb.com/reference/api/database-users/index.html
@@ -48,6 +58,24 @@ type DatabaseUser struct {
4858 Username string `json:"username,omitempty"`
4959}
5060
61+ // GetAuthDB determines the authentication database based on the type of user.
62+ // LDAP, X509 and AWSIAM should all use $external.
63+ // SCRAM-SHA should use admin
64+ func (user * DatabaseUser ) GetAuthDB () (name string ) {
65+ // base documentation https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/database_user
66+ name = "admin"
67+ _ , isX509 := adminX509Type [user .X509Type ]
68+ _ , isIAM := awsIAMType [user .AWSIAMType ]
69+
70+ isLDAP := len (user .LDAPAuthType ) > 0 && user .LDAPAuthType != "NONE"
71+
72+ if isX509 || isIAM || isLDAP {
73+ name = "$external"
74+ }
75+
76+ return
77+ }
78+
5179// Scope if presents a database user only have access to the indicated resource
5280// if none is given then it has access to all
5381type Scope struct {
@@ -158,7 +186,8 @@ func (s *DatabaseUsersServiceOp) Update(ctx context.Context, groupID, username s
158186 }
159187
160188 basePath := fmt .Sprintf (dbUsersBasePath , groupID )
161- path := fmt .Sprintf ("%s/admin/%s" , basePath , username )
189+
190+ path := fmt .Sprintf ("%s/%s/%s" , basePath , updateRequest .GetAuthDB (), username )
162191
163192 req , err := s .Client .NewRequest (ctx , http .MethodPatch , path , updateRequest )
164193 if err != nil {
0 commit comments