Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6c37952

Browse filesBrowse files
authored
fix: Bug-132 (Azure#137)
# Pull Request ## Issue Fixes Azure#132 ## Description This pull request includes significant enhancements to the `Write-TfvarsFile` function in the `Config-Helpers` module. The changes improve the handling of different data types when writing configuration values, ensuring that default values are used when necessary and that the correct format is applied for lists, maps, and objects. ### Enhancements to `Write-TfvarsFile` function: * **Improved Handling of List Data Types**: - Added a check for empty or null values and applied default values or an empty list (`[]`) as needed. (`src/ALZ/Private/Config-Helpers/Write-TfvarsFile.ps1`) * **Added Support for Map Data Types**: - Introduced logic to handle `map(string)` data types, ensuring that empty maps are represented as `{}` and non-empty maps are properly formatted. (`src/ALZ/Private/Config-Helpers/Write-TfvarsFile.ps1`) * **Added Support for List of Objects Data Types**: - Implemented handling for `list(object)` data types, ensuring that empty lists are represented as `[]` and non-empty lists are correctly formatted with key-value pairs. (`src/ALZ/Private/Config-Helpers/Write-TfvarsFile.ps1`) ## Tests Test 1: With this inputs.yaml: ![image](https://github.com/user-attachments/assets/c687c5a0-5e14-4931-a1d3-c3f71f39973e) and this variables.tf: ![image](https://github.com/user-attachments/assets/f87d79d7-6627-48d6-a913-53f6aae3c2bb) Will result in this .tfvars: ![image](https://github.com/user-attachments/assets/3ade66a1-5186-4bfb-a2d4-f4d250dd7b0e) Test 2: With the same variables.tf file from example 1 and inputs.yaml file missing declarations for the complex types: ![image](https://github.com/user-attachments/assets/e0c08316-5e97-4616-9717-a24fd2ade132) Note: Although the logic to use the default values exists, there is currently an issue $configuration.PSObject.Properties where the defaultvalue is always empty even when a default value exists. Currently something I am looking into further. ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license.
1 parent e5cb20f commit 6c37952
Copy full SHA for 6c37952

File tree

Expand file treeCollapse file tree

1 file changed

+54
-6
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+54
-6
lines changed

‎src/ALZ/Private/Config-Helpers/Write-TfvarsFile.ps1

Copy file name to clipboardExpand all lines: src/ALZ/Private/Config-Helpers/Write-TfvarsFile.ps1
+54-6Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,71 @@ function Write-TfvarsFile {
1717
foreach($configurationProperty in $configuration.PSObject.Properties) {
1818
$configurationValueRaw = $configurationProperty.Value.Value
1919

20-
if($configurationProperty.Value.Validator -eq "configuration_file_path") {
20+
if ($configurationProperty.Value.Validator -eq "configuration_file_path") {
2121
$configurationValueRaw = [System.IO.Path]::GetFileName($configurationValueRaw)
2222
}
2323

2424
$configurationValue = "`"$($configurationValueRaw)`""
2525

26-
if($configurationProperty.Value.DataType -eq "list(string)") {
27-
if($configurationValueRaw -eq "") {
28-
$configurationValue = "[]"
26+
if ($configurationProperty.Value.DataType -eq "list(string)") {
27+
if (-not $configurationValueRaw -or $configurationValueRaw.Count -eq 0) {
28+
if ($configurationProperty.Value.DefaultValue) {
29+
$configurationValue = $configurationProperty.Value.DefaultValue
30+
} else {
31+
$configurationValue = "[]"
32+
}
2933
} else {
3034
$split = $configurationValueRaw -split ","
3135
$join = $split -join "`",`""
3236
$configurationValue = "[`"$join`"]"
3337
}
3438
}
3539

36-
if($configurationProperty.Value.DataType -eq "number" -or $configurationProperty.Value.DataType -eq "bool") {
40+
if ($configurationProperty.Value.DataType -eq "map(string)") {
41+
if (-not $configurationValueRaw -or $configurationValueRaw.Count -eq 0) {
42+
if ($configurationProperty.Value.DefaultValue) {
43+
$configurationValue = $configurationProperty.Value.DefaultValue
44+
} else {
45+
$configurationValue = "{}"
46+
}
47+
} else {
48+
$configurationValue = "{"
49+
$entries = @()
50+
51+
foreach ($key in $configurationValueRaw.Keys) {
52+
$value = $configurationValueRaw[$key]
53+
$entries += "`"$key`": `"$value`""
54+
}
55+
56+
$configurationValue = $entries -join ", "
57+
$configurationValue = "{ $configurationValue }"
58+
}
59+
}
60+
61+
if ($configurationProperty.Value.DataType -like "list(object*") {
62+
if (-not $configurationValueRaw -or $configurationValueRaw.Count -eq 0) {
63+
if ($configurationProperty.Value.DefaultValue) {
64+
$configurationValue = $configurationProperty.Value.DefaultValue
65+
} else {
66+
$configurationValue = "[]"
67+
}
68+
} else {
69+
$configurationValue = "["
70+
foreach ($entry in $configurationValueRaw) {
71+
$configurationValue += "{ "
72+
foreach ($key in $entry.Keys) {
73+
$value = $entry[$key]
74+
$configurationValue += "`"$key`": `"$value`", "
75+
}
76+
$configurationValue = $configurationValue.TrimEnd(", ")
77+
$configurationValue += "}, "
78+
}
79+
$configurationValue = $configurationValue.TrimEnd(", ")
80+
$configurationValue += "]"
81+
}
82+
}
83+
84+
if ($configurationProperty.Value.DataType -eq "number" -or $configurationProperty.Value.DataType -eq "bool") {
3785
$configurationValue = $configurationValueRaw
3886
} else {
3987
$configurationValue = $configurationValue.Replace("\", "\\")
@@ -46,4 +94,4 @@ function Write-TfvarsFile {
4694

4795
terraform -chdir="$tfvarsFolderPath" fmt | Out-String | Write-Verbose
4896
}
49-
}
97+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.