From 5d684b399cd7429a69700624cdefb86d00c0b36b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 10 Dec 2016 18:52:05 -0500 Subject: [PATCH] config: error when loading multiple lifecycle blocks Fixes #8776 This introduces an error when multiple `lifecycle` blocks exist on a resource in the configuration. --- config/loader_hcl.go | 6 ++++++ config/loader_test.go | 7 +++++++ config/test-fixtures/resource-multi-lifecycle.tf | 4 ++++ 3 files changed, 17 insertions(+) create mode 100644 config/test-fixtures/resource-multi-lifecycle.tf diff --git a/config/loader_hcl.go b/config/loader_hcl.go index 881a98312..195e71b02 100644 --- a/config/loader_hcl.go +++ b/config/loader_hcl.go @@ -781,6 +781,12 @@ func loadManagedResourcesHcl(list *ast.ObjectList) ([]*Resource, error) { // destroying the existing instance var lifecycle ResourceLifecycle if o := listVal.Filter("lifecycle"); len(o.Items) > 0 { + if len(o.Items) > 1 { + return nil, fmt.Errorf( + "%s[%s]: Multiple lifecycle blocks found, expected one", + t, k) + } + // Check for invalid keys valid := []string{"create_before_destroy", "ignore_changes", "prevent_destroy"} if err := checkHCLKeys(o.Items[0].Val, valid); err != nil { diff --git a/config/loader_test.go b/config/loader_test.go index f7fad33c0..0b2044afb 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -78,6 +78,13 @@ func TestLoadFile_resourceArityMistake(t *testing.T) { } } +func TestLoadFile_resourceMultiLifecycle(t *testing.T) { + _, err := LoadFile(filepath.Join(fixtureDir, "resource-multi-lifecycle.tf")) + if err == nil { + t.Fatal("should have error") + } +} + func TestLoadFile_dataSourceArityMistake(t *testing.T) { _, err := LoadFile(filepath.Join(fixtureDir, "data-source-arity-mistake.tf")) if err == nil { diff --git a/config/test-fixtures/resource-multi-lifecycle.tf b/config/test-fixtures/resource-multi-lifecycle.tf new file mode 100644 index 000000000..68e0dad37 --- /dev/null +++ b/config/test-fixtures/resource-multi-lifecycle.tf @@ -0,0 +1,4 @@ +resource "aws_instance" "foo" { + lifecycle {} + lifecycle {} +}