Merge branch 'b-prevent-destroy-type'

This commit is contained in:
Mitchell Hashimoto 2015-06-23 22:29:54 -07:00
commit fafc32b183
4 changed files with 61 additions and 5 deletions

View File

@ -84,8 +84,8 @@ type Resource struct {
// ResourceLifecycle is used to store the lifecycle tuning parameters // ResourceLifecycle is used to store the lifecycle tuning parameters
// to allow customized behavior // to allow customized behavior
type ResourceLifecycle struct { type ResourceLifecycle struct {
CreateBeforeDestroy bool `hcl:"create_before_destroy"` CreateBeforeDestroy bool `mapstructure:"create_before_destroy"`
PreventDestroy bool `hcl:"prevent_destroy"` PreventDestroy bool `mapstructure:"prevent_destroy"`
} }
// Provisioner is a configured provisioner step on a resource. // Provisioner is a configured provisioner step on a resource.

View File

@ -6,6 +6,7 @@ import (
"github.com/hashicorp/hcl" "github.com/hashicorp/hcl"
hclobj "github.com/hashicorp/hcl/hcl" hclobj "github.com/hashicorp/hcl/hcl"
"github.com/mitchellh/mapstructure"
) )
// hclConfigurable is an implementation of configurable that knows // hclConfigurable is an implementation of configurable that knows
@ -521,8 +522,16 @@ func loadResourcesHcl(os *hclobj.Object) ([]*Resource, error) {
// destroying the existing instance // destroying the existing instance
var lifecycle ResourceLifecycle var lifecycle ResourceLifecycle
if o := obj.Get("lifecycle", false); o != nil { if o := obj.Get("lifecycle", false); o != nil {
err = hcl.DecodeObject(&lifecycle, o) var raw map[string]interface{}
if err != nil { 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( return nil, fmt.Errorf(
"Error parsing lifecycle for %s[%s]: %s", "Error parsing lifecycle for %s[%s]: %s",
t.Key, t.Key,

View File

@ -440,7 +440,44 @@ func TestLoadFile_createBeforeDestroy(t *testing.T) {
} }
} }
func TestLoadDir_temporary_files(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")) _, err := LoadDir(filepath.Join(fixtureDir, "dir-temporary-files"))
if err == nil { if err == nil {
t.Fatalf("Expected to see an error stating no config files found") t.Fatalf("Expected to see an error stating no config files found")

View File

@ -0,0 +1,10 @@
resource "aws_instance" "web" {
ami = "foo"
lifecycle {
prevent_destroy = "true"
}
}
resource "aws_instance" "bar" {
ami = "foo"
}