terraform: Adding lifecycle config block

This commit is contained in:
Armon Dadgar 2014-09-22 15:02:00 -07:00
parent aef7718778
commit 1aaddafba0
6 changed files with 37 additions and 25 deletions

View File

@ -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.

View File

@ -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,
})
}
}

View File

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

View File

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

View File

@ -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,

View File

@ -2,5 +2,7 @@ provider "aws" {}
resource "aws_instance" "bar" {
ami = "abc"
create_before_destroy = true
lifecycle {
create_before_destroy = true
}
}