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.
This commit is contained in:
James Bardin 2018-03-08 11:39:29 -05:00
parent ea50f455ed
commit 13433687cb
3 changed files with 50 additions and 1 deletions

View File

@ -118,9 +118,11 @@ func dataSourceRemoteStateRead(d *schema.ResourceData, meta interface{}) error {
log.Println("[DEBUG] empty remote state") log.Println("[DEBUG] empty remote state")
} else { } else {
for key, val := range remoteState.RootModule().Outputs { for key, val := range remoteState.RootModule().Outputs {
if val.Value != nil {
outputMap[key] = val.Value outputMap[key] = val.Value
} }
} }
}
mappedOutputs := remoteStateFlatten(outputMap) mappedOutputs := remoteStateFlatten(outputMap)

View File

@ -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) { func TestEmptyState_defaults(t *testing.T) {
resource.UnitTest(t, resource.TestCase{ resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, 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 = ` const testAccEmptyState_defaults = `
data "terraform_remote_state" "foo" { data "terraform_remote_state" "foo" {
backend = "local" backend = "local"

View File

@ -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
}
}
}
]
}