Skip to content

Commit

Permalink
fix: fixed DatabaseUserID to allows names with multiple dashes and ad…
Browse files Browse the repository at this point in the history
…ded test case to validated
  • Loading branch information
PacoDw committed Apr 30, 2020
1 parent 4d4e51b commit c92a7c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
39 changes: 23 additions & 16 deletions mongodbatlas/resource_mongodbatlas_database_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"

"strings"
"regexp"

"github.com/hashicorp/terraform/helper/schema"

Expand Down Expand Up @@ -249,33 +247,42 @@ func resourceMongoDBAtlasDatabaseUserDelete(d *schema.ResourceData, meta interfa
func resourceMongoDBAtlasDatabaseUserImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
conn := meta.(*matlas.Client)

parts := strings.SplitN(d.Id(), "-", 3)
if len(parts) != 3 {
return nil, errors.New("import format error: to import a database user, use the format {project_id}-{username}-{auth_database_name}")
projectID, username, authDatabaseName, err := splitDatabaseUserImportID(d.Id())
if err != nil {
return nil, err
}

projectID := parts[0]
username := parts[1]
authDatabaseName := parts[2]

u, _, err := conn.DatabaseUsers.Get(context.Background(), authDatabaseName, projectID, username)
u, _, err := conn.DatabaseUsers.Get(context.Background(), *authDatabaseName, *projectID, *username)
if err != nil {
return nil, fmt.Errorf("couldn't import user(%s) in project(%s), error: %s", username, projectID, err)
return nil, fmt.Errorf("couldn't import user(%s) in project(%s), error: %s", *username, *projectID, err)
}

if err := d.Set("project_id", u.GroupID); err != nil {
log.Printf("[WARN] Error setting project_id for (%s): %s", d.Id(), err)
return nil, fmt.Errorf("error setting `project_id` for database user (%s): %s", d.Id(), err)
}
if err := d.Set("auth_database_name", u.DatabaseName); err != nil {
return nil, fmt.Errorf("error setting `auth_database_name` for database user (%s): %s", d.Id(), err)
}

d.SetId(encodeStateID(map[string]string{
"project_id": projectID,
"username": username,
"auth_database_name": authDatabaseName,
"project_id": *projectID,
"username": *username,
"auth_database_name": *authDatabaseName,
}))

return []*schema.ResourceData{d}, nil
}

func splitDatabaseUserImportID(ID string) (*string, *string, *string, error) {
var re = regexp.MustCompile(`(?s)^([0-9a-fA-F]{24})-(.*)-([a-z]{1,15})$`)
parts := re.FindStringSubmatch(ID)

if len(parts) != 4 {
return nil, nil, nil, errors.New("import format error: to import a Database User, use the format {project_id}-{username}-{auth_database_name}")
}
return &parts[1], &parts[2], &parts[3], nil
}

func expandRoles(d *schema.ResourceData) []matlas.Role {
var roles []matlas.Role
if v, ok := d.GetOk("roles"); ok {
Expand Down
10 changes: 9 additions & 1 deletion mongodbatlas/resource_mongodbatlas_database_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestAccResourceMongoDBAtlasDatabaseUser_WithLabels(t *testing.T) {
func TestAccResourceMongoDBAtlasDatabaseUser_importBasic(t *testing.T) {
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")

username := fmt.Sprintf("test_username_%s", acctest.RandString(5))
username := fmt.Sprintf("test-username-%s", acctest.RandString(5))
resourceName := "mongodbatlas_database_user.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -177,6 +177,14 @@ func TestAccResourceMongoDBAtlasDatabaseUser_importBasic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasDatabaseUserConfig(projectID, "read", username, "First Key", "First value"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
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"),
),
},
{
ResourceName: resourceName,
Expand Down

0 comments on commit c92a7c8

Please sign in to comment.