From c4701ea8df4f1893c18f8f9b07c67ddd52f73693 Mon Sep 17 00:00:00 2001 From: Antonio Cabrera Date: Tue, 13 Aug 2019 22:28:59 -0500 Subject: [PATCH 1/2] Add AssignAPIKey struct to Assign request --- mongodbatlas/project_api_key.go | 11 ++++++++--- mongodbatlas/project_api_key_test.go | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/mongodbatlas/project_api_key.go b/mongodbatlas/project_api_key.go index a3840a942..b07dd7e4c 100644 --- a/mongodbatlas/project_api_key.go +++ b/mongodbatlas/project_api_key.go @@ -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) } @@ -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,omitempty"` +} + //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) { @@ -80,7 +85,7 @@ 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") } @@ -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.MethodPost, path, assignAPIKeyRequest) if err != nil { return nil, err } diff --git a/mongodbatlas/project_api_key_test.go b/mongodbatlas/project_api_key_test.go index d57e35c60..e6a20bc53 100644 --- a/mongodbatlas/project_api_key_test.go +++ b/mongodbatlas/project_api_key_test.go @@ -111,7 +111,7 @@ func TestProjectAPIKeys_Assign(t *testing.T) { testMethod(t, r, http.MethodPost) }) - _, 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) } From d6325b6966fde43b0a2c154849e202bfcc272307 Mon Sep 17 00:00:00 2001 From: Antonio Cabrera Date: Wed, 14 Aug 2019 10:39:32 -0500 Subject: [PATCH 2/2] Change ProjectAPIKeys.Assign POST -> Patch --- mongodbatlas/project_api_key.go | 6 +++--- mongodbatlas/project_api_key_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mongodbatlas/project_api_key.go b/mongodbatlas/project_api_key.go index b07dd7e4c..f3322bae3 100644 --- a/mongodbatlas/project_api_key.go +++ b/mongodbatlas/project_api_key.go @@ -28,7 +28,7 @@ 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,omitempty"` + Roles []string `json:"roles"` } //List all API-KEY in the organization associated to {GROUP-ID}. @@ -87,7 +87,7 @@ func (s *ProjectAPIKeysOp) Create(ctx context.Context, groupID string, createReq //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, assignAPIKeyRequest *AssignAPIKey) (*Response, error) { if groupID == "" { - return nil, NewArgError("apiKeyID", "must be set") + return nil, NewArgError("groupID", "must be set") } if keyID == "" { @@ -98,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, assignAPIKeyRequest) + req, err := s.client.NewRequest(ctx, http.MethodPatch, path, assignAPIKeyRequest) if err != nil { return nil, err } diff --git a/mongodbatlas/project_api_key_test.go b/mongodbatlas/project_api_key_test.go index e6a20bc53..e091a1e94 100644 --- a/mongodbatlas/project_api_key_test.go +++ b/mongodbatlas/project_api_key_test.go @@ -108,7 +108,7 @@ 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, &AssignAPIKey{})