Skip to content

Commit 35571ed

Browse files
authored
Merge pull request #16 from mongodb/fix/add-role-to-assign-project-api-key
Add AssignAPIKey struct to Assign request
2 parents 3fdb73c + d6325b6 commit 35571ed

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

mongodbatlas/project_api_key.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const projectAPIKeysPath = "groups/%s/apiKeys"
1414
type ProjectAPIKeysService interface {
1515
List(context.Context, string, *ListOptions) ([]APIKey, *Response, error)
1616
Create(context.Context, string, *APIKeyInput) (*APIKey, *Response, error)
17-
Assign(context.Context, string, string) (*Response, error)
17+
Assign(context.Context, string, string, *AssignAPIKey) (*Response, error)
1818
Unassign(context.Context, string, string) (*Response, error)
1919
}
2020

@@ -26,6 +26,11 @@ type ProjectAPIKeysOp struct {
2626

2727
var _ ProjectAPIKeysService = &ProjectAPIKeysOp{}
2828

29+
// AssignAPIKey contains the roles to be assigned to an Organization API key into a Project
30+
type AssignAPIKey struct {
31+
Roles []string `json:"roles"`
32+
}
33+
2934
//List all API-KEY in the organization associated to {GROUP-ID}.
3035
//See more: https://docs.atlas.mongodb.com/reference/api/projectApiKeys/get-all-apiKeys-in-one-project/
3136
func (s *ProjectAPIKeysOp) List(ctx context.Context, groupID string, listOptions *ListOptions) ([]APIKey, *Response, error) {
@@ -80,9 +85,9 @@ func (s *ProjectAPIKeysOp) Create(ctx context.Context, groupID string, createReq
8085

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

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

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

96-
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
101+
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, assignAPIKeyRequest)
97102
if err != nil {
98103
return nil, err
99104
}

mongodbatlas/project_api_key_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ func TestProjectAPIKeys_Assign(t *testing.T) {
108108
keyID := "5d1d12c087d9d63e6d682438"
109109

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

114-
_, err := client.ProjectAPIKeys.Assign(ctx, groupID, keyID)
114+
_, err := client.ProjectAPIKeys.Assign(ctx, groupID, keyID, &AssignAPIKey{})
115115
if err != nil {
116116
t.Errorf("ProjectAPIKeys.Assign returned error: %v", err)
117117
}

0 commit comments

Comments
 (0)