Skip to content

Commit

Permalink
Merge pull request #231 from terraform-providers/fix-#228
Browse files Browse the repository at this point in the history
Fix #228: Changes to mongodbatlas_database_user.role.collection_name are ignored
  • Loading branch information
PacoDw authored May 27, 2020
2 parents 159449b + 268bef4 commit baa3ed6
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 13 deletions.
12 changes: 6 additions & 6 deletions mongodbatlas/resource_mongodbatlas_database_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func resourceMongoDBAtlasDatabaseUser() *schema.Resource {
Default: "NONE",
},
"roles": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Expand Down Expand Up @@ -286,9 +286,9 @@ func splitDatabaseUserImportID(ID string) (*string, *string, *string, error) {
func expandRoles(d *schema.ResourceData) []matlas.Role {
var roles []matlas.Role
if v, ok := d.GetOk("roles"); ok {
if rs := v.([]interface{}); len(rs) > 0 {
roles = make([]matlas.Role, len(rs))
for k, r := range rs {
if rs := v.(*schema.Set); rs.Len() > 0 {
roles = make([]matlas.Role, rs.Len())
for k, r := range rs.List() {
roleMap := r.(map[string]interface{})
roles[k] = matlas.Role{
RoleName: roleMap["role_name"].(string),
Expand All @@ -301,8 +301,8 @@ func expandRoles(d *schema.ResourceData) []matlas.Role {
return roles
}

func flattenRoles(roles []matlas.Role) []map[string]interface{} {
roleList := make([]map[string]interface{}, 0)
func flattenRoles(roles []matlas.Role) []interface{} {
roleList := make([]interface{}, 0)
for _, v := range roles {
roleList = append(roleList, map[string]interface{}{
"role_name": v.RoleName,
Expand Down
105 changes: 98 additions & 7 deletions mongodbatlas/resource_mongodbatlas_database_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func TestAccResourceMongoDBAtlasDatabaseUser_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "username", username),
resource.TestCheckResourceAttr(resourceName, "password", "test-acc-password"),
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "admin"),
resource.TestCheckResourceAttr(resourceName, "roles.0.role_name", "atlasAdmin"),
resource.TestCheckResourceAttr(resourceName, "labels.#", "1"),
),
},
Expand All @@ -46,7 +45,6 @@ func TestAccResourceMongoDBAtlasDatabaseUser_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "username", username),
resource.TestCheckResourceAttr(resourceName, "password", "test-acc-password"),
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "admin"),
resource.TestCheckResourceAttr(resourceName, "roles.0.role_name", "read"),
resource.TestCheckResourceAttr(resourceName, "labels.#", "1"),
),
},
Expand Down Expand Up @@ -75,7 +73,6 @@ func TestAccResourceMongoDBAtlasDatabaseUser_withX509Type(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "username", username),
resource.TestCheckResourceAttr(resourceName, "x509_type", "MANAGED"),
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "$external"),
resource.TestCheckResourceAttr(resourceName, "roles.0.role_name", "atlasAdmin"),
resource.TestCheckResourceAttr(resourceName, "labels.#", "1"),
),
},
Expand Down Expand Up @@ -104,7 +101,6 @@ func TestAccResourceMongoDBAtlasDatabaseUser_WithLabels(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "username", username),
resource.TestCheckResourceAttr(resourceName, "password", "test-acc-password"),
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "admin"),
resource.TestCheckResourceAttr(resourceName, "roles.0.role_name", "atlasAdmin"),
resource.TestCheckResourceAttr(resourceName, "labels.#", "0"),
),
},
Expand All @@ -128,7 +124,6 @@ func TestAccResourceMongoDBAtlasDatabaseUser_WithLabels(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "username", username),
resource.TestCheckResourceAttr(resourceName, "password", "test-acc-password"),
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "admin"),
resource.TestCheckResourceAttr(resourceName, "roles.0.role_name", "atlasAdmin"),
resource.TestCheckResourceAttr(resourceName, "labels.#", "2"),
),
},
Expand Down Expand Up @@ -156,14 +151,74 @@ func TestAccResourceMongoDBAtlasDatabaseUser_WithLabels(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "username", username),
resource.TestCheckResourceAttr(resourceName, "password", "test-acc-password"),
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "admin"),
resource.TestCheckResourceAttr(resourceName, "roles.0.role_name", "read"),
resource.TestCheckResourceAttr(resourceName, "labels.#", "3"),
),
},
},
})
}

func TestAccResourceMongoDBAtlasDatabaseUser_withRoles(t *testing.T) {
var dbUser matlas.DatabaseUser

resourceName := "mongodbatlas_database_user.test"
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
username := acctest.RandomWithPrefix("test-acc-user-")
password := acctest.RandomWithPrefix("test-acc-pass-")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMongoDBAtlasDatabaseUserDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasDatabaseUserWithRoles(username, password, projectID,
[]*matlas.Role{
{
RoleName: "read",
DatabaseName: "admin",
CollectionName: "stir",
},
{
RoleName: "read",
DatabaseName: "admin",
CollectionName: "unpledged",
},
},
),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasDatabaseUserExists(resourceName, &dbUser),
testAccCheckMongoDBAtlasDatabaseUserAttributes(&dbUser, username),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "username", username),
resource.TestCheckResourceAttr(resourceName, "password", password),
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "admin"),
resource.TestCheckResourceAttr(resourceName, "roles.#", "2"),
),
},
{
Config: testAccMongoDBAtlasDatabaseUserWithRoles(username, password, projectID,
[]*matlas.Role{
{
RoleName: "read",
DatabaseName: "admin",
},
},
),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasDatabaseUserExists(resourceName, &dbUser),
testAccCheckMongoDBAtlasDatabaseUserAttributes(&dbUser, username),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "username", username),
resource.TestCheckResourceAttr(resourceName, "password", password),
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "admin"),
resource.TestCheckResourceAttr(resourceName, "roles.#", "1"),
),
},
},
})
}

func TestAccResourceMongoDBAtlasDatabaseUser_importBasic(t *testing.T) {
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")

Expand All @@ -182,7 +237,6 @@ func TestAccResourceMongoDBAtlasDatabaseUser_importBasic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "username", username),
resource.TestCheckResourceAttr(resourceName, "password", "test-acc-password"),
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "admin"),
resource.TestCheckResourceAttr(resourceName, "roles.0.role_name", "read"),
resource.TestCheckResourceAttr(resourceName, "labels.#", "1"),
),
},
Expand Down Expand Up @@ -329,3 +383,40 @@ func testAccMongoDBAtlasDatabaseUserWithLabelsConfig(projectID, roleName, userna
}
`, projectID, roleName, username, labelsConf)
}

func testAccMongoDBAtlasDatabaseUserWithRoles(username, password, projectID string, rolesArr []*matlas.Role) string {
var roles string
for _, role := range rolesArr {
var roleName, databaseName, collection string

if role.RoleName != "" {
roleName = fmt.Sprintf(`role_name = "%s"`, role.RoleName)
}
if role.DatabaseName != "" {
databaseName = fmt.Sprintf(`database_name = "%s"`, role.DatabaseName)
}
if role.CollectionName != "" {
collection = fmt.Sprintf(`collection_name = "%s"`, role.CollectionName)
}

roles += fmt.Sprintf(`
roles {
%s
%s
%s
}
`, roleName, databaseName, collection)
}

return fmt.Sprintf(`
resource "mongodbatlas_database_user" "test" {
username = "%s"
password = "%s"
project_id = "%s"
auth_database_name = "admin"
%s
}
`, username, password, projectID, roles)
}

0 comments on commit baa3ed6

Please sign in to comment.