From f51fb5e127a29e403d3afa6024d480e90205b831 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Thu, 26 Mar 2015 12:11:26 -0500 Subject: [PATCH] providers/aws: handle empty instancestate in state migration fixes #1309 --- .../aws/resource_aws_instance_migrate.go | 6 ++++ .../aws/resource_aws_instance_migrate_test.go | 24 ++++++++++++++++ terraform/state.go | 4 +++ terraform/state_test.go | 28 +++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/builtin/providers/aws/resource_aws_instance_migrate.go b/builtin/providers/aws/resource_aws_instance_migrate.go index adb7a01ec..5d7075f75 100644 --- a/builtin/providers/aws/resource_aws_instance_migrate.go +++ b/builtin/providers/aws/resource_aws_instance_migrate.go @@ -24,7 +24,13 @@ func resourceAwsInstanceMigrateState( } func migrateStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { + if is.Empty() { + log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") + return is, nil + } + log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes) + // Delete old count delete(is.Attributes, "block_device.#") diff --git a/builtin/providers/aws/resource_aws_instance_migrate_test.go b/builtin/providers/aws/resource_aws_instance_migrate_test.go index 5738f2508..d39294331 100644 --- a/builtin/providers/aws/resource_aws_instance_migrate_test.go +++ b/builtin/providers/aws/resource_aws_instance_migrate_test.go @@ -115,6 +115,7 @@ func TestAWSInstanceMigrateState(t *testing.T) { for tn, tc := range cases { is := &terraform.InstanceState{ + ID: "i-abc123", Attributes: tc.Attributes, } is, err := resourceAwsInstanceMigrateState( @@ -133,3 +134,26 @@ func TestAWSInstanceMigrateState(t *testing.T) { } } } + +func TestAWSInstanceMigrateState_empty(t *testing.T) { + var is *terraform.InstanceState + var meta interface{} + + // should handle nil + is, err := resourceAwsInstanceMigrateState(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 = resourceAwsInstanceMigrateState(0, is, meta) + + if err != nil { + t.Fatalf("err: %#v", err) + } +} diff --git a/terraform/state.go b/terraform/state.go index 1a1a28bea..d4341302d 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -885,6 +885,10 @@ func (i *InstanceState) deepcopy() *InstanceState { return n } +func (s *InstanceState) Empty() bool { + return s == nil || s.ID == "" +} + func (s *InstanceState) Equal(other *InstanceState) bool { // Short circuit some nil checks if s == nil || other == nil { diff --git a/terraform/state_test.go b/terraform/state_test.go index e222b1b72..7f3dbb567 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -366,6 +366,34 @@ func TestResourceStateTaint(t *testing.T) { } } +func TestInstanceStateEmpty(t *testing.T) { + cases := map[string]struct { + In *InstanceState + Result bool + }{ + "nil is empty": { + nil, + true, + }, + "non-nil but without ID is empty": { + &InstanceState{}, + true, + }, + "with ID is not empty": { + &InstanceState{ + ID: "i-abc123", + }, + false, + }, + } + + for tn, tc := range cases { + if tc.In.Empty() != tc.Result { + t.Fatalf("%q expected %#v to be empty: %#v", tn, tc.In, tc.Result) + } + } +} + func TestInstanceStateEqual(t *testing.T) { cases := []struct { Result bool