Skip to content

Commit

Permalink
Adding ignore_updates lifecycle flag
Browse files Browse the repository at this point in the history
  • Loading branch information
robzienert committed Jun 27, 2015
1 parent 594f04e commit b728c62
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Resource struct {
type ResourceLifecycle struct {
CreateBeforeDestroy bool `mapstructure:"create_before_destroy"`
PreventDestroy bool `mapstructure:"prevent_destroy"`
IgnoreUpdates bool `mapstructure:"ignore_updates"`
}

// Provisioner is a configured provisioner step on a resource.
Expand Down
44 changes: 44 additions & 0 deletions config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,43 @@ func TestLoadFile_createBeforeDestroy(t *testing.T) {
}
}

func TestLoadFile_ignoreUpdates(t *testing.T) {
c, err := LoadFile(filepath.Join(fixtureDir, "ignore-updates.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(ignoreUpdatesResourcesStr) {
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 ignore updates
if !r.Lifecycle.IgnoreUpdates {
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 ignore updates
if r.Lifecycle.IgnoreUpdates {
t.Fatalf("Bad: %#v", r)
}
}

func TestLoad_preventDestroyString(t *testing.T) {
c, err := LoadFile(filepath.Join(fixtureDir, "prevent-destroy-string.tf"))
if err != nil {
Expand Down Expand Up @@ -676,3 +713,10 @@ aws_instance[bar] (x1)
aws_instance[web] (x1)
ami
`

const ignoreUpdatesResourcesStr = `
aws_instance[bar] (x1)
ami
aws_instance[web] (x1)
ami
`
13 changes: 13 additions & 0 deletions config/test-fixtures/ignore-updates.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "aws_instance" "web" {
ami = "foo"
lifecycle {
ignore_updates = true
}
}

resource "aws_instance" "bar" {
ami = "foo"
lifecycle {
ignore_updates = false
}
}
18 changes: 18 additions & 0 deletions terraform/transform_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,24 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
Then: EvalNoop{},
},

// We don't want to apply if the resource has the IgnoreUpdates lifecycle
&EvalIf{
If: func(ctx EvalContext) (bool, error) {
update := false
if diffApply != nil {
update = !diffApply.Empty()
}

ignoreUpdatesEnabled := n.Resource.Lifecycle.IgnoreUpdates && update

return ignoreUpdatesEnabled, nil
},
Then: &EvalWriteDiff{
Name: n.stateId(),
Diff: nil,
},
},

&EvalIf{
If: func(ctx EvalContext) (bool, error) {
destroy := false
Expand Down
6 changes: 6 additions & 0 deletions website/source/docs/configuration/resources.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ The `lifecycle` block allows the following keys to be set:
destruction of a given resource. When this is set to `true`, any plan
that includes a destroy of this resource will return an error message.

* `ignore_updates` (bool) - This flag is used to allow ignoring
external changes to a resource. As an example, this can be used
to ignore dynamic network interface assignments to a resource.

-------------

Within a resource, you can optionally have a **connection block**.
Expand Down Expand Up @@ -186,6 +190,8 @@ where `LIFECYCLE` is:
```
lifecycle {
[create_before_destroy = true|false]
[prevent_destroy = true|false]
[ignore_updates = true|false]
}
```

Expand Down

0 comments on commit b728c62

Please sign in to comment.