Skip to content

Commit 376017d

Browse files
refactor and address code review comments
1 parent 6b6cc5f commit 376017d

File tree

1 file changed

+22
-56
lines changed

1 file changed

+22
-56
lines changed

google/resource_bigtable_instance.go

+22-56
Original file line numberDiff line numberDiff line change
@@ -314,75 +314,41 @@ func expandBigtableClusters(clusters []interface{}, instanceID string) []bigtabl
314314
}
315315

316316
func resourceBigtableInstanceClusterReorderTypeList(diff *schema.ResourceDiff, meta interface{}) error {
317-
keys := diff.GetChangedKeysPrefix("cluster")
318-
if len(keys) == 0 {
319-
return nil
320-
}
317+
old_count, new_count := diff.GetChange("cluster.#")
321318

322-
oldCount, newCount := diff.GetChange("cluster.#")
323-
var count int
324-
if oldCount.(int) < newCount.(int) {
325-
count = newCount.(int)
326-
} else {
327-
count = oldCount.(int)
319+
// simulate Required:true and MinItems:1 for "cluster"
320+
if new_count.(int) < 1 {
321+
return fmt.Errorf("Error applying diff. Resource definition should contain one or more cluster blocks, got %d blocks", new_count.(int))
328322
}
329323

330-
// simulate Required:true and MinItems:1
331-
if count < 1 {
324+
if old_count.(int) != new_count.(int) {
332325
return nil
333326
}
334327

335328
var old_ids []string
336-
var new_ids []string
337-
for i := 0; i < count; i++ {
338-
old, new := diff.GetChange(fmt.Sprintf("cluster.%d.cluster_id", i))
339-
if old != nil {
340-
old_ids = append(old_ids, old.(string))
341-
}
342-
if new != nil {
343-
new_ids = append(new_ids, new.(string))
344-
}
345-
}
329+
clusters := make(map[string]interface{}, new_count.(int))
346330

347-
d := difference(old_ids, new_ids)
348-
349-
// clusters have been reordered
350-
if len(new_ids) == len(old_ids) && len(d) == 0 {
351-
352-
clusterMap := make(map[string]interface{}, len(new_ids))
353-
for i := 0; i < count; i++ {
354-
_, id := diff.GetChange(fmt.Sprintf("cluster.%d.cluster_id", i))
355-
_, c := diff.GetChange(fmt.Sprintf("cluster.%d", i))
356-
clusterMap[id.(string)] = c
331+
for i := 0; i < new_count.(int); i++ {
332+
old_id, new_id := diff.GetChange(fmt.Sprintf("cluster.%d.cluster_id", i))
333+
if old_id != nil && old_id != "" {
334+
old_ids = append(old_ids, old_id.(string))
357335
}
336+
_, c := diff.GetChange(fmt.Sprintf("cluster.%d", i))
337+
clusters[new_id.(string)] = c
338+
}
358339

359-
// build a slice of the new clusters ordered by the old cluster order
360-
var old_cluster_order []interface{}
361-
for _, id := range old_ids {
362-
if c, ok := clusterMap[id]; ok {
363-
old_cluster_order = append(old_cluster_order, c)
364-
}
340+
// reorder clusters according to the old cluster order
341+
var old_cluster_order []interface{}
342+
for _, id := range old_ids {
343+
if c, ok := clusters[id]; ok {
344+
old_cluster_order = append(old_cluster_order, c)
365345
}
346+
}
366347

367-
err := diff.SetNew("cluster", old_cluster_order)
368-
if err != nil {
369-
return fmt.Errorf("Error setting cluster diff: %s", err)
370-
}
348+
err := diff.SetNew("cluster", old_cluster_order)
349+
if err != nil {
350+
return fmt.Errorf("Error setting cluster diff: %s", err)
371351
}
372352

373353
return nil
374354
}
375-
376-
func difference(a, b []string) []string {
377-
var c []string
378-
m := make(map[string]bool)
379-
for _, o := range a {
380-
m[o] = true
381-
}
382-
for _, n := range b {
383-
if _, ok := m[n]; !ok {
384-
c = append(c, n)
385-
}
386-
}
387-
return c
388-
}

0 commit comments

Comments
 (0)