Skip to content

Commit

Permalink
Merge pull request #49 from mongodb/databaseUsers
Browse files Browse the repository at this point in the history
Database Users
  • Loading branch information
marinsalinas committed Feb 4, 2020
2 parents ebe9c0c + ef0204b commit 65c6327
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 13 deletions.
35 changes: 24 additions & 11 deletions mongodbatlas/database_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const dbUsersBasePath = "groups/%s/databaseUsers"
//See more: https://docs.atlas.mongodb.com/reference/api/database-users/index.html
type DatabaseUsersService interface {
List(context.Context, string, *ListOptions) ([]DatabaseUser, *Response, error)
Get(context.Context, string, string) (*DatabaseUser, *Response, error)
Get(context.Context, string, string, string) (*DatabaseUser, *Response, error)
Create(context.Context, string, *DatabaseUser) (*DatabaseUser, *Response, error)
Update(context.Context, string, string, *DatabaseUser) (*DatabaseUser, *Response, error)
Delete(context.Context, string, string) (*Response, error)
Delete(context.Context, string, string, string) (*Response, error)
}

//DatabaseUsersServiceOp handles communication with the DatabaseUsers related methos of the
Expand All @@ -37,14 +37,15 @@ type Role struct {

// DatabaseUser represents MongoDB users in your cluster.
type DatabaseUser struct {
Roles []Role `json:"roles,omitempty"`
GroupID string `json:"groupId,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
DatabaseName string `json:"databaseName,omitempty"`
DeleteAfterDate string `json:"deleteAfterDate,omitempty"`
Labels []Label `json:"labels,omitempty"`
LDAPAuthType string `json:"ldapAuthType,omitempty"`
DeleteAfterDate string `json:"deleteAfterDate,omitempty"`
X509Type string `json:"x509Type,omitempty"`
GroupID string `json:"groupId,omitempty"`
Roles []Role `json:"roles,omitempty"`
Password string `json:"password,omitempty"`
Username string `json:"username,omitempty"`
}

// Label containing key-value pairs that tag and categorize the database user
Expand Down Expand Up @@ -91,13 +92,19 @@ func (s *DatabaseUsersServiceOp) List(ctx context.Context, groupID string, listO

//Get gets a single user in the project.
//See more: https://docs.atlas.mongodb.com/reference/api/database-users-get-single-user/
func (s *DatabaseUsersServiceOp) Get(ctx context.Context, groupID string, username string) (*DatabaseUser, *Response, error) {
func (s *DatabaseUsersServiceOp) Get(ctx context.Context, databaseName, groupID, username string) (*DatabaseUser, *Response, error) {
if databaseName == "" {
return nil, nil, NewArgError("databaseName", "must be set")
}
if groupID == "" {
return nil, nil, NewArgError("groupID", "must be set")
}
if username == "" {
return nil, nil, NewArgError("username", "must be set")
}

basePath := fmt.Sprintf(dbUsersBasePath, groupID)
path := fmt.Sprintf("%s/admin/%s", basePath, username)
path := fmt.Sprintf("%s/%s/%s", basePath, databaseName, username)

req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
Expand Down Expand Up @@ -162,13 +169,19 @@ func (s *DatabaseUsersServiceOp) Update(ctx context.Context, groupID string, use

//Delete deletes a user for the project.
// See more: https://docs.atlas.mongodb.com/reference/api/database-users-delete-a-user/
func (s *DatabaseUsersServiceOp) Delete(ctx context.Context, groupID string, username string) (*Response, error) {
func (s *DatabaseUsersServiceOp) Delete(ctx context.Context, databaseName, groupID, username string) (*Response, error) {
if databaseName == "" {
return nil, NewArgError("databaseName", "must be set")
}
if groupID == "" {
return nil, NewArgError("groupID", "must be set")
}
if username == "" {
return nil, NewArgError("username", "must be set")
}

basePath := fmt.Sprintf(dbUsersBasePath, groupID)
path := fmt.Sprintf("%s/admin/%s", basePath, username)
path := fmt.Sprintf("%s/%s/%s", basePath, databaseName, username)

req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
Expand Down
71 changes: 69 additions & 2 deletions mongodbatlas/database_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,73 @@ func TestDatabaseUsers_RetrievePageByNumber(t *testing.T) {
checkCurrentPage(t, resp, 2)
}

func TestDatabaseUsers_CreateWithX509Type(t *testing.T) {
setup()
defer teardown()

groupID := "1"

createRequest := &DatabaseUser{
DatabaseName: "$external",
Username: "test-username",
GroupID: groupID,
X509Type: "MANAGED",
Roles: []Role{{
DatabaseName: "admin",
RoleName: "readWriteAnyDatabase",
}},
}

mux.HandleFunc(fmt.Sprintf("/groups/%s/databaseUsers", groupID), func(w http.ResponseWriter, r *http.Request) {
expected := map[string]interface{}{
"databaseName": "$external",
"username": "test-username",
"groupId": groupID,
"x509Type": "MANAGED",

"roles": []interface{}{map[string]interface{}{
"databaseName": "admin",
"roleName": "readWriteAnyDatabase",
}},
}

var v map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&v)
if err != nil {
t.Fatalf("decode json: %v", err)
}

if !reflect.DeepEqual(v, expected) {
t.Errorf("Request body\n got=%#v\nwant=%#v", v, expected)
}

fmt.Fprint(w, `{
"databaseName": "$external",
"username": "test-username",
"groupId": "1",
"x509Type": "MANAGED",
"roles": [
{
"databaseName": "admin",
"roleName": "readWriteAnyDatabase"
}
]
}`)
})

dbUser, _, err := client.DatabaseUsers.Create(ctx, groupID, createRequest)
if err != nil {
t.Errorf("DatabaseUsers.Create returned error: %v", err)
}
if username := dbUser.Username; username != "test-username" {
t.Errorf("expected username '%s', received '%s'", "test-username", username)
}
if id := dbUser.GroupID; id != groupID {
t.Errorf("expected groupId '%s', received '%s'", groupID, id)
}

}

func TestDatabaseUsers_Create(t *testing.T) {
setup()
defer teardown()
Expand Down Expand Up @@ -202,7 +269,7 @@ func TestDatabaseUsers_GetDatabaseUser(t *testing.T) {
fmt.Fprint(w, `{"username":"test-username"}`)
})

dbUsers, _, err := client.DatabaseUsers.Get(ctx, "1", "test-username")
dbUsers, _, err := client.DatabaseUsers.Get(ctx, "admin", "1", "test-username")
if err != nil {
t.Errorf("DatabaseUser.Get returned error: %v", err)
}
Expand Down Expand Up @@ -299,7 +366,7 @@ func TestDatabaseUsers_Delete(t *testing.T) {
testMethod(t, r, http.MethodDelete)
})

_, err := client.DatabaseUsers.Delete(ctx, groupID, username)
_, err := client.DatabaseUsers.Delete(ctx, "admin", groupID, username)
if err != nil {
t.Errorf("DatabaseUser.Delete returned error: %v", err)
}
Expand Down

0 comments on commit 65c6327

Please sign in to comment.