Skip to content

Commit

Permalink
fix: options diff (added support to suppress diff)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddycharly committed Nov 6, 2020
1 parent e15626e commit ae44108
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
21 changes: 20 additions & 1 deletion hack/gen-structures/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type options struct {
computed []string
computedOnly []string
sensitive []string
suppressDiff []string
}

func excluded(excluded ...string) func(o options) options {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -212,6 +224,7 @@ IntOrString
panic(err)
}
}

func buildSchema(t reflect.Type, o options) {
tmplString := `
package {{ .Package }}
Expand Down Expand Up @@ -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 }}
},
Expand Down Expand Up @@ -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{})
Expand Down
4 changes: 2 additions & 2 deletions pkg/schemas/Cluster.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
},
}
}
17 changes: 12 additions & 5 deletions pkg/schemas/zzz_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ae44108

Please sign in to comment.