forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow updating additional_zones, turn it into a set (hashicorp#152)
- Loading branch information
1 parent
950715f
commit 90d3466
Showing
6 changed files
with
283 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func resourceContainerClusterMigrateState( | ||
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { | ||
if is.Empty() { | ||
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") | ||
return is, nil | ||
} | ||
|
||
switch v { | ||
case 0: | ||
log.Println("[INFO] Found Container Cluster State v0; migrating to v1") | ||
return migrateClusterStateV0toV1(is) | ||
default: | ||
return is, fmt.Errorf("Unexpected schema version: %d", v) | ||
} | ||
} | ||
|
||
func migrateClusterStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { | ||
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes) | ||
|
||
newZones := []string{} | ||
|
||
for k, v := range is.Attributes { | ||
if !strings.HasPrefix(k, "additional_zones.") { | ||
continue | ||
} | ||
|
||
if k == "additional_zones.#" { | ||
continue | ||
} | ||
|
||
// Key is now of the form additional_zones.%d | ||
kParts := strings.Split(k, ".") | ||
|
||
// Sanity check: two parts should be there and <N> should be a number | ||
badFormat := false | ||
if len(kParts) != 2 { | ||
badFormat = true | ||
} else if _, err := strconv.Atoi(kParts[1]); err != nil { | ||
badFormat = true | ||
} | ||
|
||
if badFormat { | ||
return is, fmt.Errorf("migration error: found additional_zones key in unexpected format: %s", k) | ||
} | ||
|
||
newZones = append(newZones, v) | ||
delete(is.Attributes, k) | ||
} | ||
|
||
for _, v := range newZones { | ||
hash := schema.HashString(v) | ||
newKey := fmt.Sprintf("additional_zones.%d", hash) | ||
is.Attributes[newKey] = v | ||
} | ||
|
||
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes) | ||
return is, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package google | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestContainerClusterMigrateState(t *testing.T) { | ||
cases := map[string]struct { | ||
StateVersion int | ||
Attributes map[string]string | ||
Expected map[string]string | ||
Meta interface{} | ||
}{ | ||
"change additional_zones from list to set": { | ||
StateVersion: 0, | ||
Attributes: map[string]string{ | ||
"additional_zones.#": "2", | ||
"additional_zones.0": "us-central1-c", | ||
"additional_zones.1": "us-central1-b", | ||
}, | ||
Expected: map[string]string{ | ||
"additional_zones.#": "2", | ||
"additional_zones.90274510": "us-central1-c", | ||
"additional_zones.1919306328": "us-central1-b", | ||
}, | ||
Meta: &Config{}, | ||
}, | ||
} | ||
|
||
for tn, tc := range cases { | ||
is := &terraform.InstanceState{ | ||
ID: "i-abc123", | ||
Attributes: tc.Attributes, | ||
} | ||
is, err := resourceContainerClusterMigrateState( | ||
tc.StateVersion, is, tc.Meta) | ||
|
||
if err != nil { | ||
t.Fatalf("bad: %s, err: %#v", tn, err) | ||
} | ||
|
||
for k, v := range tc.Expected { | ||
if is.Attributes[k] != v { | ||
t.Fatalf( | ||
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v", | ||
tn, k, v, k, is.Attributes[k], is.Attributes) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestContainerClusterMigrateState_empty(t *testing.T) { | ||
var is *terraform.InstanceState | ||
var meta *Config | ||
|
||
// should handle nil | ||
is, err := resourceContainerClusterMigrateState(0, is, meta) | ||
|
||
if err != nil { | ||
t.Fatalf("err: %#v", err) | ||
} | ||
if is != nil { | ||
t.Fatalf("expected nil instancestate, got: %#v", is) | ||
} | ||
|
||
// should handle non-nil but empty | ||
is = &terraform.InstanceState{} | ||
is, err = resourceContainerClusterMigrateState(0, is, meta) | ||
|
||
if err != nil { | ||
t.Fatalf("err: %#v", err) | ||
} | ||
} |
Oops, something went wrong.