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

Load Template Content for Push Deployment in-memory #870

Merged
merged 1 commit into from
Mar 28, 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
6 changes: 5 additions & 1 deletion src/functions/Invoke-AzOpsPush.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,11 @@
#Starting deployment
$WhatIfPreference = $WhatIfPreferenceState
$uniqueProperties = 'Scope', 'DeploymentName', 'TemplateFilePath', 'TemplateParameterFilePath'
$uniqueDeployment = $deploymentList | Select-Object $uniqueProperties -Unique
$uniqueDeployment = $deploymentList | Select-Object $uniqueProperties -Unique | ForEach-Object {
$TemplateFileContent = [System.IO.File]::ReadAllText($_.TemplateFilePath)
$TemplateObject = ConvertFrom-Json $TemplateFileContent -AsHashtable
$_ | Add-Member -MemberType NoteProperty -Name 'TemplateObject' -Value $TemplateObject -PassThru
}
$deploymentResult = @()

if ($uniqueDeployment) {
Expand Down
22 changes: 15 additions & 7 deletions src/internal/functions/New-AzOpsDeployment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
Name under which to deploy the state.
.PARAMETER TemplateFilePath
Path where the ARM templates can be found.
.PARAMETER TemplateObject
TemplateObject where the templates content is stored in-memory.
.PARAMETER TemplateParameterFilePath
Path where the parameters of the ARM templates can be found.
.PARAMETER Mode
Expand Down Expand Up @@ -44,6 +46,10 @@
[string]
$TemplateFilePath = (Get-PSFConfigValue -FullName 'AzOps.Core.MainTemplate'),

[Parameter(ValueFromPipelineByPropertyName = $true)]
[hashtable]
$TemplateObject,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[AllowEmptyString()]
[AllowNull()]
Expand Down Expand Up @@ -89,9 +95,11 @@
#endregion Resolve Scope

#region Parse Content
$templateContent = Get-Content $TemplateFilePath | ConvertFrom-Json -AsHashtable

if ($templateContent.metadata._generator.name -eq 'bicep') {
if ($null -eq $TemplateObject) {
$TemplateFileContent = [System.IO.File]::ReadAllText($TemplateFilePath)
$TemplateObject = ConvertFrom-Json $TemplateFileContent -AsHashtable
}
if ($TemplateObject.metadata._generator.name -eq 'bicep') {
# Detect bicep templates
$bicepTemplate = $true
}
Expand All @@ -101,15 +109,15 @@
# Configure variables/parameters and the WhatIf/Deployment cmdlets to be used per scope
$defaultDeploymentRegion = (Get-PSFConfigValue -FullName 'AzOps.Core.DefaultDeploymentRegion')
$parameters = @{
'TemplateFile' = $TemplateFilePath
'TemplateObject' = $TemplateObject
'SkipTemplateParameterPrompt' = $true
'Location' = $defaultDeploymentRegion
}
if ($WhatIfResultFormat) {
$parameters.ResultFormat = $WhatIfResultFormat
}
# Resource Groups excluding Microsoft.Resources/resourceGroups that needs to be submitted at subscription scope
if ($scopeObject.resourcegroup -and $templateContent.resources[0].type -ne 'Microsoft.Resources/resourceGroups') {
if ($scopeObject.resourcegroup -and $TemplateObject.resources[0].type -ne 'Microsoft.Resources/resourceGroups') {
Write-AzOpsMessage -LogLevel Verbose -LogString 'New-AzOpsDeployment.ResourceGroup.Processing' -LogStringValues $scopeObject -Target $scopeObject
Set-AzOpsContext -ScopeObject $scopeObject
$whatIfCommand = 'Get-AzResourceGroupDeploymentWhatIfResult'
Expand Down Expand Up @@ -220,12 +228,12 @@
Write-AzOpsMessage -LogLevel Verbose -LogString 'New-AzOpsDeployment.WhatIfResults' -LogStringValues ($results | Out-String) -Target $scopeObject
Write-AzOpsMessage -LogLevel InternalComment -LogString 'New-AzOpsDeployment.WhatIfFile' -Target $scopeObject
if ($parameters.TemplateParameterFile) {
$deploymentResult.filePath = $parameters.TemplateFile
$deploymentResult.filePath = $TemplateFilePath
$deploymentResult.parameterFilePath = $parameters.TemplateParameterFile
$deploymentResult.results = $results
}
else {
$deploymentResult.filePath = $parameters.TemplateFile
$deploymentResult.filePath = $TemplateFilePath
$deploymentResult.results = $results
}
}
Expand Down