Skip to content

Commit 6b6cc5f

Browse files
test is passing for reordering clusters
1 parent 47b23d9 commit 6b6cc5f

File tree

2 files changed

+119
-12
lines changed

2 files changed

+119
-12
lines changed

google/resource_bigtable_instance.go

+81-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77

8+
"github.com/hashicorp/terraform/helper/customdiff"
89
"github.com/hashicorp/terraform/helper/schema"
910
"github.com/hashicorp/terraform/helper/validation"
1011

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

22+
CustomizeDiff: customdiff.All(
23+
resourceBigtableInstanceClusterReorderTypeList,
24+
),
25+
2126
Schema: map[string]*schema.Schema{
2227
"name": {
2328
Type: schema.TypeString,
@@ -27,7 +32,8 @@ func resourceBigtableInstance() *schema.Resource {
2732

2833
"cluster": {
2934
Type: schema.TypeList,
30-
Required: true,
35+
Optional: true,
36+
Computed: true,
3137
Elem: &schema.Resource{
3238
Schema: map[string]*schema.Schema{
3339
"cluster_id": {
@@ -306,3 +312,77 @@ func expandBigtableClusters(clusters []interface{}, instanceID string) []bigtabl
306312
}
307313
return results
308314
}
315+
316+
func resourceBigtableInstanceClusterReorderTypeList(diff *schema.ResourceDiff, meta interface{}) error {
317+
keys := diff.GetChangedKeysPrefix("cluster")
318+
if len(keys) == 0 {
319+
return nil
320+
}
321+
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)
328+
}
329+
330+
// simulate Required:true and MinItems:1
331+
if count < 1 {
332+
return nil
333+
}
334+
335+
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+
}
346+
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
357+
}
358+
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+
}
365+
}
366+
367+
err := diff.SetNew("cluster", old_cluster_order)
368+
if err != nil {
369+
return fmt.Errorf("Error setting cluster diff: %s", err)
370+
}
371+
}
372+
373+
return nil
374+
}
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+
}

google/resource_bigtable_instance_test.go

+38-11
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ func TestAccBigtableInstance_cluster(t *testing.T) {
5454
ExpectError: regexp.MustCompile("config is invalid: Too many cluster blocks: No more than 4 \"cluster\" blocks are allowed"),
5555
},
5656
{
57-
Config: testAccBigtableInstance_cluster(instanceName),
57+
Config: testAccBigtableInstance_cluster(instanceName, 3),
5858
Check: resource.ComposeTestCheckFunc(
5959
testAccBigtableInstanceExists(
6060
"google_bigtable_instance.instance", 3),
6161
),
6262
},
63+
{
64+
Config: testAccBigtableInstance_cluster_reordered(instanceName, 5),
65+
Check: resource.ComposeTestCheckFunc(
66+
testAccBigtableInstanceExists(
67+
"google_bigtable_instance.instance", 5),
68+
),
69+
},
6370
},
6471
})
6572
}
@@ -166,36 +173,36 @@ resource "google_bigtable_instance" "instance" {
166173
`, instanceName, instanceName, numNodes)
167174
}
168175

169-
func testAccBigtableInstance_cluster(instanceName string) string {
176+
func testAccBigtableInstance_cluster(instanceName string, numNodes int) string {
170177
return fmt.Sprintf(`
171178
resource "google_bigtable_instance" "instance" {
172179
name = "%s"
173180
cluster {
174181
cluster_id = "%s-a"
175182
zone = "us-central1-a"
176-
num_nodes = 3
183+
num_nodes = %d
177184
storage_type = "HDD"
178185
}
179186
cluster {
180187
cluster_id = "%s-b"
181188
zone = "us-central1-b"
182-
num_nodes = 3
189+
num_nodes = %d
183190
storage_type = "HDD"
184191
}
185192
cluster {
186193
cluster_id = "%s-c"
187194
zone = "us-central1-c"
188-
num_nodes = 3
195+
num_nodes = %d
189196
storage_type = "HDD"
190197
}
191198
cluster {
192199
cluster_id = "%s-d"
193200
zone = "us-central1-f"
194-
num_nodes = 3
201+
num_nodes = %d
195202
storage_type = "HDD"
196203
}
197204
}
198-
`, instanceName, instanceName, instanceName, instanceName, instanceName)
205+
`, instanceName, numNodes, instanceName, numNodes, instanceName, numNodes, instanceName, numNodes)
199206
}
200207

201208
func testAccBigtableInstance_clusterMax(instanceName string) string {
@@ -217,23 +224,43 @@ resource "google_bigtable_instance" "instance" {
217224
cluster {
218225
cluster_id = "%s-c"
219226
zone = "us-central1-c"
220-
num_nodes = 3
227+
num_nodes = %d
228+
storage_type = "HDD"
229+
}
230+
}
231+
`, instanceName, instanceName, instanceName, instanceName, instanceName, instanceName)
232+
}
233+
234+
func testAccBigtableInstance_cluster_reordered(instanceName string, numNodes int) string {
235+
return fmt.Sprintf(`
236+
resource "google_bigtable_instance" "instance" {
237+
name = "%s"
238+
cluster {
239+
num_nodes = %d
240+
cluster_id = "%s-b"
241+
zone = "us-central1-c"
221242
storage_type = "HDD"
222243
}
223244
cluster {
224245
cluster_id = "%s-d"
225246
zone = "us-central1-f"
226-
num_nodes = 3
247+
num_nodes = %d
248+
storage_type = "HDD"
249+
}
250+
cluster {
251+
zone = "us-central1-b"
252+
cluster_id = "%s-a"
253+
num_nodes = %d
227254
storage_type = "HDD"
228255
}
229256
cluster {
230257
cluster_id = "%s-e"
231258
zone = "us-east1-a"
232-
num_nodes = 3
259+
num_nodes = %d
233260
storage_type = "HDD"
234261
}
235262
}
236-
`, instanceName, instanceName, instanceName, instanceName, instanceName, instanceName)
263+
`, instanceName, numNodes, instanceName, instanceName, numNodes, instanceName, numNodes, instanceName, numNodes, instanceName)
237264
}
238265

239266
func testAccBigtableInstance_development(instanceName string) string {

0 commit comments

Comments
 (0)