From c92a7c8a8d46dc14df8c5e327b21b3271886f061 Mon Sep 17 00:00:00 2001 From: PacoDw <__pm@outlook.com> Date: Wed, 29 Apr 2020 20:35:56 -0500 Subject: [PATCH] fix: fixed DatabaseUserID to allows names with multiple dashes and added test case to validated --- .../resource_mongodbatlas_database_user.go | 39 +++++++++++-------- ...esource_mongodbatlas_database_user_test.go | 10 ++++- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_database_user.go b/mongodbatlas/resource_mongodbatlas_database_user.go index f0376fc9d0..cae9977db7 100644 --- a/mongodbatlas/resource_mongodbatlas_database_user.go +++ b/mongodbatlas/resource_mongodbatlas_database_user.go @@ -4,9 +4,7 @@ import ( "context" "errors" "fmt" - "log" - - "strings" + "regexp" "github.com/hashicorp/terraform/helper/schema" @@ -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 { diff --git a/mongodbatlas/resource_mongodbatlas_database_user_test.go b/mongodbatlas/resource_mongodbatlas_database_user_test.go index e2197b4a7a..10a80e865f 100644 --- a/mongodbatlas/resource_mongodbatlas_database_user_test.go +++ b/mongodbatlas/resource_mongodbatlas_database_user_test.go @@ -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{ @@ -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,