Skip to content

Commit ed70360

Browse files
Handling File Targeted Configuration and fixing Environment Computed Values (#39)
Adding Computed values to Environment, also adding file targeted config to ALZ-BICEP-CONFIG # Pull Request ## Issue Fixes #38 ## Description Handling 'Computed' values for the Environment file. (This needs a refactor of how we handle computed values, which I'll do after this change #40) Supporting file specific replacement values with configuration. ``` { Name = {Name of the setting in Bicep config or Environment} File = {FileName} Destination = {Parameters|Environment} } ``` ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license.
1 parent aab11e0 commit ed70360

5 files changed

+161
-7
lines changed

src/ALZ/Assets/alz-bicep-config/v0.14.1-pre.config.json

+34-5
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@
273273
"Description": "The identifier of the Identity Subscription. (e.g '00000000-0000-0000-0000-000000000000')",
274274
"Valid": "^( {){0,1}[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(}){0,1}$",
275275
"Targets": [
276-
{
276+
{
277277
"Name": "IDENTITY_SUBSCRIPTION_ID",
278278
"Destination": "Environment"
279279
}
@@ -310,7 +310,7 @@
310310
"Valid": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
311311
"Targets": [
312312
{
313-
"Name":"parPolicyAssignmentParameters.value.emailSecurityContact.value",
313+
"Name": "parPolicyAssignmentParameters.value.emailSecurityContact.value",
314314
"Destination": "Parameters"
315315
}
316316
],
@@ -327,7 +327,6 @@
327327
}
328328
]
329329
},
330-
331330
"LogAnalyticsResourceId": {
332331
"Type": "Computed",
333332
"Value": "/subscriptions/{%ManagementSubscriptionId%}/resourcegroups/alz-logging/providers/microsoft.operationalinsights/workspaces/alz-log-analytics",
@@ -384,7 +383,7 @@
384383
}
385384
]
386385
},
387-
"VirtualWanName":{
386+
"VirtualWanName": {
388387
"Type": "Computed",
389388
"Value": "alz-vwan-{%Location%}",
390389
"Targets": [
@@ -394,7 +393,7 @@
394393
}
395394
]
396395
},
397-
"FirewallPoliciesName":{
396+
"FirewallPoliciesName": {
398397
"Type": "Computed",
399398
"Value": "alz-azfwpolicy-{%Location%}",
400399
"Targets": [
@@ -453,6 +452,36 @@
453452
"Destination": "Environment"
454453
}
455454
]
455+
},
456+
"ConnectivityResourceGroupName": {
457+
"Type": "Computed",
458+
"Value": "rg-{%Prefix%}-connectivity",
459+
"Targets": [
460+
{
461+
"Name": "CONNECTIVITY_RESOURCE_GROUP",
462+
"Destination": "Environment"
463+
},
464+
{
465+
"File": "resourceGroupConnectivity.parameters.all.json",
466+
"Name": "parResourceGroupName.value",
467+
"Destination": "Parameters"
468+
}
469+
]
470+
},
471+
"LoggingResourceGroupName": {
472+
"Type": "Computed",
473+
"Value": "rg-{%Prefix%}-logging",
474+
"Targets": [
475+
{
476+
"Name": "LOGGING_RESOURCE_GROUP",
477+
"Destination": "Environment"
478+
},
479+
{
480+
"File": "resourceGroupLoggingAndSentinel.parameters.all.json",
481+
"Name": "parResourceGroupName.value",
482+
"Destination": "Parameters"
483+
}
484+
]
456485
}
457486
}
458487
}

src/ALZ/Private/Build-ALZDeploymentEnvFile.ps1

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ function Build-ALZDeploymentEnvFile {
2424
foreach ($configurationValue in $configuration.PsObject.Properties) {
2525
foreach ($target in $configurationValue.Value.Targets) {
2626
if ($target.Destination -eq "Environment") {
27-
Add-Content -Path $envFile -Value "$($($target.Name))=`"$($configurationValue.Value.Value)`"" | Out-String | Write-Verbose
27+
28+
$formattedValue = $configurationValue.Value.Value
29+
if ($configurationValue.Value.Type -eq "Computed") {
30+
$formattedValue = Format-TokenizedConfigurationString -tokenizedString $configurationValue.Value.Value -configuration $configuration
31+
}
32+
33+
Add-Content -Path $envFile -Value "$($($target.Name))=`"$formattedValue`"" | Out-String | Write-Verbose
2834
}
2935
}
3036
}

src/ALZ/Private/Edit-ALZConfigurationFilesInPlace.ps1

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ function Edit-ALZConfigurationFilesInPlace {
2525

2626
foreach ($configKey in $configuration.PsObject.Properties) {
2727
foreach ($target in $configKey.Value.Targets) {
28+
# Is this configuration value for this file?
29+
$targetedAtThisFile = $null -eq $target.File -or $target.File -eq $file.Name
30+
if ($targetedAtThisFile -eq $false) {
31+
continue
32+
}
2833

2934
# Find the appropriate item which will be changed in the Bicep file.
3035
# Remove array '[' ']' characters so we can use the index value direct.
@@ -56,7 +61,7 @@ function Edit-ALZConfigurationFilesInPlace {
5661

5762
} while (($null -ne $bicepConfigNode) -and ($index -lt $propertyNames.Length - 1))
5863

59-
# If we're here, we've got the object at the bottom of the hierarchy - and we can modify values on it.
64+
# If we're here, we can modify this file and we've got an actual object specified by the Name path value - and we can modify values on it.
6065
if ($target.Destination -eq "Parameters" -and $null -ne $bicepConfigNode) {
6166
$leafPropertyName = $propertyNames[-1]
6267

src/Tests/Unit/Private/Build-ALZDeploymentEnvFile.Tests.ps1

+32
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,38 @@ InModuleScope 'ALZ' {
7777
Should -Invoke New-Item -ParameterFilter { $Path -match ".env$" } -Scope It -Times 1 -Exactly
7878
Should -Invoke Add-Content -Scope It -Times 1 -Exactly
7979
}
80+
81+
It 'Handles Computed values correctly and adds to the .env file.' {
82+
83+
Mock -CommandName New-Item
84+
Mock -CommandName Add-Content
85+
86+
$configuration = [pscustomobject]@{
87+
Setting1 = [pscustomobject]@{
88+
Targets = @(
89+
[pscustomobject]@{
90+
Name = "Setting1"
91+
Destination = "Environment"
92+
})
93+
Value = "Test"
94+
}
95+
Setting2 = [pscustomobject]@{
96+
Targets = @(
97+
[pscustomobject]@{
98+
Name = "Setting2"
99+
Destination = "Environment"
100+
})
101+
Type = "Computed"
102+
Value = "{%Setting1%}"
103+
}
104+
}
105+
106+
Build-ALZDeploymentEnvFile -configuration $configuration -destination "test"
107+
108+
Should -Invoke New-Item -ParameterFilter { $Path -match ".env$" } -Scope It -Times 1 -Exactly
109+
Should -Invoke Add-Content -ParameterFilter { $Value -match "^Setting1=`"Test`"$" } -Scope It -Times 1 -Exactly
110+
Should -Invoke Add-Content -ParameterFilter { $Value -match "^Setting2=`"Test`"$" } -Scope It -Times 1 -Exactly
111+
}
80112
}
81113
}
82114
}

src/Tests/Unit/Private/Edit-ALZConfigurationFilesInPlace.Tests.ps1

+82
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,88 @@ InModuleScope 'ALZ' {
715715
-ParameterFilter { $FilePath -eq $testFile1Name -and $InputObject -eq $expectedContent } `
716716
-Scope It
717717
}
718+
719+
It 'Multiple files with file specific configuration should be changed correctly' {
720+
$defaultConfig = [pscustomobject]@{
721+
Value1 = [pscustomobject]@{
722+
Description = "The prefix that will be added to all resources created by this deployment."
723+
Targets = @(
724+
[pscustomobject]@{
725+
File = "test1.parameters.json"
726+
Name = "parCompanyPrefix.value"
727+
Destination = "Parameters"
728+
})
729+
Value = "value1"
730+
DefaultValue = "alz"
731+
}
732+
Value2 = [pscustomobject]@{
733+
Description = "The prefix that will be added to all resources created by this deployment."
734+
Targets = @(
735+
[pscustomobject]@{
736+
File = "test2.parameters.json"
737+
Name = "parCompanyPrefix.value"
738+
Destination = "Parameters"
739+
})
740+
Value = "value2"
741+
DefaultValue = "alz"
742+
}
743+
}
744+
745+
$firstFileContent = '{
746+
"parameters": {
747+
"parCompanyPrefix": {
748+
"value": ""
749+
},
750+
}
751+
}'
752+
$secondFileContent = '{
753+
"parameters": {
754+
"parCompanyPrefix": {
755+
"value": ""
756+
},
757+
}
758+
}'
759+
760+
Mock -CommandName Get-ChildItem -ParameterFilter { $Path -match 'config$' } -MockWith {
761+
@(
762+
[PSCustomObject]@{
763+
Name = 'test1.parameters.json'
764+
FullName = 'test1.parameters.json'
765+
},
766+
[PSCustomObject]@{
767+
Name = 'test2.parameters.json'
768+
FullName = 'test2.parameters.json'
769+
}
770+
)
771+
}
772+
773+
Mock -CommandName Get-Content -ParameterFilter { $Path -eq 'test1.parameters.json' } -MockWith {
774+
$firstFileContent
775+
}
776+
Mock -CommandName Get-Content -ParameterFilter { $Path -eq 'test2.parameters.json' } -MockWith {
777+
$secondFileContent
778+
}
779+
780+
Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination '.' -configuration $defaultConfig
781+
782+
Should -Invoke -CommandName Out-File -Scope It -Times 2
783+
784+
# Assert that the file was written back with the new values
785+
$contentAfterParsing = ConvertFrom-Json -InputObject $firstFileContent -AsHashtable
786+
$contentAfterParsing.parameters.parCompanyPrefix.value = 'value1'
787+
788+
$contentStringAfterParsing = ConvertTo-Json -InputObject $contentAfterParsing
789+
Write-InformationColored $contentStringAfterParsing -ForegroundColor Yellow -InformationAction Continue
790+
Should -Invoke -CommandName Out-File -ParameterFilter { $FilePath -eq "test1.parameters.json" -and $InputObject -eq $contentStringAfterParsing } -Scope It
791+
792+
$contentAfterParsing = ConvertFrom-Json -InputObject $secondFileContent -AsHashtable
793+
$contentAfterParsing.parameters.parCompanyPrefix.value = 'value2'
794+
795+
$contentStringAfterParsing = ConvertTo-Json -InputObject $contentAfterParsing
796+
Write-InformationColored $contentStringAfterParsing -ForegroundColor Yellow -InformationAction Continue
797+
Should -Invoke -CommandName Out-File -ParameterFilter { $FilePath -eq "test2.parameters.json" -and $InputObject -eq $contentStringAfterParsing } -Scope It
798+
799+
}
718800
}
719801
}
720802
}

0 commit comments

Comments
 (0)