Skip to content

Commit

Permalink
Add import support to google_bigtable_instance (#4598)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored and rileykarson committed Oct 1, 2019
1 parent 1cf95a9 commit 1d4ef86
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 76 deletions.
67 changes: 54 additions & 13 deletions google/resource_bigtable_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func resourceBigtableInstance() *schema.Resource {
Update: resourceBigtableInstanceUpdate,
Delete: resourceBigtableInstanceDestroy,

Importer: &schema.ResourceImporter{
State: resourceBigtableInstanceImport,
},

CustomizeDiff: customdiff.All(
resourceBigtableInstanceValidateDevelopment,
resourceBigtableInstanceClusterReorderTypeList,
Expand Down Expand Up @@ -185,22 +189,31 @@ func resourceBigtableInstanceRead(d *schema.ResourceData, meta interface{}) erro

d.Set("project", project)

clusters := d.Get("cluster").([]interface{})
clusterState := []map[string]interface{}{}
for _, cl := range clusters {
cluster := cl.(map[string]interface{})
clus, err := c.GetCluster(ctx, instance.Name, cluster["cluster_id"].(string))
if err != nil {
if isGoogleApiErrorWithCode(err, 404) {
log.Printf("[WARN] Cluster %q not found, not setting it in state", cluster["cluster_id"].(string))
continue
clusters, err := c.Clusters(ctx, instance.Name)
if err != nil {
return fmt.Errorf("Error retrieving instance clusters. %s", err)
}

clustersNewState := []map[string]interface{}{}
for i, cluster := range clusters {
// DEVELOPMENT clusters have num_nodes = 0 on their first (and only)
// cluster while PRODUCTION clusters will have at least 3.
if i == 0 {
var instanceType string
if cluster.ServeNodes == 0 {
instanceType = "DEVELOPMENT"
} else {
instanceType = "PRODUCTION"
}
return fmt.Errorf("Error retrieving cluster %q: %s", cluster["cluster_id"].(string), err.Error())

d.Set("instance_type", instanceType)
}
clusterState = append(clusterState, flattenBigtableCluster(clus, cluster["storage_type"].(string)))

clustersNewState = append(clustersNewState, flattenBigtableCluster(cluster))
}

err = d.Set("cluster", clusterState)
log.Printf("[DEBUG] Setting clusters in state: %#v", clustersNewState)
err = d.Set("cluster", clustersNewState)
if err != nil {
return fmt.Errorf("Error setting clusters in state: %s", err.Error())
}
Expand Down Expand Up @@ -279,7 +292,15 @@ func resourceBigtableInstanceDestroy(d *schema.ResourceData, meta interface{}) e
return nil
}

func flattenBigtableCluster(c *bigtable.ClusterInfo, storageType string) map[string]interface{} {
func flattenBigtableCluster(c *bigtable.ClusterInfo) map[string]interface{} {
var storageType string
switch c.StorageType {
case bigtable.SSD:
storageType = "SSD"
case bigtable.HDD:
storageType = "HDD"
}

return map[string]interface{}{
"zone": c.Zone,
"num_nodes": c.ServeNodes,
Expand Down Expand Up @@ -366,3 +387,23 @@ func resourceBigtableInstanceClusterReorderTypeList(diff *schema.ResourceDiff, m

return nil
}

func resourceBigtableInstanceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{
"projects/(?P<project>[^/]+)/instances/(?P<name>[^/]+)",
"(?P<project>[^/]+)/(?P<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
}
88 changes: 25 additions & 63 deletions google/resource_bigtable_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ func TestAccBigtableInstance_basic(t *testing.T) {
},
{
Config: testAccBigtableInstance(instanceName, 3),
Check: resource.ComposeTestCheckFunc(
testAccBigtableInstanceExists(
"google_bigtable_instance.instance", 3),
),
},
{
ResourceName: "google_bigtable_instance.instance",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccBigtableInstance(instanceName, 4),
Check: resource.ComposeTestCheckFunc(
testAccBigtableInstanceExists(
"google_bigtable_instance.instance", 4),
),
},
{
ResourceName: "google_bigtable_instance.instance",
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand All @@ -59,17 +61,19 @@ func TestAccBigtableInstance_cluster(t *testing.T) {
},
{
Config: testAccBigtableInstance_cluster(instanceName, 3),
Check: resource.ComposeTestCheckFunc(
testAccBigtableInstanceExists(
"google_bigtable_instance.instance", 3),
),
},
{
ResourceName: "google_bigtable_instance.instance",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccBigtableInstance_cluster_reordered(instanceName, 5),
Check: resource.ComposeTestCheckFunc(
testAccBigtableInstanceExists(
"google_bigtable_instance.instance", 5),
),
},
{
ResourceName: "google_bigtable_instance.instance",
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand All @@ -95,10 +99,11 @@ func TestAccBigtableInstance_development(t *testing.T) {
},
{
Config: testAccBigtableInstance_development(instanceName),
Check: resource.ComposeTestCheckFunc(
testAccBigtableInstanceExists(
"google_bigtable_instance.instance", 0),
),
},
{
ResourceName: "google_bigtable_instance.instance",
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down Expand Up @@ -128,49 +133,6 @@ func testAccCheckBigtableInstanceDestroy(s *terraform.State) error {
return nil
}

func testAccBigtableInstanceExists(n string, numNodes int) 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.NewInstanceAdminClient(config.Project)
if err != nil {
return fmt.Errorf("Error starting instance admin client. %s", err)
}

defer c.Close()

_, err = c.InstanceInfo(ctx, rs.Primary.Attributes["name"])
if err != nil {
return fmt.Errorf("Error retrieving instance %s.", rs.Primary.Attributes["name"])
}

clusters, err := c.Clusters(ctx, rs.Primary.Attributes["name"])
if err != nil {
return fmt.Errorf("Error retrieving cluster list for instance %s.", rs.Primary.Attributes["name"])
}

for _, c := range clusters {
if c.ServeNodes != numNodes {
return fmt.Errorf("Expected cluster %s to have %d nodes but got %d nodes for instance %s.",
c.Name,
numNodes,
c.ServeNodes,
rs.Primary.Attributes["name"])
}
}

return nil
}
}

func testAccBigtableInstance(instanceName string, numNodes int) string {
return fmt.Sprintf(`
resource "google_bigtable_instance" "instance" {
Expand Down
10 changes: 10 additions & 0 deletions website/docs/r/bigtable_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@ The `cluster` block supports the following arguments:
## Attributes Reference

Only the arguments listed above are exposed as attributes.

## Import

Bigtable Instances can be imported using any of these accepted formats:

```
$ terraform import google_bigtable_instance.default projects/{{project}}/instances/{{name}}
$ terraform import google_bigtable_instance.default {{project}}/{{name}}
$ terraform import google_bigtable_instance.default {{name}}
```

0 comments on commit 1d4ef86

Please sign in to comment.