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

INTMDB-313: Update the project resource with new settings #741

Merged
merged 11 commits into from
Jun 3, 2022
Merged
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ require (
github.com/mwielbut/pointy v1.1.0
github.com/spf13/cast v1.5.0
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20210625132053-af2d5c0ad54f
go.mongodb.org/atlas v0.16.1-0.20220527133640-ba676d378a30
go.mongodb.org/atlas v0.16.1-0.20220531163122-551edbfb2f27
go.mongodb.org/realm v0.1.0
)
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1231,12 +1231,10 @@ go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsX
go.mongodb.org/atlas v0.12.0/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
go.mongodb.org/atlas v0.15.1-0.20220215171307-4b760c3c624f h1:IvKkFdSSBLC5kqB1X87vn8CRAI7eXoMSK7u2lG+WUg8=
go.mongodb.org/atlas v0.15.1-0.20220215171307-4b760c3c624f/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/atlas v0.16.0 h1:IqnDuK3XAZUgJ5lPHc4v4z4B8F6mvsS37O4ck7tOYVc=
go.mongodb.org/atlas v0.16.0/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/atlas v0.16.1-0.20220518082525-1080bb496bd4 h1:l2rsLubhVznOdSspZcAUy8KjZAvqw9uichFO087edEI=
go.mongodb.org/atlas v0.16.1-0.20220518082525-1080bb496bd4/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/atlas v0.16.1-0.20220527133640-ba676d378a30 h1:UVbPMJSXVDyvIA/JEHI2HAwc+B4R6xpnmSya/pbANpY=
go.mongodb.org/atlas v0.16.1-0.20220527133640-ba676d378a30/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/atlas v0.16.1-0.20220531163122-551edbfb2f27 h1:rGTb8CaE9ZKNjmdUJ58jFcHopLg6o6Kzfm9AIayq1Hw=
go.mongodb.org/atlas v0.16.1-0.20220531163122-551edbfb2f27/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M=
go.mongodb.org/realm v0.1.0/go.mod h1:4Vj6iy+Puo1TDERcoh4XZ+pjtwbOzPpzqy3Cwe8ZmDM=
go.mozilla.org/mozlog v0.0.0-20170222151521-4bb13139d403/go.mod h1:jHoPAGnDrCy6kaI2tAze5Prf0Nr0w/oNkROt2lw3n3o=
Expand Down
39 changes: 39 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ func dataSourceMongoDBAtlasProject() *schema.Resource {
},
},
},
"is_collect_database_specifics_statistics_enabled": {
Type: schema.TypeBool,
Computed: true,
},
Comment on lines +84 to +87
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] Could you update the tests with these new fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests added

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you show me where? I do not see them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies SDK had these will add to datasource

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests to datasource to verify new attributes exist

"is_data_explorer_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"is_performance_advisor_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"is_realtime_performance_panel_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"is_schema_advisor_enabled": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -147,6 +167,10 @@ func dataSourceMongoDBAtlasProjectRead(ctx context.Context, d *schema.ResourceDa
}
log.Println("[WARN] `api_keys` will be empty because the user has no permissions to read the api keys endpoint")
}
projectSettings, _, err := conn.Projects.GetProjectSettings(ctx, project.ID)
if err != nil {
return diag.Errorf("error getting project's settings assigned (%s): %s", projectID, err)
}

if err := d.Set("org_id", project.OrgID); err != nil {
return diag.Errorf(errorProjectSetting, `org_id`, project.ID, err)
Expand All @@ -167,6 +191,21 @@ func dataSourceMongoDBAtlasProjectRead(ctx context.Context, d *schema.ResourceDa
if err := d.Set("api_keys", flattenAPIKeys(apiKeys)); err != nil {
return diag.Errorf(errorProjectSetting, `api_keys`, project.ID, err)
}
if err := d.Set("is_collect_database_specifics_statistics_enabled", projectSettings.IsCollectDatabaseSpecificsStatisticsEnabled); err != nil {
return diag.Errorf(errorProjectSetting, `is_collect_database_specifics_statistics_enabled`, project.ID, err)
}
if err := d.Set("is_data_explorer_enabled", projectSettings.IsDataExplorerEnabled); err != nil {
return diag.Errorf(errorProjectSetting, `is_data_explorer_enabled`, project.ID, err)
}
if err := d.Set("is_performance_advisor_enabled", projectSettings.IsPerformanceAdvisorEnabled); err != nil {
return diag.Errorf(errorProjectSetting, `is_performance_advisor_enabled`, project.ID, err)
}
if err := d.Set("is_realtime_performance_panel_enabled", projectSettings.IsRealtimePerformancePanelEnabled); err != nil {
return diag.Errorf(errorProjectSetting, `is_realtime_performance_panel_enabled`, project.ID, err)
}
if err := d.Set("is_schema_advisor_enabled", projectSettings.IsSchemaAdvisorEnabled); err != nil {
return diag.Errorf(errorProjectSetting, `is_schema_advisor_enabled`, project.ID, err)
}

d.SetId(project.ID)

Expand Down
55 changes: 55 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,61 @@ func TestAccDataSourceMongoDBAtlasProject_byName(t *testing.T) {
})
}

func TestAccDataSourceMongoDBAtlasProject_defaultFlags(t *testing.T) {
projectName := fmt.Sprintf("test-datasource-project-%s", acctest.RandString(10))
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
teamsIds := strings.Split(os.Getenv("MONGODB_ATLAS_TEAMS_IDS"), ",")
apiKeysIds := strings.Split(os.Getenv("MONGODB_ATLAS_API_KEYS_IDS"), ",")
if len(teamsIds) < 2 {
t.Skip("`MONGODB_ATLAS_TEAMS_IDS` must have 2 team ids for this acceptance testing")
}
if len(apiKeysIds) < 2 {
t.Skip("`MONGODB_ATLAS_API_KEYS_IDS` must have 2 api key ids for this acceptance testing")
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); checkTeamsIds(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasProjectConfigWithDSByName(projectName, orgID,
[]*matlas.ProjectTeam{
{
TeamID: teamsIds[0],
RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"},
},
{

TeamID: teamsIds[1],
RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"},
},
},
[]*apiKey{
{
id: apiKeysIds[0],
roles: []string{"GROUP_READ_ONLY"},
},
{
id: apiKeysIds[1],
roles: []string{"GROUP_OWNER"},
},
},
),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "name"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "org_id"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "is_collect_database_specifics_statistics_enabled"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "is_data_explorer_enabled"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "is_performance_advisor_enabled"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "is_realtime_performance_panel_enabled"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "is_schema_advisor_enabled"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccMongoDBAtlasProjectConfigWithDSByID(projectName, orgID string, teams []*matlas.ProjectTeam, apiKeys []*apiKey) string {
return fmt.Sprintf(`
%s
Expand Down
31 changes: 31 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ func dataSourceMongoDBAtlasProjects() *schema.Resource {
},
},
},
"is_collect_database_specifics_statistics_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"is_data_explorer_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"is_performance_advisor_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"is_realtime_performance_panel_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"is_schema_advisor_enabled": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -139,6 +159,12 @@ func flattenProjects(ctx context.Context, conn *matlas.Client, projects []*matla
if err != nil {
fmt.Printf("[WARN] error getting project's api keys (%s): %s", project.ID, err)
}

projectSettings, _, err := conn.Projects.GetProjectSettings(ctx, project.ID)
if err != nil {
fmt.Printf("[WARN] error getting project's settings assigned (%s): %s", project.ID, err)
}

results[k] = map[string]interface{}{
"id": project.ID,
"org_id": project.OrgID,
Expand All @@ -147,6 +173,11 @@ func flattenProjects(ctx context.Context, conn *matlas.Client, projects []*matla
"created": project.Created,
"teams": flattenTeams(teams),
"api_keys": flattenAPIKeys(apiKeys),
"is_collect_database_specifics_statistics_enabled": projectSettings.IsCollectDatabaseSpecificsStatisticsEnabled,
"is_data_explorer_enabled": projectSettings.IsDataExplorerEnabled,
"is_performance_advisor_enabled": projectSettings.IsPerformanceAdvisorEnabled,
"is_realtime_performance_panel_enabled": projectSettings.IsRealtimePerformancePanelEnabled,
"is_schema_advisor_enabled": projectSettings.IsSchemaAdvisorEnabled,
}
}
}
Expand Down
92 changes: 92 additions & 0 deletions mongodbatlas/resource_mongodbatlas_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ func resourceMongoDBAtlasProject() *schema.Resource {
},
},
},
"is_collect_database_specifics_statistics_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"is_data_explorer_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"is_performance_advisor_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"is_realtime_performance_panel_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"is_schema_advisor_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
Expand Down Expand Up @@ -151,6 +176,23 @@ func resourceMongoDBAtlasProjectCreate(ctx context.Context, d *schema.ResourceDa
}
}

projectSettings := &matlas.ProjectSettings{}

projectSettings.IsCollectDatabaseSpecificsStatisticsEnabled = pointy.Bool(d.Get("is_collect_database_specifics_statistics_enabled").(bool))

projectSettings.IsDataExplorerEnabled = pointy.Bool(d.Get("is_data_explorer_enabled").(bool))

projectSettings.IsPerformanceAdvisorEnabled = pointy.Bool(d.Get("is_performance_advisor_enabled").(bool))

projectSettings.IsRealtimePerformancePanelEnabled = pointy.Bool(d.Get("is_realtime_performance_panel_enabled").(bool))

projectSettings.IsSchemaAdvisorEnabled = pointy.Bool(d.Get("is_schema_advisor_enabled").(bool))

_, _, err = conn.Projects.UpdateProjectSettings(ctx, project.ID, projectSettings)
if err != nil {
return diag.Errorf("error updating project's settings assigned (%s): %s", project.ID, err)
}

d.SetId(project.ID)

return resourceMongoDBAtlasProjectRead(ctx, d, meta)
Expand Down Expand Up @@ -184,6 +226,11 @@ func resourceMongoDBAtlasProjectRead(ctx context.Context, d *schema.ResourceData
log.Println("[WARN] `api_keys` will be empty because the user has no permissions to read the api keys endpoint")
}

projectSettings, _, err := conn.Projects.GetProjectSettings(ctx, projectID)
if err != nil {
return diag.Errorf("error getting project's settings assigned (%s): %s", projectID, err)
}

if err := d.Set("name", projectRes.Name); err != nil {
return diag.Errorf(errorProjectSetting, `name`, projectID, err)
}
Expand All @@ -208,6 +255,22 @@ func resourceMongoDBAtlasProjectRead(ctx context.Context, d *schema.ResourceData
return diag.Errorf(errorProjectSetting, `api_keys`, projectID, err)
}

if err := d.Set("is_collect_database_specifics_statistics_enabled", projectSettings.IsCollectDatabaseSpecificsStatisticsEnabled); err != nil {
return diag.Errorf(errorProjectSetting, `is_collect_database_specifics_statistics_enabled`, projectID, err)
}
if err := d.Set("is_data_explorer_enabled", projectSettings.IsDataExplorerEnabled); err != nil {
return diag.Errorf(errorProjectSetting, `is_data_explorer_enabled`, projectID, err)
}
if err := d.Set("is_performance_advisor_enabled", projectSettings.IsPerformanceAdvisorEnabled); err != nil {
return diag.Errorf(errorProjectSetting, `is_performance_advisor_enabled`, projectID, err)
}
if err := d.Set("is_realtime_performance_panel_enabled", projectSettings.IsRealtimePerformancePanelEnabled); err != nil {
return diag.Errorf(errorProjectSetting, `is_realtime_performance_panel_enabled`, projectID, err)
}
if err := d.Set("is_schema_advisor_enabled", projectSettings.IsSchemaAdvisorEnabled); err != nil {
return diag.Errorf(errorProjectSetting, `is_schema_advisor_enabled`, projectID, err)
}

return nil
}

Expand Down Expand Up @@ -292,6 +355,35 @@ func resourceMongoDBAtlasProjectUpdate(ctx context.Context, d *schema.ResourceDa
}
}

projectSettings, _, err := conn.Projects.GetProjectSettings(ctx, projectID)

if err != nil {
return diag.Errorf("error getting project's settings assigned (%s): %s", projectID, err)
}

if d.HasChange("is_collect_database_specifics_statistics_enabled") {
projectSettings.IsCollectDatabaseSpecificsStatisticsEnabled = pointy.Bool(d.Get("is_collect_database_specifics_statistics_enabled").(bool))
}

if d.HasChange("is_data_explorer_enabled") {
projectSettings.IsDataExplorerEnabled = pointy.Bool(d.Get("is_data_explorer_enabled").(bool))
}
if d.HasChange("is_performance_advisor_enabled") {
projectSettings.IsPerformanceAdvisorEnabled = pointy.Bool(d.Get("is_performance_advisor_enabled").(bool))
}
if d.HasChange("is_realtime_performance_panel_enabled") {
projectSettings.IsRealtimePerformancePanelEnabled = pointy.Bool(d.Get("is_realtime_performance_panel_enabled").(bool))
}
if d.HasChange("is_schema_advisor_enabled") {
projectSettings.IsSchemaAdvisorEnabled = pointy.Bool(d.Get("is_schema_advisor_enabled").(bool))
}
if d.HasChange("is_collect_database_specifics_statistics_enabled") || d.HasChange("is_data_explorer_enabled") ||
d.HasChange("is_performance_advisor_enabled") || d.HasChange("is_realtime_performance_panel_enabled") || d.HasChange("is_schema_advisor_enabled") {
_, _, err := conn.Projects.UpdateProjectSettings(ctx, projectID, projectSettings)
if err != nil {
return diag.Errorf("error updating project's settings assigned (%s): %s", projectID, err)
}
}
return resourceMongoDBAtlasProjectRead(ctx, d, meta)
}

Expand Down
43 changes: 43 additions & 0 deletions mongodbatlas/resource_mongodbatlas_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,33 @@ func TestAccResourceMongoDBAtlasProject_CreateWithFalseDefaultSettings(t *testin
})
}

func TestAccResourceMongoDBAtlasProject_CreateWithFalseDefaultAdvSettings(t *testing.T) {
var (
project matlas.Project
resourceName = "mongodbatlas_project.test"
projectName = fmt.Sprintf("testacc-project-%s", acctest.RandString(10))
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectOwnerID = os.Getenv("MONGODB_ATLAS_PROJECT_OWNER_ID")
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckMongoDBAtlasProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasProjectConfigWithFalseDefaultAdvSettings(projectName, orgID, projectOwnerID),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasProjectExists(resourceName, &project),
testAccCheckMongoDBAtlasProjectAttributes(&project, projectName),
resource.TestCheckResourceAttr(resourceName, "name", projectName),
resource.TestCheckResourceAttr(resourceName, "org_id", orgID),
),
},
},
})
}

func TestAccResourceMongoDBAtlasProject_withUpdatedRole(t *testing.T) {
var (
resourceName = "mongodbatlas_project.test"
Expand Down Expand Up @@ -428,6 +455,22 @@ func testAccMongoDBAtlasProjectConfigWithFalseDefaultSettings(projectName, orgID
`, projectName, orgID, projectOwnerID)
}

func testAccMongoDBAtlasProjectConfigWithFalseDefaultAdvSettings(projectName, orgID, projectOwnerID string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = "%[1]s"
org_id = "%[2]s"
project_owner_id = "%[3]s"
with_default_alerts_settings = false
is_collect_database_specifics_statistics_enabled = false
is_data_explorer_enabled = false
is_performance_advisor_enabled = false
is_realtime_performance_panel_enabled = false
is_schema_advisor_enabled = false
}
`, projectName, orgID, projectOwnerID)
}

func testAccMongoDBAtlasProjectConfigWithAdvancedCluster(projectName, orgID, projectOwnerID, clusterName string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
Expand Down
7 changes: 7 additions & 0 deletions website/docs/d/project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,11 @@ The following are valid roles:
* `GROUP_DATA_ACCESS_READ_ONLY`
* `GROUP_CLUSTER_MANAGER`


* `is_collect_database_specifics_statistics_enabled` - Flag that indicates whether to enable statistics in [cluster metrics](https://www.mongodb.com/docs/atlas/monitor-cluster-metrics/) collection for the project.
* `is_data_explorer_enabled` - Flag that indicates whether to enable Data Explorer for the project. If enabled, you can query your database with an easy to use interface.
* `is_performance_advisor_enabled` - Flag that indicates whether to enable Performance Advisor and Profiler for the project. If enabled, you can analyze database logs to recommend performance improvements.
* `is_realtime_performance_panel_enabled` - Flag that indicates whether to enable Real Time Performance Panel for the project. If enabled, you can see real time metrics from your MongoDB database.
* `is_schema_advisor_enabled` - Flag that indicates whether to enable Schema Advisor for the project. If enabled, you receive customized recommendations to optimize your data model and enhance performance. Disable this setting to disable schema suggestions in the [Performance Advisor](https://www.mongodb.com/docs/atlas/performance-advisor/#std-label-performance-advisor) and the [Data Explorer](https://www.mongodb.com/docs/atlas/atlas-ui/#std-label-atlas-ui).

See [MongoDB Atlas API - Project](https://docs.atlas.mongodb.com/reference/api/project-get-one/) - [and MongoDB Atlas API - Teams](https://docs.atlas.mongodb.com/reference/api/project-get-teams/) Documentation for more information.
Loading