Add wildcard (match all) support to ignore_changes (#8599)

This commit is contained in:
Sander van Harmelen 2016-09-02 15:44:35 +02:00 committed by GitHub
parent 370be7df67
commit 47dd1ad153
11 changed files with 193 additions and 1 deletions

View File

@ -570,6 +570,15 @@ func (c *Config) Validate() error {
} }
} }
} }
// Verify ignore_changes contains valid entries
for _, v := range r.Lifecycle.IgnoreChanges {
if strings.Contains(v, "*") && v != "*" {
errs = append(errs, fmt.Errorf(
"%s: ignore_changes does not support using a partial string "+
"together with a wildcard: %s", n, v))
}
}
} }
for source, vs := range vars { for source, vs := range vars {

View File

@ -244,6 +244,20 @@ func TestConfigValidate_dupResource(t *testing.T) {
} }
} }
func TestConfigValidate_ignoreChanges(t *testing.T) {
c := testConfig(t, "validate-ignore-changes")
if err := c.Validate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestConfigValidate_ignoreChangesBad(t *testing.T) {
c := testConfig(t, "validate-ignore-changes-bad")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_moduleNameBad(t *testing.T) { func TestConfigValidate_moduleNameBad(t *testing.T) {
c := testConfig(t, "validate-module-name-bad") c := testConfig(t, "validate-module-name-bad")
if err := c.Validate(); err == nil { if err := c.Validate(); err == nil {

View File

@ -0,0 +1,21 @@
variable "foo" {
default = "ami-abcd1234"
}
variable "bar" {
default = "t2.micro"
}
provider "aws" {
access_key = "foo"
secret_key = "bar"
}
resource aws_instance "web" {
ami = "${var.foo}"
instance_type = "${var.bar}"
lifecycle {
ignore_changes = ["ami", "instance*"]
}
}

View File

@ -0,0 +1,21 @@
variable "foo" {
default = "ami-abcd1234"
}
variable "bar" {
default = "t2.micro"
}
provider "aws" {
access_key = "foo"
secret_key = "bar"
}
resource aws_instance "web" {
ami = "${var.foo}"
instance_type = "${var.bar}"
lifecycle {
ignore_changes = ["*"]
}
}

View File

@ -5034,6 +5034,47 @@ func TestContext2Apply_ignoreChangesWithDep(t *testing.T) {
} }
} }
func TestContext2Apply_ignoreChangesWildcard(t *testing.T) {
m := testModule(t, "apply-ignore-changes-wildcard")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
if p, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
} else {
t.Logf(p.String())
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
mod := state.RootModule()
if len(mod.Resources) != 1 {
t.Fatalf("bad: %s", state)
}
actual := strings.TrimSpace(state.String())
// Expect no changes from original state
expected := strings.TrimSpace(`
aws_instance.foo:
ID = foo
required_field = set
type = aws_instance
`)
if actual != expected {
t.Fatalf("expected:\n%s\ngot:\n%s", expected, actual)
}
}
// https://github.com/hashicorp/terraform/issues/7378 // https://github.com/hashicorp/terraform/issues/7378
func TestContext2Apply_destroyNestedModuleWithAttrsReferencingResource(t *testing.T) { func TestContext2Apply_destroyNestedModuleWithAttrsReferencingResource(t *testing.T) {
m := testModule(t, "apply-destroy-nested-module-with-attrs") m := testModule(t, "apply-destroy-nested-module-with-attrs")

View File

@ -2326,6 +2326,56 @@ func TestContext2Plan_ignoreChanges(t *testing.T) {
} }
} }
func TestContext2Plan_ignoreChangesWildcard(t *testing.T) {
m := testModule(t, "plan-ignore-changes-wildcard")
p := testProvider("aws")
p.DiffFn = testDiffFn
s := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.foo": &ResourceState{
Primary: &InstanceState{
ID: "bar",
Attributes: map[string]string{
"ami": "ami-abcd1234",
"instance_type": "t2.micro",
},
},
},
},
},
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
Variables: map[string]interface{}{
"foo": "ami-1234abcd",
"bar": "t2.small",
},
State: s,
})
plan, err := ctx.Plan()
if err != nil {
t.Fatalf("err: %s", err)
}
if len(plan.Diff.RootModule().Resources) > 0 {
t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources)
}
actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanIgnoreChangesWildcardStr)
if actual != expected {
t.Fatalf("bad:\n%s\n\nexpected\n\n%s", actual, expected)
}
}
func TestContext2Plan_moduleMapLiteral(t *testing.T) { func TestContext2Plan_moduleMapLiteral(t *testing.T) {
m := testModule(t, "plan-module-map-literal") m := testModule(t, "plan-module-map-literal")
p := testProvider("aws") p := testProvider("aws")

View File

@ -188,7 +188,7 @@ func (n *EvalDiff) processIgnoreChanges(diff *InstanceDiff) error {
ignorableAttrKeys := make(map[string]bool) ignorableAttrKeys := make(map[string]bool)
for _, ignoredKey := range ignoreChanges { for _, ignoredKey := range ignoreChanges {
for k := range diff.CopyAttributes() { for k := range diff.CopyAttributes() {
if strings.HasPrefix(k, ignoredKey) { if ignoredKey == "*" || strings.HasPrefix(k, ignoredKey) {
ignorableAttrKeys[k] = true ignorableAttrKeys[k] = true
} }
} }

View File

@ -1407,6 +1407,19 @@ aws_instance.foo:
ami = ami-abcd1234 ami = ami-abcd1234
` `
const testTerraformPlanIgnoreChangesWildcardStr = `
DIFF:
STATE:
aws_instance.foo:
ID = bar
ami = ami-abcd1234
instance_type = t2.micro
`
const testTerraformPlanComputedValueInMap = ` const testTerraformPlanComputedValueInMap = `
DIFF: DIFF:

View File

@ -0,0 +1,7 @@
resource "aws_instance" "foo" {
required_field = "set"
lifecycle {
ignore_changes = ["*"]
}
}

View File

@ -0,0 +1,12 @@
variable "foo" {}
variable "bar" {}
resource "aws_instance" "foo" {
ami = "${var.foo}"
instance_type = "${var.bar}"
lifecycle {
ignore_changes = ["*"]
}
}

View File

@ -83,6 +83,10 @@ include `create_before_destroy`. Referencing a resource that does not include
~> **NOTE on ignore\_changes:** Ignored attribute names can be matched by their ~> **NOTE on ignore\_changes:** Ignored attribute names can be matched by their
name, not state ID. For example, if an `aws_route_table` has two routes defined name, not state ID. For example, if an `aws_route_table` has two routes defined
and the `ignore_changes` list contains "route", both routes will be ignored. and the `ignore_changes` list contains "route", both routes will be ignored.
Additionally you can also use a single entry with a wildcard (e.g. `"*"`)
which will match all attribute names. Using a partial string together with a
wildcard (e.g. `"rout*"`) is **not** supported.
------------- -------------