From 8599e2072cd9db8904d549b16308ece42f42b542 Mon Sep 17 00:00:00 2001 From: Riley Karson Date: Fri, 8 Nov 2019 22:18:22 +0000 Subject: [PATCH] Add import support to google_bigtable_table Signed-off-by: Modular Magician --- google/resource_bigtable_table.go | 25 ++++++++ google/resource_bigtable_table_test.go | 70 ++++++++------------- website/docs/r/bigtable_table.html.markdown | 14 +++++ 3 files changed, 65 insertions(+), 44 deletions(-) diff --git a/google/resource_bigtable_table.go b/google/resource_bigtable_table.go index 52e678d1f56..0c7678cd5ca 100644 --- a/google/resource_bigtable_table.go +++ b/google/resource_bigtable_table.go @@ -14,6 +14,10 @@ func resourceBigtableTable() *schema.Resource { Read: resourceBigtableTableRead, Delete: resourceBigtableTableDestroy, + Importer: &schema.ResourceImporter{ + State: resourceBigtableTableImport, + }, + Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -182,3 +186,24 @@ func flattenColumnFamily(families []string) []map[string]interface{} { return result } + +//TODO(rileykarson): Fix the stored import format after rebasing 3.0.0 +func resourceBigtableTableImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + if err := parseImportId([]string{ + "projects/(?P[^/]+)/instances/(?P[^/]+)/tables/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := replaceVars(d, config, "{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} diff --git a/google/resource_bigtable_table_test.go b/google/resource_bigtable_table_test.go index eed98f86023..470b56c7f71 100644 --- a/google/resource_bigtable_table_test.go +++ b/google/resource_bigtable_table_test.go @@ -23,10 +23,13 @@ func TestAccBigtableTable_basic(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccBigtableTable(instanceName, tableName), - Check: resource.ComposeTestCheckFunc( - testAccBigtableTableExists( - "google_bigtable_table.table"), - ), + }, + { + ResourceName: "google_bigtable_table.table", + ImportState: true, + ImportStateVerify: true, + //TODO(rileykarson): Remove ImportStateId when id format is fixed in 3.0.0 + ImportStateId: fmt.Sprintf("%s/%s", instanceName, tableName), }, }, }) @@ -45,10 +48,13 @@ func TestAccBigtableTable_splitKeys(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccBigtableTable_splitKeys(instanceName, tableName), - Check: resource.ComposeTestCheckFunc( - testAccBigtableTableExists( - "google_bigtable_table.table"), - ), + }, + { + ResourceName: "google_bigtable_table.table", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"split_keys"}, + ImportStateId: fmt.Sprintf("%s/%s", instanceName, tableName), }, }, }) @@ -68,10 +74,12 @@ func TestAccBigtableTable_family(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccBigtableTable_family(instanceName, tableName, family), - Check: resource.ComposeTestCheckFunc( - testAccBigtableTableExists( - "google_bigtable_table.table"), - ), + }, + { + ResourceName: "google_bigtable_table.table", + ImportState: true, + ImportStateVerify: true, + ImportStateId: fmt.Sprintf("%s/%s", instanceName, tableName), }, }, }) @@ -91,10 +99,12 @@ func TestAccBigtableTable_familyMany(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccBigtableTable_familyMany(instanceName, tableName, family), - Check: resource.ComposeTestCheckFunc( - testAccBigtableTableExists( - "google_bigtable_table.table"), - ), + }, + { + ResourceName: "google_bigtable_table.table", + ImportState: true, + ImportStateVerify: true, + ImportStateId: fmt.Sprintf("%s/%s", instanceName, tableName), }, }, }) @@ -125,34 +135,6 @@ func testAccCheckBigtableTableDestroy(s *terraform.State) error { return nil } -func testAccBigtableTableExists(n string) resource.TestCheckFunc { - var ctx = context.Background() - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - config := testAccProvider.Meta().(*Config) - c, err := config.bigtableClientFactory.NewAdminClient(config.Project, rs.Primary.Attributes["instance_name"]) - if err != nil { - return fmt.Errorf("Error starting admin client. %s", err) - } - - _, err = c.TableInfo(ctx, rs.Primary.Attributes["name"]) - if err != nil { - return fmt.Errorf("Error retrieving table. Could not find %s in %s.", rs.Primary.Attributes["name"], rs.Primary.Attributes["instance_name"]) - } - - c.Close() - - return nil - } -} - func testAccBigtableTable(instanceName, tableName string) string { return fmt.Sprintf(` resource "google_bigtable_instance" "instance" { diff --git a/website/docs/r/bigtable_table.html.markdown b/website/docs/r/bigtable_table.html.markdown index ad391a59c37..b69fcd5bc55 100644 --- a/website/docs/r/bigtable_table.html.markdown +++ b/website/docs/r/bigtable_table.html.markdown @@ -56,3 +56,17 @@ The following arguments are supported: ## Attributes Reference Only the arguments listed above are exposed as attributes. + +## Import + +Bigtable Tables can be imported using any of these accepted formats: + +``` +$ terraform import google_bigtable_table.default projects/{{project}}/instances/{{instance_name}}/tables/{{name}} +$ terraform import google_bigtable_table.default {{project}}/{{instance_name}}/{{name}} +$ terraform import google_bigtable_table.default {{instance_name}}/{{name}} +``` + +The following fields can't be read and will show diffs if set in config when imported: + +- `split_keys`