package digitalocean import ( "testing" "fmt" "regexp" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func TestAccDigitalOceanDatabaseConnectionPool_importBasic(t *testing.T) { resourceName := "digitalocean_database_connection_pool.pool-01" databaseName := randomTestName() databaseConnectionPoolName := randomTestName() resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: testAccProviderFactories, CheckDestroy: testAccCheckDigitalOceanDatabaseConnectionPoolDestroy, Steps: []resource.TestStep{ { Config: fmt.Sprintf(testAccCheckDigitalOceanDatabaseConnectionPoolConfigBasic, databaseName, databaseConnectionPoolName), }, { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, // Requires passing both the cluster ID and pool name ImportStateIdFunc: testAccDatabasePoolImportID(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", databaseConnectionPoolName), ExpectError: regexp.MustCompile(`(Please verify the ID is correct|Cannot import non-existent remote object)`), }, }, }) } func testAccDatabasePoolImportID(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 } }