Skip to content

Commit b84cecc

Browse files
Fixing bugs with complex value replacement needed. (#26)
# Pull Request ## Issue #3 #23 ## Description Adding functionality to modify values before they're set based on simple PowerShell expressions which allows us to solve some problems with changes needed based on customer data. (see #3 and #23) ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license.
1 parent 7b2b1a5 commit b84cecc

File tree

3 files changed

+168
-8
lines changed

3 files changed

+168
-8
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,6 @@
183183
"Name": "parAutomationAccountLocation.value",
184184
"Destination": "Parameters"
185185
},
186-
{
187-
"Name": "parLogAnalyticsWorkspaceLocation.value",
188-
"Destination": "Parameters"
189-
},
190186
{
191187
"Name": "parPolicyAssignmentParameters.value.ascExportResourceGroupLocation.value",
192188
"Destination": "Parameters"
@@ -320,6 +316,18 @@
320316
],
321317
"Value": ""
322318
},
319+
"LogAnalyticsWorkspaceLocation": {
320+
"Type": "Computed",
321+
"Value": "{%Location%}",
322+
"Process": "($args[0] -eq \"eastus\") ? \"eastus2\" : ($args[0] -eq \"eastus2\") ? \"eastus\" : $args[0]",
323+
"Targets": [
324+
{
325+
"Name": "parLogAnalyticsWorkspaceLocation.value",
326+
"Destination": "Parameters"
327+
}
328+
]
329+
},
330+
323331
"LogAnalyticsResourceId": {
324332
"Type": "Computed",
325333
"Value": "/subscriptions/{%ManagementSubscriptionId%}/resourcegroups/alz-logging/providers/microsoft.operationalinsights/workspaces/alz-log-analytics",
@@ -332,6 +340,7 @@
332340
},
333341
"AllSubscriptionIds": {
334342
"Type": "Computed",
343+
"Process": "@($args | Select-Object -Unique)",
335344
"Value": [
336345
"{%ManagementSubscriptionId%}",
337346
"{%ConnectivitySubscriptionId%}",

src/ALZ/Private/Edit-ALZConfigurationFilesInPlace.ps1

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,23 @@ function Edit-ALZConfigurationFilesInPlace {
6767
foreach($formatString in $configKey.Value.Value) {
6868
$formattedValues += Format-TokenizedConfigurationString -tokenizedString $formatString -configuration $configuration
6969
}
70+
71+
if ($null -ne $configKey.Value.Process) {
72+
$scriptBlock = [ScriptBlock]::Create($configKey.Value.Process)
73+
$formattedValues = Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $formattedValues
74+
}
75+
7076
$bicepConfigNode[$leafPropertyName] = $formattedValues
7177
} else {
72-
$bicepConfigNode[$leafPropertyName] = Format-TokenizedConfigurationString -tokenizedString $configKey.Value.Value -configuration $configuration
78+
79+
$formattedValue = Format-TokenizedConfigurationString -tokenizedString $configKey.Value.Value -configuration $configuration
80+
81+
if ($null -ne $configKey.Value.Process) {
82+
$scriptBlock = [ScriptBlock]::Create($configKey.Value.Process)
83+
$formattedValue = Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $formattedValue
84+
}
85+
86+
$bicepConfigNode[$leafPropertyName] = $formattedValue
7387
}
7488
} else {
7589
$bicepConfigNode[$leafPropertyName] = $configKey.Value.Value

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

Lines changed: 140 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ InModuleScope 'ALZ' {
3434

3535
[Parameter(Mandatory = $true)]
3636
[string]$withValue
37-
)
38-
39-
return [pscustomobject]@{
37+
)
38+
$config = [pscustomobject]@{
4039
Nested = [pscustomobject]@{
4140
Type = "Computed"
4241
Description = "A Test Value"
@@ -48,6 +47,8 @@ InModuleScope 'ALZ' {
4847
})
4948
}
5049
}
50+
51+
return $config
5152
}
5253

5354
function Format-ExpectedResult {
@@ -481,6 +482,142 @@ InModuleScope 'ALZ' {
481482
Write-InformationColored $contentStringAfterParsing -ForegroundColor Yellow -InformationAction Continue
482483
Should -Invoke -CommandName Out-File -ParameterFilter { $FilePath -eq "test2.parameters.json" -and $InputObject -eq $contentStringAfterParsing } -Scope It
483484
}
485+
486+
It 'Computed, Processed array values replace values correctly' {
487+
$config = [pscustomobject]@{
488+
Nested = [pscustomobject]@{
489+
Type = "Computed"
490+
Description = "A Test Value"
491+
Process = '@($args | Select-Object -Unique)'
492+
Value = @(
493+
"1",
494+
"1",
495+
"3"
496+
)
497+
Targets = @(
498+
[pscustomobject]@{
499+
Name = "parValue.value"
500+
Destination = "Parameters"
501+
})
502+
}
503+
}
504+
505+
$fileContent = '{
506+
"parameters": {
507+
"parValue": {
508+
"value": []
509+
}
510+
}
511+
}'
512+
513+
$expectedContent = '{
514+
"parameters": {
515+
"parValue": {
516+
"value": [ "1", "3" ]
517+
}
518+
}
519+
}'
520+
521+
Mock -CommandName Get-Content -ParameterFilter { $Path -eq $testFile1Name } -MockWith {
522+
$fileContent
523+
}
524+
525+
$expectedContent = Format-ExpectedResult -expectedJson $expectedContent
526+
527+
Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination '.' -configuration $config
528+
529+
Should -Invoke -CommandName Out-File `
530+
-ParameterFilter { $FilePath -eq $testFile1Name -and $InputObject -eq $expectedContent } `
531+
-Scope It
532+
}
533+
534+
It 'Computed, Processed values replace values correctly' {
535+
$config = [pscustomobject]@{
536+
Nested = [pscustomobject]@{
537+
Type = "Computed"
538+
Description = "A Test Value"
539+
Process = '($args[0] -eq "eastus") ? "eastus2" : ($args[0] -eq "eastus2") ? "eastus" : $args[0]'
540+
Value = "eastus"
541+
Targets = @(
542+
[pscustomobject]@{
543+
Name = "parValue.value"
544+
Destination = "Parameters"
545+
})
546+
}
547+
}
548+
549+
$fileContent = '{
550+
"parameters": {
551+
"parValue": {
552+
"value": "replace_me"
553+
}
554+
}
555+
}'
556+
557+
$expectedContent = '{
558+
"parameters": {
559+
"parValue": {
560+
"value": "eastus2"
561+
}
562+
}
563+
}'
564+
565+
Mock -CommandName Get-Content -ParameterFilter { $Path -eq $testFile1Name } -MockWith {
566+
$fileContent
567+
}
568+
569+
$expectedContent = Format-ExpectedResult -expectedJson $expectedContent
570+
571+
Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination '.' -configuration $config
572+
573+
Should -Invoke -CommandName Out-File `
574+
-ParameterFilter { $FilePath -eq $testFile1Name -and $InputObject -eq $expectedContent } `
575+
-Scope It
576+
}
577+
578+
It 'Computed, Processed values replace values correctly' {
579+
$config = [pscustomobject]@{
580+
Nested = [pscustomobject]@{
581+
Type = "Computed"
582+
Description = "A Test Value"
583+
Process = '($args[0] -eq "goodbye") ? "Hello" : "Goodbye"'
584+
Value = "goodbye"
585+
Targets = @(
586+
[pscustomobject]@{
587+
Name = "parValue.value"
588+
Destination = "Parameters"
589+
})
590+
}
591+
}
592+
593+
$fileContent = '{
594+
"parameters": {
595+
"parValue": {
596+
"value": "replace_me"
597+
}
598+
}
599+
}'
600+
601+
$expectedContent = '{
602+
"parameters": {
603+
"parValue": {
604+
"value": "Hello"
605+
}
606+
}
607+
}'
608+
609+
Mock -CommandName Get-Content -ParameterFilter { $Path -eq $testFile1Name } -MockWith {
610+
$fileContent
611+
}
612+
613+
$expectedContent = Format-ExpectedResult -expectedJson $expectedContent
614+
615+
Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination '.' -configuration $config
616+
617+
Should -Invoke -CommandName Out-File `
618+
-ParameterFilter { $FilePath -eq $testFile1Name -and $InputObject -eq $expectedContent } `
619+
-Scope It
620+
}
484621
}
485622
}
486623
}

0 commit comments

Comments
 (0)