Skip to content

Commit 7205552

Browse files
Refactor how Computed values are calculated. (#44)
# Pull Request ## Issue #40 ## Description The previous implementation had computed calculations happening in several places, that was quite bad(tm). Making the calculation of 'Computed' values it's own step after values are calculated making things easier to test. ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license.
1 parent 9b2aa2a commit 7205552

8 files changed

+200
-302
lines changed

src/ALZ/Private/Build-ALZDeploymentEnvFile.ps1

-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ function Build-ALZDeploymentEnvFile {
2626
if ($target.Destination -eq "Environment") {
2727

2828
$formattedValue = $configurationValue.Value.Value
29-
if ($configurationValue.Value.Type -eq "Computed") {
30-
$formattedValue = Format-TokenizedConfigurationString -tokenizedString $configurationValue.Value.Value -configuration $configuration
31-
}
32-
3329
Add-Content -Path $envFile -Value "$($($target.Name))=`"$formattedValue`"" | Out-String | Write-Verbose
3430
}
3531
}

src/ALZ/Private/Edit-ALZConfigurationFilesInPlace.ps1

+1-31
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,7 @@ function Edit-ALZConfigurationFilesInPlace {
6464
# 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.
6565
if ($target.Destination -eq "Parameters" -and $null -ne $bicepConfigNode) {
6666
$leafPropertyName = $propertyNames[-1]
67-
68-
if ($configKey.Value.Type -eq "Computed") {
69-
# If the value type is computed we replace the value with another which already exists in the configuration hierarchy.
70-
if ($configKey.Value.Value -is [array]) {
71-
$formattedValues = @()
72-
foreach($formatString in $configKey.Value.Value) {
73-
$formattedValues += Format-TokenizedConfigurationString -tokenizedString $formatString -configuration $configuration
74-
}
75-
76-
if ($null -ne $configKey.Value.Process) {
77-
$scriptBlock = [ScriptBlock]::Create($configKey.Value.Process)
78-
$formattedValues = Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $formattedValues
79-
$formattedValues = @($formattedValues)
80-
}
81-
82-
$bicepConfigNode[$leafPropertyName] = $formattedValues
83-
} else {
84-
85-
$formattedValue = Format-TokenizedConfigurationString -tokenizedString $configKey.Value.Value -configuration $configuration
86-
87-
if ($null -ne $configKey.Value.Process) {
88-
$scriptBlock = [ScriptBlock]::Create($configKey.Value.Process)
89-
$formattedValue = Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $formattedValue
90-
}
91-
92-
$bicepConfigNode[$leafPropertyName] = $formattedValue
93-
}
94-
} else {
95-
$bicepConfigNode[$leafPropertyName] = $configKey.Value.Value
96-
}
97-
67+
$bicepConfigNode[$leafPropertyName] = $configKey.Value.Value
9868
$modified = $true
9969
}
10070
}

src/ALZ/Private/New-ALZDirectoryEnvironment.ps1

-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ function New-ALZDirectoryEnvironment {
1818
New-Item -ItemType Directory -Path $config -Force | Out-String | Write-Verbose
1919
New-Item -ItemType Directory -Path $upstream -Force | Out-String | Write-Verbose
2020
New-Item -ItemType Directory -Path $configModules -Force | Out-String | Write-Verbose
21-
2221
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# If the value type is computed we replace the value with another which already exists in the configuration hierarchy.
2+
function Set-ComputedConfiguration {
3+
[CmdletBinding(SupportsShouldProcess = $true)]
4+
param (
5+
[Parameter(Mandatory = $true)]
6+
[PSCustomObject] $configuration
7+
)
8+
9+
if ($PSCmdlet.ShouldProcess("ALZ-Bicep computed configuration.", "calculate computed values")) {
10+
foreach ($configKey in $configuration.PsObject.Properties) {
11+
if ($configKey.Value.Type -ne "Computed") {
12+
continue;
13+
}
14+
15+
if ($configKey.Value.Value -is [array]) {
16+
$formattedValues = @()
17+
foreach($formatString in $configKey.Value.Value) {
18+
$formattedValues += Format-TokenizedConfigurationString -tokenizedString $formatString -configuration $configuration
19+
}
20+
21+
if ($null -ne $configKey.Value.Process) {
22+
$scriptBlock = [ScriptBlock]::Create($configKey.Value.Process)
23+
$formattedValues = Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $formattedValues
24+
$formattedValues = @($formattedValues)
25+
}
26+
27+
$configKey.Value.Value = $formattedValues
28+
} else {
29+
30+
$formattedValue = Format-TokenizedConfigurationString -tokenizedString $configKey.Value.Value -configuration $configuration
31+
32+
if ($null -ne $configKey.Value.Process) {
33+
$scriptBlock = [ScriptBlock]::Create($configKey.Value.Process)
34+
$formattedValue = Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $formattedValue
35+
}
36+
37+
$configKey.Value.Value = $formattedValue
38+
}
39+
}
40+
}
41+
}

src/ALZ/Public/New-ALZEnvironment.ps1

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ function New-ALZEnvironment {
5454
$alzEnvironmentDestinationInternalCode = Join-Path $alzEnvironmentDestination "upstream-releases"
5555

5656
Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestinationInternalCode -githubRepoUrl $bicepConfig.module_url -releases @($bicepConfig.version) | Out-String | Write-Verbose
57-
57+
5858
Write-InformationColored "Copying ALZ-Bicep module to $alzEnvironmentDestinationInternalCode" -ForegroundColor Green -InformationAction Continue
5959
Copy-ALZParametersFile -alzEnvironmentDestination $alzEnvironmentDestination -upstreamReleaseDirectory $(Join-Path $alzEnvironmentDestinationInternalCode $bicepConfig.version) -configFiles $bicepConfig.config_files | Out-String | Write-Verbose
6060
Write-InformationColored "ALZ-Bicep source directory: $alzBicepSourceDirectory" -ForegroundColor Green -InformationAction Continue
6161

6262
$configuration = Request-ALZEnvironmentConfig -configurationParameters $bicepConfig.parameters
6363

64+
Set-ComputedConfiguration -configuration $configuration | Out-String | Write-Verbose
6465
Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination $alzEnvironmentDestination -configuration $configuration | Out-String | Write-Verbose
6566
Build-ALZDeploymentEnvFile -configuration $configuration -Destination $alzEnvironmentDestination | Out-String | Write-Verbose
6667

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

-32
Original file line numberDiff line numberDiff line change
@@ -77,38 +77,6 @@ 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-
}
11280
}
11381
}
11482
}

0 commit comments

Comments
 (0)