Skip to content

Commit 5c16337

Browse files
author
Samyak Rout
committed
feat: Add dynamic type handling for variables.tf template generation
- Integrated `formatType` function into the template rendering pipeline. - Updated `utils/file_utils.go` to include `formatType` in the function map. - Added support for dynamic type handling in `formatType`, covering: - Primitive types: string, number, bool - Complex types: list, map, object, and tuple - Modified `models.Variable` to include `Attributes` for object and tuple types. - Ensured backward compatibility with existing variable definitions. - Fixed template error: undefined function `formatType` in `variables.tf.tmpl`. Tested with: - Basic and complex variable types from `terraform-generator.json`. - Terraform generation flow for `resource_group` module.
1 parent cc9ea80 commit 5c16337

File tree

6 files changed

+48
-42
lines changed

6 files changed

+48
-42
lines changed

infra_as_code/terraform_generator/backend/models/config.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ type ModuleOutput struct {
5050
}
5151

5252
type Variable struct {
53-
Type string `json:"type"`
54-
Description string `json:"description"`
55-
Default interface{} `json:"default,omitempty"`
56-
Sensitive bool `json:"sensitive,omitempty"`
57-
Value interface{} `json:"value,omitempty"`
58-
Validation *Validation `json:"validation,omitempty"` // Add Validation field
53+
Type string `json:"type"`
54+
Description string `json:"description"`
55+
Default interface{} `json:"default,omitempty"`
56+
Sensitive bool `json:"sensitive,omitempty"`
57+
Value interface{} `json:"value,omitempty"`
58+
Attributes map[string]interface{} `json:"attributes,omitempty"` // Add attributes for object/tuple types
59+
Validation *Validation `json:"validation,omitempty"`
5960
}
6061

6162
type Validation struct {

infra_as_code/terraform_generator/backend/templates/azure/resource_group/variables.tf.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{{- range $name, $var := .Module.Variables }}
22
variable "{{ $name }}" {
33
description = "{{ or $var.Description "No description provided" }}"
4-
type = {{ $var.Type }}
4+
type = {{ formatType $var.Type $var.Attributes }}
55
{{- if $var.Default }}
6-
default = {{ formatDefault $var }}
6+
default = {{ formatDefault $var }}
77
{{- end }}
88
{{- if $var.Sensitive }}
9-
sensitive = true
9+
sensitive = true
1010
{{- end }}
1111
{{- if $var.Validation }}
1212
validation {

infra_as_code/terraform_generator/backend/templates/azure/vnet/variables.tf.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{{- range $name, $var := .Module.Variables }}
22
variable "{{ $name }}" {
33
description = "{{ or $var.Description "No description provided" }}"
4-
type = {{ $var.Type }}
4+
type = {{ formatType $var.Type $var.Attributes }}
55
{{- if $var.Default }}
6-
default = {{ formatDefault $var }}
6+
default = {{ formatDefault $var }}
77
{{- end }}
88
{{- if $var.Sensitive }}
9-
sensitive = true
9+
sensitive = true
1010
{{- end }}
1111
{{- if $var.Validation }}
1212
validation {

infra_as_code/terraform_generator/backend/templates/generic/variables.tf.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{{- range $name, $var := .Variables }}
22
variable "{{ $name }}" {
33
description = "{{ or $var.Description "No description provided" }}"
4-
type = {{ $var.Type }}
4+
type = {{ formatType $var.Type $var.Attributes }}
55
{{- if $var.Default }}
6-
default = {{ formatDefault $var }}
6+
default = {{ formatDefault $var }}
77
{{- end }}
88
{{- if $var.Sensitive }}
9-
sensitive = true
9+
sensitive = true
1010
{{- end }}
1111
{{- if $var.Validation }}
1212
validation {

infra_as_code/terraform_generator/backend/utils/file_utils.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,11 @@ func GenerateFileFromTemplate(templatePath, destinationPath string, data interfa
156156
case "bool", "number":
157157
return fmt.Sprintf("%v", value)
158158
case "string":
159-
// Determine if value is an expression or a literal
160159
expr, ok := value.(string)
161160
if ok && strings.HasPrefix(expr, "var.") {
162-
return expr // Expression
161+
return expr
163162
}
164-
return fmt.Sprintf("\"%v\"", value) // Literal
163+
return fmt.Sprintf("\"%v\"", value)
165164
case "list(string)", "set(string)":
166165
list, ok := value.([]interface{})
167166
if !ok {
@@ -188,35 +187,12 @@ func GenerateFileFromTemplate(templatePath, destinationPath string, data interfa
188187
}
189188
}
190189
return fmt.Sprintf("{ %s }", strings.Join(entries, ", "))
191-
case "object({ provision_vm_agent = bool, enable_automatic_upgrades = bool })",
192-
"object({ publisher = string, offer = string, sku = string, version = string })",
193-
"object({ name = string, caching = string, create_option = string, managed_disk_type = string })":
194-
// Assume value is an expression like var.os_profile_windows_config
195-
expr, ok := value.(string)
196-
if ok {
197-
return expr
198-
}
199-
return "{}" // Default to empty object if not an expression
200-
case "tuple":
201-
tuple, ok := value.([]interface{})
202-
if !ok {
203-
return "[]"
204-
}
205-
var items []string
206-
for _, item := range tuple {
207-
switch item.(type) {
208-
case string:
209-
items = append(items, fmt.Sprintf("\"%v\"", item))
210-
default:
211-
items = append(items, fmt.Sprintf("%v", item))
212-
}
213-
}
214-
return fmt.Sprintf("[%s]", strings.Join(items, ", "))
215190
default:
216191
return fmt.Sprintf("%v", value)
217192
}
218193
},
219194
"formatDefault": FormatDefault,
195+
"formatType": formatType, // Add formatType to the funcMap
220196
}
221197

222198
// Parse the template with the function map

infra_as_code/terraform_generator/backend/utils/terraform_utils.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,32 @@ func ExtractModuleVariables(module models.Module) map[string]interface{} {
8585
}
8686
return variables
8787
}
88+
89+
func formatType(varType string, attributes map[string]interface{}) string {
90+
switch varType {
91+
case "string", "number", "bool":
92+
return varType
93+
case "list(string)":
94+
return "list(string)"
95+
case "map(string)":
96+
return "map(string)"
97+
case "object":
98+
// Construct the object structure
99+
var fields []string
100+
for name, attrType := range attributes {
101+
fields = append(fields, fmt.Sprintf("%s = %s", name, attrType))
102+
}
103+
return fmt.Sprintf("object({ %s })", strings.Join(fields, ", "))
104+
case "tuple":
105+
// Construct the tuple structure
106+
var elements []string
107+
if tuple, ok := attributes["tuple_elements"].([]interface{}); ok {
108+
for _, elem := range tuple {
109+
elements = append(elements, fmt.Sprintf("%s", elem))
110+
}
111+
}
112+
return fmt.Sprintf("tuple([%s])", strings.Join(elements, ", "))
113+
default:
114+
return "any"
115+
}
116+
}

0 commit comments

Comments
 (0)