diff --git a/builtin/providers/aws/resource_aws_vpc.go b/builtin/providers/aws/resource_aws_vpc.go index e3bcee3a6..6807706b6 100644 --- a/builtin/providers/aws/resource_aws_vpc.go +++ b/builtin/providers/aws/resource_aws_vpc.go @@ -22,6 +22,9 @@ func resourceAwsVpc() *schema.Resource { State: resourceAwsVpcInstanceImport, }, + SchemaVersion: 1, + MigrateState: resourceAwsVpcMigrateState, + Schema: map[string]*schema.Schema{ "cidr_block": { Type: schema.TypeString, diff --git a/builtin/providers/aws/resource_aws_vpc_migrate.go b/builtin/providers/aws/resource_aws_vpc_migrate.go new file mode 100644 index 000000000..90738d1f2 --- /dev/null +++ b/builtin/providers/aws/resource_aws_vpc_migrate.go @@ -0,0 +1,33 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform/terraform" +) + +func resourceAwsVpcMigrateState( + v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { + switch v { + case 0: + log.Println("[INFO] Found AWS VPC State v0; migrating to v1") + return migrateVpcStateV0toV1(is) + default: + return is, fmt.Errorf("Unexpected schema version: %d", v) + } +} + +func migrateVpcStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { + if is.Empty() || is.Attributes == nil { + log.Println("[DEBUG] Empty VPC State; nothing to migrate.") + return is, nil + } + + log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes) + + is.Attributes["assign_generated_ipv6_cidr_block"] = "false" + + log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes) + return is, nil +} diff --git a/builtin/providers/aws/resource_aws_vpc_migrate_test.go b/builtin/providers/aws/resource_aws_vpc_migrate_test.go new file mode 100644 index 000000000..55be6d75b --- /dev/null +++ b/builtin/providers/aws/resource_aws_vpc_migrate_test.go @@ -0,0 +1,49 @@ +package aws + +import ( + "testing" + + "github.com/hashicorp/terraform/terraform" +) + +func TestAWSVpcMigrateState(t *testing.T) { + cases := map[string]struct { + StateVersion int + ID string + Attributes map[string]string + Expected string + Meta interface{} + }{ + "v0_1": { + StateVersion: 0, + ID: "some_id", + Attributes: map[string]string{ + "assign_generated_ipv6_cidr_block": "true", + }, + Expected: "false", + }, + "v0_1_without_value": { + StateVersion: 0, + ID: "some_id", + Attributes: map[string]string{}, + Expected: "false", + }, + } + + for tn, tc := range cases { + is := &terraform.InstanceState{ + ID: tc.ID, + Attributes: tc.Attributes, + } + is, err := resourceAwsVpcMigrateState( + tc.StateVersion, is, tc.Meta) + + if err != nil { + t.Fatalf("bad: %s, err: %#v", tn, err) + } + + if is.Attributes["assign_generated_ipv6_cidr_block"] != tc.Expected { + t.Fatalf("bad VPC Migrate: %s\n\n expected: %s", is.Attributes["assign_generated_ipv6_cidr_block"], tc.Expected) + } + } +}