diff --git a/mongodbatlas/organizations.go b/mongodbatlas/organizations.go index 2e053c9d..5cee3135 100644 --- a/mongodbatlas/organizations.go +++ b/mongodbatlas/organizations.go @@ -29,6 +29,7 @@ type OrganizationsService interface { List(context.Context, *OrganizationsListOptions) (*Organizations, *Response, error) Invitations(context.Context, string, *InvitationOptions) ([]*Invitation, *Response, error) Get(context.Context, string) (*Organization, *Response, error) + Update(context.Context, string, *Organization) (*Organization, *Response, error) Create(context.Context, *CreateOrganizationRequest) (*CreateOrganizationResponse, *Response, error) Invitation(context.Context, string, string) (*Invitation, *Response, error) Projects(context.Context, string, *ProjectsListOptions) (*Projects, *Response, error) @@ -138,6 +139,30 @@ func (s *OrganizationsServiceOp) Get(ctx context.Context, orgID string) (*Organi return root, resp, err } +// Update updates a single organization. +// +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Organizations/operation/renameOrganization +func (s *OrganizationsServiceOp) Update(ctx context.Context, orgID string, org *Organization) (*Organization, *Response, error) { + if orgID == "" { + return nil, nil, NewArgError("orgID", "must be set") + } + + path := fmt.Sprintf("%s/%s", orgsBasePath, orgID) + + req, err := s.Client.NewRequest(ctx, http.MethodPatch, path, org) + if err != nil { + return nil, nil, err + } + + root := new(Organization) + resp, err := s.Client.Do(ctx, req, root) + if err != nil { + return nil, resp, err + } + + return root, resp, err +} + // Projects gets all projects for the given organization ID. // // See more: https://docs.atlas.mongodb.com/reference/api/organization-get-all-projects/ diff --git a/mongodbatlas/organizations_test.go b/mongodbatlas/organizations_test.go index dacad4cd..e7db2719 100644 --- a/mongodbatlas/organizations_test.go +++ b/mongodbatlas/organizations_test.go @@ -210,6 +210,45 @@ func TestOrganizationsServiceOp_Get(t *testing.T) { } } +func TestOrganizationsServiceOp_Update(t *testing.T) { + client, mux, teardown := setup() + defer teardown() + + mux.HandleFunc(fmt.Sprintf("/%s/%s", orgsBasePath, orgID), func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPatch) + _, _ = fmt.Fprint(w, `{ + "id": "5a0a1e7e0f2912c554080adc", + "lastActiveAgent": "2016-03-09T18:19:37Z", + "links": [{ + "href": "https://cloud.mongodb.com/api/public/v1.0/orgs/5a0a1e7e0f2912c554080adc", + "rel": "self" + }], + "name": "test" + }`) + }) + + body := &Organization{Name: "test"} + response, _, err := client.Organizations.Update(ctx, orgID, body) + if err != nil { + t.Fatalf("Organizations.Update returned error: %v", err) + } + + expected := &Organization{ + ID: "5a0a1e7e0f2912c554080adc", + Links: []*Link{ + { + Href: "https://cloud.mongodb.com/api/public/v1.0/orgs/5a0a1e7e0f2912c554080adc", + Rel: "self", + }, + }, + Name: "test", + } + + if diff := deep.Equal(response, expected); diff != nil { + t.Error(diff) + } +} + func TestOrganizationsServiceOp_Projects(t *testing.T) { client, mux, teardown := setup() defer teardown()