Skip to content

Commit

Permalink
Add diff suppress for time format to `google_compute_resource_policie…
Browse files Browse the repository at this point in the history
…s` (#11486) (#8067)

[upstream:b0849f05edb147b9a81a577048d72e30caa51516]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Aug 28, 2024
1 parent c841ea3 commit f24c870
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/11486.txt
Original file line number Diff line number Diff line change
@@ -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
```
20 changes: 14 additions & 6 deletions google-beta/services/compute/resource_compute_resource_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.`,
Expand All @@ -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`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit f24c870

Please sign in to comment.