diff --git a/src/functions/Initialize-AzOpsEnvironment.ps1 b/src/functions/Initialize-AzOpsEnvironment.ps1 index 418a12bf..33cbc621 100644 --- a/src/functions/Initialize-AzOpsEnvironment.ps1 +++ b/src/functions/Initialize-AzOpsEnvironment.ps1 @@ -113,8 +113,6 @@ $script:AzOpsResourceProvider = Get-AzResourceProvider -ListAvailable $script:AzOpsAzManagementGroup = @() $script:AzOpsPartialRoot = @() - $script:AzOpsTranspiledTemplate = @() - $script:AzOpsTranspiledParameter = @() #endregion Initialize & Prepare #region Management Group Processing diff --git a/src/functions/Invoke-AzOpsPush.ps1 b/src/functions/Invoke-AzOpsPush.ps1 index 1c35ac9c..b94d4e3c 100644 --- a/src/functions/Invoke-AzOpsPush.ps1 +++ b/src/functions/Invoke-AzOpsPush.ps1 @@ -48,12 +48,14 @@ param ( [AzOpsScope] $ScopeObject, - [string] $FilePath, - [string] - $AzOpsMainTemplate + $AzOpsMainTemplate, + [array] + $ConvertedTemplate, + [array] + $ConvertedParameter ) #region Initialization Prep @@ -66,7 +68,9 @@ $result = [PSCustomObject] @{ TemplateFilePath = $null + TranspiledTemplateNew = $false TemplateParameterFilePath = $null + TranspiledParametersNew = $false DeploymentName = $null ScopeObject = $ScopeObject Scope = $ScopeObject.Scope @@ -111,7 +115,8 @@ } elseif (Test-Path $bicepTemplatePath) { Write-PSFMessage -Level Verbose @common -String 'Invoke-AzOpsPush.Resolve.FoundBicepTemplate' -StringValues $FilePath, $bicepTemplatePath - $transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $bicepTemplatePath -SkipParam + $transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $bicepTemplatePath -SkipParam -ConvertedTemplate $ConvertedTemplate + $result.TranspiledTemplateNew = $transpiledTemplatePaths.transpiledTemplateNew $result.TemplateFilePath = $transpiledTemplatePaths.transpiledTemplatePath $newScopeObject = New-AzOpsScope -Path $result.TemplateFilePath -StatePath $StatePath -ErrorAction Stop $result.ScopeObject = $newScopeObject @@ -129,7 +134,9 @@ } if (Test-Path $bicepTemplatePath) { Write-PSFMessage -Level Verbose @common -String 'Invoke-AzOpsPush.Resolve.FoundBicepTemplate' -StringValues $FilePath, $bicepTemplatePath - $transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $bicepTemplatePath -BicepParamTemplatePath $fileItem.FullName + $transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $bicepTemplatePath -BicepParamTemplatePath $fileItem.FullName -ConvertedTemplate $ConvertedTemplate -ConvertedParameter $ConvertedParameter + $result.TranspiledTemplateNew = $transpiledTemplatePaths.transpiledTemplateNew + $result.TranspiledParametersNew = $transpiledTemplatePaths.transpiledParametersNew $result.TemplateFilePath = $transpiledTemplatePaths.transpiledTemplatePath $result.TemplateParameterFilePath = $transpiledTemplatePaths.transpiledParametersPath $newScopeObject = New-AzOpsScope -Path $result.TemplateFilePath -StatePath $StatePath -ErrorAction Stop @@ -193,7 +200,7 @@ # 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-PSFMessage -Level Verbose @common -String 'Invoke-AzOpsPush.Resolve.MultipleTemplateParameterFile' -StringValues $paramFile.FullName - $multiResult += Resolve-ArmFileAssociation -ScopeObject $scopeObject -FilePath $paramFile -AzOpsMainTemplate $AzOpsMainTemplate + $multiResult += Resolve-ArmFileAssociation -ScopeObject $scopeObject -FilePath $paramFile -AzOpsMainTemplate $AzOpsMainTemplate -ConvertedTemplate $ConvertedTemplate -ConvertedParameter $ConvertedParameter } } if ($multiResult) { @@ -238,6 +245,10 @@ $WhatIfPreferenceState = $WhatIfPreference $WhatIfPreference = $false + # Create arrays to track bicep file conversion + $AzOpsTranspiledTemplate = @() + $AzOpsTranspiledParameter = @() + # Remove lingering files from previous run $tempPath = [System.IO.Path]::GetTempPath() if ((Test-Path -Path ($tempPath + 'OUTPUT.md')) -or (Test-Path -Path ($tempPath + 'OUTPUT.json'))) { @@ -350,7 +361,13 @@ # Handle Bicep templates if ($addition.EndsWith(".bicep")) { - $transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $addition | Select-Object transpiledTemplatePath + $transpiledTemplatePaths = ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath $addition -ConvertedTemplate $AzOpsTranspiledTemplate -ConvertedParameter $AzOpsTranspiledParameter + if ($transpiledTemplatePaths.transpiledTemplateNew -eq $true) { + $AzOpsTranspiledTemplate += $transpiledTemplatePaths.transpiledTemplatePath + } + if ($transpiledTemplatePaths.transpiledParametersNew -eq $true) { + $AzOpsTranspiledParameter += $transpiledTemplatePaths.transpiledParametersPath + } $addition = $transpiledTemplatePaths.transpiledTemplatePath } @@ -362,7 +379,16 @@ continue } - Resolve-ArmFileAssociation -ScopeObject $scopeObject -FilePath $addition -AzOpsMainTemplate $AzOpsMainTemplate + $resolvedArmFileAssociation = Resolve-ArmFileAssociation -ScopeObject $scopeObject -FilePath $addition -AzOpsMainTemplate $AzOpsMainTemplate -ConvertedTemplate $AzOpsTranspiledTemplate -ConvertedParameter $AzOpsTranspiledParameter + foreach ($fileAssociation in $resolvedArmFileAssociation) { + if ($fileAssociation.transpiledTemplateNew -eq $true) { + $AzOpsTranspiledTemplate += $fileAssociation.TemplateFilePath + } + if ($fileAssociation.transpiledParametersNew -eq $true) { + $AzOpsTranspiledParameter += $fileAssociation.TemplateParameterFilePath + } + } + $resolvedArmFileAssociation } $deletionList = foreach ($deletion in $deleteSet | Where-Object { $_ -match ((Get-Item $StatePath).Name) }) { diff --git a/src/internal/functions/ConvertFrom-AzOpsBicepTemplate.ps1 b/src/internal/functions/ConvertFrom-AzOpsBicepTemplate.ps1 index b81bede3..21fad679 100644 --- a/src/internal/functions/ConvertFrom-AzOpsBicepTemplate.ps1 +++ b/src/internal/functions/ConvertFrom-AzOpsBicepTemplate.ps1 @@ -9,10 +9,16 @@ BicepParamTemplatePath, when provided function does not attempt default parameter file discovery. .PARAMETER SkipParam Switch when set will avoid parameter file discovery. + .PARAMETER ConvertedTemplate + Array of already converted base template, if file is on list skip conversion. + .PARAMETER ConvertedParameter + Array of already converted parameter, if file is on list skip conversion. .EXAMPLE ConvertFrom-AzOpsBicepTemplate -BicepTemplatePath "root/tenant root group (xxxx-xxxx-xxxx-xxxx-xxxx)/es (es)/subscription (xxxx-xxxx-xxxx-xxxx)/resource-rg/main.bicep" transpiledTemplatePath : root/tenant root group (xxxx-xxxx-xxxx-xxxx-xxxx)/es (es)/subscription (xxxx-xxxx-xxxx-xxxx)/resource-rg/main.json + transpiledTemplateNew : True transpiledParametersPath : root/tenant root group (xxxx-xxxx-xxxx-xxxx-xxxx)/es (es)/subscription (xxxx-xxxx-xxxx-xxxx)/resource-rg/main.parameters.json + transpiledParametersNew : True #> [CmdletBinding()] @@ -23,20 +29,27 @@ [string] $BicepParamTemplatePath, [switch] - $SkipParam + $SkipParam, + [array] + $ConvertedTemplate, + [array] + $ConvertedParameter ) begin { # Assert bicep binaries Assert-AzOpsBicepDependency -Cmdlet $PSCmdlet + # Default transpiled values to false + $transpiledTemplateNew = $false + $transpiledParametersNew = $false } process { $transpiledTemplatePath = [IO.Path]::GetFullPath("$($BicepTemplatePath -replace '\.bicep', '.json')") - if ($transpiledTemplatePath -notin $script:AzOpsTranspiledTemplate) { + if ($transpiledTemplatePath -notin $ConvertedTemplate) { # Convert bicep template Write-PSFMessage -Level Verbose -String 'ConvertFrom-AzOpsBicepTemplate.Resolve.ConvertBicepTemplate' -StringValues $BicepTemplatePath, $transpiledTemplatePath Invoke-AzOpsNativeCommand -ScriptBlock { bicep build $bicepTemplatePath --outfile $transpiledTemplatePath } - $script:AzOpsTranspiledTemplate += $transpiledTemplatePath + $transpiledTemplateNew = $true # Check if bicep build created (ARM) template if (-not (Test-Path $transpiledTemplatePath)) { # If bicep build did not produce file exit with error @@ -57,11 +70,11 @@ } if ($bicepParametersPath -and (Test-Path $bicepParametersPath)) { $transpiledParametersPath = [IO.Path]::GetFullPath("$($bicepParametersPath -replace '\.bicepparam', '.parameters.json')") - if ($transpiledParametersPath -notin $script:AzOpsTranspiledParameter) { + if ($transpiledParametersPath -notin $ConvertedParameter) { # Convert bicepparam to ARM parameter file Write-PSFMessage -Level Verbose -String 'ConvertFrom-AzOpsBicepTemplate.Resolve.ConvertBicepParam' -StringValues $bicepParametersPath, $transpiledParametersPath Invoke-AzOpsNativeCommand -ScriptBlock { bicep build-params $bicepParametersPath --outfile $transpiledParametersPath } - $script:AzOpsTranspiledParameter += $transpiledParametersPath + $transpiledParametersNew = $true # Check if bicep build-params created (ARM) parameters if (-not (Test-Path $transpiledParametersPath)) { # If bicep build-params did not produce file exit with error @@ -77,7 +90,9 @@ # Return transpiled (ARM) template paths $return = [PSCustomObject]@{ transpiledTemplatePath = $transpiledTemplatePath + transpiledTemplateNew = $transpiledTemplateNew transpiledParametersPath = $transpiledParametersPath + transpiledParametersNew = $transpiledParametersNew } return $return }