Relax diff on maintenance_policy.daily_maintenance_window.start_time (#726)

* Relax diff on maintenance_policy.daily_maintenance_window.start_time

If the maintenance window has been set outside of Terraform to a time with a
single-digit hour (such as 1:00), and the terraform definition is set to the
same hour but with a leading zero as per validation (i.e. 01:00), do not
consider the time to be changed (as we currently don't support update on this
property).

Fixes #719

* Generalise rfc3339TimeDiffSuppress and add more test cases
This commit is contained in:
Michael Bannister 2017-11-13 19:30:26 +00:00 committed by Vincent Roseberry
parent df247070e0
commit ee641e0b1f
3 changed files with 57 additions and 4 deletions

View File

@ -187,10 +187,11 @@ func resourceContainerCluster() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_time": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateRFC3339Time,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateRFC3339Time,
DiffSuppressFunc: rfc3339TimeDiffSuppress,
},
"duration": {
Type: schema.TypeString,

View File

@ -293,6 +293,15 @@ func portRangeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
return false
}
// Single-digit hour is equivalent to hour with leading zero e.g. suppress diff 1:00 => 01:00.
// Assume either value could be in either format.
func rfc3339TimeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
if (len(old) == 4 && "0"+old == new) || (len(new) == 4 && "0"+new == old) {
return true
}
return false
}
// expandLabels pulls the value of "labels" out of a schema.ResourceData as a map[string]string.
func expandLabels(d *schema.ResourceData) map[string]string {
return expandStringMap(d, "labels")

View File

@ -93,3 +93,46 @@ func TestIpCidrRangeDiffSuppress(t *testing.T) {
}
}
}
func TestRfc3339TimeDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSupress bool
}{
"same time, format changed to have leading zero": {
Old: "2:00",
New: "02:00",
ExpectDiffSupress: true,
},
"same time, format changed not to have leading zero": {
Old: "02:00",
New: "2:00",
ExpectDiffSupress: true,
},
"different time, both without leading zero": {
Old: "2:00",
New: "3:00",
ExpectDiffSupress: false,
},
"different time, old with leading zero, new without": {
Old: "02:00",
New: "3:00",
ExpectDiffSupress: false,
},
"different time, new with leading zero, oldwithout": {
Old: "2:00",
New: "03:00",
ExpectDiffSupress: false,
},
"different time, both with leading zero": {
Old: "02:00",
New: "03:00",
ExpectDiffSupress: false,
},
}
for tn, tc := range cases {
if rfc3339TimeDiffSuppress("time", tc.Old, tc.New, nil) != tc.ExpectDiffSupress {
t.Errorf("bad: %s, '%s' => '%s' expect DiffSuppress to return %t", tn, tc.Old, tc.New, tc.ExpectDiffSupress)
}
}
}