From a681124301ccc635e014c60347dfaead33ff326b Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 16 Nov 2018 10:33:41 -0500 Subject: [PATCH] verify DiffSuppresFunc behavior Terraform used to provide empty diffs to the provider when calculating `ignore_changes`, which would cause some DiffSuppressFunc to fail, as can be seen in #18209. Verify that this is no longer the case in 0.12 --- .../providers/test/resource_diff_suppress.go | 53 +++++++++++-- .../test/resource_diff_suppress_test.go | 79 +++++++++++++++++++ 2 files changed, 127 insertions(+), 5 deletions(-) diff --git a/builtin/providers/test/resource_diff_suppress.go b/builtin/providers/test/resource_diff_suppress.go index 5c01a1d09d..cb5f7358f5 100644 --- a/builtin/providers/test/resource_diff_suppress.go +++ b/builtin/providers/test/resource_diff_suppress.go @@ -1,23 +1,36 @@ package test import ( + "fmt" + "math/rand" "strings" "github.com/hashicorp/terraform/helper/schema" ) func testResourceDiffSuppress() *schema.Resource { + diffSuppress := func(k, old, new string, d *schema.ResourceData) bool { + if old == "" || strings.Contains(new, "replace") { + return false + } + return true + } + return &schema.Resource{ Create: testResourceDiffSuppressCreate, Read: testResourceDiffSuppressRead, - Update: testResourceDiffSuppressUpdate, Delete: testResourceDiffSuppressDelete, + Update: testResourceDiffSuppressUpdate, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, Schema: map[string]*schema.Schema{ + "optional": { + Type: schema.TypeString, + Optional: true, + }, "val_to_upper": { Type: schema.TypeString, Required: true, @@ -29,18 +42,48 @@ func testResourceDiffSuppress() *schema.Resource { return strings.ToUpper(old) == strings.ToUpper(new) }, }, - "optional": { - Type: schema.TypeString, + "network": { + Type: schema.TypeString, + Optional: true, + Default: "default", + ForceNew: true, + DiffSuppressFunc: diffSuppress, + }, + "subnetwork": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + DiffSuppressFunc: diffSuppress, + }, + + "node_pool": { + Type: schema.TypeList, Optional: true, + Computed: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + }, }, }, } } func testResourceDiffSuppressCreate(d *schema.ResourceData, meta interface{}) error { - d.SetId("testId") + d.Set("network", "modified") + d.Set("subnetwork", "modified") - return testResourceRead(d, meta) + id := fmt.Sprintf("%x", rand.Int63()) + d.SetId(id) + return nil } func testResourceDiffSuppressRead(d *schema.ResourceData, meta interface{}) error { diff --git a/builtin/providers/test/resource_diff_suppress_test.go b/builtin/providers/test/resource_diff_suppress_test.go index 59490e3584..89416f32a1 100644 --- a/builtin/providers/test/resource_diff_suppress_test.go +++ b/builtin/providers/test/resource_diff_suppress_test.go @@ -1,10 +1,14 @@ package test import ( + "errors" "strings" "testing" + "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" ) func TestResourceDiffSuppress_create(t *testing.T) { @@ -45,3 +49,78 @@ resource "test_resource_diff_suppress" "foo" { }, }) } + +func TestResourceDiffSuppress_updateIgnoreChanges(t *testing.T) { + // None of these steps should replace the instance + id := "" + checkFunc := func(s *terraform.State) error { + root := s.ModuleByPath(addrs.RootModuleInstance) + res := root.Resources["test_resource_diff_suppress.foo"] + if id != "" && res.Primary.ID != id { + return errors.New("expected no resource replacement") + } + id = res.Primary.ID + return nil + } + + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_diff_suppress" "foo" { + val_to_upper = "foo" + + network = "foo" + subnetwork = "foo" + + node_pool { + name = "default-pool" + } + lifecycle { + ignore_changes = ["node_pool"] + } +} + `), + Check: checkFunc, + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_diff_suppress" "foo" { + val_to_upper = "foo" + + network = "ignored" + subnetwork = "ignored" + + node_pool { + name = "default-pool" + } + lifecycle { + ignore_changes = ["node_pool"] + } +} + `), + Check: checkFunc, + }, + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_diff_suppress" "foo" { + val_to_upper = "foo" + + network = "ignored" + subnetwork = "ignored" + + node_pool { + name = "ignored" + } + lifecycle { + ignore_changes = ["node_pool"] + } +} + `), + Check: checkFunc, + }, + }, + }) +}