forked from digitalocean/terraform-provider-digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_digitalocean_database_user_test.go
63 lines (56 loc) · 1.92 KB
/
import_digitalocean_database_user_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package digitalocean
import (
"fmt"
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestAccDigitalOceanDatabaseUser_importBasic(t *testing.T) {
resourceName := "digitalocean_database_user.foobar_user"
databaseClusterName := randomTestName()
databaseUserName := randomTestName()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckDigitalOceanDatabaseUserDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDigitalOceanDatabaseUserConfigBasic, databaseClusterName, databaseUserName),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// Requires passing both the cluster ID and user name
ImportStateIdFunc: testAccDatabaseUserImportID(resourceName),
},
// Test importing non-existent resource provides expected error.
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: false,
ImportStateId: fmt.Sprintf("%s,%s", "this-cluster-id-does-not-exist", databaseUserName),
ExpectError: regexp.MustCompile(`(Please verify the ID is correct|Cannot import non-existent remote object)`),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: false,
ImportStateId: "username",
ExpectError: regexp.MustCompile("joined with a comma"),
},
},
})
}
func testAccDatabaseUserImportID(n string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[n]
if !ok {
return "", fmt.Errorf("Not found: %s", n)
}
clusterID := rs.Primary.Attributes["cluster_id"]
name := rs.Primary.Attributes["name"]
return fmt.Sprintf("%s,%s", clusterID, name), nil
}
}