Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing bugs with complex value replacement needed. #26

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/ALZ/Assets/alz-bicep-config/v0.14.1-pre.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@
"Name": "parAutomationAccountLocation.value",
"Destination": "Parameters"
},
{
"Name": "parLogAnalyticsWorkspaceLocation.value",
"Destination": "Parameters"
},
{
"Name": "parPolicyAssignmentParameters.value.ascExportResourceGroupLocation.value",
"Destination": "Parameters"
Expand Down Expand Up @@ -320,6 +316,18 @@
],
"Value": ""
},
"LogAnalyticsWorkspaceLocation": {
"Type": "Computed",
"Value": "{%Location%}",
"Process": "($args[0] -eq \"eastus\") ? \"eastus2\" : ($args[0] -eq \"eastus2\") ? \"eastus\" : $args[0]",
"Targets": [
{
"Name": "parLogAnalyticsWorkspaceLocation.value",
"Destination": "Parameters"
}
]
},

"LogAnalyticsResourceId": {
"Type": "Computed",
"Value": "/subscriptions/{%ManagementSubscriptionId%}/resourcegroups/alz-logging/providers/microsoft.operationalinsights/workspaces/alz-log-analytics",
Expand All @@ -332,6 +340,7 @@
},
"AllSubscriptionIds": {
"Type": "Computed",
"Process": "@($args | Select-Object -Unique)",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to consider casing here... Perhaps tolower on the inputs before we test for uniqueness...

"Value": [
"{%ManagementSubscriptionId%}",
"{%ConnectivitySubscriptionId%}",
Expand Down
16 changes: 15 additions & 1 deletion src/ALZ/Private/Edit-ALZConfigurationFilesInPlace.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,23 @@ function Edit-ALZConfigurationFilesInPlace {
foreach($formatString in $configKey.Value.Value) {
$formattedValues += Format-TokenizedConfigurationString -tokenizedString $formatString -configuration $configuration
}

if ($null -ne $configKey.Value.Process) {
$scriptBlock = [ScriptBlock]::Create($configKey.Value.Process)
$formattedValues = Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $formattedValues
}

$bicepConfigNode[$leafPropertyName] = $formattedValues
} else {
$bicepConfigNode[$leafPropertyName] = Format-TokenizedConfigurationString -tokenizedString $configKey.Value.Value -configuration $configuration

$formattedValue = Format-TokenizedConfigurationString -tokenizedString $configKey.Value.Value -configuration $configuration

if ($null -ne $configKey.Value.Process) {
$scriptBlock = [ScriptBlock]::Create($configKey.Value.Process)
$formattedValue = Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $formattedValue
}

$bicepConfigNode[$leafPropertyName] = $formattedValue
}
} else {
$bicepConfigNode[$leafPropertyName] = $configKey.Value.Value
Expand Down
143 changes: 140 additions & 3 deletions src/Tests/Unit/Private/Edit-ALZConfigurationFilesInPlace.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ InModuleScope 'ALZ' {

[Parameter(Mandatory = $true)]
[string]$withValue
)

return [pscustomobject]@{
)
$config = [pscustomobject]@{
Nested = [pscustomobject]@{
Type = "Computed"
Description = "A Test Value"
Expand All @@ -48,6 +47,8 @@ InModuleScope 'ALZ' {
})
}
}

return $config
}

function Format-ExpectedResult {
Expand Down Expand Up @@ -481,6 +482,142 @@ InModuleScope 'ALZ' {
Write-InformationColored $contentStringAfterParsing -ForegroundColor Yellow -InformationAction Continue
Should -Invoke -CommandName Out-File -ParameterFilter { $FilePath -eq "test2.parameters.json" -and $InputObject -eq $contentStringAfterParsing } -Scope It
}

It 'Computed, Processed array values replace values correctly' {
$config = [pscustomobject]@{
Nested = [pscustomobject]@{
Type = "Computed"
Description = "A Test Value"
Process = '@($args | Select-Object -Unique)'
Value = @(
"1",
"1",
"3"
)
Targets = @(
[pscustomobject]@{
Name = "parValue.value"
Destination = "Parameters"
})
}
}

$fileContent = '{
"parameters": {
"parValue": {
"value": []
}
}
}'

$expectedContent = '{
"parameters": {
"parValue": {
"value": [ "1", "3" ]
}
}
}'

Mock -CommandName Get-Content -ParameterFilter { $Path -eq $testFile1Name } -MockWith {
$fileContent
}

$expectedContent = Format-ExpectedResult -expectedJson $expectedContent

Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination '.' -configuration $config

Should -Invoke -CommandName Out-File `
-ParameterFilter { $FilePath -eq $testFile1Name -and $InputObject -eq $expectedContent } `
-Scope It
}

It 'Computed, Processed values replace values correctly' {
$config = [pscustomobject]@{
Nested = [pscustomobject]@{
Type = "Computed"
Description = "A Test Value"
Process = '($args[0] -eq "eastus") ? "eastus2" : ($args[0] -eq "eastus2") ? "eastus" : $args[0]'
Value = "eastus"
Targets = @(
[pscustomobject]@{
Name = "parValue.value"
Destination = "Parameters"
})
}
}

$fileContent = '{
"parameters": {
"parValue": {
"value": "replace_me"
}
}
}'

$expectedContent = '{
"parameters": {
"parValue": {
"value": "eastus2"
}
}
}'

Mock -CommandName Get-Content -ParameterFilter { $Path -eq $testFile1Name } -MockWith {
$fileContent
}

$expectedContent = Format-ExpectedResult -expectedJson $expectedContent

Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination '.' -configuration $config

Should -Invoke -CommandName Out-File `
-ParameterFilter { $FilePath -eq $testFile1Name -and $InputObject -eq $expectedContent } `
-Scope It
}

It 'Computed, Processed values replace values correctly' {
$config = [pscustomobject]@{
Nested = [pscustomobject]@{
Type = "Computed"
Description = "A Test Value"
Process = '($args[0] -eq "goodbye") ? "Hello" : "Goodbye"'
Value = "goodbye"
Targets = @(
[pscustomobject]@{
Name = "parValue.value"
Destination = "Parameters"
})
}
}

$fileContent = '{
"parameters": {
"parValue": {
"value": "replace_me"
}
}
}'

$expectedContent = '{
"parameters": {
"parValue": {
"value": "Hello"
}
}
}'

Mock -CommandName Get-Content -ParameterFilter { $Path -eq $testFile1Name } -MockWith {
$fileContent
}

$expectedContent = Format-ExpectedResult -expectedJson $expectedContent

Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination '.' -configuration $config

Should -Invoke -CommandName Out-File `
-ParameterFilter { $FilePath -eq $testFile1Name -and $InputObject -eq $expectedContent } `
-Scope It
}
}
}
}