Skip to content

Commit

Permalink
test is passing for reordering clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
mackenziestarr committed Aug 7, 2019
1 parent 47b23d9 commit 6b6cc5f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 12 deletions.
82 changes: 81 additions & 1 deletion google/resource_bigtable_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/customdiff"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"

Expand All @@ -18,6 +19,10 @@ func resourceBigtableInstance() *schema.Resource {
Update: resourceBigtableInstanceUpdate,
Delete: resourceBigtableInstanceDestroy,

CustomizeDiff: customdiff.All(
resourceBigtableInstanceClusterReorderTypeList,
),

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -27,7 +32,8 @@ func resourceBigtableInstance() *schema.Resource {

"cluster": {
Type: schema.TypeList,
Required: true,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cluster_id": {
Expand Down Expand Up @@ -306,3 +312,77 @@ func expandBigtableClusters(clusters []interface{}, instanceID string) []bigtabl
}
return results
}

func resourceBigtableInstanceClusterReorderTypeList(diff *schema.ResourceDiff, meta interface{}) error {
keys := diff.GetChangedKeysPrefix("cluster")
if len(keys) == 0 {
return nil
}

oldCount, newCount := diff.GetChange("cluster.#")
var count int
if oldCount.(int) < newCount.(int) {
count = newCount.(int)
} else {
count = oldCount.(int)
}

// simulate Required:true and MinItems:1
if count < 1 {
return nil
}

var old_ids []string
var new_ids []string
for i := 0; i < count; i++ {
old, new := diff.GetChange(fmt.Sprintf("cluster.%d.cluster_id", i))
if old != nil {
old_ids = append(old_ids, old.(string))
}
if new != nil {
new_ids = append(new_ids, new.(string))
}
}

d := difference(old_ids, new_ids)

// clusters have been reordered
if len(new_ids) == len(old_ids) && len(d) == 0 {

clusterMap := make(map[string]interface{}, len(new_ids))
for i := 0; i < count; i++ {
_, id := diff.GetChange(fmt.Sprintf("cluster.%d.cluster_id", i))
_, c := diff.GetChange(fmt.Sprintf("cluster.%d", i))
clusterMap[id.(string)] = c
}

// build a slice of the new clusters ordered by the old cluster order
var old_cluster_order []interface{}
for _, id := range old_ids {
if c, ok := clusterMap[id]; ok {
old_cluster_order = append(old_cluster_order, c)
}
}

err := diff.SetNew("cluster", old_cluster_order)
if err != nil {
return fmt.Errorf("Error setting cluster diff: %s", err)
}
}

return nil
}

func difference(a, b []string) []string {
var c []string
m := make(map[string]bool)
for _, o := range a {
m[o] = true
}
for _, n := range b {
if _, ok := m[n]; !ok {
c = append(c, n)
}
}
return c
}
49 changes: 38 additions & 11 deletions google/resource_bigtable_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,19 @@ func TestAccBigtableInstance_cluster(t *testing.T) {
ExpectError: regexp.MustCompile("config is invalid: Too many cluster blocks: No more than 4 \"cluster\" blocks are allowed"),
},
{
Config: testAccBigtableInstance_cluster(instanceName),
Config: testAccBigtableInstance_cluster(instanceName, 3),
Check: resource.ComposeTestCheckFunc(
testAccBigtableInstanceExists(
"google_bigtable_instance.instance", 3),
),
},
{
Config: testAccBigtableInstance_cluster_reordered(instanceName, 5),
Check: resource.ComposeTestCheckFunc(
testAccBigtableInstanceExists(
"google_bigtable_instance.instance", 5),
),
},
},
})
}
Expand Down Expand Up @@ -166,36 +173,36 @@ resource "google_bigtable_instance" "instance" {
`, instanceName, instanceName, numNodes)
}

func testAccBigtableInstance_cluster(instanceName string) string {
func testAccBigtableInstance_cluster(instanceName string, numNodes int) string {
return fmt.Sprintf(`
resource "google_bigtable_instance" "instance" {
name = "%s"
cluster {
cluster_id = "%s-a"
zone = "us-central1-a"
num_nodes = 3
num_nodes = %d
storage_type = "HDD"
}
cluster {
cluster_id = "%s-b"
zone = "us-central1-b"
num_nodes = 3
num_nodes = %d
storage_type = "HDD"
}
cluster {
cluster_id = "%s-c"
zone = "us-central1-c"
num_nodes = 3
num_nodes = %d
storage_type = "HDD"
}
cluster {
cluster_id = "%s-d"
zone = "us-central1-f"
num_nodes = 3
num_nodes = %d
storage_type = "HDD"
}
}
`, instanceName, instanceName, instanceName, instanceName, instanceName)
`, instanceName, numNodes, instanceName, numNodes, instanceName, numNodes, instanceName, numNodes)
}

func testAccBigtableInstance_clusterMax(instanceName string) string {
Expand All @@ -217,23 +224,43 @@ resource "google_bigtable_instance" "instance" {
cluster {
cluster_id = "%s-c"
zone = "us-central1-c"
num_nodes = 3
num_nodes = %d
storage_type = "HDD"
}
}
`, instanceName, instanceName, instanceName, instanceName, instanceName, instanceName)
}

func testAccBigtableInstance_cluster_reordered(instanceName string, numNodes int) string {
return fmt.Sprintf(`
resource "google_bigtable_instance" "instance" {
name = "%s"
cluster {
num_nodes = %d
cluster_id = "%s-b"
zone = "us-central1-c"
storage_type = "HDD"
}
cluster {
cluster_id = "%s-d"
zone = "us-central1-f"
num_nodes = 3
num_nodes = %d
storage_type = "HDD"
}
cluster {
zone = "us-central1-b"
cluster_id = "%s-a"
num_nodes = %d
storage_type = "HDD"
}
cluster {
cluster_id = "%s-e"
zone = "us-east1-a"
num_nodes = 3
num_nodes = %d
storage_type = "HDD"
}
}
`, instanceName, instanceName, instanceName, instanceName, instanceName, instanceName)
`, instanceName, numNodes, instanceName, instanceName, numNodes, instanceName, numNodes, instanceName, numNodes, instanceName)
}

func testAccBigtableInstance_development(instanceName string) string {
Expand Down

0 comments on commit 6b6cc5f

Please sign in to comment.