Skip to content

Commit 5f3070e

Browse files
committed
add diff suppress for empty network policy in container cluster
1 parent 97cc6fb commit 5f3070e

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

google/resource_container_cluster.go

+1
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ func resourceContainerCluster() *schema.Resource {
310310
},
311311
},
312312
},
313+
DiffSuppressFunc: emptyOrDefaultStringSuppress("PROVIDER_UNSPECIFIED"),
313314
},
314315

315316
"node_config": schemaNodeConfig,

google/utils.go

+6
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ func optionalPrefixSuppress(prefix string) schema.SchemaDiffSuppressFunc {
184184
}
185185
}
186186

187+
func emptyOrDefaultStringSuppress(defaultVal string) schema.SchemaDiffSuppressFunc {
188+
return func(k, old, new string, d *schema.ResourceData) bool {
189+
return (old == "" && new == defaultVal) || (new == "" && old == defaultVal)
190+
}
191+
}
192+
187193
func ipCidrRangeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
188194
// The range may be a:
189195
// A) single IP address (e.g. 10.2.3.4)

google/utils_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,43 @@ func TestDatasourceSchemaFromResourceSchema(t *testing.T) {
404404
})
405405
}
406406
}
407+
408+
func TestEmptyOrDefaultStringSuppress(t *testing.T) {
409+
testFunc := emptyOrDefaultStringSuppress("default value")
410+
411+
cases := map[string]struct {
412+
Old, New string
413+
ExpectDiffSupress bool
414+
}{
415+
"same value, format changed from empty to default": {
416+
Old: "",
417+
New: "default value",
418+
ExpectDiffSupress: true,
419+
},
420+
"same value, format changed from default to empty": {
421+
Old: "default value",
422+
New: "",
423+
ExpectDiffSupress: true,
424+
},
425+
"different value, format changed from empty to non-default": {
426+
Old: "",
427+
New: "not default new",
428+
ExpectDiffSupress: false,
429+
},
430+
"different value, format changed from non-default to empty": {
431+
Old: "not default old",
432+
New: "",
433+
ExpectDiffSupress: false,
434+
},
435+
"different value, format changed from non-default to non-default": {
436+
Old: "not default 1",
437+
New: "not default 2",
438+
ExpectDiffSupress: false,
439+
},
440+
}
441+
for tn, tc := range cases {
442+
if testFunc("", tc.Old, tc.New, nil) != tc.ExpectDiffSupress {
443+
t.Errorf("bad: %s, '%s' => '%s' expect DiffSuppress to return %t", tn, tc.Old, tc.New, tc.ExpectDiffSupress)
444+
}
445+
}
446+
}

0 commit comments

Comments
 (0)