From f24c8708a9bb5147b922946f6cbca35fba289634 Mon Sep 17 00:00:00 2001 From: The Magician Date: Wed, 28 Aug 2024 09:09:46 -0700 Subject: [PATCH] Add diff suppress for time format to `google_compute_resource_policies` (#11486) (#8067) [upstream:b0849f05edb147b9a81a577048d72e30caa51516] Signed-off-by: Modular Magician --- .changelog/11486.txt | 3 ++ .../resource_compute_resource_policy.go | 20 ++++++--- ..._compute_resource_policy_generated_test.go | 42 +++++++++++++++++++ .../resource_compute_resource_policy_test.go | 41 ++++++++++++++++++ 4 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 .changelog/11486.txt diff --git a/.changelog/11486.txt b/.changelog/11486.txt new file mode 100644 index 0000000000..4ebdb68b8a --- /dev/null +++ b/.changelog/11486.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +compute: fixed a permadiff caused by setting `start_time` in an incorrect H:mm format in `google_compute_resource_policies` resources +``` \ No newline at end of file diff --git a/google-beta/services/compute/resource_compute_resource_policy.go b/google-beta/services/compute/resource_compute_resource_policy.go index bb0d363293..a655bc3469 100644 --- a/google-beta/services/compute/resource_compute_resource_policy.go +++ b/google-beta/services/compute/resource_compute_resource_policy.go @@ -32,6 +32,12 @@ import ( "github.com/hashicorp/terraform-provider-google-beta/google-beta/verify" ) +// Suppresses a diff on cases like 1:00 when it should be 01:00. +// Because API will normalize this value +func HourlyFormatSuppressDiff(_, old, new string, _ *schema.ResourceData) bool { + return old == "0"+new +} + func ResourceComputeResourcePolicy() *schema.Resource { return &schema.Resource{ Create: resourceComputeResourcePolicyCreate, @@ -220,9 +226,10 @@ from the tz database: http://en.wikipedia.org/wiki/Tz_database.`, Description: `Defines a schedule with units measured in days. The value determines how many days pass between the start of each cycle. Days in cycle for snapshot schedule policy must be 1.`, }, "start_time": { - Type: schema.TypeString, - Required: true, - ValidateFunc: verify.ValidateHourlyOnly, + Type: schema.TypeString, + Required: true, + ValidateFunc: verify.ValidateHourlyOnly, + DiffSuppressFunc: HourlyFormatSuppressDiff, Description: `This must be in UTC format that resolves to one of 00:00, 04:00, 08:00, 12:00, 16:00, or 20:00. For example, both 13:00-5 and 08:00 are valid.`, @@ -244,9 +251,10 @@ both 13:00-5 and 08:00 are valid.`, Description: `The number of hours between snapshots.`, }, "start_time": { - Type: schema.TypeString, - Required: true, - ValidateFunc: verify.ValidateHourlyOnly, + Type: schema.TypeString, + Required: true, + ValidateFunc: verify.ValidateHourlyOnly, + DiffSuppressFunc: HourlyFormatSuppressDiff, Description: `Time within the window to start the operations. It must be in an hourly format "HH:MM", where HH : [00-23] and MM : [00] GMT. eg: 21:00`, diff --git a/google-beta/services/compute/resource_compute_resource_policy_generated_test.go b/google-beta/services/compute/resource_compute_resource_policy_generated_test.go index 0b2e6ad0bd..e1f820c410 100644 --- a/google-beta/services/compute/resource_compute_resource_policy_generated_test.go +++ b/google-beta/services/compute/resource_compute_resource_policy_generated_test.go @@ -72,6 +72,48 @@ resource "google_compute_resource_policy" "foo" { `, context) } +func TestAccComputeResourcePolicy_resourcePolicyHourlyFormatExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckComputeResourcePolicyDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeResourcePolicy_resourcePolicyHourlyFormatExample(context), + }, + { + ResourceName: "google_compute_resource_policy.foo", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"region"}, + }, + }, + }) +} + +func testAccComputeResourcePolicy_resourcePolicyHourlyFormatExample(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_compute_resource_policy" "foo" { + name = "tf-test-gce-policy%{random_suffix}" + region = "us-central1" + snapshot_schedule_policy { + schedule { + daily_schedule { + days_in_cycle = 1 + start_time = "4:00" + } + } + } +} +`, context) +} + func TestAccComputeResourcePolicy_resourcePolicyFullExample(t *testing.T) { t.Parallel() diff --git a/google-beta/services/compute/resource_compute_resource_policy_test.go b/google-beta/services/compute/resource_compute_resource_policy_test.go index afd1eb6702..e56741f65b 100644 --- a/google-beta/services/compute/resource_compute_resource_policy_test.go +++ b/google-beta/services/compute/resource_compute_resource_policy_test.go @@ -8,8 +8,49 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/services/compute" ) +// Value returned from the API will always be in format "HH:MM", so we need the suppress only on new values +func TestHourlyFormatSuppressDiff(t *testing.T) { + cases := map[string]struct { + Old, New string + ExpectDiffSuppress bool + }{ + "Same value": { + Old: "01:00", + New: "01:00", + ExpectDiffSuppress: false, + }, + "Same value but different format": { + Old: "01:00", + New: "1:00", + ExpectDiffSuppress: true, + }, + "Changed value": { + Old: "01:00", + New: "02:00", + ExpectDiffSuppress: false, + }, + "Changed value but different format": { + Old: "01:00", + New: "2:00", + ExpectDiffSuppress: false, + }, + "Check interference with unaffected values": { + Old: "11:00", + New: "22:00", + ExpectDiffSuppress: false, + }, + } + + for tn, tc := range cases { + if compute.HourlyFormatSuppressDiff("", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress { + t.Errorf("bad: %s, %q => %q expect DiffSuppress to return %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress) + } + } +} + func TestAccComputeResourcePolicy_attached(t *testing.T) { t.Parallel()