forked from Azure/azure-quickstart-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzureQuickstartTemplatesQualityControl.psm1
129 lines (99 loc) · 4.77 KB
/
AzureQuickstartTemplatesQualityControl.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#requires -version 3
# AutoCompletion for
$rootPath = Split-Path $PSScriptRoot -Parent
$completion_Template = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
Get-ChildItem $rootPath -Directory | Sort-Object -Property Name -Unique | Where-Object { $_.Name -like "$wordToComplete*" } |ForEach-Object {
New-Object System.Management.Automation.CompletionResult $_.Name, $_.Name, 'ParameterValue', ('{0} ({1})' -f $_.Description, $_.ID)
}
}
if (-not $global:options) { $global:options = @{CustomArgumentCompleters = @{};NativeArgumentCompleters = @{}}}
$global:options['CustomArgumentCompleters']['Test-AzureQuickstartTemplate:TemplateName'] = $Completion_Template
$function:tabexpansion2 = $function:tabexpansion2 -replace 'End\r\n{','End { if ($null -ne $options) { $options += $global:options} else {$options = $global:options}'
#
function Get-TemplateUniqueValue {
param(
[Parameter(Position = 0, Mandatory = $true)]
[string]
$TemplateName
)
$uniqueTemplateName = '{0}{1}{2}' -f $TemplateName,$env:COMPUTERNAME,$env:USERNAME
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$utf8 = new-object -TypeName System.Text.UTF8Encoding
$hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($uniqueTemplateName)))
'a{0}{1}' -f $env:USERNAME[0],($hash -replace '-','').ToLower().Substring(1,12)
}
<#
.SYNOPSIS
Executes one or many ARM template(s) deployment.
.DESCRIPTION
Enable testing of ARM templates using deterministic value generation that mimic the validation the "Testing Bot" is performing on azure-quickstart-templates.
The script load the parameters file in memory and generate at runtime a deterministic value that will replace '#####' values that are present.
A ResourceGroup will be created and named like your template, each processed template will have their own ResourceGroup
.PARAMETER TemplateName
one or many template name(s). Template name correspond to the name of the folder of a template.
.PARAMETER Location
the location of Resource Group to be created
.EXAMPLE
Execute the deployment of a template named: 100-starter-template-with-validation
Test-AzureQuickstartTemplate -TemplateName '100-starter-template-with-validation'
.NOTES
Make sure you authenticate properly in PowerShell and you are on the desired subscription because this script will not perform login/switch subscription for you.
Clean up is not performed after deployment, make sure you delete the created ResourceGroup to avoid cost.
Created by Stephane Lapointe <[email protected]>
http://www.codeisahighway.com
#>
function Test-AzureQuickstartTemplate {
param(
[Parameter(Position = 0, Mandatory = $true)]
[string[]]
$TemplateName,
[Parameter(Position = 1)]
[string]
$Location = 'West US'
)
$ErrorActionPreference = 'Stop'
if (Get-Command Get-AzureRmContext -ErrorAction SilentlyContinue)
{
$newAzureRmModule = $true
}
else
{
Switch-AzureMode -Name AzureResourceManager
$newAzureRmModule = $false
}
$TemplateName | ForEach-Object -Process {
$currentTemplateName = Split-Path $_ -Leaf
$templateFolder = Resolve-Path -Path $_ -ErrorAction SilentlyContinue
$uniqueValue = Get-TemplateUniqueValue -TemplateName $currentTemplateName
if(-not $templateFolder) {
$templateFolder = Join-Path -Path $PSScriptRoot -ChildPath ..\$_ -Resolve -ErrorAction SilentlyContinue
}
Write-Host -Object ("Processing template '{0}' in location: '{1}' " -f $currentTemplateName, $templateFolder)
if($templateFolder -eq $null) {
Write-Error -Message "Unable to locate/resolve properly template '$currentTemplateName' "
}
#load parameters and replace ##### by unique value
$paramFile = Get-Content -Path $templateFolder\azuredeploy.parameters.json -Raw | ConvertFrom-Json
$params = @{}
$paramFile.parameters | Get-Member -MemberType NoteProperty | ForEach-Object -Process {
$value = $paramFile.parameters.$($_.Name).value
if ($value -eq '#####') {
$value = $uniqueValue
}
$params[$_.Name] = $value
}
$resourceGroupName = 'azure-quickstart-templates--{0}' -f $currentTemplateName
if($newAzureRmModule)
{
New-AzureRmResourceGroup -Name $resourceGroupName -Location $Location -Force -Verbose
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $templateFolder\azuredeploy.json -TemplateParameterObject $params -Mode Complete -Force -Verbose
}
else
{
New-AzureResourceGroup -Name $resourceGroupName -Location $Location -Force -Verbose
New-AzureResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $templateFolder\azuredeploy.json -TemplateParameterObject $params -Mode Complete -Force -Verbose
}
}
}
Export-ModuleMember -Function Test-AzureQuickstartTemplate