diff --git a/mongodbatlas/organizations.go b/mongodbatlas/organizations.go index 5872048c1..99992e2b2 100644 --- a/mongodbatlas/organizations.go +++ b/mongodbatlas/organizations.go @@ -24,11 +24,12 @@ const orgsBasePath = "api/atlas/v1.0/orgs" // OrganizationsService provides access to the organization related functions in the Atlas API. // -// See more: https://docs.atlas.mongodb.com/reference/api/organizations/ +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Organizations 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) + Create(context.Context, *CreateOrganizationRequest) (*CreateOrganizationResponse, *Response, error) Invitation(context.Context, string, string) (*Invitation, *Response, error) Projects(context.Context, string, *ProjectsListOptions) (*Projects, *Response, error) Users(context.Context, string, *ListOptions) (*AtlasUsersResponse, *Response, error) @@ -72,6 +73,20 @@ type Organizations struct { TotalCount int `json:"totalCount"` } +// CreateOrganizationRequest struct for CreateOrganizationRequest. +type CreateOrganizationRequest struct { + APIKey *APIKeyInput `json:"apiKey,omitempty"` + Name string `json:"name"` + OrgOwnerID string `json:"orgOwnerId"` +} + +// CreateOrganizationResponse struct for CreateOrganizationResponse. +type CreateOrganizationResponse struct { + APIKey *APIKey `json:"apiKey,omitempty"` + OrgOwnerID *string `json:"orgOwnerId,omitempty"` + Organization *Organization `json:"organization,omitempty"` +} + // List gets all organizations. // // See more: https://docs.atlas.mongodb.com/reference/api/organization-get-all/ @@ -198,3 +213,25 @@ func (s *OrganizationsServiceOp) Delete(ctx context.Context, orgID string) (*Res return resp, err } + +// Create creates an organization. +// +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Organizations/operation/createOrganization +func (s *OrganizationsServiceOp) Create(ctx context.Context, request *CreateOrganizationRequest) (*CreateOrganizationResponse, *Response, error) { + if request == nil { + return nil, nil, NewArgError("request", "must be set") + } + + req, err := s.Client.NewRequest(ctx, http.MethodPost, orgsBasePath, request) + if err != nil { + return nil, nil, err + } + + root := new(CreateOrganizationResponse) + resp, err := s.Client.Do(ctx, req, root) + if err != nil { + return nil, resp, err + } + + return root, resp, nil +} diff --git a/mongodbatlas/organizations_test.go b/mongodbatlas/organizations_test.go index 5cedf337b..dacad4cd4 100644 --- a/mongodbatlas/organizations_test.go +++ b/mongodbatlas/organizations_test.go @@ -407,3 +407,63 @@ func TestOrganizations_Delete(t *testing.T) { t.Fatalf("Organizations.Delete returned error: %v", err) } } + +func TestOrganizationsServiceOp_Create(t *testing.T) { + client, mux, teardown := setup() + defer teardown() + + mux.HandleFunc("/api/atlas/v1.0/orgs", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPost) + _, _ = fmt.Fprint(w, `{ + "apiKey": { + "desc": "string", + "id": "32b6e34b3d91647abb20e7b8", + "privateKey": "55c3bbb6-b4bb-0be1-e66d20841f3e", + "publicKey": "zmmrboas", + "roles": [ + { + "orgId": "32b6e34b3d91647abb20e7b8", + "roleName": "ORG_OWNER" + } + ] + }, + "organization": { + "id": "32b6e34b3d91647abb20e7b8", + "name": "test" + } + }`) + }) + + body := &CreateOrganizationRequest{ + APIKey: &APIKeyInput{ + Desc: "string", + Roles: []string{"ORG_OWNER"}, + }, + Name: "test", + } + + response, _, err := client.Organizations.Create(ctx, body) + if err != nil { + t.Fatalf("Organizations.Create returned error: %v", err) + } + + expected := &CreateOrganizationResponse{ + APIKey: &APIKey{ + ID: "32b6e34b3d91647abb20e7b8", + Desc: "string", + Roles: []AtlasRole{ + {OrgID: "32b6e34b3d91647abb20e7b8", RoleName: "ORG_OWNER"}, + }, + PrivateKey: "55c3bbb6-b4bb-0be1-e66d20841f3e", + PublicKey: "zmmrboas", + }, + Organization: &Organization{ + ID: "32b6e34b3d91647abb20e7b8", + Name: "test", + }, + } + + if diff := deep.Equal(response, expected); diff != nil { + t.Error(diff) + } +}