Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add org creation endpoint #387

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion mongodbatlas/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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/
Expand Down Expand Up @@ -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
}
60 changes: 60 additions & 0 deletions mongodbatlas/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}