Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: complex maps not supported in terraform config #287

Merged
merged 1 commit into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ replace (

require (
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/aws/aws-sdk-go v1.39.6
github.com/aws/aws-sdk-go v1.40.0
github.com/google/go-cmp v0.5.6
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
golang.org/x/tools v0.1.4
golang.org/x/tools v0.1.5
k8s.io/api v0.21.0
k8s.io/apimachinery v0.21.0
k8s.io/client-go v0.21.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ github.com/aws/aws-sdk-go v1.31.12/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZve
github.com/aws/aws-sdk-go v1.34.30/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48=
github.com/aws/aws-sdk-go v1.35.24/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k=
github.com/aws/aws-sdk-go v1.38.29/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/aws/aws-sdk-go v1.39.6 h1:7NoOv+XsP/7A6Yaieg8r2+bnPD3ZTARa/5tHl8N8JKY=
github.com/aws/aws-sdk-go v1.39.6/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/aws/aws-sdk-go v1.40.0 h1:nTCSQAeahNt15SOYxuDwJ8XvMhOU3Uqe7eJUPv7+Vsk=
github.com/aws/aws-sdk-go v1.40.0/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -1560,8 +1560,8 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.4 h1:cVngSRcfgyZCzys3KYOpCFa+4dqX/Oub9tAq00ttGVs=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
44 changes: 44 additions & 0 deletions hack/gen-tf-code/templates/schema.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ var _ = Schema
{{- else if isList . -}}
List({{ template "schemaElem" .Elem }})
{{- else if isMap . -}}
{{- if or (isBool .Elem) (isInt .Elem) (isString .Elem) (isFloat .Elem) -}}
Map({{ template "schemaElem" .Elem }})
{{- else -}}
ComplexMap({{ template "schemaElem" .Elem }})
{{- end -}}
{{- else if isDuration . -}}
Duration()
{{- else if isQuantity . -}}
Expand Down Expand Up @@ -164,6 +168,7 @@ func (in interface{}) {{ .String }} {
return out
}(in)
{{- else if isMap . -}}
{{- if or (isBool .Elem) (isInt .Elem) (isString .Elem) (isFloat .Elem) -}}
func (in interface{}) map[string]{{ .Elem.String }} {
if in == nil {
return nil
Expand All @@ -179,6 +184,29 @@ func (in interface{}) map[string]{{ .Elem.String }} {
}
return nil
}(in)
{{- else -}}
func (in interface{}) map[string]{{ .Elem.String }} {
if in == nil {
return nil
}
if in, ok := in.([]interface{}); ok {
if len(in) > 0 {
out := {{ .String }}{}
for _, in := range in {
if in, ok := in.(map[string]interface{}); ok {
key := ExpandString(in["key"])
value := func(in interface{}) {{ .Elem.String }} {
return {{ template "expand" .Elem }}
}(in["value"])
out[key] = value
}
}
return out
}
}
return nil
}(in)
{{- end -}}
{{- else if isDuration . -}}
ExpandDuration(in)
{{- else if isQuantity . -}}
Expand Down Expand Up @@ -241,6 +269,7 @@ func (in {{ .String }}) []interface{} {
return out
}(in)
{{- else if isMap . -}}
{{- if or (isBool .Elem) (isInt .Elem) (isString .Elem) (isFloat .Elem) -}}
func (in map[string]{{ .Elem.String }}) map[string]interface{} {
if in == nil {
return nil
Expand All @@ -251,6 +280,21 @@ func (in map[string]{{ .Elem.String }}) map[string]interface{} {
}
return out
}(in)
{{- else -}}
func (in map[string]{{ .Elem.String }}) []interface{} {
if in == nil {
return nil
}
var out []interface{}
for key, in := range in {
out = append(out, map[string]interface{}{
"key": key,
"value": {{ template "flattenElem" .Elem }},
})
}
return out
}(in)
{{- end -}}
{{- else if isDuration . -}}
FlattenDuration(in)
{{- else if isQuantity . -}}
Expand Down
8 changes: 8 additions & 0 deletions hack/gen-tf-code/templates/test.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ nil
{{- else if isList . -}}
func() []interface{} { return nil }()
{{- else if isMap . -}}
{{- if or (isBool .Elem) (isInt .Elem) (isString .Elem) (isFloat .Elem) -}}
func() map[string]interface{} { return nil }()
{{- else -}}
func() []interface{} { return nil }()
{{- end -}}
{{- else if isDuration . -}}
""
{{- else if isQuantity . -}}
Expand All @@ -68,7 +72,11 @@ nil
{{- else if isList . -}}
func() []interface{} { return nil }()
{{- else if isMap . -}}
{{- if or (isBool .Elem) (isInt .Elem) (isString .Elem) (isFloat .Elem) -}}
func() map[string]interface{} { return nil }()
{{- else -}}
func() []interface{} { return nil }()
{{- end -}}
{{- else if isDuration . -}}
""
{{- else if isQuantity . -}}
Expand Down
53 changes: 31 additions & 22 deletions pkg/schemas/kops/DataSource_ClusterSpec.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func DataSourceClusterSpec() *schema.Resource {
"kubernetes_api_access": ComputedList(String()),
"isolate_masters": ComputedBool(),
"update_policy": ComputedString(),
"external_policies": ComputedMap(List(String())),
"external_policies": ComputedComplexMap(List(String())),
"additional_policies": ComputedMap(String()),
"file_assets": ComputedList(DataSourceFileAssetSpec()),
"etcd_cluster": ComputedList(DataSourceEtcdClusterSpec()),
Expand Down Expand Up @@ -348,20 +348,26 @@ func ExpandDataSourceClusterSpec(in map[string]interface{}) kops.ClusterSpec {
if in == nil {
return nil
}
if in, ok := in.(map[string]interface{}); ok {
if in, ok := in.([]interface{}); ok {
if len(in) > 0 {
out := map[string][]string{}
for key, in := range in {
out[key] = func(in interface{}) []string {
if in == nil {
return nil
}
var out []string
for _, in := range in.([]interface{}) {
out = append(out, string(ExpandString(in)))
}
return out
}(in)
for _, in := range in {
if in, ok := in.(map[string]interface{}); ok {
key := ExpandString(in["key"])
value := func(in interface{}) []string {
return func(in interface{}) []string {
if in == nil {
return nil
}
var out []string
for _, in := range in.([]interface{}) {
out = append(out, string(ExpandString(in)))
}
return out
}(in)
}(in["value"])
out[key] = value
}
}
return out
}
Expand Down Expand Up @@ -1225,19 +1231,22 @@ func FlattenDataSourceClusterSpecInto(in kops.ClusterSpec, out map[string]interf
return nil
}
return func(in map[string][]string) interface{} {
return func(in map[string][]string) map[string]interface{} {
return func(in map[string][]string) []interface{} {
if in == nil {
return nil
}
out := map[string]interface{}{}
var out []interface{}
for key, in := range in {
out[key] = func(in []string) []interface{} {
var out []interface{}
for _, in := range in {
out = append(out, FlattenString(string(in)))
}
return out
}(in)
out = append(out, map[string]interface{}{
"key": key,
"value": func(in []string) []interface{} {
var out []interface{}
for _, in := range in {
out = append(out, FlattenString(string(in)))
}
return out
}(in),
})
}
return out
}(in)
Expand Down
53 changes: 31 additions & 22 deletions pkg/schemas/kops/DataSource_ContainerdConfig.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func DataSourceContainerdConfig() *schema.Resource {
"config_override": ComputedString(),
"log_level": ComputedString(),
"packages": ComputedStruct(DataSourcePackagesConfig()),
"registry_mirrors": ComputedMap(List(String())),
"registry_mirrors": ComputedComplexMap(List(String())),
"root": ComputedString(),
"skip_install": ComputedBool(),
"state": ComputedString(),
Expand Down Expand Up @@ -113,20 +113,26 @@ func ExpandDataSourceContainerdConfig(in map[string]interface{}) kops.Containerd
if in == nil {
return nil
}
if in, ok := in.(map[string]interface{}); ok {
if in, ok := in.([]interface{}); ok {
if len(in) > 0 {
out := map[string][]string{}
for key, in := range in {
out[key] = func(in interface{}) []string {
if in == nil {
return nil
}
var out []string
for _, in := range in.([]interface{}) {
out = append(out, string(ExpandString(in)))
}
return out
}(in)
for _, in := range in {
if in, ok := in.(map[string]interface{}); ok {
key := ExpandString(in["key"])
value := func(in interface{}) []string {
return func(in interface{}) []string {
if in == nil {
return nil
}
var out []string
for _, in := range in.([]interface{}) {
out = append(out, string(ExpandString(in)))
}
return out
}(in)
}(in["value"])
out[key] = value
}
}
return out
}
Expand Down Expand Up @@ -241,19 +247,22 @@ func FlattenDataSourceContainerdConfigInto(in kops.ContainerdConfig, out map[str
}(in)
}(in.Packages)
out["registry_mirrors"] = func(in map[string][]string) interface{} {
return func(in map[string][]string) map[string]interface{} {
return func(in map[string][]string) []interface{} {
if in == nil {
return nil
}
out := map[string]interface{}{}
var out []interface{}
for key, in := range in {
out[key] = func(in []string) []interface{} {
var out []interface{}
for _, in := range in {
out = append(out, FlattenString(string(in)))
}
return out
}(in)
out = append(out, map[string]interface{}{
"key": key,
"value": func(in []string) []interface{} {
var out []interface{}
for _, in := range in {
out = append(out, FlattenString(string(in)))
}
return out
}(in),
})
}
return out
}(in)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestExpandDataSourceContainerdConfig(t *testing.T) {
"config_override": nil,
"log_level": nil,
"packages": nil,
"registry_mirrors": func() map[string]interface{} { return nil }(),
"registry_mirrors": func() []interface{} { return nil }(),
"root": nil,
"skip_install": false,
"state": nil,
Expand All @@ -51,7 +51,7 @@ func TestFlattenDataSourceContainerdConfigInto(t *testing.T) {
"config_override": nil,
"log_level": nil,
"packages": nil,
"registry_mirrors": func() map[string]interface{} { return nil }(),
"registry_mirrors": func() []interface{} { return nil }(),
"root": nil,
"skip_install": false,
"state": nil,
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestFlattenDataSourceContainerdConfig(t *testing.T) {
"config_override": nil,
"log_level": nil,
"packages": nil,
"registry_mirrors": func() map[string]interface{} { return nil }(),
"registry_mirrors": func() []interface{} { return nil }(),
"root": nil,
"skip_install": false,
"state": nil,
Expand Down
Loading