Skip to content

Add diff suppress for time format to google_compute_resource_policies #8067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
```
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