Skip to content

Commit

Permalink
refactor and address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mackenziestarr committed Aug 7, 2019
1 parent 6b6cc5f commit 376017d
Showing 1 changed file with 22 additions and 56 deletions.
78 changes: 22 additions & 56 deletions google/resource_bigtable_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,75 +314,41 @@ func expandBigtableClusters(clusters []interface{}, instanceID string) []bigtabl
}

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

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 for "cluster"
if new_count.(int) < 1 {
return fmt.Errorf("Error applying diff. Resource definition should contain one or more cluster blocks, got %d blocks", new_count.(int))
}

// simulate Required:true and MinItems:1
if count < 1 {
if old_count.(int) != new_count.(int) {
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))
}
}
clusters := make(map[string]interface{}, new_count.(int))

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
for i := 0; i < new_count.(int); i++ {
old_id, new_id := diff.GetChange(fmt.Sprintf("cluster.%d.cluster_id", i))
if old_id != nil && old_id != "" {
old_ids = append(old_ids, old_id.(string))
}
_, c := diff.GetChange(fmt.Sprintf("cluster.%d", i))
clusters[new_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)
}
// reorder clusters according to the old cluster order
var old_cluster_order []interface{}
for _, id := range old_ids {
if c, ok := clusters[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)
}
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
}

0 comments on commit 376017d

Please sign in to comment.