Skip to content

Commit

Permalink
Merge pull request #16 from mongodb/fix/add-role-to-assign-project-ap…
Browse files Browse the repository at this point in the history
…i-key

Add AssignAPIKey struct to Assign request
  • Loading branch information
marinsalinas committed Aug 14, 2019
2 parents 3fdb73c + d6325b6 commit 35571ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions mongodbatlas/project_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const projectAPIKeysPath = "groups/%s/apiKeys"
type ProjectAPIKeysService interface {
List(context.Context, string, *ListOptions) ([]APIKey, *Response, error)
Create(context.Context, string, *APIKeyInput) (*APIKey, *Response, error)
Assign(context.Context, string, string) (*Response, error)
Assign(context.Context, string, string, *AssignAPIKey) (*Response, error)
Unassign(context.Context, string, string) (*Response, error)
}

Expand All @@ -26,6 +26,11 @@ type ProjectAPIKeysOp struct {

var _ ProjectAPIKeysService = &ProjectAPIKeysOp{}

// AssignAPIKey contains the roles to be assigned to an Organization API key into a Project
type AssignAPIKey struct {
Roles []string `json:"roles"`
}

//List all API-KEY in the organization associated to {GROUP-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/projectApiKeys/get-all-apiKeys-in-one-project/
func (s *ProjectAPIKeysOp) List(ctx context.Context, groupID string, listOptions *ListOptions) ([]APIKey, *Response, error) {
Expand Down Expand Up @@ -80,9 +85,9 @@ func (s *ProjectAPIKeysOp) Create(ctx context.Context, groupID string, createReq

//Assign an API-KEY related to {GROUP-ID} to a the project with {API-KEY-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/projectApiKeys/assign-one-org-apiKey-to-one-project/
func (s *ProjectAPIKeysOp) Assign(ctx context.Context, groupID string, keyID string) (*Response, error) {
func (s *ProjectAPIKeysOp) Assign(ctx context.Context, groupID string, keyID string, assignAPIKeyRequest *AssignAPIKey) (*Response, error) {
if groupID == "" {
return nil, NewArgError("apiKeyID", "must be set")
return nil, NewArgError("groupID", "must be set")
}

if keyID == "" {
Expand All @@ -93,7 +98,7 @@ func (s *ProjectAPIKeysOp) Assign(ctx context.Context, groupID string, keyID str

path := fmt.Sprintf("%s/%s", basePath, keyID)

req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, assignAPIKeyRequest)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions mongodbatlas/project_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ func TestProjectAPIKeys_Assign(t *testing.T) {
keyID := "5d1d12c087d9d63e6d682438"

mux.HandleFunc(fmt.Sprintf("/groups/%s/apiKeys/%s", groupID, keyID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
testMethod(t, r, http.MethodPatch)
})

_, err := client.ProjectAPIKeys.Assign(ctx, groupID, keyID)
_, err := client.ProjectAPIKeys.Assign(ctx, groupID, keyID, &AssignAPIKey{})
if err != nil {
t.Errorf("ProjectAPIKeys.Assign returned error: %v", err)
}
Expand Down

0 comments on commit 35571ed

Please sign in to comment.