From 13433687cbbb9cfd00df23920a8b6df9234063aa Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 8 Mar 2018 11:39:29 -0500 Subject: [PATCH] filter null output values from state While null values should not normally appear in a state file, we should filter the values rather than crash. --- .../providers/terraform/data_source_state.go | 4 +++- .../terraform/data_source_state_test.go | 23 ++++++++++++++++++ .../test-fixtures/null_outputs.tfstate | 24 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 builtin/providers/terraform/test-fixtures/null_outputs.tfstate diff --git a/builtin/providers/terraform/data_source_state.go b/builtin/providers/terraform/data_source_state.go index 1f0fbea48..e6797eb4d 100644 --- a/builtin/providers/terraform/data_source_state.go +++ b/builtin/providers/terraform/data_source_state.go @@ -118,7 +118,9 @@ func dataSourceRemoteStateRead(d *schema.ResourceData, meta interface{}) error { log.Println("[DEBUG] empty remote state") } else { for key, val := range remoteState.RootModule().Outputs { - outputMap[key] = val.Value + if val.Value != nil { + outputMap[key] = val.Value + } } } diff --git a/builtin/providers/terraform/data_source_state_test.go b/builtin/providers/terraform/data_source_state_test.go index bfabd27b1..f80b738be 100644 --- a/builtin/providers/terraform/data_source_state_test.go +++ b/builtin/providers/terraform/data_source_state_test.go @@ -63,6 +63,20 @@ func TestState_complexOutputs(t *testing.T) { }) } +// outputs should never have a null value, but don't crash if we ever encounter +// them. +func TestState_nullOutputs(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccState_nullOutputs, + }, + }, + }) +} + func TestEmptyState_defaults(t *testing.T) { resource.UnitTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -142,6 +156,15 @@ resource "terraform_remote_state" "foo" { } }` +const testAccState_nullOutputs = ` +resource "terraform_remote_state" "foo" { + backend = "local" + + config { + path = "./test-fixtures/null_outputs.tfstate" + } +}` + const testAccEmptyState_defaults = ` data "terraform_remote_state" "foo" { backend = "local" diff --git a/builtin/providers/terraform/test-fixtures/null_outputs.tfstate b/builtin/providers/terraform/test-fixtures/null_outputs.tfstate new file mode 100644 index 000000000..fa27a1563 --- /dev/null +++ b/builtin/providers/terraform/test-fixtures/null_outputs.tfstate @@ -0,0 +1,24 @@ +{ + "version": 3, + "terraform_version": "0.7.0", + "serial": 3, + "modules": [ + { + "path": [ + "root" + ], + "outputs": { + "map": { + "sensitive": false, + "type": "map", + "value": null + }, + "list": { + "sensitive": false, + "type": "list", + "value": null + } + } + } + ] +}