-
Notifications
You must be signed in to change notification settings - Fork 437
[New Feature] Add WhatIf feature to Test module locally script #4241
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
Merged
AlexanderSehr
merged 6 commits into
Azure:main
from
TaoYang-Cloud:feature/test-locally-whatif
Nov 28, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7aaf1fd
Add WhatIf feature to Test module locally script
tyconsulting 3104c50
update
tyconsulting bff3022
update
tyconsulting 311df3e
update
tyconsulting eace03c
update
tyconsulting 94a1888
Update modules/web/serverfarm/tests/e2e/max/main.test.bicep
tyconsulting File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
185 changes: 185 additions & 0 deletions
185
utilities/pipelines/resourceDeployment/Get-TemplateDeploymenWhatIf.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Get a template What-If deployment result using a given parameter file | ||
|
|
||
| .DESCRIPTION | ||
| Get a template What-If deployment resultusing a given parameter file | ||
| Works on a resource group, subscription, managementgroup and tenant level | ||
|
|
||
| .PARAMETER parametersBasePath | ||
| Mandatory. The path to the root of the parameters folder to test with | ||
|
|
||
| .PARAMETER templateFilePath | ||
| Mandatory. Path to the template file from root. | ||
|
|
||
| .PARAMETER parameterFilePath | ||
| Optional. Path to the parameter file from root. | ||
|
|
||
| .PARAMETER location | ||
| Mandatory. Location to test in. E.g. WestEurope | ||
|
|
||
| .PARAMETER resourceGroupName | ||
| Optional. Name of the resource group to deploy into. Mandatory if deploying into a resource group (resource group level) | ||
|
|
||
| .PARAMETER subscriptionId | ||
| Optional. ID of the subscription to deploy into. Mandatory if deploying into a subscription (subscription level) using a Management groups service connection | ||
|
|
||
| .PARAMETER managementGroupId | ||
| Optional. Name of the management group to deploy into. Mandatory if deploying into a management group (management group level) | ||
|
|
||
| .PARAMETER additionalParameters | ||
| Optional. Additional parameters you can provide with the deployment. E.g. @{ resourceGroupName = 'myResourceGroup' } | ||
|
|
||
| .EXAMPLE | ||
| Get-TemplateDeploymenWhatIf -templateFilePath 'C:/key-vault/vault/main.bicep' -parameterFilePath 'C:/key-vault/vault/.test/parameters.json' -location 'WestEurope' -resourceGroupName 'aLegendaryRg' | ||
|
|
||
| Get What-If deployment result for the main.bicep of the KeyVault module with the parameter file 'parameters.json' using the resource group 'aLegendaryRg' in location 'WestEurope' | ||
|
|
||
| .EXAMPLE | ||
| Get-TemplateDeploymenWhatIf -templateFilePath 'C:/key-vault/vault/main.bicep' -location 'WestEurope' -resourceGroupName 'aLegendaryRg' | ||
|
|
||
| Get What-If deployment result for the main.bicep of the KeyVault module using the resource group 'aLegendaryRg' in location 'WestEurope' | ||
|
|
||
| .EXAMPLE | ||
| Get-TemplateDeploymenWhatIf -templateFilePath 'C:/resources/resource-group/main.json' -parameterFilePath 'C:/resources/resource-group/.test/parameters.json' -location 'WestEurope' | ||
|
|
||
| Get What-If deployment result for the main.json of the ResourceGroup module with the parameter file 'parameters.json' in location 'WestEurope' | ||
| #> | ||
| function Get-TemplateDeploymenWhatIf { | ||
|
|
||
| [CmdletBinding(SupportsShouldProcess)] | ||
| param ( | ||
| [Parameter(Mandatory)] | ||
| [string] $templateFilePath, | ||
|
|
||
| [Parameter(Mandatory)] | ||
| [string] $location, | ||
|
|
||
| [Parameter(Mandatory = $false)] | ||
| [string] $parameterFilePath, | ||
|
|
||
| [Parameter(Mandatory = $false)] | ||
| [string] $resourceGroupName, | ||
|
|
||
| [Parameter(Mandatory = $false)] | ||
| [string] $subscriptionId, | ||
|
|
||
| [Parameter(Mandatory = $false)] | ||
| [string] $managementGroupId, | ||
|
|
||
| [Parameter(Mandatory = $false)] | ||
| [Hashtable] $additionalParameters | ||
| ) | ||
|
|
||
| begin { | ||
| Write-Debug ('{0} entered' -f $MyInvocation.MyCommand) | ||
|
|
||
| # Load helper | ||
| . (Join-Path (Get-Item -Path $PSScriptRoot).parent.FullName 'sharedScripts' 'Get-ScopeOfTemplateFile.ps1') | ||
| } | ||
|
|
||
| process { | ||
| $DeploymentInputs = @{ | ||
| TemplateFile = $templateFilePath | ||
| Verbose = $true | ||
| OutVariable = 'ValidationErrors' | ||
| } | ||
| if (-not [String]::IsNullOrEmpty($parameterFilePath)) { | ||
| $DeploymentInputs['TemplateParameterFile'] = $parameterFilePath | ||
| } | ||
| $ValidationErrors = $null | ||
|
|
||
| # Additional parameter object provided yes/no | ||
| if ($additionalParameters) { | ||
| $DeploymentInputs += $additionalParameters | ||
| } | ||
|
|
||
| $deploymentScope = Get-ScopeOfTemplateFile -TemplateFilePath $templateFilePath -Verbose | ||
|
|
||
| $deploymentNamePrefix = Split-Path -Path (Split-Path $templateFilePath -Parent) -LeafBase | ||
| if ([String]::IsNullOrEmpty($deploymentNamePrefix)) { | ||
| $deploymentNamePrefix = 'templateDeployment-{0}' -f (Split-Path $templateFilePath -LeafBase) | ||
| } | ||
| if ($templateFilePath -match '.*(\\|\/)Microsoft.+') { | ||
| # If we can assume we're operating in a module structure, we can further fetch the provider namespace & resource type | ||
| $shortPathElem = (($templateFilePath -split 'Microsoft\.')[1] -replace '\\', '/') -split '/' # e.g., AppConfiguration, configurationStores, .test, common, main.test.bicep | ||
| $providerNamespace = $shortPathElem[0] # e.g., AppConfiguration | ||
| $providerNamespaceShort = ($providerNamespace -creplace '[^A-Z]').ToLower() # e.g., ac | ||
|
|
||
| $resourceType = $shortPathElem[1] # e.g., configurationStores | ||
| $resourceTypeShort = ('{0}{1}' -f ($resourceType.ToLower())[0], ($resourceType -creplace '[^A-Z]')).ToLower() # e.g. cs | ||
|
|
||
| $testFolderShort = Split-Path (Split-Path $templateFilePath -Parent) -Leaf # e.g., common | ||
|
|
||
| $deploymentNamePrefix = "$providerNamespaceShort-$resourceTypeShort-$testFolderShort" # e.g., ac-cs-common | ||
| } | ||
|
|
||
| # Generate a valid deployment name. Must match ^[-\w\._\(\)]+$ | ||
| do { | ||
| $deploymentName = ('{0}-{1}' -f $deploymentNamePrefix, (Get-Date -Format 'yyyyMMddTHHMMssffffZ'))[0..63] -join '' | ||
| } while ($deploymentName -notmatch '^[-\w\._\(\)]+$') | ||
|
|
||
| if ($deploymentScope -ne 'resourceGroup') { | ||
| Write-Verbose "What-If Deployment Test with deployment name [$deploymentName]" -Verbose | ||
| $DeploymentInputs['DeploymentName'] = $deploymentName | ||
| } | ||
|
|
||
| ################# | ||
| ## INVOKE TEST ## | ||
| ################# | ||
| switch ($deploymentScope) { | ||
| 'resourceGroup' { | ||
| if (-not [String]::IsNullOrEmpty($subscriptionId)) { | ||
| Write-Verbose ('Setting context to subscription [{0}]' -f $subscriptionId) | ||
| $null = Set-AzContext -Subscription $subscriptionId | ||
| } | ||
| if (-not (Get-AzResourceGroup -Name $resourceGroupName -ErrorAction 'SilentlyContinue')) { | ||
| if ($PSCmdlet.ShouldProcess("Resource group [$resourceGroupName] in location [$location]", 'Create')) { | ||
| $null = New-AzResourceGroup -Name $resourceGroupName -Location $location | ||
| } | ||
| } | ||
| if ($PSCmdlet.ShouldProcess('Resource group level deployment', 'WhatIf')) { | ||
| $res = New-AzResourceGroupDeployment @DeploymentInputs -WhatIf | ||
| } | ||
| break | ||
| } | ||
| 'subscription' { | ||
| if (-not [String]::IsNullOrEmpty($subscriptionId)) { | ||
| Write-Verbose ('Setting context to subscription [{0}]' -f $subscriptionId) | ||
| $null = Set-AzContext -Subscription $subscriptionId | ||
| } | ||
| if ($PSCmdlet.ShouldProcess('Subscription level deployment', 'WhatIf')) { | ||
| $res = New-AzDeployment @DeploymentInputs -Location $Location -WhatIf | ||
| } | ||
| break | ||
| } | ||
| 'managementGroup' { | ||
| if ($PSCmdlet.ShouldProcess('Management group level deployment', 'WhatIf')) { | ||
| $res = New-AzManagementGroupDeployment @DeploymentInputs -Location $Location -ManagementGroupId $ManagementGroupId -WhatIf | ||
| } | ||
| break | ||
| } | ||
| 'tenant' { | ||
| Write-Verbose 'Handling tenant level validation' | ||
| if ($PSCmdlet.ShouldProcess('Tenant level deployment', 'WhatIf')) { | ||
| $res = New-AzTenantDeployment @DeploymentInputs -Location $location -WhatIf | ||
| } | ||
| break | ||
| } | ||
| default { | ||
| throw "[$deploymentScope] is a non-supported template scope" | ||
| } | ||
| } | ||
| if ($ValidationErrors) { | ||
| if ($res.Details) { Write-Warning ($res.Details | ConvertTo-Json -Depth 10 | Out-String) } | ||
| if ($res.Message) { Write-Warning $res.Message } | ||
| Write-Error 'Template is not valid.' | ||
| } else { | ||
| Write-Verbose 'Template is valid' -Verbose | ||
| } | ||
| } | ||
|
|
||
| end { | ||
| Write-Debug ('{0} exited' -f $MyInvocation.MyCommand) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.