Skip to content

Commit

Permalink
What-If to output raw Json (#381)
Browse files Browse the repository at this point in the history
* WhatIf now Outputs JSON

* WhatIf outputs Json

* Added loc for WhatIf message

Co-authored-by: Gordon Byers <gobyers@microsoft.com>
  • Loading branch information
Gordon Byers and Gordon Byers authored Jul 12, 2021
1 parent 00367fc commit bc6aa65
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
19 changes: 7 additions & 12 deletions src/internal/functions/New-AzOpsDeployment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,9 @@
return
}
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
$output = 'WhatIf Results:{0}```json{0}{1}{0}```{0}' -f [environment]::NewLine, ($results.Changes | ConvertTo-Json -Depth 5)
Set-Content -Path '/tmp/OUTPUT.md' -Value $output -WhatIf:$false
Write-PSFMessage -Level Verbose -String 'Set-AzOpsWhatIfOutput.WhatIfResults' -StringValues ($results | Out-String) -Target $scopeObject
Write-PSFMessage -Level Verbose -String 'Set-AzOpsWhatIfOutput.WhatIfFile' -Target $scopeObject
Set-AzOpsWhatIfOutput -results $results
}

$parameters.Name = $DeploymentName
Expand Down Expand Up @@ -148,8 +147,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
$output = 'WhatIf Results:{0}```json{0}{1}{0}```{0}' -f [environment]::NewLine, ($results.Changes | ConvertTo-Json -Depth 5)
Set-Content -Path '/tmp/OUTPUT.md' -Value $output -WhatIf:$false
Set-AzOpsWhatIfOutput -results $results
}

$parameters.Name = $DeploymentName
Expand Down Expand Up @@ -197,8 +195,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
$output = 'WhatIf Results:{0}```json{0}{1}{0}```{0}' -f [environment]::NewLine, ($results.Changes | ConvertTo-Json -Depth 5)
Set-Content -Path '/tmp/OUTPUT.md' -Value $output -WhatIf:$false
Set-AzOpsWhatIfOutput -results $results
}

$parameters.Name = $DeploymentName
Expand Down Expand Up @@ -242,8 +239,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
$output = 'WhatIf Results:{0}```json{0}{1}{0}```{0}' -f [environment]::NewLine, ($results.Changes | ConvertTo-Json -Depth 5)
Set-Content -Path '/tmp/OUTPUT.md' -Value $output -WhatIf:$false
Set-AzOpsWhatIfOutput -results $results
}

$parameters.Name = $DeploymentName
Expand Down Expand Up @@ -283,8 +279,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
$output = 'WhatIf Results:{0}```json{0}{1}{0}```{0}' -f [environment]::NewLine, ($results.Changes | ConvertTo-Json -Depth 5)
Set-Content -Path '/tmp/OUTPUT.md' -Value $output -WhatIf:$false
Set-AzOpsWhatIfOutput -results $results
}

$parameters.Name = $DeploymentName
Expand Down
30 changes: 30 additions & 0 deletions src/internal/functions/Set-AzOpsWhatIfOutput.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function Set-AzOpsWhatIfOutput {

<#
.SYNOPSIS
Logs the output from a What-If deployment
.DESCRIPTION
Logs the output from a What-If deployment
.PARAMETER results
The WhatIf result from a deployment
.EXAMPLE
> et-WhatIfOutput -results $results
#>

[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
$results
)

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

$resultJson=($results.Changes | ConvertTo-Json -Depth 5)
$mdOutput = 'WhatIf Results:{0}```json{0}{1}{0}```{0}' -f [environment]::NewLine, $resultJson

Set-Content -Path '/tmp/OUTPUT.md' -Value $mdOutput -WhatIf:$false
Set-Content -Path '/tmp/OUTPUT.json' -Value $resultJson -WhatIf:$false
}

}
3 changes: 2 additions & 1 deletion src/localized/en-us/Strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,5 @@
'Save-AzOpsManagementGroupChildren.Subscription.NotFound' = 'Unable to locate subscription: {0} within AzOpsSubscriptions object' #child.Name

'Set-AzOpsContext.Change' = 'Changing active subscription from {0} to {1} ({2})' # $context.Subscription.Name, $ScopeObject.SubscriptionDisplayName, $ScopeObject.Subscription
}
'Set-AzOpsWhatIfOutput.WhatIfFile' = 'Creating WhatIf markdown and json files' #
}

0 comments on commit bc6aa65

Please sign in to comment.