diff --git a/pkg/opentofu/schematotofu_test.go b/pkg/opentofu/schematotofu_test.go index 8611bfb..397b298 100644 --- a/pkg/opentofu/schematotofu_test.go +++ b/pkg/opentofu/schematotofu_test.go @@ -28,6 +28,9 @@ func TestSchemaToTofu(t *testing.T) { { name: "topleveldep", }, + { + name: "ifthenelse", + }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/opentofu/testdata/schemas/ifthenelse.json b/pkg/opentofu/testdata/schemas/ifthenelse.json new file mode 100644 index 0000000..443070f --- /dev/null +++ b/pkg/opentofu/testdata/schemas/ifthenelse.json @@ -0,0 +1,62 @@ +{ + "required": [ + "single" + ], + "properties": { + "single": { + "type": "string" + } + }, + "if": { + "properties": { + "single": { + "const": "foo" + } + } + }, + "then": { + "required": ["then"], + "properties": { + "then": { + "type": "object", + "required": ["double"], + "properties": { + "double": { + "type": "string" + } + }, + "if": { + "properties": { + "double": { + "const": "bar" + } + } + }, + "then": { + "required": ["another"], + "properties": { + "another": { + "type": "string" + } + } + }, + "else": { + "required": ["nested"], + "properties": { + "nested": { + "type": "string" + } + } + } + } + } + }, + "else": { + "required": ["else"], + "properties": { + "else": { + "type": "string" + } + } + } +} diff --git a/pkg/opentofu/testdata/schemas/ifthenelse.tf b/pkg/opentofu/testdata/schemas/ifthenelse.tf new file mode 100644 index 0000000..4b70a9f --- /dev/null +++ b/pkg/opentofu/testdata/schemas/ifthenelse.tf @@ -0,0 +1,15 @@ +variable "single" { + type = string +} +variable "then" { + type = object({ + double = string + another = optional(string) + nested = optional(string) + }) + default = null +} +variable "else" { + type = string + default = null +} diff --git a/pkg/schema/expand.go b/pkg/schema/expand.go index 1103a47..2dceea0 100644 --- a/pkg/schema/expand.go +++ b/pkg/schema/expand.go @@ -35,6 +35,15 @@ func ExpandProperties(schema *Schema) *orderedmap.OrderedMap[string, *Schema] { } } + if schema.If != nil { + if schema.Then != nil { + result = mergeProperties(result, ExpandProperties(schema.Then)) + } + if schema.Else != nil { + result = mergeProperties(result, ExpandProperties(schema.Else)) + } + } + return result }