Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import support to google_bigtable_table #4849

Merged
merged 1 commit into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions google/resource_bigtable_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<project>[^/]+)/instances/(?P<instance_name>[^/]+)/tables/(?P<name>[^/]+)",
"(?P<project>[^/]+)/(?P<instance_name>[^/]+)/(?P<name>[^/]+)",
"(?P<instance_name>[^/]+)/(?P<name>[^/]+)",
}, 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
}
70 changes: 26 additions & 44 deletions google/resource_bigtable_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
},
})
Expand All @@ -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),
},
},
})
Expand All @@ -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),
},
},
})
Expand All @@ -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),
},
},
})
Expand Down Expand Up @@ -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" {
Expand Down
14 changes: 14 additions & 0 deletions website/docs/r/bigtable_table.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`