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

Fix File Pair Processing of Modify and Deletion Overlap #881

Merged
merged 2 commits into from
May 2, 2024
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
72 changes: 62 additions & 10 deletions src/functions/Invoke-AzOpsPush.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,19 @@
[string[]]
$ConvertedTemplate,
[string[]]
$ConvertedParameter
$ConvertedParameter,
[switch]
$CompareDeploymentToDeletion
)

# Avoid adding files destined for deletion to a deployment list
if ($CompareDeploymentToDeletion) {
if ($FilePath -in $deleteSet -or $FilePath -in ($deleteSet | Resolve-Path).Path) {
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.DeployDeletionOverlap' -LogStringValues $FilePath
continue
}
}

# Avoid duplicate entries in the deployment list
if ($FilePath.EndsWith(".parameters.json")) {
if ($FileSet -contains $FilePath.Replace(".parameters.json", ".json") -or $FileSet -contains $FilePath.Replace(".parameters.json", ".bicep")) {
Expand All @@ -72,7 +82,7 @@

# Handle Bicep templates
if ($FilePath.EndsWith(".bicep")) {
$transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $FilePath -ConvertedTemplate $ConvertedTemplate -ConvertedParameter $ConvertedParameter
$transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $FilePath -ConvertedTemplate $ConvertedTemplate -ConvertedParameter $ConvertedParameter -CompareDeploymentToDeletion:$CompareDeploymentToDeletion
if ($true -eq $transpiledTemplatePaths.transpiledTemplateNew) {
$ConvertedTemplate += $transpiledTemplatePaths.transpiledTemplatePath
}
Expand All @@ -93,7 +103,7 @@
}

# Resolve ARM file association
$resolvedArmFileAssociation = Resolve-ArmFileAssociation -ScopeObject $scopeObject -FilePath $FilePath -AzOpsMainTemplate $AzOpsMainTemplate -ConvertedTemplate $ConvertedTemplate -ConvertedParameter $ConvertedParameter
$resolvedArmFileAssociation = Resolve-ArmFileAssociation -ScopeObject $scopeObject -FilePath $FilePath -AzOpsMainTemplate $AzOpsMainTemplate -ConvertedTemplate $ConvertedTemplate -ConvertedParameter $ConvertedParameter -CompareDeploymentToDeletion:$CompareDeploymentToDeletion
if ($resolvedArmFileAssociation) {
foreach ($fileAssociation in $resolvedArmFileAssociation) {
if ($true -eq $transpiledTemplatePaths.transpiledTemplateNew -and $fileAssociation.TemplateFilePath -eq $transpiledTemplatePaths.transpiledTemplatePath) {
Expand All @@ -118,7 +128,9 @@
[string[]]
$ConvertedTemplate,
[string[]]
$ConvertedParameter
$ConvertedParameter,
[switch]
$CompareDeploymentToDeletion
)

#region Initialization Prep
Expand Down Expand Up @@ -163,6 +175,13 @@
$bicepTemplatePath = $fileItem.FullName -replace '.parameters.json', '.bicep'
}
if (Test-Path $templatePath) {
if ($CompareDeploymentToDeletion) {
# Avoid adding files destined for deletion to a deployment list
if ($templatePath -in $deleteSet -or $templatePath -in ($deleteSet | Resolve-Path).Path) {
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.DeployDeletionOverlap' -LogStringValues $templatePath
return
}
}
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.FoundTemplate' -LogStringValues $FilePath, $templatePath
$result.TemplateFilePath = $templatePath
$newScopeObject = New-AzOpsScope -Path $result.TemplateFilePath -StatePath $StatePath -ErrorAction Stop
Expand All @@ -171,8 +190,15 @@
return $result
}
elseif (Test-Path $bicepTemplatePath) {
if ($CompareDeploymentToDeletion) {
# Avoid adding files destined for deletion to a deployment list
if ($bicepTemplatePath -in $deleteSet -or $bicepTemplatePath -in ($deleteSet | Resolve-Path).Path) {
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.DeployDeletionOverlap' -LogStringValues $bicepTemplatePath
return
}
}
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.FoundBicepTemplate' -LogStringValues $FilePath, $bicepTemplatePath
$transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $bicepTemplatePath -SkipParam -ConvertedTemplate $ConvertedTemplate
$transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $bicepTemplatePath -SkipParam -ConvertedTemplate $ConvertedTemplate -CompareDeploymentToDeletion:$CompareDeploymentToDeletion
$result.TranspiledTemplateNew = $transpiledTemplatePaths.transpiledTemplateNew
$result.TemplateFilePath = $transpiledTemplatePaths.transpiledTemplatePath
$newScopeObject = New-AzOpsScope -Path $result.TemplateFilePath -StatePath $StatePath -ErrorAction Stop
Expand All @@ -190,8 +216,15 @@
$bicepTemplatePath = $fileItem.FullName -replace '\.bicepparam', '.bicep'
}
if (Test-Path $bicepTemplatePath) {
if ($CompareDeploymentToDeletion) {
# Avoid adding files destined for deletion to a deployment list
if ($bicepTemplatePath -in $deleteSet -or $bicepTemplatePath -in ($deleteSet | Resolve-Path).Path) {
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.DeployDeletionOverlap' -LogStringValues $bicepTemplatePath
return
}
}
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.FoundBicepTemplate' -LogStringValues $FilePath, $bicepTemplatePath
$transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $bicepTemplatePath -BicepParamTemplatePath $fileItem.FullName -ConvertedTemplate $ConvertedTemplate -ConvertedParameter $ConvertedParameter
$transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $bicepTemplatePath -BicepParamTemplatePath $fileItem.FullName -ConvertedTemplate $ConvertedTemplate -ConvertedParameter $ConvertedParameter -CompareDeploymentToDeletion:$CompareDeploymentToDeletion
$result.TranspiledTemplateNew = $transpiledTemplatePaths.transpiledTemplateNew
$result.TranspiledParametersNew = $transpiledTemplatePaths.transpiledParametersNew
$result.TemplateFilePath = $transpiledTemplatePaths.transpiledTemplatePath
Expand Down Expand Up @@ -245,19 +278,38 @@
$result.TemplateFilePath = $fileItem.FullName
$parameterPath = Join-Path $fileItem.Directory.FullName -ChildPath ($fileItem.BaseName + '.parameters' + (Get-PSFConfigValue -FullName 'AzOps.Core.TemplateParameterFileSuffix'))
if (Test-Path -Path $parameterPath) {
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.ParameterFound' -LogStringValues $FilePath, $parameterPath
$result.TemplateParameterFilePath = $parameterPath
if ($CompareDeploymentToDeletion) {
# Avoid adding files destined for deletion to a deployment list
if ($parameterPath -in $deleteSet -or $parameterPath -in ($deleteSet | Resolve-Path).Path) {
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.DeployDeletionOverlap' -LogStringValues $parameterPath
$skipParameters = $true
}
}
else {
$skipParameters = $false
}
if (-not $skipParameters) {
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.ParameterFound' -LogStringValues $FilePath, $parameterPath
$result.TemplateParameterFilePath = $parameterPath
}
}
elseif ((Get-PSFConfigValue -FullName 'AzOps.Core.AllowMultipleTemplateParameterFiles') -eq $true -and (Get-PSFConfigValue -FullName 'AzOps.Core.DeployAllMultipleTemplateParameterFiles') -eq $true) {
# Check for multiple associated template parameter files
$paramFileList = Get-ChildItem -Path $fileItem.Directory | Where-Object { ($_.Name.Split('.')[-3] -match $(Get-PSFConfigValue -FullName 'AzOps.Core.MultipleTemplateParameterFileSuffix').Replace('.','')) -or ($_.Name.Split('.')[-2] -match $(Get-PSFConfigValue -FullName 'AzOps.Core.MultipleTemplateParameterFileSuffix').Replace('.','')) }
if ($paramFileList) {
$multiResult = @()
foreach ($paramFile in $paramFileList) {
if ($CompareDeploymentToDeletion) {
# Avoid adding files destined for deletion to a deployment list
if ($paramFile.VersionInfo.FileName -in $deleteSet -or $paramFile.VersionInfo.FileName -in ($deleteSet | Resolve-Path).Path) {
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.DeployDeletionOverlap' -LogStringValues $paramFile.VersionInfo.FileName
continue
}
}
# Process possible parameter files for template equivalent
if (($fileItem.FullName.Split('.')[-2] -eq $paramFile.FullName.Split('.')[-3]) -or ($fileItem.FullName.Split('.')[-2] -eq $paramFile.FullName.Split('.')[-4])) {
Write-AzOpsMessage -LogLevel Debug -LogString 'Invoke-AzOpsPush.Resolve.MultipleTemplateParameterFile' -LogStringValues $paramFile.FullName
$multiResult += Resolve-ArmFileAssociation -ScopeObject $scopeObject -FilePath $paramFile -AzOpsMainTemplate $AzOpsMainTemplate -ConvertedTemplate $ConvertedTemplate -ConvertedParameter $ConvertedParameter
$multiResult += Resolve-ArmFileAssociation -ScopeObject $scopeObject -FilePath $paramFile -AzOpsMainTemplate $AzOpsMainTemplate -ConvertedTemplate $ConvertedTemplate -ConvertedParameter $ConvertedParameter -CompareDeploymentToDeletion:$CompareDeploymentToDeletion
}
}
if ($multiResult) {
Expand Down Expand Up @@ -416,7 +468,7 @@
#region Create DeploymentList
$deploymentList = foreach ($addition in $addModifySet | Where-Object { $_ -match ((Get-Item $StatePath).Name) }) {
# Create a list of deployment file associations using the New-AzOpsList function
$deployFileAssociationList = New-AzOpsList -FilePath $addition -FileSet $addModifySet -AzOpsMainTemplate $AzOpsMainTemplate -ConvertedTemplate $AzOpsTranspiledTemplate -ConvertedParameter $AzOpsTranspiledParameter
$deployFileAssociationList = New-AzOpsList -FilePath $addition -FileSet $addModifySet -AzOpsMainTemplate $AzOpsMainTemplate -ConvertedTemplate $AzOpsTranspiledTemplate -ConvertedParameter $AzOpsTranspiledParameter -CompareDeploymentToDeletion
# Iterate through each file association in the list
foreach ($fileAssociation in $deployFileAssociationList) {
# Check if the transpiled template is new and add it to the collection if true
Expand Down
20 changes: 18 additions & 2 deletions src/internal/functions/ConvertFrom-AzOpsBicepTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
[string[]]
$ConvertedTemplate,
[string[]]
$ConvertedParameter
$ConvertedParameter,
[switch]
$CompareDeploymentToDeletion
)

begin {
Expand All @@ -44,6 +46,13 @@
$transpiledParametersNew = $false
}
process {
if ($CompareDeploymentToDeletion) {
# Avoid adding files destined for deletion to a deployment list
if ($BicepTemplatePath -in $deleteSet -or $BicepTemplatePath -in ($deleteSet | Resolve-Path).Path) {
Write-AzOpsMessage -LogLevel Debug -LogString 'ConvertFrom-AzOpsBicepTemplate.Resolve.DeployDeletionOverlap' -LogStringValues $BicepTemplatePath
continue
}
}
$transpiledTemplatePath = [IO.Path]::GetFullPath("$($BicepTemplatePath -replace '\.bicep', '.json')")
if ($transpiledTemplatePath -notin $ConvertedTemplate) {
# Convert bicep template
Expand All @@ -68,7 +77,14 @@
$bicepParametersPath = $BicepParamTemplatePath
Write-AzOpsMessage -LogLevel Debug -LogString 'ConvertFrom-AzOpsBicepTemplate.Resolve.BicepParam' -LogStringValues $BicepTemplatePath, $bicepParametersPath
}
if ($bicepParametersPath -and (Test-Path $bicepParametersPath)) {
if ($CompareDeploymentToDeletion) {
# Avoid adding files destined for deletion to a deployment list
if ($bicepParametersPath -in $deleteSet -or $bicepParametersPath -in ($deleteSet | Resolve-Path).Path) {
Write-AzOpsMessage -LogLevel Debug -LogString 'ConvertFrom-AzOpsBicepTemplate.Resolve.DeployDeletionOverlap' -LogStringValues $bicepParametersPath
$skipParameters = $true
}
}
if (-not $skipParameters -and $bicepParametersPath -and (Test-Path $bicepParametersPath)) {
$transpiledParametersPath = [IO.Path]::GetFullPath("$($bicepParametersPath -replace '\.bicepparam', '.parameters.json')")
if ($transpiledParametersPath -notin $ConvertedParameter) {
# Convert bicepparam to ARM parameter file
Expand Down
2 changes: 2 additions & 0 deletions src/localized/en-us/Strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
'AzOpsScope.InitializeMemberVariablesFromFile.resource' = 'Determine scope based on ResourceType {0} and Resource Name {1}' # ResourceType and #Resource Name
'AzOpsScope.ChildResource.InitializeMemberVariables' = 'Determine scope of Child Resource based on ResourceType {0}, Resource Name {1} and Parent ResourceID {2}' # ResourceType, Resource Name, Parent ResourceId

'ConvertFrom-AzOpsBicepTemplate.Resolve.DeployDeletionOverlap' = 'The file: {0} is targeted for deletion, skipping deployment file association' #
'ConvertFrom-AzOpsBicepTemplate.Resolve.ConvertBicepTemplate' = 'Converting Bicep template ({0}) to ARM Template JSON ({1})' # $BicepTemplatePath, $transpiledTemplatePath
'ConvertFrom-AzOpsBicepTemplate.Resolve.ConvertBicepTemplate.Error' = 'Failed to convert Bicep template ({0}) to ARM Template JSON' # $BicepTemplatePath
'ConvertFrom-AzOpsBicepTemplate.Resolve.BicepParam' = 'Determine if Bicep template ({0}) has a bicepparam file at ({1})' # $BicepTemplatePath, $bicepParametersPath
Expand Down Expand Up @@ -210,6 +211,7 @@
'Invoke-AzOpsPush.Dependency.Missing' = 'Missing resource dependency for successfull deletion. Error exiting runtime.'
'Invoke-AzOpsPush.DeploymentList.NotFound' = 'Expecting deploymentList object, it was not found. Error exiting runtime.'
'Invoke-AzOpsPush.Duration' = 'AzOps Push completed in {0}' # $stopWatch.Elapsed
'Invoke-AzOpsPush.Resolve.DeployDeletionOverlap' = 'The file: {0} is targeted for deletion, skipping deployment file association' #
'Invoke-AzOpsPush.Resolve.FoundTemplate' = 'Found template {1} for parameters {0}' # $FilePath, $templatePath
'Invoke-AzOpsPush.Resolve.FoundBicepTemplate' = 'Found Bicep template {1} for parameters {0}' # $FilePath, $bicepTemplatePath
'Invoke-AzOpsPush.Resolve.FromMainTemplate' = 'Determining template from main template file: {0}' # $mainTemplateItem.FullName
Expand Down