Skip to content

Commit

Permalink
Set-AzOpsWhatIfOutput limit calc update (#665)
Browse files Browse the repository at this point in the history
* UpdateToLogic

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update
  • Loading branch information
Jefajers authored Jul 1, 2022
1 parent de859a1 commit 180a151
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 46 deletions.
7 changes: 4 additions & 3 deletions src/internal/functions/New-AzOpsDeployment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@

#region Process Scope
# Configure variables/parameters and the WhatIf/Deployment cmdlets to be used per scope
$defaultDeploymentRegion = (Get-PSFConfigValue -FullName 'AzOps.Core.DefaultDeploymentRegion')
$parameters = @{
'TemplateFile' = $TemplateFilePath
'SkipTemplateParameterPrompt' = $true
'Location' = (Get-PSFConfigValue -FullName 'AzOps.Core.DefaultDeploymentRegion')
'Location' = $defaultDeploymentRegion
}
# 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') {
Expand Down Expand Up @@ -144,7 +145,7 @@
# Handle WhatIf prediction errors
elseif ($resultsErrorMessage -match 'DeploymentWhatIfResourceError' -and $resultsErrorMessage -match "The request to predict template deployment") {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -Target $scopeObject -Tag Error -StringValues $resultsErrorMessage
Set-AzOpsWhatIfOutput -TemplatePath $TemplateFilePath -Results ('{0}WhatIf prediction failed with error - validate changes manually before merging:{0}{1}' -f [environment]::NewLine, $resultsErrorMessage)
Set-AzOpsWhatIfOutput -StatePath $scopeObject.StatePath -Results ('{0}WhatIf prediction failed with error - validate changes manually before merging:{0}{1}' -f [environment]::NewLine, $resultsErrorMessage)
}
else {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -Target $scopeObject -Tag Error -StringValues $resultsErrorMessage
Expand All @@ -158,7 +159,7 @@
else {
Write-PSFMessage -Level Verbose -String 'New-AzOpsDeployment.WhatIfResults' -StringValues ($results | Out-String) -Target $scopeObject
Write-PSFMessage -Level Verbose -String 'New-AzOpsDeployment.WhatIfFile' -Target $scopeObject
Set-AzOpsWhatIfOutput -TemplatePath $TemplateFilePath -Results $results
Set-AzOpsWhatIfOutput -StatePath $scopeObject.StatePath -Results $results
}
# Remove ExcludeChangeType parameter as it doesn't exist for deployment cmdlets
if ($parameters.ExcludeChangeType) {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/functions/Remove-AzOpsDeployment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@
if (-not $resourceToDelete) {
Write-PSFMessage -Level Warning -String 'Remove-AzOpsDeployment.ResourceNotFound' -StringValues $scopeObject.Resource, $scopeObject.Scope -Target $scopeObject
$results = '{0}: What if Operation Failed: Deletion of target resource {1}. Resource could not be found' -f $removeJobName, $scopeObject.scope
Set-AzOpsWhatIfOutput -TemplatePath $TemplateFilePath -Results $results -RemoveAzOpsFlag $true
Set-AzOpsWhatIfOutput -StatePath $scopeObject.StatePath -Results $results -RemoveAzOpsFlag $true
return
}
$results = '{0}: What if Successful: Performing the operation: Deletion of target resource {1}.' -f $removeJobName, $scopeObject.scope
Write-PSFMessage -Level Verbose -String 'Set-AzOpsWhatIfOutput.WhatIfResults' -StringValues $results -Target $scopeObject
Write-PSFMessage -Level Verbose -String 'Set-AzOpsWhatIfOutput.WhatIfFile' -Target $scopeObject
Set-AzOpsWhatIfOutput -TemplatePath $TemplateFilePath -Results $results -RemoveAzOpsFlag $true
Set-AzOpsWhatIfOutput -StatePath $scopeObject.StatePath -Results $results -RemoveAzOpsFlag $true

if ($PSCmdlet.ShouldProcess("Remove $($scopeObject.Scope)?")) {
$null = Remove-AzResource -ResourceId $scopeObject.Scope -Force
Expand Down
75 changes: 50 additions & 25 deletions src/internal/functions/Set-AzOpsWhatIfOutput.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
Logs the output from a What-If deployment
.DESCRIPTION
Logs the output from a What-If deployment
.PARAMETER results
.PARAMETER Results
The WhatIf result from a deployment
.PARAMETER RemoveAzOpsFlag
RemoveAzOpsFlag is set to true when a need to push content about deletion is required
.PARAMETER ResultSizeLimit
The character limit allowed for comments 64,000
.PARAMETER ResultSizeMaxLimit
The maximum upper character limit allowed for comments 64,600
.PARAMETER StatePath
File in scope of WhatIf
.EXAMPLE
> Set-AzOpsWhatIfOutput -results $results -removeAzOpsFlag $true
$removeAzOpsFlag is set to true when we need to push contents for Remove-AzopsDeployment to PR
> Set-AzOpsWhatIfOutput -Results $results
> Set-AzOpsWhatIfOutput -Results $results -RemoveAzOpsFlag $true
#>

[CmdletBinding()]
Expand All @@ -24,42 +32,59 @@
$ResultSizeLimit = "64000",

[Parameter(Mandatory = $false)]
$TemplatePath
$ResultSizeMaxLimit = "64600",

[Parameter(Mandatory = $false)]
$StatePath
)

process {
Write-PSFMessage -Level Verbose -String 'Set-AzOpsWhatIfOutput.WhatIfFile'

if (-not (Test-Path -Path '/tmp/OUTPUT.md')) {
New-Item -Path '/tmp/OUTPUT.md' -WhatIf:$false
New-Item -Path '/tmp/OUTPUT.json' -WhatIf:$false
}
if ($TemplatePath -match '/') {
$TemplatePath = ($TemplatePath -split '/')[-1]
if ($StatePath -match '/') {
$StatePath = ($StatePath -split '/')[-1]
}
if ($RemoveAzOpsFlag) {
$mdOutput = '{0}WhatIf Results for Resource Deletion of {2}:{0}```{0}{1}{0}```' -f [environment]::NewLine, $Results, $TemplatePath
# Measure input $Results.Changes content
$resultJson = ($Results.Changes | ConvertTo-Json -Depth 100)
$resultString = $Results | Out-String
$resultStringMeasure = $resultString | Measure-Object -Line -Character -Word
# Measure current /tmp/OUTPUT.md content
$existingContentMd = Get-Content -Path '/tmp/OUTPUT.md' -Raw
$existingContentStringMd = $existingContentMd | Out-String
$existingContentStringMeasureMd = $existingContentStringMd | Measure-Object -Line -Character -Word
# Gather current /tmp/OUTPUT.json content
$existingContent = @(Get-Content -Path '/tmp/OUTPUT.json' -Raw | ConvertFrom-Json -Depth 100)
# Check if $existingContentStringMeasureMd and $resultStringMeasure exceed allowed size in $ResultSizeLimit
if (($existingContentStringMeasureMd.Characters + $resultStringMeasure.Characters) -gt $ResultSizeLimit) {
Write-PSFMessage -Level Warning -String 'Set-AzOpsWhatIfOutput.WhatIfFileMax' -StringValues $ResultSizeLimit
$mdOutput = 'WhatIf Results for {1}:{0} WhatIf is too large for comment field, for more details look at PR files to determine changes.' -f [environment]::NewLine, $StatePath
}
else {
$resultJson = ($Results.Changes | ConvertTo-Json -Depth 100)
$resultString = $Results | Out-String
$resultStringMeasure = $resultString | Measure-Object -Line -Character -Word
if ($($resultStringMeasure.Characters) -gt $ResultSizeLimit) {
$mdOutput = 'WhatIf Results for {1}:{0} WhatIf is too large for comment field, for more details look at PR files to determine changes.' -f [environment]::NewLine, $TemplatePath
if ($RemoveAzOpsFlag) {
$mdOutput = '{0}WhatIf Results for Resource Deletion of {2}:{0}```{0}{1}{0}```' -f [environment]::NewLine, $Results, $StatePath
}
else {
$mdOutput = 'WhatIf Results for {2}:{0}```{0}{1}{0}```{0}' -f [environment]::NewLine, $resultString, $TemplatePath
if ($existingContent.count -gt 0) {
$existingContent += $results.Changes
$existingContent = $existingContent | ConvertTo-Json -Depth 100
}
else {
$existingContent = $resultJson
}
$mdOutput = 'WhatIf Results for {2}:{0}```{0}{1}{0}```{0}' -f [environment]::NewLine, $resultString, $StatePath
Write-PSFMessage -Level Verbose -String 'Set-AzOpsWhatIfOutput.WhatIfFileAddingJson'
Set-Content -Path '/tmp/OUTPUT.json' -Value $existingContent -WhatIf:$false
}
$existingContent = @(Get-Content -Path '/tmp/OUTPUT.json' -Raw | ConvertFrom-Json)
if ($existingContent.count -gt 0) {
$existingContent += $results.Changes
$existingContent = $existingContent | ConvertTo-Json -Depth 100
}
else {
$existingContent = $resultJson
}
Set-Content -Path '/tmp/OUTPUT.json' -Value $existingContent -WhatIf:$false
}
Add-Content -Path '/tmp/OUTPUT.md' -Value $mdOutput -WhatIf:$false
if ((($mdOutput | Measure-Object -Line -Character -Word).Characters + $existingContentStringMeasureMd.Characters) -le $ResultSizeMaxLimit) {
Write-PSFMessage -Level Verbose -String 'Set-AzOpsWhatIfOutput.WhatIfFileAddingMd'
Add-Content -Path '/tmp/OUTPUT.md' -Value $mdOutput -WhatIf:$false
}
else {
Write-PSFMessage -Level Warning -String 'Set-AzOpsWhatIfOutput.WhatIfMessageMax' -StringValues $ResultSizeMaxLimit
}
}
}
6 changes: 5 additions & 1 deletion src/localized/en-us/Strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,9 @@
'Set-AzOpsStringLength.WithInLimit' = 'String {0} within limit of {1}' # $String

'Set-AzOpsWhatIfOutput.WhatIfFile' = 'Creating WhatIf markdown and json files' #
'Set-AzOpsWhatIfOutput.WhatIfResults' = 'WhatIf Output {0}'
'Set-AzOpsWhatIfOutput.WhatIfFileAddingJson' = 'Adding content to WhatIf json file' #
'Set-AzOpsWhatIfOutput.WhatIfFileAddingMd' = 'Adding content to WhatIf markdown file' #
'Set-AzOpsWhatIfOutput.WhatIfFileMax' = 'WhatIf markdown and json files have reached character limit, unable to append more information to files. WhatIf is too large for comment field, for more details look at PR files to determine changes.' # $ResultSizeMaxLimit, $ResultSizeLimit
'Set-AzOpsWhatIfOutput.WhatIfMessageMax' = 'WhatIf have reached maximum character limit, unable to append warning message. WhatIf is too large for comment field, for more details look at PR files to determine changes.' # $ResultSizeMaxLimit, $ResultSizeLimit
'Set-AzOpsWhatIfOutput.WhatIfResults' = 'WhatIf Output {0}' # $results
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - policyAssignments" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "Functional Tests"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "Functional Tests"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - roleAssignments" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "Functional Tests"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "Functional Tests"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - roleEligibilityScheduleRequests" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "Functional Tests"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "Functional Tests"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - vaults" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "Functional Tests"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "Functional Tests"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - managementGroups" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "Functional Tests"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "Functional Tests"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - azureFirewalls" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "BeforeAll"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "BeforeAll"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - networkInterfaces" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "BeforeAll"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "BeforeAll"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - networkSecurityGroups" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "BeforeAll"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "BeforeAll"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - publicIPAddresses" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "BeforeAll"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "BeforeAll"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - routeTables" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "BeforeAll"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "BeforeAll"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - virtualNetworks" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "BeforeAll"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "BeforeAll"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - resourceGroups" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "Functional Tests"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "Functional Tests"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - storageAccounts" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "Functional Tests"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "Functional Tests"
#endregion Paths

#region Push Primer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - serverfarms" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "Functional Tests"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "Functional Tests"
#endregion Paths

#region Push Primer
Expand Down
2 changes: 1 addition & 1 deletion src/tests/functional/Microsoft.Web/sites/scenario.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "Scenario - sites" {
$script:directory = ($script:path).Directory
$script:file = ($script:path).FullName
$script:fileContents = Get-Content -Path $script:file -Raw | ConvertFrom-Json -Depth 25
Write-PSFMessage -Level Debug -Message "TestResoucePath: $($script:file)" -FunctionName "Functional Tests"
Write-PSFMessage -Level Debug -Message "TestResourcePath: $($script:file)" -FunctionName "Functional Tests"
#endregion Paths

#region Push Primer
Expand Down

0 comments on commit 180a151

Please sign in to comment.