Skip to content

Commit

Permalink
Correctly resolve template file from parameter files when suffix appe…
Browse files Browse the repository at this point in the history
…ars multiple times in path (#885)

* Correctly resolve template file from parameter files when suffix appears multiple times in path

* addon

---------

Co-authored-by: Kasper Schultz Davidsen <kada@bizzkit.com>
Co-authored-by: Jesper Fajers <jesper@fajers.se>
  • Loading branch information
3 people authored Jun 5, 2024
1 parent 0a85ba5 commit 882f748
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/functions/Invoke-AzOpsPush.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@
{ $_.EndsWith('.parameters.json') } {
if ((Get-PSFConfigValue -FullName 'AzOps.Core.AllowMultipleTemplateParameterFiles') -eq $true -and $fileItem.FullName.Split('.')[-3] -match $(Get-PSFConfigValue -FullName 'AzOps.Core.MultipleTemplateParameterFileSuffix').Replace('.','')) {
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.MultipleTemplateParameterFile' -LogStringValues $FilePath
$templatePath = $fileItem.FullName -replace (".$($fileItem.FullName.Split('.')[-3])"), '' -replace '\.parameters.json', '.json'
$bicepTemplatePath = $fileItem.FullName -replace (".$($fileItem.FullName.Split('.')[-3])"), '' -replace '.parameters.json', '.bicep'
$templatePath = $fileItem.FullName -replace "\.$($fileItem.FullName.Split('.')[-3])\.parameters.json$", '.json'
$bicepTemplatePath = $fileItem.FullName -replace "\.$($fileItem.FullName.Split('.')[-3])\.parameters.json$", '.bicep'
}
else {
$templatePath = $fileItem.FullName -replace '\.parameters.json', (Get-PSFConfigValue -FullName 'AzOps.Core.TemplateParameterFileSuffix')
$bicepTemplatePath = $fileItem.FullName -replace '.parameters.json', '.bicep'
$templatePath = $fileItem.FullName -replace '\.parameters.json$', (Get-PSFConfigValue -FullName 'AzOps.Core.TemplateParameterFileSuffix')
$bicepTemplatePath = $fileItem.FullName -replace '\.parameters.json$', '.bicep'
}
if (Test-Path $templatePath) {
if ($CompareDeploymentToDeletion) {
Expand Down Expand Up @@ -210,10 +210,10 @@
{ $_.EndsWith('.bicepparam') } {
if ((Get-PSFConfigValue -FullName 'AzOps.Core.AllowMultipleTemplateParameterFiles') -eq $true -and $fileItem.FullName.Split('.')[-2] -match $(Get-PSFConfigValue -FullName 'AzOps.Core.MultipleTemplateParameterFileSuffix').Replace('.','')) {
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.MultipleTemplateParameterFile' -LogStringValues $FilePath
$bicepTemplatePath = $fileItem.FullName -replace (".$($fileItem.FullName.Split('.')[-2])"), '' -replace '\.bicepparam', '.bicep'
$bicepTemplatePath = $fileItem.FullName -replace "\.$($fileItem.FullName.Split('.')[-2])\.bicepparam$", '.bicep'
}
else {
$bicepTemplatePath = $fileItem.FullName -replace '\.bicepparam', '.bicep'
$bicepTemplatePath = $fileItem.FullName -replace '\.bicepparam$', '.bicep'
}
if (Test-Path $bicepTemplatePath) {
if ($CompareDeploymentToDeletion) {
Expand Down
6 changes: 3 additions & 3 deletions src/internal/functions/ConvertFrom-AzOpsBicepTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
continue
}
}
$transpiledTemplatePath = [IO.Path]::GetFullPath("$($BicepTemplatePath -replace '\.bicep', '.json')")
$transpiledTemplatePath = [IO.Path]::GetFullPath("$($BicepTemplatePath -replace '\.bicep$', '.json')")
if ($transpiledTemplatePath -notin $ConvertedTemplate) {
# Convert bicep template
Write-AzOpsMessage -LogLevel Debug -LogString 'ConvertFrom-AzOpsBicepTemplate.Resolve.ConvertBicepTemplate' -LogStringValues $BicepTemplatePath, $transpiledTemplatePath
Expand All @@ -69,7 +69,7 @@
if (-not $SkipParam) {
if (-not $BicepParamTemplatePath) {
# Check if bicep template has associated bicepparam file
$bicepParametersPath = $BicepTemplatePath -replace '\.bicep', '.bicepparam'
$bicepParametersPath = $BicepTemplatePath -replace '\.bicep$', '.bicepparam'
Write-AzOpsMessage -LogLevel Debug -LogString 'ConvertFrom-AzOpsBicepTemplate.Resolve.BicepParam' -LogStringValues $BicepTemplatePath, $bicepParametersPath
}
elseif ($BicepParamTemplatePath) {
Expand All @@ -85,7 +85,7 @@
}
}
if (-not $skipParameters -and $bicepParametersPath -and (Test-Path $bicepParametersPath)) {
$transpiledParametersPath = [IO.Path]::GetFullPath("$($bicepParametersPath -replace '\.bicepparam', '.parameters.json')")
$transpiledParametersPath = [IO.Path]::GetFullPath("$($bicepParametersPath -replace '\.bicepparam$', '.parameters.json')")
if ($transpiledParametersPath -notin $ConvertedParameter) {
# Convert bicepparam to ARM parameter file
Write-AzOpsMessage -LogLevel Debug -LogString 'ConvertFrom-AzOpsBicepTemplate.Resolve.ConvertBicepParam' -LogStringValues $bicepParametersPath, $transpiledParametersPath
Expand Down
17 changes: 17 additions & 0 deletions src/tests/integration/Repository.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,23 @@ Describe "Repository" {
}
#endregion

#region Bicep multiple parameter files with change, suffix appears multiple times in the path of the associated template
It "Deploy Bicep base template with parameter files where suffix appears multiple times in path" {
Set-PSFConfig -FullName AzOps.Core.AllowMultipleTemplateParameterFiles -Value $true
Set-PSFConfig -FullName AzOps.Core.MultipleTemplateParameterFileSuffix -Value ".\w+"
$script:bicepRepeatSuffixPath = Get-ChildItem -Path "$($global:testRoot)/templates/rtsuffix0102*" | Copy-Item -Destination $script:resourceGroupDirectory -PassThru -Force
$changeSet = @(
"A`t$($script:bicepRepeatSuffixPath.FullName[0])",
"A`t$($script:bicepRepeatSuffixPath.FullName[1])"
)
{Invoke-AzOpsPush -ChangeSet $changeSet} | Should -Not -Throw
Start-Sleep -Seconds 5
$script:bicepRepeatSuffixPathDeployment = Get-AzResource -ResourceGroupName $($script:resourceGroup).ResourceGroupName -ResourceType 'Microsoft.Network/routeTables' | Where-Object {$_.name -like "rtsuffix*"}
$script:bicepRepeatSuffixPathDeployment.Count | Should -Be 2
Set-PSFConfig -FullName AzOps.Core.MultipleTemplateParameterFileSuffix -Value ".x"
}
#endregion

#region Bicep base template with no 1-1 parameter file and AllowMultipleTemplateParameterFile set to true Test should not deploy
It "Try deployment of Bicep base template with missing defaultValue parameter with no 1-1 parameter file and AllowMultipleTemplateParameterFile set to true, Test should not deploy and exit gracefully" {
Set-PSFConfig -FullName AzOps.Core.AllowMultipleTemplateParameterFiles -Value $true
Expand Down
3 changes: 3 additions & 0 deletions src/tests/templates/rtsuffix0102.01.bicepparam
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using 'rtsuffix0102.bicep'

param name = 'rtsuffix01'
9 changes: 9 additions & 0 deletions src/tests/templates/rtsuffix0102.02.parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"name": {
"value": "rtsuffix02"
}
}
}
12 changes: 12 additions & 0 deletions src/tests/templates/rtsuffix0102.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
param name string
param location string = resourceGroup().location

resource symbolicname 'Microsoft.Network/routeTables@2023-04-01' = {
name: name
location: location
properties: {
disableBgpRoutePropagation: false
routes: [
]
}
}

0 comments on commit 882f748

Please sign in to comment.