Skip to content

Commit

Permalink
INTMDB-914: Add update to project (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo authored Jul 6, 2023
1 parent f3bc4ec commit 1f2f93c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
33 changes: 33 additions & 0 deletions mongodbatlas/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type ProjectsService interface {
GetOneProject(context.Context, string) (*Project, *Response, error)
GetOneProjectByName(context.Context, string) (*Project, *Response, error)
Create(context.Context, *Project, *CreateProjectOptions) (*Project, *Response, error)
Update(context.Context, string, *ProjectUpdateRequest) (*Project, *Response, error)
Delete(context.Context, string) (*Response, error)
GetProjectTeamsAssigned(context.Context, string) (*TeamsAssigned, *Response, error)
AddTeamsToProject(context.Context, string, []*ProjectTeam) (*TeamsAssigned, *Response, error)
Expand Down Expand Up @@ -106,6 +107,11 @@ type CreateProjectOptions struct {
ProjectOwnerID string `url:"projectOwnerId,omitempty"` // Unique 24-hexadecimal digit string that identifies the Atlas user account to be granted the Project Owner role on the specified project.
}

// ProjectUpdateRequest represents an update request used in ProjectsService.Update.
type ProjectUpdateRequest struct {
Name string `json:"name"`
}

// GetAllProjects gets all project.
//
// See more: https://docs.atlas.mongodb.com/reference/api/project-get-all/
Expand Down Expand Up @@ -208,6 +214,33 @@ func (s *ProjectsServiceOp) Create(ctx context.Context, createRequest *Project,
return root, resp, err
}

// Update updates a project.
//
// https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Projects/operation/updateProject
func (s *ProjectsServiceOp) Update(ctx context.Context, projectID string, updateRequest *ProjectUpdateRequest) (*Project, *Response, error) {
if updateRequest == nil {
return nil, nil, NewArgError("updateRequest", "cannot be nil")
}

if projectID == "" {
return nil, nil, NewArgError("projectID", "must be set")
}

basePath := fmt.Sprintf("%s/%s", projectBasePath, projectID)
req, err := s.Client.NewRequest(ctx, http.MethodPatch, basePath, updateRequest)
if err != nil {
return nil, nil, err
}

root := new(Project)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, err
}

// Delete deletes a project.
//
// See more: https://docs.atlas.mongodb.com/reference/api/project-delete-one/
Expand Down
49 changes: 49 additions & 0 deletions mongodbatlas/projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,55 @@ func TestProject_Create(t *testing.T) {
}
}

func TestProject_Update(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

projectID := "5a0a1e7e0f2912c554080adc"
updateRequest := &ProjectUpdateRequest{
Name: "test",
}

mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s", projectID), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{
"clusterCount": 2,
"created": "2016-07-14T14:19:33Z",
"id": "5a0a1e7e0f2912c554080adc",
"links": [{
"href": "https://cloud.mongodb.com/api/atlas/v1.0/groups/5a0a1e7e0f2912c554080ae6",
"rel": "self"
}],
"name": "test",
"orgId": "5a0a1e7e0f2912c554080adc",
"withDefaultAlertsSettings": true
}`)
})

project, _, err := client.Projects.Update(ctx, projectID, updateRequest)
if err != nil {
t.Fatalf("Projects.Update returned error: %v", err)
}

expected := &Project{
ClusterCount: 2,
Created: "2016-07-14T14:19:33Z",
ID: projectID,
Links: []*Link{
{
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/5a0a1e7e0f2912c554080ae6",
Rel: "self",
},
},
Name: "test",
OrgID: "5a0a1e7e0f2912c554080adc",
WithDefaultAlertsSettings: pointer(true),
}

if diff := deep.Equal(project, expected); diff != nil {
t.Error(diff)
}
}

func TestProject_Create_without_opts(t *testing.T) {
client, mux, teardown := setup()
defer teardown()
Expand Down

0 comments on commit 1f2f93c

Please sign in to comment.