Skip to content

Commit

Permalink
config: parse lifecycle block with mapstructure for weak decode
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jun 8, 2015
1 parent 66c51d4 commit aa1e66c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ type Resource struct {
// ResourceLifecycle is used to store the lifecycle tuning parameters
// to allow customized behavior
type ResourceLifecycle struct {
CreateBeforeDestroy bool `hcl:"create_before_destroy"`
PreventDestroy bool `hcl:"prevent_destroy"`
CreateBeforeDestroy bool `mapstructure:"create_before_destroy"`
PreventDestroy bool `mapstructure:"prevent_destroy"`
}

// Provisioner is a configured provisioner step on a resource.
Expand Down
13 changes: 11 additions & 2 deletions config/loader_hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/hcl"
hclobj "github.com/hashicorp/hcl/hcl"
"github.com/mitchellh/mapstructure"
)

// hclConfigurable is an implementation of configurable that knows
Expand Down Expand Up @@ -521,8 +522,16 @@ func loadResourcesHcl(os *hclobj.Object) ([]*Resource, error) {
// destroying the existing instance
var lifecycle ResourceLifecycle
if o := obj.Get("lifecycle", false); o != nil {
err = hcl.DecodeObject(&lifecycle, o)
if err != nil {
var raw map[string]interface{}
if err = hcl.DecodeObject(&raw, o); err != nil {
return nil, fmt.Errorf(
"Error parsing lifecycle for %s[%s]: %s",
t.Key,
k,
err)
}

if err := mapstructure.WeakDecode(raw, &lifecycle); err != nil {
return nil, fmt.Errorf(
"Error parsing lifecycle for %s[%s]: %s",
t.Key,
Expand Down
37 changes: 37 additions & 0 deletions config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,43 @@ func TestLoad_createBeforeDestroy(t *testing.T) {
}
}

func TestLoad_preventDestroyString(t *testing.T) {
c, err := Load(filepath.Join(fixtureDir, "prevent-destroy-string.tf"))
if err != nil {
t.Fatalf("err: %s", err)
}

if c == nil {
t.Fatal("config should not be nil")
}

actual := resourcesStr(c.Resources)
if actual != strings.TrimSpace(createBeforeDestroyResourcesStr) {
t.Fatalf("bad:\n%s", actual)
}

// Check for the flag value
r := c.Resources[0]
if r.Name != "web" && r.Type != "aws_instance" {
t.Fatalf("Bad: %#v", r)
}

// Should enable create before destroy
if !r.Lifecycle.PreventDestroy {
t.Fatalf("Bad: %#v", r)
}

r = c.Resources[1]
if r.Name != "bar" && r.Type != "aws_instance" {
t.Fatalf("Bad: %#v", r)
}

// Should not enable create before destroy
if r.Lifecycle.PreventDestroy {
t.Fatalf("Bad: %#v", r)
}
}

func TestLoad_temporary_files(t *testing.T) {
_, err := LoadDir(filepath.Join(fixtureDir, "dir-temporary-files"))
if err == nil {
Expand Down
10 changes: 10 additions & 0 deletions config/test-fixtures/prevent-destroy-string.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "aws_instance" "web" {
ami = "foo"
lifecycle {
prevent_destroy = "true"
}
}

resource "aws_instance" "bar" {
ami = "foo"
}

0 comments on commit aa1e66c

Please sign in to comment.