Skip to content

Commit 774bf48

Browse files
authored
feat: add org creation endpoint (#387)
1 parent 01d7604 commit 774bf48

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

mongodbatlas/organizations.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ const orgsBasePath = "api/atlas/v1.0/orgs"
2424

2525
// OrganizationsService provides access to the organization related functions in the Atlas API.
2626
//
27-
// See more: https://docs.atlas.mongodb.com/reference/api/organizations/
27+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Organizations
2828
type OrganizationsService interface {
2929
List(context.Context, *OrganizationsListOptions) (*Organizations, *Response, error)
3030
Invitations(context.Context, string, *InvitationOptions) ([]*Invitation, *Response, error)
3131
Get(context.Context, string) (*Organization, *Response, error)
32+
Create(context.Context, *CreateOrganizationRequest) (*CreateOrganizationResponse, *Response, error)
3233
Invitation(context.Context, string, string) (*Invitation, *Response, error)
3334
Projects(context.Context, string, *ProjectsListOptions) (*Projects, *Response, error)
3435
Users(context.Context, string, *ListOptions) (*AtlasUsersResponse, *Response, error)
@@ -72,6 +73,20 @@ type Organizations struct {
7273
TotalCount int `json:"totalCount"`
7374
}
7475

76+
// CreateOrganizationRequest struct for CreateOrganizationRequest.
77+
type CreateOrganizationRequest struct {
78+
APIKey *APIKeyInput `json:"apiKey,omitempty"`
79+
Name string `json:"name"`
80+
OrgOwnerID string `json:"orgOwnerId"`
81+
}
82+
83+
// CreateOrganizationResponse struct for CreateOrganizationResponse.
84+
type CreateOrganizationResponse struct {
85+
APIKey *APIKey `json:"apiKey,omitempty"`
86+
OrgOwnerID *string `json:"orgOwnerId,omitempty"`
87+
Organization *Organization `json:"organization,omitempty"`
88+
}
89+
7590
// List gets all organizations.
7691
//
7792
// 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
198213

199214
return resp, err
200215
}
216+
217+
// Create creates an organization.
218+
//
219+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Organizations/operation/createOrganization
220+
func (s *OrganizationsServiceOp) Create(ctx context.Context, request *CreateOrganizationRequest) (*CreateOrganizationResponse, *Response, error) {
221+
if request == nil {
222+
return nil, nil, NewArgError("request", "must be set")
223+
}
224+
225+
req, err := s.Client.NewRequest(ctx, http.MethodPost, orgsBasePath, request)
226+
if err != nil {
227+
return nil, nil, err
228+
}
229+
230+
root := new(CreateOrganizationResponse)
231+
resp, err := s.Client.Do(ctx, req, root)
232+
if err != nil {
233+
return nil, resp, err
234+
}
235+
236+
return root, resp, nil
237+
}

mongodbatlas/organizations_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,63 @@ func TestOrganizations_Delete(t *testing.T) {
407407
t.Fatalf("Organizations.Delete returned error: %v", err)
408408
}
409409
}
410+
411+
func TestOrganizationsServiceOp_Create(t *testing.T) {
412+
client, mux, teardown := setup()
413+
defer teardown()
414+
415+
mux.HandleFunc("/api/atlas/v1.0/orgs", func(w http.ResponseWriter, r *http.Request) {
416+
testMethod(t, r, http.MethodPost)
417+
_, _ = fmt.Fprint(w, `{
418+
"apiKey": {
419+
"desc": "string",
420+
"id": "32b6e34b3d91647abb20e7b8",
421+
"privateKey": "55c3bbb6-b4bb-0be1-e66d20841f3e",
422+
"publicKey": "zmmrboas",
423+
"roles": [
424+
{
425+
"orgId": "32b6e34b3d91647abb20e7b8",
426+
"roleName": "ORG_OWNER"
427+
}
428+
]
429+
},
430+
"organization": {
431+
"id": "32b6e34b3d91647abb20e7b8",
432+
"name": "test"
433+
}
434+
}`)
435+
})
436+
437+
body := &CreateOrganizationRequest{
438+
APIKey: &APIKeyInput{
439+
Desc: "string",
440+
Roles: []string{"ORG_OWNER"},
441+
},
442+
Name: "test",
443+
}
444+
445+
response, _, err := client.Organizations.Create(ctx, body)
446+
if err != nil {
447+
t.Fatalf("Organizations.Create returned error: %v", err)
448+
}
449+
450+
expected := &CreateOrganizationResponse{
451+
APIKey: &APIKey{
452+
ID: "32b6e34b3d91647abb20e7b8",
453+
Desc: "string",
454+
Roles: []AtlasRole{
455+
{OrgID: "32b6e34b3d91647abb20e7b8", RoleName: "ORG_OWNER"},
456+
},
457+
PrivateKey: "55c3bbb6-b4bb-0be1-e66d20841f3e",
458+
PublicKey: "zmmrboas",
459+
},
460+
Organization: &Organization{
461+
ID: "32b6e34b3d91647abb20e7b8",
462+
Name: "test",
463+
},
464+
}
465+
466+
if diff := deep.Equal(response, expected); diff != nil {
467+
t.Error(diff)
468+
}
469+
}

0 commit comments

Comments
 (0)