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

CLOUDP-118028: [atlascli] Allow to list and edit project settings #287

Merged
merged 5 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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:"isRealtimePerformancePanelEnabled,omitempty"`
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
}

// 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