"defaults" property for terraform_remote_state

This allows providing default values for keys that might not be set in the retrieved state.
This commit is contained in:
Kaveh Mousavi Zamani 2017-09-13 18:34:00 +02:00 committed by Martin Atkins
parent ffbe8d6540
commit 2b8843cc2c
3 changed files with 74 additions and 6 deletions

View File

@ -37,6 +37,11 @@ func dataSourceRemoteState() *schema.Resource {
Optional: true,
},
"defaults": {
Type: schema.TypeMap,
Optional: true,
},
"environment": {
Type: schema.TypeString,
Optional: true,
@ -88,19 +93,22 @@ func dataSourceRemoteStateRead(d *schema.ResourceData, meta interface{}) error {
if err := state.RefreshState(); err != nil {
return err
}
d.SetId(time.Now().UTC().String())
outputMap := make(map[string]interface{})
defaults := d.Get("defaults").(map[string]interface{})
for key, val := range defaults {
outputMap[key] = val
}
remoteState := state.State()
if remoteState.Empty() {
log.Println("[DEBUG] empty remote state")
return nil
}
for key, val := range remoteState.RootModule().Outputs {
outputMap[key] = val.Value
} else {
for key, val := range remoteState.RootModule().Outputs {
outputMap[key] = val.Value
}
}
mappedOutputs := remoteStateFlatten(outputMap)
@ -108,5 +116,6 @@ func dataSourceRemoteStateRead(d *schema.ResourceData, meta interface{}) error {
for key, val := range mappedOutputs {
d.UnsafeSetFieldRaw(key, val)
}
return nil
}

View File

@ -63,6 +63,38 @@ func TestState_complexOutputs(t *testing.T) {
})
}
func TestEmptyState_defaults(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccEmptyState_defaults,
Check: resource.ComposeTestCheckFunc(
testAccCheckStateValue(
"data.terraform_remote_state.foo", "foo", "bar"),
),
},
},
})
}
func TestState_defaults(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccEmptyState_defaults,
Check: resource.ComposeTestCheckFunc(
testAccCheckStateValue(
"data.terraform_remote_state.foo", "foo", "bar"),
),
},
},
})
}
func testAccCheckStateValue(id, name, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[id]
@ -109,3 +141,29 @@ resource "terraform_remote_state" "foo" {
path = "./test-fixtures/complex_outputs.tfstate"
}
}`
const testAccEmptyState_defaults = `
data "terraform_remote_state" "foo" {
backend = "local"
config {
path = "./test-fixtures/empty.tfstate"
}
defaults {
foo = "bar"
}
}`
const testAccState_defaults = `
data "terraform_remote_state" "foo" {
backend = "local"
config {
path = "./test-fixtures/basic.tfstate"
}
defaults {
foo = "not bar"
}
}`

View File

@ -33,6 +33,7 @@ The following arguments are supported:
* `backend` - (Required) The remote backend to use.
* `environment` - (Optional) The Terraform environment to use.
* `config` - (Optional) The configuration of the remote backend.
* `defaults` - (Optional) default value for outputs in case state file is empty or it does not have the output.
* Remote state config docs can be found [here](/docs/backends/types/terraform-enterprise.html)
## Attributes Reference