From 24c9b7c9877ab0f11ec170062782c82a391a8398 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 23 Jun 2014 20:40:32 -0700 Subject: [PATCH] terraform: make sure state is set to something on refresh --- terraform/terraform.go | 5 +++++ terraform/terraform_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/terraform/terraform.go b/terraform/terraform.go index 016840659..0090f6cd3 100644 --- a/terraform/terraform.go +++ b/terraform/terraform.go @@ -209,6 +209,11 @@ func (t *Terraform) planWalkFn( return nil, err } + // Make sure the state is set to at the very least the empty state + if newState == nil { + newState = new(ResourceState) + } + // Get a diff from the newest state diff, err := r.Provider.Diff(newState, r.Config) if err != nil { diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 216e7cb99..5c2cc5d48 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -375,11 +375,31 @@ func TestTerraformPlan_providerInit(t *testing.T) { if !p.ConfigureCalled { t.Fatal("configure should be called") } - if p.ConfigureConfig.Raw["foo"].(string) != "2" { + if p.ConfigureConfig.Config["foo"].(string) != "2" { t.Fatalf("bad: %#v", p.ConfigureConfig) } } +func TestTerraformPlan_refreshNil(t *testing.T) { + tf := testTerraform(t, "plan-nil") + + s := new(State) + s.init() + s.Resources["aws_instance.foo"] = &ResourceState{ + Attributes: map[string]string{ + "nil": "1", + }, + } + + plan, err := tf.Plan(s) + if err != nil { + t.Fatalf("err: %s", err) + } + if len(plan.Diff.Resources) != 0 { + t.Fatalf("bad: %#v", plan.Diff.Resources) + } +} + func testConfig(t *testing.T, name string) *config.Config { c, err := config.Load(filepath.Join(fixtureDir, name, "main.tf")) if err != nil { @@ -484,6 +504,10 @@ func testProviderFunc(n string, rs []string) ResourceProviderFactory { } refreshFn := func(s *ResourceState) (*ResourceState, error) { + if _, ok := s.Attributes["nil"]; ok { + return nil, nil + } + return s, nil }