Skip to content

Commit b26b1ca

Browse files
authored
INTMDB-270: Added 'with_default_alerts_settings' to project resource (#595)
* added 'with_default_alerts_settings' to project resource * change project documentation
1 parent 0c8ac6f commit b26b1ca

File tree

5 files changed

+64
-17
lines changed

5 files changed

+64
-17
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ require (
1212
github.com/mwielbut/pointy v1.1.0
1313
github.com/spf13/cast v1.4.1
1414
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20210625132053-af2d5c0ad54f
15-
go.mongodb.org/atlas v0.13.1-0.20211015211338-c17a67cbe2bf
15+
go.mongodb.org/atlas v0.13.1-0.20211103172854-77290cf48e1e
1616
go.mongodb.org/realm v0.1.0
1717
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,8 @@ go.mongodb.org/atlas v0.13.0 h1:JkJOWsKm9k2mcFaivaaMNDpKDsxJJj1O0eUsDtnNvuE=
11991199
go.mongodb.org/atlas v0.13.0/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
12001200
go.mongodb.org/atlas v0.13.1-0.20211015211338-c17a67cbe2bf h1:AczRmIosmEvyLpHNnvykZlUpDv90oZ0FEf1zs+CNYH0=
12011201
go.mongodb.org/atlas v0.13.1-0.20211015211338-c17a67cbe2bf/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
1202+
go.mongodb.org/atlas v0.13.1-0.20211103172854-77290cf48e1e h1:Fcx4rdJT5074x9TIENhX59vaaxgkkpMPhbJKql+FqxA=
1203+
go.mongodb.org/atlas v0.13.1-0.20211103172854-77290cf48e1e/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
12021204
go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M=
12031205
go.mongodb.org/realm v0.1.0/go.mod h1:4Vj6iy+Puo1TDERcoh4XZ+pjtwbOzPpzqy3Cwe8ZmDM=
12041206
go.mozilla.org/mozlog v0.0.0-20170222151521-4bb13139d403/go.mod h1:jHoPAGnDrCy6kaI2tAze5Prf0Nr0w/oNkROt2lw3n3o=

mongodbatlas/resource_mongodbatlas_project.go

+22-16
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package mongodbatlas
22

33
import (
44
"context"
5-
"fmt"
65
"net/http"
76

87
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
98
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/mwielbut/pointy"
1010
matlas "go.mongodb.org/atlas/mongodbatlas"
1111
)
1212

@@ -68,6 +68,11 @@ func resourceMongoDBAtlasProject() *schema.Resource {
6868
Type: schema.TypeString,
6969
Optional: true,
7070
},
71+
"with_default_alerts_settings": {
72+
Type: schema.TypeBool,
73+
Optional: true,
74+
Default: true,
75+
},
7176
},
7277
}
7378
}
@@ -77,8 +82,9 @@ func resourceMongoDBAtlasProjectCreate(ctx context.Context, d *schema.ResourceDa
7782
conn := meta.(*MongoDBClient).Atlas
7883

7984
projectReq := &matlas.Project{
80-
OrgID: d.Get("org_id").(string),
81-
Name: d.Get("name").(string),
85+
OrgID: d.Get("org_id").(string),
86+
Name: d.Get("name").(string),
87+
WithDefaultAlertsSettings: pointy.Bool(d.Get("with_default_alerts_settings").(bool)),
8288
}
8389

8490
var createProjectOptions *matlas.CreateProjectOptions
@@ -91,15 +97,15 @@ func resourceMongoDBAtlasProjectCreate(ctx context.Context, d *schema.ResourceDa
9197

9298
project, _, err := conn.Projects.Create(ctx, projectReq, createProjectOptions)
9399
if err != nil {
94-
return diag.FromErr(fmt.Errorf(errorProjectCreate, err))
100+
return diag.Errorf(errorProjectCreate, err)
95101
}
96102

97103
// Check if teams were set, if so we need to add the teams into the project
98104
if teams, ok := d.GetOk("teams"); ok {
99105
// adding the teams into the project
100106
_, _, err := conn.Projects.AddTeamsToProject(ctx, project.ID, expandTeamsSet(teams.(*schema.Set)))
101107
if err != nil {
102-
return diag.FromErr(fmt.Errorf("error adding teams into the project: %s", err))
108+
return diag.Errorf("error adding teams into the project: %s", err)
103109
}
104110
}
105111

@@ -119,32 +125,32 @@ func resourceMongoDBAtlasProjectRead(ctx context.Context, d *schema.ResourceData
119125
return nil
120126
}
121127

122-
return diag.FromErr(fmt.Errorf(errorProjectRead, projectID, err))
128+
return diag.Errorf(errorProjectRead, projectID, err)
123129
}
124130

125131
teams, _, err := conn.Projects.GetProjectTeamsAssigned(ctx, projectID)
126132
if err != nil {
127-
return diag.FromErr(fmt.Errorf("error getting project's teams assigned (%s): %s", projectID, err))
133+
return diag.Errorf("error getting project's teams assigned (%s): %s", projectID, err)
128134
}
129135

130136
if err := d.Set("name", projectRes.Name); err != nil {
131-
return diag.FromErr(fmt.Errorf(errorProjectSetting, `name`, projectID, err))
137+
return diag.Errorf(errorProjectSetting, `name`, projectID, err)
132138
}
133139

134140
if err := d.Set("org_id", projectRes.OrgID); err != nil {
135-
return diag.FromErr(fmt.Errorf(errorProjectSetting, `org_id`, projectID, err))
141+
return diag.Errorf(errorProjectSetting, `org_id`, projectID, err)
136142
}
137143

138144
if err := d.Set("cluster_count", projectRes.ClusterCount); err != nil {
139-
return diag.FromErr(fmt.Errorf(errorProjectSetting, `clusterCount`, projectID, err))
145+
return diag.Errorf(errorProjectSetting, `clusterCount`, projectID, err)
140146
}
141147

142148
if err := d.Set("created", projectRes.Created); err != nil {
143-
return diag.FromErr(fmt.Errorf(errorProjectSetting, `created`, projectID, err))
149+
return diag.Errorf(errorProjectSetting, `created`, projectID, err)
144150
}
145151

146152
if err := d.Set("teams", flattenTeams(teams)); err != nil {
147-
return diag.FromErr(fmt.Errorf(errorProjectSetting, `created`, projectID, err))
153+
return diag.Errorf(errorProjectSetting, `created`, projectID, err)
148154
}
149155

150156
return nil
@@ -162,7 +168,7 @@ func resourceMongoDBAtlasProjectUpdate(ctx context.Context, d *schema.ResourceDa
162168
if len(newTeams) > 0 {
163169
_, _, err := conn.Projects.AddTeamsToProject(ctx, projectID, expandTeamsList(newTeams))
164170
if err != nil {
165-
return diag.FromErr(fmt.Errorf("error adding teams into the project(%s): %s", projectID, err))
171+
return diag.Errorf("error adding teams into the project(%s): %s", projectID, err)
166172
}
167173
}
168174

@@ -172,7 +178,7 @@ func resourceMongoDBAtlasProjectUpdate(ctx context.Context, d *schema.ResourceDa
172178

173179
_, err := conn.Teams.RemoveTeamFromProject(ctx, projectID, teamID)
174180
if err != nil {
175-
return diag.FromErr(fmt.Errorf("error removing team(%s) from the project(%s): %s", teamID, projectID, err))
181+
return diag.Errorf("error removing team(%s) from the project(%s): %s", teamID, projectID, err)
176182
}
177183
}
178184

@@ -186,7 +192,7 @@ func resourceMongoDBAtlasProjectUpdate(ctx context.Context, d *schema.ResourceDa
186192
},
187193
)
188194
if err != nil {
189-
return diag.FromErr(fmt.Errorf("error updating role names for the team(%s): %s", team["team_id"], err))
195+
return diag.Errorf("error updating role names for the team(%s): %s", team["team_id"], err)
190196
}
191197
}
192198
}
@@ -200,7 +206,7 @@ func resourceMongoDBAtlasProjectDelete(ctx context.Context, d *schema.ResourceDa
200206

201207
_, err := conn.Projects.Delete(ctx, projectID)
202208
if err != nil {
203-
return diag.FromErr(fmt.Errorf(errorProjectDelete, projectID, err))
209+
return diag.Errorf(errorProjectDelete, projectID, err)
204210
}
205211

206212
return nil

mongodbatlas/resource_mongodbatlas_project_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,33 @@ func TestAccResourceMongoDBAtlasProject_CreateWithProjectOwner(t *testing.T) {
142142
})
143143
}
144144

145+
func TestAccResourceMongoDBAtlasProject_CreateWithFalseDefaultSettings(t *testing.T) {
146+
var (
147+
project matlas.Project
148+
resourceName = "mongodbatlas_project.test"
149+
projectName = fmt.Sprintf("testacc-project-%s", acctest.RandString(10))
150+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
151+
projectOwnerID = os.Getenv("MONGODB_ATLAS_PROJECT_OWNER_ID")
152+
)
153+
154+
resource.ParallelTest(t, resource.TestCase{
155+
PreCheck: func() { testAccPreCheck(t) },
156+
ProviderFactories: testAccProviderFactories,
157+
CheckDestroy: testAccCheckMongoDBAtlasProjectDestroy,
158+
Steps: []resource.TestStep{
159+
{
160+
Config: testAccMongoDBAtlasProjectConfigWithFalseDefaultSettings(projectName, orgID, projectOwnerID),
161+
Check: resource.ComposeTestCheckFunc(
162+
testAccCheckMongoDBAtlasProjectExists(resourceName, &project),
163+
testAccCheckMongoDBAtlasProjectAttributes(&project, projectName),
164+
resource.TestCheckResourceAttr(resourceName, "name", projectName),
165+
resource.TestCheckResourceAttr(resourceName, "org_id", orgID),
166+
),
167+
},
168+
},
169+
})
170+
}
171+
145172
func TestAccResourceMongoDBAtlasProject_withUpdatedRole(t *testing.T) {
146173
var (
147174
resourceName = "mongodbatlas_project.test"
@@ -312,3 +339,14 @@ func testAccMongoDBAtlasProjectConfigWithProjectOwner(projectName, orgID, projec
312339
}
313340
`, projectName, orgID, projectOwnerID)
314341
}
342+
343+
func testAccMongoDBAtlasProjectConfigWithFalseDefaultSettings(projectName, orgID, projectOwnerID string) string {
344+
return fmt.Sprintf(`
345+
resource "mongodbatlas_project" "test" {
346+
name = "%[1]s"
347+
org_id = "%[2]s"
348+
project_owner_id = "%[3]s"
349+
with_default_alerts_settings = false
350+
}
351+
`, projectName, orgID, projectOwnerID)
352+
}

website/docs/r/project.html.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ resource "mongodbatlas_project" "test" {
3737
* `name` - (Required) The name of the project you want to create. (Cannot be changed via this Provider after creation.)
3838
* `org_id` - (Required) The ID of the organization you want to create the project within.
3939
* `project_owner_id` - (Optional) Unique 24-hexadecimal digit string that identifies the Atlas user account to be granted the [Project Owner](https://docs.atlas.mongodb.com/reference/user-roles/#mongodb-authrole-Project-Owner) role on the specified project. If you set this parameter, it overrides the default value of the oldest [Organization Owner](https://docs.atlas.mongodb.com/reference/user-roles/#mongodb-authrole-Organization-Owner).
40+
* `with_default_alerts_settings` - (Optional) It allows users to disable the creation of the default alert settings. By default, this flag is set to true.
4041

4142
### Teams
4243
Teams attribute is optional

0 commit comments

Comments
 (0)