Skip to content

Commit f20459e

Browse files
committed
feat: improve bicep templating
1 parent eb8c233 commit f20459e

File tree

7 files changed

+78
-32
lines changed

7 files changed

+78
-32
lines changed

src/ALZ/Private/Config-Helpers/Convert-BicepConfigToInputConfig.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function Convert-BicepConfigToInputConfig {
4444
$configItem | Add-Member -NotePropertyName "targets" -NotePropertyValue $variable.Value.targets
4545
}
4646

47+
$configItem | Add-Member -NotePropertyName "Sensitive" -NotePropertyValue $false
48+
4749
$configItem | Add-Member -NotePropertyName "Description" -NotePropertyValue $description
4850
$configItems | Add-Member -NotePropertyName $variable.Name -NotePropertyValue $configItem
4951
}

src/ALZ/Private/Config-Helpers/Convert-HCLVariablesToInputConfig.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ function Convert-HCLVariablesToInputConfig {
4141

4242
$configItem | Add-Member -NotePropertyName "Description" -NotePropertyValue $description
4343

44+
$sensitive = $false
45+
if ($variable.Value[0].PSObject.Properties.Name -contains "sensitive" -and $variable.Value[0].sensitive -eq $true) {
46+
$sensitive = $true
47+
Write-Verbose "Marking variable $($variable.Name) as sensitive..."
48+
}
49+
$configItem | Add-Member -NotePropertyName "Sensitive" -NotePropertyValue $sensitive
50+
4451
Write-Verbose "Adding variable $($variable.Name) to the configuration..."
4552
$configItems | Add-Member -NotePropertyName $variable.Name -NotePropertyValue $configItem
4653
}

src/ALZ/Private/Config-Helpers/Convert-ParametersToInputConfig.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ function Convert-ParametersToInputConfig {
1515
Write-Verbose "Alias $parameterAlias exists in input config, renaming..."
1616
$configItem = $inputConfig.PSObject.Properties | Where-Object { $_.Name -eq $parameterAlias }
1717
$inputConfig | Add-Member -NotePropertyName $parameterKey -NotePropertyValue @{
18-
Value = $configItem.Value.Value
19-
Source = $configItem.Value.Source
18+
Value = $configItem.Value.Value
19+
Source = $configItem.Value.Source
20+
Sensitive = $configItem.Value.Sensitive
2021
}
2122
$inputConfig.PSObject.Properties.Remove($configItem.Name)
2223
continue
@@ -38,8 +39,9 @@ function Convert-ParametersToInputConfig {
3839
}
3940
Write-Verbose "Adding parameter $parameterKey with value $variableValue"
4041
$inputConfig | Add-Member -NotePropertyName $parameterKey -NotePropertyValue @{
41-
Value = $variableValue
42-
Source = "parameter"
42+
Value = $variableValue
43+
Source = "parameter"
44+
Sensitive = $false
4345
}
4446
}
4547
}

src/ALZ/Private/Config-Helpers/Get-ALZConfig.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ function Get-ALZConfig {
5757

5858
foreach ($property in $config.PSObject.Properties) {
5959
$inputConfig | Add-Member -NotePropertyName $property.Name -NotePropertyValue @{
60-
Value = $property.Value
61-
Source = $extension
60+
Value = $property.Value
61+
Source = $extension
62+
Sensitive = $false
6263
}
6364
}
6465

src/ALZ/Private/Config-Helpers/Write-JsonFile.ps1

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ function Write-JsonFile {
55
[string] $jsonFilePath,
66

77
[Parameter(Mandatory = $false)]
8-
[PSObject] $configuration
8+
[PSObject[]] $configurations,
9+
10+
[Parameter(Mandatory = $false)]
11+
[switch] $all
912
)
1013

1114
if ($PSCmdlet.ShouldProcess("Download Terraform Tools", "modify")) {
@@ -16,10 +19,24 @@ function Write-JsonFile {
1619

1720
$environmentVariables = [ordered]@{}
1821

19-
foreach ($configKey in $configuration.PsObject.Properties | Sort-Object Name) {
20-
foreach ($target in $configKey.Value.Targets) {
21-
if ($target.Destination -eq "Environment") {
22-
$environmentVariables.$($target.Name) = $configKey.Value.Value
22+
foreach ($configuration in $configurations) {
23+
Write-Verbose "Processing configuration for JSON output to $($jsonFilePath)"
24+
foreach ($configKey in $configuration.PsObject.Properties | Sort-Object Name) {
25+
Write-Verbose "Processing configuration key $($configKey.Name) for $($jsonFilePath)"
26+
Write-Verbose "Configuration key value: $(ConvertTo-Json $configKey.Value -Depth 100)"
27+
if($configKey.Value.Sensitive) {
28+
Write-Verbose "Obfuscating sensitive configuration $($configKey.Name) from JSON output"
29+
$environmentVariables.$($configKey.Name) = "<sensitive>"
30+
continue
31+
}
32+
if($all) {
33+
$environmentVariables.$($configKey.Name) = $configKey.Value.Value
34+
continue
35+
}
36+
foreach ($target in $configKey.Value.Targets) {
37+
if ($target.Destination -eq "Environment") {
38+
$environmentVariables.$($target.Name) = $configKey.Value.Value
39+
}
2340
}
2441
}
2542
}

src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ function New-Bootstrap {
125125

126126
# Add the root module folder to bootstrap input config
127127
$inputConfig | Add-Member -NotePropertyName "root_module_folder_relative_path" -NotePropertyValue @{
128-
Value = $starterRootModuleFolder
129-
Source = "calculated"
128+
Value = $starterRootModuleFolder
129+
Source = "calculated"
130+
Sensitive = $false
130131
}
131132

132133
# Set the starter root module folder full path
@@ -146,6 +147,8 @@ function New-Bootstrap {
146147
$bootstrapParameters = Convert-HCLVariablesToInputConfig -targetVariableFile $terraformFile.FullName -hclParserToolPath $hclParserToolPath -appendToObject $bootstrapParameters
147148
}
148149

150+
Write-Verbose "Bootstrap Parameters before setting config: $(ConvertTo-Json $bootstrapParameters -Depth 100)"
151+
149152
# Getting the configuration for the starter module user input
150153
$starterParameters = [PSCustomObject]@{}
151154

@@ -165,19 +168,22 @@ function New-Bootstrap {
165168

166169
# Set computed inputs
167170
$inputConfig | Add-Member -NotePropertyName "module_folder_path" -NotePropertyValue @{
168-
Value = $starterModulePath
169-
Source = "calculated"
171+
Value = $starterModulePath
172+
Source = "calculated"
173+
Sensitive = $false
170174
}
171175
$inputConfig | Add-Member -NotePropertyName "availability_zones_bootstrap" -NotePropertyValue @{
172-
Value = @(Get-AvailabilityZonesSupport -region $inputConfig.bootstrap_location.Value -zonesSupport $zonesSupport)
173-
Source = "calculated"
176+
Value = @(Get-AvailabilityZonesSupport -region $inputConfig.bootstrap_location.Value -zonesSupport $zonesSupport)
177+
Source = "calculated"
178+
Sensitive = $false
174179
}
175180

176181
if ($inputConfig.PSObject.Properties.Name -contains "starter_location" -and $inputConfig.PSObject.Properties.Name -notcontains "starter_locations") {
177182
Write-Verbose "Converting starter_location $($inputConfig.starter_location.Value) to starter_locations..."
178183
$inputConfig | Add-Member -NotePropertyName "starter_locations" -NotePropertyValue @{
179-
Value = @($inputConfig.starter_location.Value)
180-
Source = "calculated"
184+
Value = @($inputConfig.starter_location.Value)
185+
Source = "calculated"
186+
Sensitive = $false
181187
}
182188
}
183189

@@ -187,8 +193,9 @@ function New-Bootstrap {
187193
$availabilityZonesStarter += , @(Get-AvailabilityZonesSupport -region $region -zonesSupport $zonesSupport)
188194
}
189195
$inputConfig | Add-Member -NotePropertyName "availability_zones_starter" -NotePropertyValue @{
190-
Value = $availabilityZonesStarter
191-
Source = "calculated"
196+
Value = $availabilityZonesStarter
197+
Source = "calculated"
198+
Sensitive = $false
192199
}
193200
}
194201

@@ -200,20 +207,23 @@ function New-Bootstrap {
200207
-configurationParameters $bootstrapParameters `
201208
-inputConfig $inputConfig
202209

210+
Write-Verbose "Final Bootstrap Parameters: $(ConvertTo-Json $bootstrapConfiguration -Depth 100)"
211+
203212
# Getting the input for the starter module
204213
Write-Verbose "Setting the configuration for the starter module..."
205214
$starterConfiguration = Set-Config `
206215
-configurationParameters $starterParameters `
207216
-inputConfig $inputConfig `
208217
-copyEnvVarToConfig
209218

210-
Write-Verbose "Final Starter Parameters: $(ConvertTo-Json $starterParameters -Depth 100)"
219+
Write-Verbose "Final Starter Parameters: $(ConvertTo-Json $starterConfiguration -Depth 100)"
211220

212221
# Creating the tfvars files for the bootstrap and starter module
213222
$tfVarsFileName = "terraform.tfvars.json"
214223
$bootstrapTfvarsPath = Join-Path -Path $bootstrapModulePath -ChildPath $tfVarsFileName
215224
$starterTfvarsPath = Join-Path -Path $starterRootModuleFolderPath -ChildPath "terraform.tfvars.json"
216225
$starterBicepVarsPath = Join-Path -Path $starterModulePath -ChildPath "parameters.json"
226+
$starterBicepAllVarsPath = Join-Path -Path $starterModulePath -ChildPath "parameters.all.json"
217227

218228
# Write the tfvars file for the bootstrap and starter module
219229
Write-TfvarsJsonFile -tfvarsFilePath $bootstrapTfvarsPath -configuration $bootstrapConfiguration
@@ -270,10 +280,12 @@ function New-Bootstrap {
270280
Set-ComputedConfiguration -configuration $starterConfiguration
271281
Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination $starterModulePath -configuration $starterConfiguration
272282
Write-JsonFile -jsonFilePath $starterBicepVarsPath -configuration $starterConfiguration
283+
Write-JsonFile -jsonFilePath $starterBicepAllVarsPath -configuration @($inputConfig, $starterConfiguration, $bootstrapConfiguration) -all
273284

274285
# Remove unrequired files
275286
$foldersOrFilesToRetain = $starterConfig.starter_modules.Value.$($inputConfig.starter_module_name.Value).folders_or_files_to_retain
276287
$foldersOrFilesToRetain += "parameters.json"
288+
$foldersOrFilesToRetain += "parameters.all.json"
277289
$foldersOrFilesToRetain += "config"
278290
$foldersOrFilesToRetain += ".config"
279291

src/ALZ/Public/Deploy-Accelerator.ps1

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,24 +354,29 @@ function Deploy-Accelerator {
354354

355355
# Set computed interface inputs
356356
$inputConfig | Add-Member -MemberType NoteProperty -Name "bicep_config_file_path" -Value @{
357-
Value = $starterConfigFilePath
358-
Source = "calculated"
357+
Value = $starterConfigFilePath
358+
Source = "calculated"
359+
Sensitive = $false
359360
}
360361
$inputConfig | Add-Member -MemberType NoteProperty -Name "on_demand_folder_repository" -Value @{
361-
Value = $starterModuleUrl
362-
Source = "calculated"
362+
Value = $starterModuleUrl
363+
Source = "calculated"
364+
Sensitive = $false
363365
}
364366
$inputConfig | Add-Member -MemberType NoteProperty -Name "on_demand_folder_artifact_name" -Value @{
365-
Value = $starterReleaseArtifactName
366-
Source = "calculated"
367+
Value = $starterReleaseArtifactName
368+
Source = "calculated"
369+
Sensitive = $false
367370
}
368371
$inputConfig | Add-Member -MemberType NoteProperty -Name "release_version" -Value @{
369-
Value = ($starterReleaseTag -eq "local" ? $inputConfig.starter_module_version.Value : $starterReleaseTag)
370-
Source = "calculated"
372+
Value = ($starterReleaseTag -eq "local" ? $inputConfig.starter_module_version.Value : $starterReleaseTag)
373+
Source = "calculated"
374+
Sensitive = $false
371375
}
372376
$inputConfig | Add-Member -MemberType NoteProperty -Name "time_stamp" -Value @{
373-
Value = (Get-Date).ToString("yyyy-MM-dd-HH-mm-ss")
374-
Source = "calculated"
377+
Value = (Get-Date).ToString("yyyy-MM-dd-HH-mm-ss")
378+
Source = "calculated"
379+
Sensitive = $false
375380
}
376381

377382
# Run the bootstrap

0 commit comments

Comments
 (0)