Skip to content

Commit f24c870

Browse files
Add diff suppress for time format to google_compute_resource_policies (#11486) (#8067)
[upstream:b0849f05edb147b9a81a577048d72e30caa51516] Signed-off-by: Modular Magician <[email protected]>
1 parent c841ea3 commit f24c870

File tree

4 files changed

+100
-6
lines changed

4 files changed

+100
-6
lines changed

.changelog/11486.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: fixed a permadiff caused by setting `start_time` in an incorrect H:mm format in `google_compute_resource_policies` resources
3+
```

google-beta/services/compute/resource_compute_resource_policy.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ import (
3232
"github.com/hashicorp/terraform-provider-google-beta/google-beta/verify"
3333
)
3434

35+
// Suppresses a diff on cases like 1:00 when it should be 01:00.
36+
// Because API will normalize this value
37+
func HourlyFormatSuppressDiff(_, old, new string, _ *schema.ResourceData) bool {
38+
return old == "0"+new
39+
}
40+
3541
func ResourceComputeResourcePolicy() *schema.Resource {
3642
return &schema.Resource{
3743
Create: resourceComputeResourcePolicyCreate,
@@ -220,9 +226,10 @@ from the tz database: http://en.wikipedia.org/wiki/Tz_database.`,
220226
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.`,
221227
},
222228
"start_time": {
223-
Type: schema.TypeString,
224-
Required: true,
225-
ValidateFunc: verify.ValidateHourlyOnly,
229+
Type: schema.TypeString,
230+
Required: true,
231+
ValidateFunc: verify.ValidateHourlyOnly,
232+
DiffSuppressFunc: HourlyFormatSuppressDiff,
226233
Description: `This must be in UTC format that resolves to one of
227234
00:00, 04:00, 08:00, 12:00, 16:00, or 20:00. For example,
228235
both 13:00-5 and 08:00 are valid.`,
@@ -244,9 +251,10 @@ both 13:00-5 and 08:00 are valid.`,
244251
Description: `The number of hours between snapshots.`,
245252
},
246253
"start_time": {
247-
Type: schema.TypeString,
248-
Required: true,
249-
ValidateFunc: verify.ValidateHourlyOnly,
254+
Type: schema.TypeString,
255+
Required: true,
256+
ValidateFunc: verify.ValidateHourlyOnly,
257+
DiffSuppressFunc: HourlyFormatSuppressDiff,
250258
Description: `Time within the window to start the operations.
251259
It must be in an hourly format "HH:MM",
252260
where HH : [00-23] and MM : [00] GMT. eg: 21:00`,

google-beta/services/compute/resource_compute_resource_policy_generated_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,48 @@ resource "google_compute_resource_policy" "foo" {
7272
`, context)
7373
}
7474

75+
func TestAccComputeResourcePolicy_resourcePolicyHourlyFormatExample(t *testing.T) {
76+
t.Parallel()
77+
78+
context := map[string]interface{}{
79+
"random_suffix": acctest.RandString(t, 10),
80+
}
81+
82+
acctest.VcrTest(t, resource.TestCase{
83+
PreCheck: func() { acctest.AccTestPreCheck(t) },
84+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
85+
CheckDestroy: testAccCheckComputeResourcePolicyDestroyProducer(t),
86+
Steps: []resource.TestStep{
87+
{
88+
Config: testAccComputeResourcePolicy_resourcePolicyHourlyFormatExample(context),
89+
},
90+
{
91+
ResourceName: "google_compute_resource_policy.foo",
92+
ImportState: true,
93+
ImportStateVerify: true,
94+
ImportStateVerifyIgnore: []string{"region"},
95+
},
96+
},
97+
})
98+
}
99+
100+
func testAccComputeResourcePolicy_resourcePolicyHourlyFormatExample(context map[string]interface{}) string {
101+
return acctest.Nprintf(`
102+
resource "google_compute_resource_policy" "foo" {
103+
name = "tf-test-gce-policy%{random_suffix}"
104+
region = "us-central1"
105+
snapshot_schedule_policy {
106+
schedule {
107+
daily_schedule {
108+
days_in_cycle = 1
109+
start_time = "4:00"
110+
}
111+
}
112+
}
113+
}
114+
`, context)
115+
}
116+
75117
func TestAccComputeResourcePolicy_resourcePolicyFullExample(t *testing.T) {
76118
t.Parallel()
77119

google-beta/services/compute/resource_compute_resource_policy_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,49 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1010
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
11+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/compute"
1112
)
1213

14+
// Value returned from the API will always be in format "HH:MM", so we need the suppress only on new values
15+
func TestHourlyFormatSuppressDiff(t *testing.T) {
16+
cases := map[string]struct {
17+
Old, New string
18+
ExpectDiffSuppress bool
19+
}{
20+
"Same value": {
21+
Old: "01:00",
22+
New: "01:00",
23+
ExpectDiffSuppress: false,
24+
},
25+
"Same value but different format": {
26+
Old: "01:00",
27+
New: "1:00",
28+
ExpectDiffSuppress: true,
29+
},
30+
"Changed value": {
31+
Old: "01:00",
32+
New: "02:00",
33+
ExpectDiffSuppress: false,
34+
},
35+
"Changed value but different format": {
36+
Old: "01:00",
37+
New: "2:00",
38+
ExpectDiffSuppress: false,
39+
},
40+
"Check interference with unaffected values": {
41+
Old: "11:00",
42+
New: "22:00",
43+
ExpectDiffSuppress: false,
44+
},
45+
}
46+
47+
for tn, tc := range cases {
48+
if compute.HourlyFormatSuppressDiff("", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
49+
t.Errorf("bad: %s, %q => %q expect DiffSuppress to return %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
50+
}
51+
}
52+
}
53+
1354
func TestAccComputeResourcePolicy_attached(t *testing.T) {
1455
t.Parallel()
1556

0 commit comments

Comments
 (0)