Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefajers authored Jan 5, 2024
1 parent 1d056a9 commit 0f44568
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
2 changes: 0 additions & 2 deletions src/functions/Initialize-AzOpsEnvironment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@
$script:AzOpsResourceProvider = Get-AzResourceProvider -ListAvailable
$script:AzOpsAzManagementGroup = @()
$script:AzOpsPartialRoot = @()
$script:AzOpsTranspiledTemplate = @()
$script:AzOpsTranspiledParameter = @()
#endregion Initialize & Prepare

#region Management Group Processing
Expand Down
42 changes: 34 additions & 8 deletions src/functions/Invoke-AzOpsPush.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@
param (
[AzOpsScope]
$ScopeObject,

[string]
$FilePath,

[string]
$AzOpsMainTemplate
$AzOpsMainTemplate,
[array]
$ConvertedTemplate,
[array]
$ConvertedParameter
)

#region Initialization Prep
Expand All @@ -66,7 +68,9 @@

$result = [PSCustomObject] @{
TemplateFilePath = $null
TranspiledTemplateNew = $false
TemplateParameterFilePath = $null
TranspiledParametersNew = $false
DeploymentName = $null
ScopeObject = $ScopeObject
Scope = $ScopeObject.Scope
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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'))) {
Expand Down Expand Up @@ -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
}

Expand All @@ -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) }) {
Expand Down
25 changes: 20 additions & 5 deletions src/internal/functions/ConvertFrom-AzOpsBicepTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -77,7 +90,9 @@
# Return transpiled (ARM) template paths
$return = [PSCustomObject]@{
transpiledTemplatePath = $transpiledTemplatePath
transpiledTemplateNew = $transpiledTemplateNew
transpiledParametersPath = $transpiledParametersPath
transpiledParametersNew = $transpiledParametersNew
}
return $return
}
Expand Down

0 comments on commit 0f44568

Please sign in to comment.