2019-03-09 01:31:22 +01:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceConfigMode(t *testing.T) {
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckResourceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource_config_mode" "foo" {
|
|
|
|
resource_as_attr = [
|
|
|
|
{
|
|
|
|
foo = "resource_as_attr 0"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
foo = "resource_as_attr 1"
|
|
|
|
},
|
|
|
|
]
|
2019-03-21 19:59:53 +01:00
|
|
|
resource_as_attr_dynamic = [
|
|
|
|
{
|
|
|
|
foo = "resource_as_attr_dynamic 0"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
},
|
|
|
|
]
|
2019-03-27 01:27:14 +01:00
|
|
|
}
|
|
|
|
`),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr.#", "2"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr.0.foo", "resource_as_attr 0"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr.1.foo", "resource_as_attr 1"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr_dynamic.#", "2"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr_dynamic.0.foo", "resource_as_attr_dynamic 0"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr_dynamic.1.foo", "default"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource_config_mode" "foo" {
|
|
|
|
# Due to a preprocessing fixup we do in lang.EvalBlock, it's allowed
|
|
|
|
# to specify resource_as_attr members using one or more nested blocks
|
|
|
|
# instead of attribute syntax, if desired. This should be equivalent
|
|
|
|
# to the previous config.
|
|
|
|
#
|
|
|
|
# This allowance is made for backward-compatibility with existing providers
|
|
|
|
# before Terraform v0.12 that were expecting nested block types to also
|
|
|
|
# support attribute syntax; it should not be used for any new use-cases.
|
|
|
|
resource_as_attr {
|
|
|
|
foo = "resource_as_attr 0"
|
|
|
|
}
|
|
|
|
resource_as_attr {
|
|
|
|
foo = "resource_as_attr 1"
|
|
|
|
}
|
|
|
|
resource_as_attr_dynamic = [
|
|
|
|
{
|
|
|
|
foo = "resource_as_attr_dynamic 0"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
},
|
|
|
|
]
|
2019-03-09 01:31:22 +01:00
|
|
|
}
|
|
|
|
`),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr.#", "2"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr.0.foo", "resource_as_attr 0"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr.1.foo", "resource_as_attr 1"),
|
2019-03-21 19:59:53 +01:00
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr_dynamic.#", "2"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr_dynamic.0.foo", "resource_as_attr_dynamic 0"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr_dynamic.1.foo", "default"),
|
2019-03-09 01:31:22 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource_config_mode" "foo" {
|
|
|
|
resource_as_attr = [
|
|
|
|
{
|
|
|
|
foo = "resource_as_attr 0 updated"
|
|
|
|
},
|
|
|
|
]
|
2019-03-21 19:59:53 +01:00
|
|
|
resource_as_attr_dynamic = [
|
|
|
|
{
|
|
|
|
},
|
|
|
|
]
|
2019-03-09 01:31:22 +01:00
|
|
|
}
|
|
|
|
`),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr.0.foo", "resource_as_attr 0 updated"),
|
2019-03-21 19:59:53 +01:00
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr_dynamic.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr_dynamic.0.foo", "default"),
|
2019-03-09 01:31:22 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
2019-04-17 00:48:37 +02:00
|
|
|
resource "test_resource_config_mode" "foo" {
|
|
|
|
resource_as_attr_dynamic = [
|
|
|
|
{
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr_dynamic.#", "1"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
2019-03-09 01:31:22 +01:00
|
|
|
resource "test_resource_config_mode" "foo" {
|
|
|
|
resource_as_attr = []
|
2019-03-21 19:59:53 +01:00
|
|
|
resource_as_attr_dynamic = []
|
2019-03-09 01:31:22 +01:00
|
|
|
}
|
|
|
|
`),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr.#", "0"),
|
2019-03-21 19:59:53 +01:00
|
|
|
resource.TestCheckResourceAttr("test_resource_config_mode.foo", "resource_as_attr_dynamic.#", "0"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource_config_mode" "foo" {
|
|
|
|
}
|
|
|
|
`),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestCheckNoResourceAttr("test_resource_config_mode.foo", "resource_as_attr.#"),
|
|
|
|
resource.TestCheckNoResourceAttr("test_resource_config_mode.foo", "resource_as_attr_dynamic.#"),
|
2019-03-09 01:31:22 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|