From 1aaddafba090083e8a4714dd8c81e5f98270d893 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 22 Sep 2014 15:02:00 -0700 Subject: [PATCH] terraform: Adding lifecycle config block --- config/config.go | 20 ++++++++++------ config/loader_hcl.go | 24 +++++++++---------- config/loader_test.go | 4 ++-- config/test-fixtures/create-before-destroy.tf | 8 +++++-- terraform/graph.go | 2 +- .../graph-diff-create-before/main.tf | 4 +++- 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/config/config.go b/config/config.go index b7a1fef6b..0b4350a2a 100644 --- a/config/config.go +++ b/config/config.go @@ -54,13 +54,19 @@ type ProviderConfig struct { // A Terraform resource is something that represents some component that // can be created and managed, and has some properties associated with it. type Resource struct { - Name string - Type string - Count int - RawConfig *RawConfig - Provisioners []*Provisioner - DependsOn []string - CreateBeforeDestroy bool + Name string + Type string + Count int + RawConfig *RawConfig + Provisioners []*Provisioner + DependsOn []string + Lifecycle ResourceLifecycle +} + +// ResourceLifecycle is used to store the lifecycle tuning parameters +// to allow customized behavior +type ResourceLifecycle struct { + CreateBeforeDestroy bool `hcl:"create_before_destroy"` } // Provisioner is a configured provisioner step on a resource. diff --git a/config/loader_hcl.go b/config/loader_hcl.go index 4bf34c376..d777873e7 100644 --- a/config/loader_hcl.go +++ b/config/loader_hcl.go @@ -394,7 +394,7 @@ func loadResourcesHcl(os *hclobj.Object) ([]*Resource, error) { delete(config, "count") delete(config, "depends_on") delete(config, "provisioner") - delete(config, "create_before_destroy") + delete(config, "lifecycle") rawConfig, err := NewRawConfig(config) if err != nil { @@ -460,12 +460,12 @@ func loadResourcesHcl(os *hclobj.Object) ([]*Resource, error) { // Check if the resource should be re-created before // destroying the existing instance - var createBeforeDestroy bool - if o := obj.Get("create_before_destroy", false); o != nil { - err = hcl.DecodeObject(&createBeforeDestroy, o) + var lifecycle ResourceLifecycle + if o := obj.Get("lifecycle", false); o != nil { + err = hcl.DecodeObject(&lifecycle, o) if err != nil { return nil, fmt.Errorf( - "Error parsing create_before_destroy for %s[%s]: %s", + "Error parsing lifecycle for %s[%s]: %s", t.Key, k, err) @@ -473,13 +473,13 @@ func loadResourcesHcl(os *hclobj.Object) ([]*Resource, error) { } result = append(result, &Resource{ - Name: k, - Type: t.Key, - Count: count, - RawConfig: rawConfig, - Provisioners: provisioners, - DependsOn: dependsOn, - CreateBeforeDestroy: createBeforeDestroy, + Name: k, + Type: t.Key, + Count: count, + RawConfig: rawConfig, + Provisioners: provisioners, + DependsOn: dependsOn, + Lifecycle: lifecycle, }) } } diff --git a/config/loader_test.go b/config/loader_test.go index d8ab2b6c2..5bb77b931 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -368,7 +368,7 @@ func TestLoad_createBeforeDestroy(t *testing.T) { } // Should enable create before destroy - if !r.CreateBeforeDestroy { + if !r.Lifecycle.CreateBeforeDestroy { t.Fatalf("Bad: %#v", r) } @@ -378,7 +378,7 @@ func TestLoad_createBeforeDestroy(t *testing.T) { } // Should not enable create before destroy - if r.CreateBeforeDestroy { + if r.Lifecycle.CreateBeforeDestroy { t.Fatalf("Bad: %#v", r) } } diff --git a/config/test-fixtures/create-before-destroy.tf b/config/test-fixtures/create-before-destroy.tf index 7f348aeac..a45ff5c19 100644 --- a/config/test-fixtures/create-before-destroy.tf +++ b/config/test-fixtures/create-before-destroy.tf @@ -1,10 +1,14 @@ resource "aws_instance" "web" { ami = "foo" - create_before_destroy = true + lifecycle { + create_before_destroy = true + } } resource "aws_instance" "bar" { ami = "foo" - create_before_destroy = false + lifecycle { + create_before_destroy = false + } } diff --git a/terraform/graph.go b/terraform/graph.go index afc439d25..f375b1f4b 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -534,7 +534,7 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error { // The dependency ordering depends on if the CreateBeforeDestroy // flag is enabled. If so, we must create the replacement first, // and then destroy the old instance. - if rn.Config != nil && rn.Config.CreateBeforeDestroy && !rd.Empty() { + if rn.Config != nil && rn.Config.Lifecycle.CreateBeforeDestroy && !rd.Empty() { dep := &depgraph.Dependency{ Name: n.Name, Source: newN, diff --git a/terraform/test-fixtures/graph-diff-create-before/main.tf b/terraform/test-fixtures/graph-diff-create-before/main.tf index 8d9dccd80..2cfe794d1 100644 --- a/terraform/test-fixtures/graph-diff-create-before/main.tf +++ b/terraform/test-fixtures/graph-diff-create-before/main.tf @@ -2,5 +2,7 @@ provider "aws" {} resource "aws_instance" "bar" { ami = "abc" - create_before_destroy = true + lifecycle { + create_before_destroy = true + } }