@@ -314,75 +314,41 @@ func expandBigtableClusters(clusters []interface{}, instanceID string) []bigtabl
314
314
}
315
315
316
316
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.#" )
321
318
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 ))
328
322
}
329
323
330
- // simulate Required:true and MinItems:1
331
- if count < 1 {
324
+ if old_count .(int ) != new_count .(int ) {
332
325
return nil
333
326
}
334
327
335
328
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 ))
346
330
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 ))
357
335
}
336
+ _ , c := diff .GetChange (fmt .Sprintf ("cluster.%d" , i ))
337
+ clusters [new_id .(string )] = c
338
+ }
358
339
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 )
365
345
}
346
+ }
366
347
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 )
371
351
}
372
352
373
353
return nil
374
354
}
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