add diff suppress for empty network policy in container cluster

This commit is contained in:
Emily Ye 2018-01-31 14:36:03 -08:00
parent 97cc6fb404
commit 5f3070e223
3 changed files with 47 additions and 0 deletions

View File

@ -310,6 +310,7 @@ func resourceContainerCluster() *schema.Resource {
},
},
},
DiffSuppressFunc: emptyOrDefaultStringSuppress("PROVIDER_UNSPECIFIED"),
},
"node_config": schemaNodeConfig,

View File

@ -184,6 +184,12 @@ func optionalPrefixSuppress(prefix string) schema.SchemaDiffSuppressFunc {
}
}
func emptyOrDefaultStringSuppress(defaultVal string) schema.SchemaDiffSuppressFunc {
return func(k, old, new string, d *schema.ResourceData) bool {
return (old == "" && new == defaultVal) || (new == "" && old == defaultVal)
}
}
func ipCidrRangeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
// The range may be a:
// A) single IP address (e.g. 10.2.3.4)

View File

@ -404,3 +404,43 @@ func TestDatasourceSchemaFromResourceSchema(t *testing.T) {
})
}
}
func TestEmptyOrDefaultStringSuppress(t *testing.T) {
testFunc := emptyOrDefaultStringSuppress("default value")
cases := map[string]struct {
Old, New string
ExpectDiffSupress bool
}{
"same value, format changed from empty to default": {
Old: "",
New: "default value",
ExpectDiffSupress: true,
},
"same value, format changed from default to empty": {
Old: "default value",
New: "",
ExpectDiffSupress: true,
},
"different value, format changed from empty to non-default": {
Old: "",
New: "not default new",
ExpectDiffSupress: false,
},
"different value, format changed from non-default to empty": {
Old: "not default old",
New: "",
ExpectDiffSupress: false,
},
"different value, format changed from non-default to non-default": {
Old: "not default 1",
New: "not default 2",
ExpectDiffSupress: false,
},
}
for tn, tc := range cases {
if testFunc("", 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)
}
}
}