Skip to content

Commit 1f2f93c

Browse files
INTMDB-914: Add update to project (#505)
1 parent f3bc4ec commit 1f2f93c

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

mongodbatlas/projects.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type ProjectsService interface {
4343
GetOneProject(context.Context, string) (*Project, *Response, error)
4444
GetOneProjectByName(context.Context, string) (*Project, *Response, error)
4545
Create(context.Context, *Project, *CreateProjectOptions) (*Project, *Response, error)
46+
Update(context.Context, string, *ProjectUpdateRequest) (*Project, *Response, error)
4647
Delete(context.Context, string) (*Response, error)
4748
GetProjectTeamsAssigned(context.Context, string) (*TeamsAssigned, *Response, error)
4849
AddTeamsToProject(context.Context, string, []*ProjectTeam) (*TeamsAssigned, *Response, error)
@@ -106,6 +107,11 @@ type CreateProjectOptions struct {
106107
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.
107108
}
108109

110+
// ProjectUpdateRequest represents an update request used in ProjectsService.Update.
111+
type ProjectUpdateRequest struct {
112+
Name string `json:"name"`
113+
}
114+
109115
// GetAllProjects gets all project.
110116
//
111117
// See more: https://docs.atlas.mongodb.com/reference/api/project-get-all/
@@ -208,6 +214,33 @@ func (s *ProjectsServiceOp) Create(ctx context.Context, createRequest *Project,
208214
return root, resp, err
209215
}
210216

217+
// Update updates a project.
218+
//
219+
// https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Projects/operation/updateProject
220+
func (s *ProjectsServiceOp) Update(ctx context.Context, projectID string, updateRequest *ProjectUpdateRequest) (*Project, *Response, error) {
221+
if updateRequest == nil {
222+
return nil, nil, NewArgError("updateRequest", "cannot be nil")
223+
}
224+
225+
if projectID == "" {
226+
return nil, nil, NewArgError("projectID", "must be set")
227+
}
228+
229+
basePath := fmt.Sprintf("%s/%s", projectBasePath, projectID)
230+
req, err := s.Client.NewRequest(ctx, http.MethodPatch, basePath, updateRequest)
231+
if err != nil {
232+
return nil, nil, err
233+
}
234+
235+
root := new(Project)
236+
resp, err := s.Client.Do(ctx, req, root)
237+
if err != nil {
238+
return nil, resp, err
239+
}
240+
241+
return root, resp, err
242+
}
243+
211244
// Delete deletes a project.
212245
//
213246
// See more: https://docs.atlas.mongodb.com/reference/api/project-delete-one/

mongodbatlas/projects_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,55 @@ func TestProject_Create(t *testing.T) {
247247
}
248248
}
249249

250+
func TestProject_Update(t *testing.T) {
251+
client, mux, teardown := setup()
252+
defer teardown()
253+
254+
projectID := "5a0a1e7e0f2912c554080adc"
255+
updateRequest := &ProjectUpdateRequest{
256+
Name: "test",
257+
}
258+
259+
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s", projectID), func(w http.ResponseWriter, r *http.Request) {
260+
fmt.Fprint(w, `{
261+
"clusterCount": 2,
262+
"created": "2016-07-14T14:19:33Z",
263+
"id": "5a0a1e7e0f2912c554080adc",
264+
"links": [{
265+
"href": "https://cloud.mongodb.com/api/atlas/v1.0/groups/5a0a1e7e0f2912c554080ae6",
266+
"rel": "self"
267+
}],
268+
"name": "test",
269+
"orgId": "5a0a1e7e0f2912c554080adc",
270+
"withDefaultAlertsSettings": true
271+
}`)
272+
})
273+
274+
project, _, err := client.Projects.Update(ctx, projectID, updateRequest)
275+
if err != nil {
276+
t.Fatalf("Projects.Update returned error: %v", err)
277+
}
278+
279+
expected := &Project{
280+
ClusterCount: 2,
281+
Created: "2016-07-14T14:19:33Z",
282+
ID: projectID,
283+
Links: []*Link{
284+
{
285+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/5a0a1e7e0f2912c554080ae6",
286+
Rel: "self",
287+
},
288+
},
289+
Name: "test",
290+
OrgID: "5a0a1e7e0f2912c554080adc",
291+
WithDefaultAlertsSettings: pointer(true),
292+
}
293+
294+
if diff := deep.Equal(project, expected); diff != nil {
295+
t.Error(diff)
296+
}
297+
}
298+
250299
func TestProject_Create_without_opts(t *testing.T) {
251300
client, mux, teardown := setup()
252301
defer teardown()

0 commit comments

Comments
 (0)