-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathConvert-HCLVariablesToInputConfig.ps1
More file actions
78 lines (61 loc) · 3.23 KB
/
Convert-HCLVariablesToInputConfig.ps1
File metadata and controls
78 lines (61 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function Convert-HCLVariablesToInputConfig {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory = $false)]
[string] $targetVariableFile,
[Parameter(Mandatory = $false)]
[string] $hclParserToolPath,
[Parameter(Mandatory = $false)]
[PSCustomObject]$validators,
[Parameter(Mandatory = $false)]
[PSCustomObject]$appendToObject = $null
)
if ($PSCmdlet.ShouldProcess("Parse HCL Variables into Config", "modify")) {
$terraformVariables = & $hclParserToolPath $targetVariableFile | ConvertFrom-Json
if ($terraformVariables.PSObject.Properties.Name -notcontains "variable") {
Write-Verbose "No variables found in $targetVariableFile, skipping..."
return $appendToObject
}
Write-Verbose "Variables found in $targetVariableFile, processing..."
$configItems = [PSCustomObject]@{}
if ($appendToObject -ne $null) {
$configItems = $appendToObject
}
foreach ($variable in $terraformVariables.variable.PSObject.Properties) {
if ($variable.Value[0].PSObject.Properties.Name -contains "description") {
$description = $variable.Value[0].description
$validationTypeSplit = $description -split "\|"
$hasValidation = $false
if ($validationTypeSplit.Length -gt 1) {
$description = $validationTypeSplit[0].Trim()
}
if ($validationTypeSplit.Length -eq 2) {
$splitItem = $validationTypeSplit[1].Trim()
$validationType = $splitItem
$hasValidation = $true
}
}
$configItem = [PSCustomObject]@{}
$configItem | Add-Member -NotePropertyName "Value" -NotePropertyValue ""
$configItem | Add-Member -NotePropertyName "Source" -NotePropertyValue "input"
if ($variable.Value[0].PSObject.Properties.Name -contains "default") {
$configItem | Add-Member -NotePropertyName "DefaultValue" -NotePropertyValue $variable.Value[0].default
}
if ($hasValidation) {
$validator = $validators.PSObject.Properties[$validationType].Value
$description = "$description ($($validator.Description))"
if ($validator.Type -eq "AllowedValues") {
$configItem | Add-Member -NotePropertyName "AllowedValues" -NotePropertyValue $validator.AllowedValues
}
if ($validator.Type -eq "Valid") {
$configItem | Add-Member -NotePropertyName "Valid" -NotePropertyValue $validator.Valid
}
$configItem | Add-Member -NotePropertyName "Validator" -NotePropertyValue $validationType
}
$configItem | Add-Member -NotePropertyName "Description" -NotePropertyValue $description
Write-Verbose "Adding variable $($variable.Name) to the configuration..."
$configItems | Add-Member -NotePropertyName $variable.Name -NotePropertyValue $configItem
}
}
return $configItems
}