From ae44108f8f25d218505db4b40f866320aebbd4af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Fri, 6 Nov 2020 18:17:02 +0100 Subject: [PATCH] fix: options diff (added support to suppress diff) --- hack/gen-structures/main.go | 21 ++++++++++++++++++++- pkg/schemas/Cluster.generated.go | 4 ++-- pkg/schemas/zzz_utils.go | 17 ++++++++++++----- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/hack/gen-structures/main.go b/hack/gen-structures/main.go index 94cb1829..2aa79053 100644 --- a/hack/gen-structures/main.go +++ b/hack/gen-structures/main.go @@ -19,6 +19,7 @@ type options struct { computed []string computedOnly []string sensitive []string + suppressDiff []string } func excluded(excluded ...string) func(o options) options { @@ -56,6 +57,13 @@ func sensitive(sensitive ...string) func(o options) options { } } +func suppressDiff(suppressDiff ...string) func(o options) options { + return func(o options) options { + o.suppressDiff = append(o.suppressDiff, suppressDiff...) + return o + } +} + func schemaType(in reflect.Type) string { switch in.Kind() { case reflect.String: @@ -101,6 +109,7 @@ func funcMap(o options) template.FuncMap { computed := sets.NewString(o.computed...) computedOnly := sets.NewString(o.computedOnly...) sensitive := sets.NewString(o.sensitive...) + suppressDiff := sets.NewString(o.suppressDiff...) return template.FuncMap{ "fields": func(t reflect.Type) []reflect.StructField { var ret []reflect.StructField @@ -122,6 +131,9 @@ func funcMap(o options) template.FuncMap { "isSensitive": func(in string) bool { return sensitive.Has(in) }, + "suppressDiff": func(in string) bool { + return suppressDiff.Has(in) + }, "fieldName": fieldName, "isValueType": isValueType, "code": func(in string) string { @@ -212,6 +224,7 @@ IntOrString panic(err) } } + func buildSchema(t reflect.Type, o options) { tmplString := ` package {{ .Package }} @@ -286,7 +299,12 @@ func {{ .Name }}() *schema.Resource { Schema: map[string]*schema.Schema{ {{- range (fields .) }} {{- if not (has .Name $.Exclude) }} - {{ fieldName .Name | snakecase | quote }}: {{ template "prop" . }}, + {{ fieldName .Name | snakecase | quote }}: + {{- if suppressDiff .Name -}} + SuppressDiff({{ template "prop" . }}), + {{- else -}} + {{ template "prop" . }}, + {{- end -}} {{- end }} {{- end }} }, @@ -551,6 +569,7 @@ func main() { computed("MasterPublicName", "MasterInternalName", "ConfigBase", "NetworkCIDR", "NonMasqueradeCIDR", "IAM"), computedOnly("KubeConfig"), sensitive("AdminSshKey", "KubeConfig"), + suppressDiff("RollingUpdateOptions", "ValidateOptions"), ) build(api.RollingUpdateOptions{}) build(api.ValidateOptions{}) diff --git a/pkg/schemas/Cluster.generated.go b/pkg/schemas/Cluster.generated.go index e8984d3a..30beacf3 100644 --- a/pkg/schemas/Cluster.generated.go +++ b/pkg/schemas/Cluster.generated.go @@ -71,8 +71,8 @@ func Cluster() *schema.Resource { "rolling_update": OptionalStruct(RollingUpdate()), "instance_group": RequiredList(InstanceGroup()), "kube_config": Sensitive(ComputedStruct(KubeConfig())), - "rolling_update_options": OptionalStruct(RollingUpdateOptions()), - "validate_options": OptionalStruct(ValidateOptions()), + "rolling_update_options": SuppressDiff(OptionalStruct(RollingUpdateOptions())), + "validate_options": SuppressDiff(OptionalStruct(ValidateOptions())), }, } } diff --git a/pkg/schemas/zzz_utils.go b/pkg/schemas/zzz_utils.go index c52b2c15..dcb456e2 100644 --- a/pkg/schemas/zzz_utils.go +++ b/pkg/schemas/zzz_utils.go @@ -6,11 +6,6 @@ import ( // Tools -func Sensitive(s *schema.Schema) *schema.Schema { - s.Sensitive = true - return s -} - func Schema(t schema.ValueType, elem interface{}, required, optional, computed bool, maxItems int) *schema.Schema { return &schema.Schema{ Type: t, @@ -42,6 +37,18 @@ func SimpleOptionalComputed(t schema.ValueType) *schema.Schema { return Simple(t, false, true, true) } +func Sensitive(s *schema.Schema) *schema.Schema { + s.Sensitive = true + return s +} + +func SuppressDiff(s *schema.Schema) *schema.Schema { + s.DiffSuppressFunc = func(_, _, _ string, _ *schema.ResourceData) bool { + return true + } + return s +} + // Quantity func OptionalQuantity() *schema.Schema {