Skip to content

Commit

Permalink
CLOUDP-118028: Allow to list and edit project settings (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo committed Apr 8, 2022
1 parent bc8b40e commit e1828a0
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
77 changes: 77 additions & 0 deletions mongodbatlas/project_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2022 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mongodbatlas

import (
"context"
"fmt"
"net/http"
)

const projectSettingsBasePath = projectBasePath + "/%s/settings"

type ProjectSettings struct {
IsCollectDatabaseSpecificsStatisticsEnabled *bool `url:"isCollectDatabaseSpecificsStatisticsEnabled,omitempty"`
IsDataExplorerEnabled *bool `url:"isDataExplorerEnabled,omitempty"`
IsPerformanceAdvisorEnabled *bool `url:"isPerformanceAdvisorEnabled,omitempty"`
IsRealtimePerformancePanelEnabled *bool `url:"isRealtimePerformancePanelEnabled,omitempty"`
IsSchemaAdvisorEnabled *bool `url:"isSchemaAdvisorEnabled,omitempty"`
}

// GetProjectSettings gets details about the settings for specified project.
//
// See more: https://www.mongodb.com/docs/atlas/reference/api/project-settings-get-one/
func (s *ProjectsServiceOp) GetProjectSettings(ctx context.Context, groupID string) (*ProjectSettings, *Response, error) {
if groupID == "" {
return nil, nil, NewArgError("groupID", "must be set")
}

path := fmt.Sprintf(projectSettingsBasePath, groupID)
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

var root *ProjectSettings
resp, err := s.Client.Do(ctx, req, &root)
if err != nil {
return nil, resp, err
}

return root, resp, nil
}

// UpdateProjectSettings updates the settings for the specified project.
//
// See more: https://www.mongodb.com/docs/atlas/reference/api/project-settings-update-one/
func (s *ProjectsServiceOp) UpdateProjectSettings(ctx context.Context, groupID string, projectSettings *ProjectSettings) (*ProjectSettings, *Response, error) {
if groupID == "" {
return nil, nil, NewArgError("groupID", "must be set")
}

path := fmt.Sprintf(projectSettingsBasePath, groupID)
req, err := s.Client.NewRequest(ctx, http.MethodPatch, path, projectSettings)
if err != nil {
return nil, nil, err
}

var root *ProjectSettings
resp, err := s.Client.Do(ctx, req, &root)
if err != nil {
return nil, resp, err
}

return root, resp, nil
}
98 changes: 98 additions & 0 deletions mongodbatlas/project_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2022 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mongodbatlas

import (
"fmt"
"net/http"
"testing"

"github.com/go-test/deep"
"github.com/openlyinc/pointy"
)

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

mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/settings", groupID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
_, _ = fmt.Fprint(w, `{
"isCollectDatabaseSpecificsStatisticsEnabled": true,
"isDataExplorerEnabled": true,
"isPerformanceAdvisorEnabled": true,
"isRealtimePerformancePanelEnabled": true,
"isSchemaAdvisorEnabled": true
}`)
})

projectSettings, _, err := client.Projects.GetProjectSettings(ctx, groupID)
if err != nil {
t.Fatalf("Projects.GetProjectSettings returned error: %v", err)
}

expected := &ProjectSettings{
IsCollectDatabaseSpecificsStatisticsEnabled: pointy.Bool(true),
IsDataExplorerEnabled: pointy.Bool(true),
IsPerformanceAdvisorEnabled: pointy.Bool(true),
IsRealtimePerformancePanelEnabled: pointy.Bool(true),
IsSchemaAdvisorEnabled: pointy.Bool(true),
}

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

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

mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/settings", groupID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPatch)
_, _ = fmt.Fprint(w, `{
"isCollectDatabaseSpecificsStatisticsEnabled": true,
"isDataExplorerEnabled": true,
"isPerformanceAdvisorEnabled": true,
"isRealtimePerformancePanelEnabled": true,
"isSchemaAdvisorEnabled": true
}`)
})

body := &ProjectSettings{
IsCollectDatabaseSpecificsStatisticsEnabled: pointy.Bool(true),
IsDataExplorerEnabled: pointy.Bool(true),
IsPerformanceAdvisorEnabled: pointy.Bool(true),
IsRealtimePerformancePanelEnabled: pointy.Bool(true),
IsSchemaAdvisorEnabled: pointy.Bool(true),
}

response, _, err := client.Projects.UpdateProjectSettings(ctx, groupID, body)
if err != nil {
t.Fatalf("Projects.UpdateProjectSettings returned error: %v", err)
}

expected := &ProjectSettings{
IsCollectDatabaseSpecificsStatisticsEnabled: pointy.Bool(true),
IsDataExplorerEnabled: pointy.Bool(true),
IsPerformanceAdvisorEnabled: pointy.Bool(true),
IsRealtimePerformancePanelEnabled: pointy.Bool(true),
IsSchemaAdvisorEnabled: pointy.Bool(true),
}

if diff := deep.Equal(response, expected); diff != nil {
t.Error(diff)
}
}
2 changes: 2 additions & 0 deletions mongodbatlas/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type ProjectsService interface {
UpdateInvitation(context.Context, string, *Invitation) (*Invitation, *Response, error)
UpdateInvitationByID(context.Context, string, string, *Invitation) (*Invitation, *Response, error)
DeleteInvitation(context.Context, string, string) (*Response, error)
GetProjectSettings(context.Context, string) (*ProjectSettings, *Response, error)
UpdateProjectSettings(context.Context, string, *ProjectSettings) (*ProjectSettings, *Response, error)
}

// ProjectsServiceOp handles communication with the Projects related methods of the
Expand Down

0 comments on commit e1828a0

Please sign in to comment.