Skip to content

Commit e3606de

Browse files
coderGo93Edgar López
andauthored
Database user scopes (#307)
* chore: updated vendor from client mongo * feat: added scopes in database user and its test * chore: updated vendor modules * added parameter scopes in datasource of database user and update docs * refactor: made changes suggested by melissa Co-authored-by: Edgar López <[email protected]>
1 parent aeae3b5 commit e3606de

28 files changed

+567
-28
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ require (
1111
github.com/spf13/cast v1.3.1
1212
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20200518153306-40099de47e37
1313
github.com/terraform-providers/terraform-provider-google v1.20.1-0.20200518165017-1dd21651c496
14-
go.mongodb.org/atlas v0.4.1-0.20200820152733-8dc4a7c19a2b
14+
go.mongodb.org/atlas v0.4.1-0.20200903102338-049d0778b833
1515
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,8 @@ go.mongodb.org/atlas v0.4.1-0.20200819194203-09c49e85aa0d h1:5GrmSNMip4s758EBOch
609609
go.mongodb.org/atlas v0.4.1-0.20200819194203-09c49e85aa0d/go.mod h1:QlKvZKT43+R6lhHlaTy2E7Q/3AoAljMI6v5apfqslIs=
610610
go.mongodb.org/atlas v0.4.1-0.20200820152733-8dc4a7c19a2b h1:AuAQZDrQLesdmz9mIPaIn07OJRoG4Vfm+M3xd31HGgo=
611611
go.mongodb.org/atlas v0.4.1-0.20200820152733-8dc4a7c19a2b/go.mod h1:QlKvZKT43+R6lhHlaTy2E7Q/3AoAljMI6v5apfqslIs=
612+
go.mongodb.org/atlas v0.4.1-0.20200903102338-049d0778b833 h1:gH8Ih2OacuB6qVitO+wI5EBKdbtM/YdbhJstiMR2Vfw=
613+
go.mongodb.org/atlas v0.4.1-0.20200903102338-049d0778b833/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
612614
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
613615
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
614616
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=

mongodbatlas/data_source_mongodbatlas_database_user.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ func dataSourceMongoDBAtlasDatabaseUser() *schema.Resource {
7777
},
7878
},
7979
},
80+
"scopes": {
81+
Type: schema.TypeList,
82+
Computed: true,
83+
Elem: &schema.Resource{
84+
Schema: map[string]*schema.Schema{
85+
"name": {
86+
Type: schema.TypeString,
87+
Computed: true,
88+
},
89+
"type": {
90+
Type: schema.TypeString,
91+
Computed: true,
92+
},
93+
},
94+
},
95+
},
8096
},
8197
}
8298
}
@@ -136,6 +152,10 @@ func dataSourceMongoDBAtlasDatabaseUserRead(d *schema.ResourceData, meta interfa
136152
return fmt.Errorf("error setting `labels` for database user (%s): %s", d.Id(), err)
137153
}
138154

155+
if err := d.Set("scopes", flattenScopes(dbUser.Scopes)); err != nil {
156+
return fmt.Errorf("error setting `scopes` for database user (%s): %s", d.Id(), err)
157+
}
158+
139159
d.SetId(encodeStateID(map[string]string{
140160
"project_id": projectID,
141161
"username": username,

mongodbatlas/data_source_mongodbatlas_database_users.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ func dataSourceMongoDBAtlasDatabaseUsers() *schema.Resource {
8181
},
8282
},
8383
},
84+
"scopes": {
85+
Type: schema.TypeList,
86+
Computed: true,
87+
Elem: &schema.Resource{
88+
Schema: map[string]*schema.Schema{
89+
"name": {
90+
Type: schema.TypeString,
91+
Computed: true,
92+
},
93+
"type": {
94+
Type: schema.TypeString,
95+
Computed: true,
96+
},
97+
},
98+
},
99+
},
84100
},
85101
},
86102
},
@@ -123,6 +139,7 @@ func flattenDBUsers(dbUsers []matlas.DatabaseUser) []map[string]interface{} {
123139
"x509_type": dbUsers[i].X509Type,
124140
"aws_iam_type": dbUsers[i].AWSIAMType,
125141
"labels": flattenLabels(dbUsers[i].Labels),
142+
"scopes": flattenScopes(dbUsers[i].Scopes),
126143
}
127144
}
128145
}

mongodbatlas/data_source_mongodbatlas_database_users_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func TestAccDataSourceMongoDBAtlasDatabaseUsers_basic(t *testing.T) {
3737
resource.TestCheckResourceAttrSet(resourceName, "results.0.x509_type"),
3838
resource.TestCheckResourceAttrSet(resourceName, "results.0.username"),
3939
resource.TestCheckResourceAttrSet(resourceName, "results.0.roles.#"),
40+
resource.TestCheckResourceAttrSet(resourceName, "results.0.scopes.#"),
4041
),
4142
},
4243
},

mongodbatlas/data_source_mongodbatlas_project_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func TestAccDataSourceMongoDBAtlasProject_byName(t *testing.T) {
6565
RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"},
6666
},
6767
{
68+
6869
TeamID: teamsIds[1],
6970
RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"},
7071
},

mongodbatlas/resource_mongodbatlas_database_user.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ func resourceMongoDBAtlasDatabaseUser() *schema.Resource {
105105
},
106106
},
107107
},
108+
"scopes": {
109+
Type: schema.TypeSet,
110+
Optional: true,
111+
Computed: true,
112+
Elem: &schema.Resource{
113+
Schema: map[string]*schema.Schema{
114+
"name": {
115+
Type: schema.TypeString,
116+
Optional: true,
117+
Computed: true,
118+
},
119+
"type": {
120+
Type: schema.TypeString,
121+
Optional: true,
122+
Computed: true,
123+
},
124+
},
125+
},
126+
},
108127
},
109128
}
110129
}
@@ -161,6 +180,10 @@ func resourceMongoDBAtlasDatabaseUserRead(d *schema.ResourceData, meta interface
161180
return fmt.Errorf("error setting `labels` for database user (%s): %s", d.Id(), err)
162181
}
163182

183+
if err := d.Set("scopes", flattenScopes(dbUser.Scopes)); err != nil {
184+
return fmt.Errorf("error setting `scopes` for database user (%s): %s", d.Id(), err)
185+
}
186+
164187
d.SetId(encodeStateID(map[string]string{
165188
"project_id": projectID,
166189
"username": username,
@@ -197,6 +220,7 @@ func resourceMongoDBAtlasDatabaseUserCreate(d *schema.ResourceData, meta interfa
197220
AWSIAMType: d.Get("aws_iam_type").(string),
198221
DatabaseName: authDatabaseName,
199222
Labels: expandLabelSliceFromSetSchema(d),
223+
Scopes: expandScopes(d),
200224
}
201225

202226
dbUserRes, _, err := conn.DatabaseUsers.Create(context.Background(), projectID, dbUserReq)
@@ -240,6 +264,10 @@ func resourceMongoDBAtlasDatabaseUserUpdate(d *schema.ResourceData, meta interfa
240264
dbUser.Labels = expandLabelSliceFromSetSchema(d)
241265
}
242266

267+
if d.HasChange("scopes") {
268+
dbUser.Scopes = expandScopes(d)
269+
}
270+
243271
_, _, err = conn.DatabaseUsers.Update(context.Background(), projectID, username, dbUser)
244272
if err != nil {
245273
return fmt.Errorf("error updating database user(%s): %s", username, err)
@@ -345,3 +373,30 @@ func flattenRoles(roles []matlas.Role) []interface{} {
345373

346374
return roleList
347375
}
376+
377+
func flattenScopes(l []matlas.Scope) []map[string]interface{} {
378+
scopes := make([]map[string]interface{}, len(l))
379+
for i, v := range l {
380+
scopes[i] = map[string]interface{}{
381+
"name": v.Name,
382+
"type": v.Type,
383+
}
384+
}
385+
386+
return scopes
387+
}
388+
389+
func expandScopes(d *schema.ResourceData) []matlas.Scope {
390+
list := d.Get("scopes").(*schema.Set)
391+
res := make([]matlas.Scope, list.Len())
392+
393+
for i, val := range list.List() {
394+
v := val.(map[string]interface{})
395+
res[i] = matlas.Scope{
396+
Type: v["type"].(string),
397+
Name: v["name"].(string),
398+
}
399+
}
400+
401+
return res
402+
}

mongodbatlas/resource_mongodbatlas_database_user_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,68 @@ func TestAccResourceMongoDBAtlasDatabaseUser_withRoles(t *testing.T) {
291291
})
292292
}
293293

294+
func TestAccResourceMongoDBAtlasDatabaseUser_withScopes(t *testing.T) {
295+
var (
296+
dbUser matlas.DatabaseUser
297+
resourceName = "mongodbatlas_database_user.test"
298+
username = acctest.RandomWithPrefix("test-acc-user-")
299+
password = acctest.RandomWithPrefix("test-acc-pass-")
300+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
301+
projectName = acctest.RandomWithPrefix("test-acc")
302+
clusterName = acctest.RandomWithPrefix("test-acc-cluster")
303+
)
304+
305+
resource.ParallelTest(t, resource.TestCase{
306+
PreCheck: func() { testAccPreCheck(t) },
307+
Providers: testAccProviders,
308+
CheckDestroy: testAccCheckMongoDBAtlasDatabaseUserDestroy,
309+
Steps: []resource.TestStep{
310+
{
311+
Config: testAccMongoDBAtlasDatabaseUserWithScopes(username, password, projectName, orgID, "atlasAdmin", clusterName,
312+
[]*matlas.Scope{
313+
{
314+
Name: "test-acc-nurk4llu2z",
315+
Type: "CLUSTER",
316+
},
317+
{
318+
Name: "test-acc-nurk4llu2z",
319+
Type: "DATA_LAKE",
320+
},
321+
},
322+
),
323+
Check: resource.ComposeTestCheckFunc(
324+
testAccCheckMongoDBAtlasDatabaseUserExists(resourceName, &dbUser),
325+
testAccCheckMongoDBAtlasDatabaseUserAttributes(&dbUser, username),
326+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
327+
resource.TestCheckResourceAttr(resourceName, "username", username),
328+
resource.TestCheckResourceAttr(resourceName, "password", password),
329+
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "admin"),
330+
resource.TestCheckResourceAttr(resourceName, "scopes.#", "2"),
331+
),
332+
},
333+
{
334+
Config: testAccMongoDBAtlasDatabaseUserWithScopes(username, password, projectName, orgID, "atlasAdmin", clusterName,
335+
[]*matlas.Scope{
336+
{
337+
Name: "test-acc-nurk4llu2z",
338+
Type: "CLUSTER",
339+
},
340+
},
341+
),
342+
Check: resource.ComposeTestCheckFunc(
343+
testAccCheckMongoDBAtlasDatabaseUserExists(resourceName, &dbUser),
344+
testAccCheckMongoDBAtlasDatabaseUserAttributes(&dbUser, username),
345+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
346+
resource.TestCheckResourceAttr(resourceName, "username", username),
347+
resource.TestCheckResourceAttr(resourceName, "password", password),
348+
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "admin"),
349+
resource.TestCheckResourceAttr(resourceName, "scopes.#", "1"),
350+
),
351+
},
352+
},
353+
})
354+
}
355+
294356
func TestAccResourceMongoDBAtlasDatabaseUser_importBasic(t *testing.T) {
295357
var (
296358
username = fmt.Sprintf("test-username-%s", acctest.RandString(5))
@@ -589,3 +651,58 @@ func testAccMongoDBAtlasDatabaseUserWithAWSIAMTypeConfig(projectName, orgID, rol
589651
}
590652
`, projectName, orgID, roleName, username, keyLabel, valueLabel)
591653
}
654+
655+
func testAccMongoDBAtlasDatabaseUserWithScopes(username, password, projectName, orgID, roleName, clusterName string, scopesArr []*matlas.Scope) string {
656+
var scopes string
657+
658+
for _, scope := range scopesArr {
659+
var scopeType string
660+
661+
if scope.Type != "" {
662+
scopeType = fmt.Sprintf(`type = "%s"`, scope.Type)
663+
}
664+
665+
scopes += fmt.Sprintf(`
666+
scopes {
667+
name = "${mongodbatlas_cluster.my_cluster.name}"
668+
%s
669+
}
670+
`, scopeType)
671+
}
672+
673+
return fmt.Sprintf(`
674+
resource "mongodbatlas_project" "test" {
675+
name = "%s"
676+
org_id = "%s"
677+
}
678+
679+
resource "mongodbatlas_cluster" "my_cluster" {
680+
project_id = "${mongodbatlas_project.test.id}"
681+
name = "%s"
682+
disk_size_gb = 5
683+
684+
// Provider Settings "block"
685+
provider_name = "AWS"
686+
provider_region_name = "US_EAST_2"
687+
provider_instance_size_name = "M10"
688+
provider_backup_enabled = true //enable cloud provider snapshots
689+
provider_disk_iops = 100
690+
provider_encrypt_ebs_volume = false
691+
}
692+
693+
resource "mongodbatlas_database_user" "test" {
694+
username = "%s"
695+
password = "%s"
696+
project_id = "${mongodbatlas_project.test.id}"
697+
auth_database_name = "admin"
698+
699+
roles {
700+
role_name = "%s"
701+
database_name = "admin"
702+
}
703+
704+
%s
705+
706+
}
707+
`, projectName, orgID, clusterName, username, password, roleName, scopes)
708+
}

vendor/go.mongodb.org/atlas/mongodbatlas/atlas_alerts.go renamed to vendor/go.mongodb.org/atlas/mongodbatlas/alerts.go

Lines changed: 10 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)