Skip to content

Commit 31dd46d

Browse files
feat: improve bicep templating (#501)
# Pull Request ## Description Improve Bicep templating to provide all inputs as potential templating sources. This allows dynamically supplying inputs for templates without the need to specify them in the Bicep module config. Sensitive values are obfuscated. ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license.
1 parent eb8c233 commit 31dd46d

File tree

10 files changed

+264
-59
lines changed

10 files changed

+264
-59
lines changed

src/ALZ/ALZ.psd1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Included Cmdlets:
4040
- Deploy-Accelerator: Deploys the Azure Landing Zone accelerator to your Azure subscription.
4141
- Grant-SubscriptionCreatorRole: Grants the Subscription Creator role to a specified user or service principal.
4242
- Remove-PlatformLandingZone: Removes the deployed Azure Landing Zone from your Azure subscription
43+
- New-AcceleratorFolderStructure: Creates a new folder structure for the Azure Landing Zone accelerator with necessary configuration files.
4344
'@
4445

4546
CompatiblePSEditions = 'Core'
@@ -85,7 +86,8 @@ Included Cmdlets:
8586
'Test-AcceleratorRequirement',
8687
'Deploy-Accelerator',
8788
'Grant-SubscriptionCreatorRole',
88-
'Remove-PlatformLandingZone'
89+
'Remove-PlatformLandingZone',
90+
'New-AcceleratorFolderStructure'
8991
)
9092

9193
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.

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/Get-BootstrapAndStarterConfig.ps1

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,12 @@ function Get-BootstrapAndStarterConfig {
2222
$starterConfigFilePath = ""
2323

2424
$bootstrapDetails = $null
25-
$zonesSupport = $null
2625

2726
# Get the bootstrap configuration
2827
$bootstrapConfigFullPath = Join-Path $bootstrapPath $bootstrapConfigPath
2928
Write-Verbose "Bootstrap config path $bootstrapConfigFullPath"
3029
$bootstrapConfig = Get-ALZConfig -configFilePath $bootstrapConfigFullPath
3130

32-
# Get the supported regions and availability zones
33-
Write-Verbose "Getting Supported Regions and Availability Zones with Terraform"
34-
$regionsAndZones = Get-AzureRegionData -toolsPath $toolsPath
35-
Write-Verbose "Supported Regions: $($regionsAndZones.supportedRegions)"
36-
$zonesSupport = $regionsAndZones.zonesSupport
37-
3831
# Get the available bootstrap modules
3932
$bootstrapModules = $bootstrapConfig.bootstrap_modules.Value
4033

@@ -72,7 +65,6 @@ function Get-BootstrapAndStarterConfig {
7265
starterModuleSourceFolder = $starterModuleSourceFolder
7366
starterReleaseArtifactName = $starterReleaseArtifactName
7467
starterConfigFilePath = $starterConfigFilePath
75-
zonesSupport = $zonesSupport
7668
}
7769
}
7870
}

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ function New-Bootstrap {
3434
[Parameter(Mandatory = $false)]
3535
[switch] $destroy,
3636

37-
[Parameter(Mandatory = $false)]
38-
[PSCustomObject] $zonesSupport = $null,
39-
4037
[Parameter(Mandatory = $false, HelpMessage = "An extra level of logging that is turned off by default for easier debugging.")]
4138
[switch]
4239
$writeVerboseLogs,
@@ -125,8 +122,9 @@ function New-Bootstrap {
125122

126123
# Add the root module folder to bootstrap input config
127124
$inputConfig | Add-Member -NotePropertyName "root_module_folder_relative_path" -NotePropertyValue @{
128-
Value = $starterRootModuleFolder
129-
Source = "calculated"
125+
Value = $starterRootModuleFolder
126+
Source = "calculated"
127+
Sensitive = $false
130128
}
131129

132130
# Set the starter root module folder full path
@@ -146,6 +144,8 @@ function New-Bootstrap {
146144
$bootstrapParameters = Convert-HCLVariablesToInputConfig -targetVariableFile $terraformFile.FullName -hclParserToolPath $hclParserToolPath -appendToObject $bootstrapParameters
147145
}
148146

147+
Write-Verbose "Bootstrap Parameters before setting config: $(ConvertTo-Json $bootstrapParameters -Depth 100)"
148+
149149
# Getting the configuration for the starter module user input
150150
$starterParameters = [PSCustomObject]@{}
151151

@@ -165,30 +165,26 @@ function New-Bootstrap {
165165

166166
# Set computed inputs
167167
$inputConfig | Add-Member -NotePropertyName "module_folder_path" -NotePropertyValue @{
168-
Value = $starterModulePath
169-
Source = "calculated"
170-
}
171-
$inputConfig | Add-Member -NotePropertyName "availability_zones_bootstrap" -NotePropertyValue @{
172-
Value = @(Get-AvailabilityZonesSupport -region $inputConfig.bootstrap_location.Value -zonesSupport $zonesSupport)
173-
Source = "calculated"
168+
Value = $starterModulePath
169+
Source = "calculated"
170+
Sensitive = $false
174171
}
175172

176-
if ($inputConfig.PSObject.Properties.Name -contains "starter_location" -and $inputConfig.PSObject.Properties.Name -notcontains "starter_locations") {
177-
Write-Verbose "Converting starter_location $($inputConfig.starter_location.Value) to starter_locations..."
178-
$inputConfig | Add-Member -NotePropertyName "starter_locations" -NotePropertyValue @{
179-
Value = @($inputConfig.starter_location.Value)
180-
Source = "calculated"
181-
}
182-
}
173+
if ($iac -eq "bicep-classic" -and $inputConfig.PSObject.Properties.Name -contains "starter_locations") {
174+
# Get the supported regions and availability zones
175+
Write-Verbose "Getting Supported Regions and Availability Zones with Terraform"
176+
$regionsAndZones = Get-AzureRegionData -toolsPath $toolsPath
177+
Write-Verbose "Supported Regions: $($regionsAndZones.supportedRegions)"
178+
$zonesSupport = $regionsAndZones.zonesSupport
183179

184-
if ($inputConfig.PSObject.Properties.Name -contains "starter_locations") {
185180
$availabilityZonesStarter = @()
186181
foreach ($region in $inputConfig.starter_locations.Value) {
187182
$availabilityZonesStarter += , @(Get-AvailabilityZonesSupport -region $region -zonesSupport $zonesSupport)
188183
}
189184
$inputConfig | Add-Member -NotePropertyName "availability_zones_starter" -NotePropertyValue @{
190-
Value = $availabilityZonesStarter
191-
Source = "calculated"
185+
Value = $availabilityZonesStarter
186+
Source = "calculated"
187+
Sensitive = $false
192188
}
193189
}
194190

@@ -200,20 +196,25 @@ function New-Bootstrap {
200196
-configurationParameters $bootstrapParameters `
201197
-inputConfig $inputConfig
202198

199+
Write-Verbose "Final Bootstrap Parameters: $(ConvertTo-Json $bootstrapConfiguration -Depth 100)"
200+
203201
# Getting the input for the starter module
204202
Write-Verbose "Setting the configuration for the starter module..."
205203
$starterConfiguration = Set-Config `
206204
-configurationParameters $starterParameters `
207205
-inputConfig $inputConfig `
208206
-copyEnvVarToConfig
209207

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

212210
# Creating the tfvars files for the bootstrap and starter module
213211
$tfVarsFileName = "terraform.tfvars.json"
214212
$bootstrapTfvarsPath = Join-Path -Path $bootstrapModulePath -ChildPath $tfVarsFileName
215213
$starterTfvarsPath = Join-Path -Path $starterRootModuleFolderPath -ChildPath "terraform.tfvars.json"
216-
$starterBicepVarsPath = Join-Path -Path $starterModulePath -ChildPath "parameters.json"
214+
$starterBicepVarsFileName = "parameters.json"
215+
$starterBicepAllVarsFileName = "template-parameters.json"
216+
$starterBicepVarsPath = Join-Path -Path $starterModulePath -ChildPath $starterBicepVarsFileName
217+
$starterBicepAllVarsPath = Join-Path -Path $starterModulePath -ChildPath $starterBicepAllVarsFileName
217218

218219
# Write the tfvars file for the bootstrap and starter module
219220
Write-TfvarsJsonFile -tfvarsFilePath $bootstrapTfvarsPath -configuration $bootstrapConfiguration
@@ -270,10 +271,12 @@ function New-Bootstrap {
270271
Set-ComputedConfiguration -configuration $starterConfiguration
271272
Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination $starterModulePath -configuration $starterConfiguration
272273
Write-JsonFile -jsonFilePath $starterBicepVarsPath -configuration $starterConfiguration
274+
Write-JsonFile -jsonFilePath $starterBicepAllVarsPath -configuration @($inputConfig, $starterConfiguration, $bootstrapConfiguration) -all
273275

274276
# Remove unrequired files
275277
$foldersOrFilesToRetain = $starterConfig.starter_modules.Value.$($inputConfig.starter_module_name.Value).folders_or_files_to_retain
276-
$foldersOrFilesToRetain += "parameters.json"
278+
$foldersOrFilesToRetain += $starterBicepVarsFileName
279+
$foldersOrFilesToRetain += $starterBicepAllVarsFileName
277280
$foldersOrFilesToRetain += "config"
278281
$foldersOrFilesToRetain += ".config"
279282

0 commit comments

Comments
 (0)