1+ function Convert-HCLVariablesToUserInputConfig {
2+ [CmdletBinding (SupportsShouldProcess = $true )]
3+ param (
4+ [Parameter (Mandatory = $false )]
5+ [string ] $targetVariableFile ,
6+
7+ [Parameter (Mandatory = $false )]
8+ [string ] $hclParserToolPath ,
9+
10+ [Parameter (Mandatory = $false )]
11+ [PSCustomObject ]$validators ,
12+
13+ [Parameter (Mandatory = $false )]
14+ [PSCustomObject ]$appendToObject = $null
15+ )
16+
17+ if ($PSCmdlet.ShouldProcess (" Parse HCL Variables into Config" , " modify" )) {
18+ $terraformVariables = & $hclParserToolPath $targetVariableFile | ConvertFrom-Json
19+
20+ $starterModuleConfiguration = [PSCustomObject ]@ {}
21+ if ($appendToObject -ne $null ) {
22+ $starterModuleConfiguration = $appendToObject
23+ }
24+
25+ foreach ($variable in $terraformVariables.variable.PSObject.Properties ) {
26+ $description = $variable.Value [0 ].description
27+ $validationTypeSplit = $description -split " \|"
28+
29+ $hasValidation = $false
30+ $order = 0
31+
32+ if ($validationTypeSplit.Length -gt 1 ) {
33+ $description = $validationTypeSplit [0 ].Trim()
34+ }
35+
36+ if ($validationTypeSplit.Length -eq 2 ) {
37+ $splitItem = $validationTypeSplit [1 ].Trim()
38+ if ($splitItem -match " ^\d+$" ) {
39+ $order = [convert ]::ToInt32($splitItem )
40+ } else {
41+ $validationType = $splitItem
42+ $hasValidation = $true
43+ }
44+ }
45+
46+ if ($validationTypeSplit.Length -eq 3 ) {
47+ $order = [convert ]::ToInt32($validationTypeSplit [1 ].Trim())
48+ $validationType = $validationTypeSplit [2 ].Trim()
49+ $hasValidation = $true
50+ }
51+
52+ if ($hasValidation -and $validationType -eq " hidden" ) {
53+ continue
54+ }
55+
56+ $inputType = " UserInput"
57+ if ($hasValidation -and $validationType -eq " hidden_azure_subscription_ids" ) {
58+ $inputType = " AzureSubscriptionIds"
59+ }
60+
61+ $dataType = $variable.Value [0 ].type
62+ $dataType = $dataType.Replace (" `$ {" , " " ).Replace(" }" , " " )
63+
64+ $starterModuleConfigurationInstance = [PSCustomObject ]@ {}
65+ $starterModuleConfigurationInstance | Add-Member - NotePropertyName " Order" - NotePropertyValue $order
66+ $starterModuleConfigurationInstance | Add-Member - NotePropertyName " Type" - NotePropertyValue $inputType
67+ $starterModuleConfigurationInstance | Add-Member - NotePropertyName " Value" - NotePropertyValue " "
68+ $starterModuleConfigurationInstance | Add-Member - NotePropertyName " DataType" - NotePropertyValue $dataType
69+
70+ if ($variable.Value [0 ].PSObject.Properties.Name -contains " default" ) {
71+ $defaultValue = $variable.Value [0 ].default
72+
73+ if ($variable.Value [0 ].default.GetType().Name -eq " Boolean" ) {
74+ $defaultValue = $variable.Value [0 ].default.ToString().ToLower()
75+ }
76+ if ($dataType -eq " list(string)" ) {
77+ $defaultValueRaw = $variable.Value [0 ].default
78+ $defaultValue = " "
79+ if ($defaultValue.Length -gt 0 ) {
80+ $join = $defaultValueRaw -join " `" ,`" "
81+ $defaultValue = " `" $join `" "
82+ }
83+ }
84+ $starterModuleConfigurationInstance | Add-Member - NotePropertyName " DefaultValue" - NotePropertyValue $defaultValue
85+ }
86+
87+ if ($hasValidation ) {
88+ $validator = $validators.PSObject.Properties [$validationType ].Value
89+ $description = " $description ($ ( $validator.Description ) )"
90+ if ($validator.Type -eq " AllowedValues" ){
91+ $starterModuleConfigurationInstance | Add-Member - NotePropertyName " AllowedValues" - NotePropertyValue $validator.AllowedValues
92+ }
93+ if ($validator.Type -eq " Valid" ){
94+ $starterModuleConfigurationInstance | Add-Member - NotePropertyName " Valid" - NotePropertyValue $validator.Valid
95+ }
96+ $starterModuleConfigurationInstance | Add-Member - NotePropertyName " Validator" - NotePropertyValue $validationType
97+ }
98+
99+ $starterModuleConfigurationInstance | Add-Member - NotePropertyName " Description" - NotePropertyValue $description
100+
101+ $starterModuleConfiguration | Add-Member - NotePropertyName $variable.Name - NotePropertyValue $starterModuleConfigurationInstance
102+ }
103+ }
104+
105+ return $starterModuleConfiguration
106+ }
0 commit comments