Skip to content

Commit b102d0a

Browse files
PacoDwEdgar López
authored and
Edgar López
committed
Fix #279 #280 (#294)
* chore: changed the type of the inherited attribute to Set type to be able to modify/remove them correctly * chore: added a test case to validate the user error * chore: changed actions attribute as optional * chore: improved test case removing actions to validate that it can be ommiting * chore: updated go.mod and dependecies
1 parent 3d9df44 commit b102d0a

17 files changed

+299
-70
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ go 1.14
44

55
require (
66
github.com/client9/misspell v0.3.4
7-
github.com/go-test/deep v1.0.3
7+
github.com/go-test/deep v1.0.7
88
github.com/hashicorp/terraform-plugin-sdk v1.14.0
99
github.com/mongodb-forks/digest v1.0.1
1010
github.com/mwielbut/pointy v1.1.0
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.3.1-0.20200716160607-1b4e8b15eadf
14+
go.mongodb.org/atlas v0.4.1-0.20200820152733-8dc4a7c19a2b
1515
)

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me
139139
github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
140140
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
141141
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
142+
github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M=
143+
github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
142144
github.com/go-toolsmith/astcast v1.0.0 h1:JojxlmI6STnFVG9yOImLeGREv8W2ocNUM+iOhR6jE7g=
143145
github.com/go-toolsmith/astcast v1.0.0/go.mod h1:mt2OdQTeAQcY4DQgPSArJjHCcOwlX+Wl/kwN+LbLGQ4=
144146
github.com/go-toolsmith/astcopy v1.0.0 h1:OMgl1b1MEpjFQ1m5ztEO06rz5CUd3oBv9RF7+DyvdG8=
@@ -602,6 +604,10 @@ github.com/zclconf/go-cty-yaml v1.0.1/go.mod h1:IP3Ylp0wQpYm50IHK8OZWKMu6sPJIUgK
602604
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
603605
go.mongodb.org/atlas v0.3.1-0.20200716160607-1b4e8b15eadf h1:uphQUeBtEbNSmlhg1MRVJOMMhFBtrT6+K6rSmXGxCPk=
604606
go.mongodb.org/atlas v0.3.1-0.20200716160607-1b4e8b15eadf/go.mod h1:xa/V3muNuVoReSG0y2pigUUnfPOx1cHF0ZV2uCE+c7I=
607+
go.mongodb.org/atlas v0.4.1-0.20200817181428-cd1e61b39f75 h1:aXDdZDxIeD37cn0uVGQl+oHbT//g9OYybLF+R4u2NHk=
608+
go.mongodb.org/atlas v0.4.1-0.20200817181428-cd1e61b39f75/go.mod h1:QlKvZKT43+R6lhHlaTy2E7Q/3AoAljMI6v5apfqslIs=
609+
go.mongodb.org/atlas v0.4.1-0.20200820152733-8dc4a7c19a2b h1:AuAQZDrQLesdmz9mIPaIn07OJRoG4Vfm+M3xd31HGgo=
610+
go.mongodb.org/atlas v0.4.1-0.20200820152733-8dc4a7c19a2b/go.mod h1:QlKvZKT43+R6lhHlaTy2E7Q/3AoAljMI6v5apfqslIs=
605611
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
606612
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
607613
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=

mongodbatlas/resource_mongodbatlas_custom_db_role.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func resourceMongoDBAtlasCustomDBRole() *schema.Resource {
4949
},
5050
"actions": {
5151
Type: schema.TypeList,
52-
Required: true,
52+
Optional: true,
5353
Elem: &schema.Resource{
5454
Schema: map[string]*schema.Schema{
5555
"action": {
@@ -80,7 +80,7 @@ func resourceMongoDBAtlasCustomDBRole() *schema.Resource {
8080
},
8181
},
8282
"inherited_roles": {
83-
Type: schema.TypeList,
83+
Type: schema.TypeSet,
8484
Optional: true,
8585
Elem: &schema.Resource{
8686
Schema: map[string]*schema.Schema{
@@ -331,23 +331,21 @@ func flattenActionResources(resources []matlas.Resource) []map[string]interface{
331331
}
332332

333333
func expandInheritedRoles(d *schema.ResourceData) []matlas.InheritedRole {
334-
var inheritedRoles []matlas.InheritedRole
334+
vIR := d.Get("inherited_roles").(*schema.Set).List()
335+
ir := make([]matlas.InheritedRole, len(vIR))
335336

336-
if v, ok := d.GetOk("inherited_roles"); ok {
337-
if rs := v.([]interface{}); len(rs) > 0 {
338-
inheritedRoles = make([]matlas.InheritedRole, len(rs))
337+
if len(vIR) != 0 {
338+
for i := range vIR {
339+
r := vIR[i].(map[string]interface{})
339340

340-
for k, r := range rs {
341-
roleMap := r.(map[string]interface{})
342-
inheritedRoles[k] = matlas.InheritedRole{
343-
Db: roleMap["database_name"].(string),
344-
Role: roleMap["role_name"].(string),
345-
}
341+
ir[i] = matlas.InheritedRole{
342+
Db: r["database_name"].(string),
343+
Role: r["role_name"].(string),
346344
}
347345
}
348346
}
349347

350-
return inheritedRoles
348+
return ir
351349
}
352350

353351
func flattenInheritedRoles(roles []matlas.InheritedRole) []map[string]interface{} {

mongodbatlas/resource_mongodbatlas_custom_db_role_test.go

+151-22
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package mongodbatlas
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"os"
87
"testing"
98

@@ -70,7 +69,7 @@ func TestAccResourceMongoDBAtlasCustomDBRoles_WithInheritedRoles(t *testing.T) {
7069

7170
inheritRole := []matlas.CustomDBRole{
7271
{
73-
RoleName: fmt.Sprintf("a_test-acc-INHERITED_ROLE-%s", acctest.RandString(5)),
72+
RoleName: fmt.Sprintf("test-acc-INHERITED_ROLE-%s", acctest.RandString(5)),
7473
Actions: []matlas.Action{{
7574
Action: "INSERT",
7675
Resources: []matlas.Resource{{
@@ -176,10 +175,6 @@ func TestAccResourceMongoDBAtlasCustomDBRoles_WithInheritedRoles(t *testing.T) {
176175
resource.TestCheckResourceAttr(testRoleResourceName, "actions.0.action", testRole.Actions[0].Action),
177176
resource.TestCheckResourceAttr(testRoleResourceName, "actions.0.resources.#", cast.ToString(len(testRole.Actions[0].Resources))),
178177
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.#", "2"),
179-
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.0.role_name", inheritRole[0].RoleName),
180-
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.0.database_name", "admin"),
181-
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.1.role_name", inheritRole[1].RoleName),
182-
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.1.database_name", "admin"),
183178
),
184179
},
185180
{
@@ -223,20 +218,18 @@ func TestAccResourceMongoDBAtlasCustomDBRoles_WithInheritedRoles(t *testing.T) {
223218
resource.TestCheckResourceAttr(testRoleResourceName, "actions.0.action", testRoleUpdated.Actions[0].Action),
224219
resource.TestCheckResourceAttr(testRoleResourceName, "actions.0.resources.#", cast.ToString(len(testRoleUpdated.Actions[0].Resources))),
225220
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.#", "2"),
226-
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.0.role_name", inheritRole[0].RoleName),
227-
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.0.database_name", "admin"),
228-
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.1.role_name", inheritRole[1].RoleName),
229-
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.1.database_name", "admin"),
230221
),
231222
},
232223
},
233224
})
234225
}
235226

236227
func TestAccResourceMongoDBAtlasCustomDBRoles_MultipleCustomRoles(t *testing.T) {
237-
testRoleResourceName := "mongodbatlas_custom_db_role.test_role"
238-
InheritedRoleResourceName := "mongodbatlas_custom_db_role.inherited_role"
239-
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
228+
var (
229+
testRoleResourceName = "mongodbatlas_custom_db_role.test_role"
230+
InheritedRoleResourceName = "mongodbatlas_custom_db_role.inherited_role"
231+
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
232+
)
240233

241234
inheritRole := &matlas.CustomDBRole{
242235
RoleName: fmt.Sprintf("test-acc-INHERITED_ROLE-%s", acctest.RandString(5)),
@@ -412,8 +405,6 @@ func TestAccResourceMongoDBAtlasCustomDBRoles_MultipleCustomRoles(t *testing.T)
412405
resource.TestCheckResourceAttr(testRoleResourceName, "actions.0.action", testRoleUpdated.Actions[0].Action),
413406
resource.TestCheckResourceAttr(testRoleResourceName, "actions.0.resources.#", cast.ToString(len(testRoleUpdated.Actions[0].Resources))),
414407
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.#", "1"),
415-
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.0.role_name", inheritRoleUpdated.RoleName),
416-
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.0.database_name", "admin"),
417408
),
418409
},
419410
},
@@ -458,11 +449,12 @@ func TestAccResourceMongoDBAtlasCustomDBRoles_MultipleResources(t *testing.T) {
458449
}
459450

460451
func TestAccResourceMongoDBAtlasCustomDBRoles_importBasic(t *testing.T) {
461-
resourceName := "mongodbatlas_custom_db_role.test"
462-
463-
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
464-
roleName := fmt.Sprintf("test-acc-custom_role-%s", acctest.RandString(5))
465-
databaseName := fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5))
452+
var (
453+
resourceName = "mongodbatlas_custom_db_role.test"
454+
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
455+
roleName = fmt.Sprintf("test-acc-custom_role-%s", acctest.RandString(5))
456+
databaseName = fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5))
457+
)
466458

467459
resource.Test(t, resource.TestCase{
468460
PreCheck: func() { testAccPreCheck(t) },
@@ -482,6 +474,145 @@ func TestAccResourceMongoDBAtlasCustomDBRoles_importBasic(t *testing.T) {
482474
})
483475
}
484476

477+
func TestAccResourceMongoDBAtlasCustomDBRoles_UpdatedInheritRoles(t *testing.T) {
478+
var (
479+
testRoleResourceName = "mongodbatlas_custom_db_role.test_role"
480+
InheritedRoleResourceName = "mongodbatlas_custom_db_role.inherited_role"
481+
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
482+
)
483+
484+
inheritRole := &matlas.CustomDBRole{
485+
RoleName: fmt.Sprintf("test-acc-INHERITED_ROLE-%s", acctest.RandString(5)),
486+
Actions: []matlas.Action{
487+
{
488+
Action: "REMOVE",
489+
Resources: []matlas.Resource{
490+
{
491+
Db: fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5)),
492+
},
493+
{
494+
Db: fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5)),
495+
},
496+
},
497+
},
498+
{
499+
Action: "FIND",
500+
Resources: []matlas.Resource{
501+
{
502+
Db: fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5)),
503+
},
504+
},
505+
},
506+
},
507+
}
508+
509+
inheritRoleUpdated := &matlas.CustomDBRole{
510+
RoleName: inheritRole.RoleName,
511+
Actions: []matlas.Action{
512+
{
513+
Action: "UPDATE",
514+
Resources: []matlas.Resource{
515+
{
516+
Db: fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5)),
517+
},
518+
},
519+
},
520+
{
521+
Action: "FIND",
522+
Resources: []matlas.Resource{
523+
{
524+
Db: fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5)),
525+
},
526+
{
527+
Db: fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5)),
528+
},
529+
},
530+
},
531+
{
532+
Action: "INSERT",
533+
Resources: []matlas.Resource{
534+
{
535+
Db: fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5)),
536+
},
537+
{
538+
Db: fmt.Sprintf("test-acc-db_name-%s", acctest.RandString(5)),
539+
},
540+
},
541+
},
542+
},
543+
}
544+
545+
testRole := &matlas.CustomDBRole{
546+
RoleName: fmt.Sprintf("test-acc-TEST_ROLE-%s", acctest.RandString(5)),
547+
InheritedRoles: []matlas.InheritedRole{
548+
{
549+
Role: inheritRole.RoleName,
550+
Db: "admin",
551+
},
552+
},
553+
}
554+
555+
resource.Test(t, resource.TestCase{
556+
PreCheck: func() { testAccPreCheck(t) },
557+
Providers: testAccProviders,
558+
CheckDestroy: testAccCheckMongoDBAtlasCustomDBRolesDestroy,
559+
Steps: []resource.TestStep{
560+
{
561+
Config: testAccMongoDBAtlasCustomDBRolesConfigMultiple(projectID, inheritRole, testRole),
562+
Check: resource.ComposeTestCheckFunc(
563+
564+
// For Inherited Role
565+
testAccCheckMongoDBAtlasCustomDBRolesExists(InheritedRoleResourceName),
566+
resource.TestCheckResourceAttrSet(InheritedRoleResourceName, "project_id"),
567+
resource.TestCheckResourceAttrSet(InheritedRoleResourceName, "role_name"),
568+
resource.TestCheckResourceAttrSet(InheritedRoleResourceName, "actions.0.action"),
569+
570+
resource.TestCheckResourceAttr(InheritedRoleResourceName, "project_id", projectID),
571+
resource.TestCheckResourceAttr(InheritedRoleResourceName, "role_name", inheritRole.RoleName),
572+
resource.TestCheckResourceAttr(InheritedRoleResourceName, "actions.#", cast.ToString(len(inheritRole.Actions))),
573+
resource.TestCheckResourceAttr(InheritedRoleResourceName, "actions.0.action", inheritRole.Actions[0].Action),
574+
resource.TestCheckResourceAttr(InheritedRoleResourceName, "actions.0.resources.#", cast.ToString(len(inheritRole.Actions[0].Resources))),
575+
576+
// For Test Role
577+
testAccCheckMongoDBAtlasCustomDBRolesExists(testRoleResourceName),
578+
resource.TestCheckResourceAttrSet(testRoleResourceName, "project_id"),
579+
resource.TestCheckResourceAttrSet(testRoleResourceName, "role_name"),
580+
581+
resource.TestCheckResourceAttr(testRoleResourceName, "project_id", projectID),
582+
resource.TestCheckResourceAttr(testRoleResourceName, "role_name", testRole.RoleName),
583+
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.#", "1"),
584+
),
585+
},
586+
{
587+
Config: testAccMongoDBAtlasCustomDBRolesConfigMultiple(projectID, inheritRoleUpdated, testRole),
588+
Check: resource.ComposeTestCheckFunc(
589+
590+
// For Inherited Role
591+
testAccCheckMongoDBAtlasCustomDBRolesExists(InheritedRoleResourceName),
592+
resource.TestCheckResourceAttrSet(InheritedRoleResourceName, "project_id"),
593+
resource.TestCheckResourceAttrSet(InheritedRoleResourceName, "role_name"),
594+
resource.TestCheckResourceAttrSet(InheritedRoleResourceName, "actions.0.action"),
595+
596+
resource.TestCheckResourceAttr(InheritedRoleResourceName, "project_id", projectID),
597+
resource.TestCheckResourceAttr(InheritedRoleResourceName, "role_name", inheritRoleUpdated.RoleName),
598+
resource.TestCheckResourceAttr(InheritedRoleResourceName, "actions.#", cast.ToString(len(inheritRoleUpdated.Actions))),
599+
resource.TestCheckResourceAttr(InheritedRoleResourceName, "actions.0.action", inheritRoleUpdated.Actions[0].Action),
600+
resource.TestCheckResourceAttr(InheritedRoleResourceName, "actions.0.resources.#", cast.ToString(len(inheritRoleUpdated.Actions[0].Resources))),
601+
602+
// For Test Role
603+
testAccCheckMongoDBAtlasCustomDBRolesExists(testRoleResourceName),
604+
resource.TestCheckResourceAttrSet(testRoleResourceName, "project_id"),
605+
resource.TestCheckResourceAttrSet(testRoleResourceName, "role_name"),
606+
607+
resource.TestCheckResourceAttr(testRoleResourceName, "project_id", projectID),
608+
resource.TestCheckResourceAttr(testRoleResourceName, "role_name", testRole.RoleName),
609+
resource.TestCheckResourceAttr(testRoleResourceName, "inherited_roles.#", "1"),
610+
),
611+
},
612+
},
613+
})
614+
}
615+
485616
func testAccCheckMongoDBAtlasCustomDBRolesExists(resourceName string) resource.TestCheckFunc {
486617
return func(s *terraform.State) error {
487618
conn := testAccProvider.Meta().(*matlas.Client)
@@ -554,8 +685,6 @@ func testAccMongoDBAtlasCustomDBRolesConfigBasic(projectID, roleName, action, da
554685
}
555686

556687
func testAccMongoDBAtlasCustomDBRolesConfigWithInheritedRoles(projectID string, inheritedRole []matlas.CustomDBRole, testRole *matlas.CustomDBRole) string {
557-
log.Printf("LOG___ inheritedRole: %#+v\n", inheritedRole[1])
558-
559688
return fmt.Sprintf(`
560689
resource "mongodbatlas_custom_db_role" "inherited_role_one" {
561690
project_id = "%s"

vendor/github.com/go-test/deep/.travis.yml

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/go-test/deep/CHANGES.md

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)