Skip to content

Commit 3dac5ee

Browse files
azure-sdkbenbp
andauthored
Sync eng/common directory with azure-sdk-tools for PR 1163 (Azure#12346)
* Update subscription configuration schema to include new parameters * Support platform specific arm template parameters and legacy hashtable format * Update arm template parameter comment to include top level key * Restore AdditionalParameters. Merge ArmTemplateParameters from stringified hash literal * Handle duplicate keys more explicitly for arm and env vars * Regenerate New-TestResources.ps1 markdown * revert variable name to environmentVariables to fix post-scripts * Handle empty arm template parameters better * Remove arm template parameter merge logic from deploy template * Add merge hashes function to New-TestResources.ps1 * Add merge hashes function to New-TestResources.ps1 * Add env variable overwrite warning. Use ContainsKey checks * Temporarily manually fix invalid generated markdown links Co-authored-by: Ben Broderick Phillips <bebroder@microsoft.com>
1 parent dc39579 commit 3dac5ee

File tree

4 files changed

+101
-26
lines changed

4 files changed

+101
-26
lines changed

eng/common/TestResources/New-TestResources.ps1

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
[CmdletBinding(DefaultParameterSetName = 'Default', SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
1212
param (
13-
# Limit $BaseName to enough characters to be under limit plus prefixes, and https://docs.microsoft.com/azure/architecture/best-practices/resource-naming.
13+
# Limit $BaseName to enough characters to be under limit plus prefixes, and https://docs.microsoft.com/azure/architecture/best-practices/resource-naming
1414
[Parameter()]
1515
[ValidatePattern('^[-a-zA-Z0-9\.\(\)_]{0,80}(?<=[a-zA-Z0-9\(\)])$')]
1616
[string] $BaseName,
@@ -58,9 +58,16 @@ param (
5858
[ValidateSet('AzureCloud', 'AzureUSGovernment', 'AzureChinaCloud', 'Dogfood')]
5959
[string] $Environment = 'AzureCloud',
6060

61+
[Parameter()]
62+
[hashtable] $ArmTemplateParameters,
63+
6164
[Parameter()]
6265
[hashtable] $AdditionalParameters,
6366

67+
[Parameter()]
68+
[ValidateNotNull()]
69+
[hashtable] $EnvironmentVariables = @{},
70+
6471
[Parameter()]
6572
[switch] $CI = ($null -ne $env:SYSTEM_TEAMPROJECTID),
6673

@@ -101,6 +108,16 @@ function Retry([scriptblock] $Action, [int] $Attempts = 5) {
101108
}
102109
}
103110

111+
function MergeHashes([hashtable] $source, [psvariable] $dest) {
112+
foreach ($key in $source.Keys) {
113+
if ($dest.Value.ContainsKey($key) -and $dest.Value[$key] -ne $source[$key]) {
114+
Write-Warning ("Overwriting '$($dest.Name).$($key)' with value '$($dest.Value[$key])' " +
115+
"to new value '$($source[$key])'")
116+
}
117+
$dest.Value[$key] = $source[$key]
118+
}
119+
}
120+
104121
# Support actions to invoke on exit.
105122
$exitActions = @({
106123
if ($exitActions.Count -gt 1) {
@@ -118,7 +135,6 @@ $repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path
118135
$root = [System.IO.Path]::Combine($repositoryRoot, "sdk", $ServiceDirectory) | Resolve-Path
119136
$templateFileName = 'test-resources.json'
120137
$templateFiles = @()
121-
$environmentVariables = @{}
122138
# Azure SDK Developer Playground
123139
$defaultSubscription = "faa080af-c1d8-40ad-9cce-e1a450ca5b57"
124140

@@ -252,7 +268,7 @@ $serviceName = if (Split-Path -IsAbsolute $ServiceDirectory) {
252268
$ServiceDirectory
253269
}
254270

255-
if ($CI) {
271+
if ($CI) {
256272
$BaseName = 't' + (New-Guid).ToString('n').Substring(0, 16)
257273
Write-Verbose "Generated base name '$BaseName' for CI build"
258274
}
@@ -289,7 +305,13 @@ if ($CI) {
289305
# Set the resource group name variable.
290306
Write-Host "Setting variable 'AZURE_RESOURCEGROUP_NAME': $ResourceGroupName"
291307
Write-Host "##vso[task.setvariable variable=AZURE_RESOURCEGROUP_NAME;]$ResourceGroupName"
292-
$environmentVariables['AZURE_RESOURCEGROUP_NAME'] = $ResourceGroupName
308+
if ($EnvironmentVariables.ContainsKey('AZURE_RESOURCEGROUP_NAME') -and `
309+
$EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] -ne $ResourceGroupName)
310+
{
311+
Write-Warning ("Overwriting 'EnvironmentVariables.AZURE_RESOURCEGROUP_NAME' with value " +
312+
"'$($EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'])' " + "to new value '$($ResourceGroupName)'")
313+
}
314+
$EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] = $ResourceGroupName
293315
}
294316

295317
Log "Creating resource group '$ResourceGroupName' in location '$Location'"
@@ -322,11 +344,11 @@ if ($TenantId) {
322344
if ($TestApplicationSecret) {
323345
$templateParameters.Add('testApplicationSecret', $TestApplicationSecret)
324346
}
325-
if ($AdditionalParameters) {
326-
$templateParameters += $AdditionalParameters
327-
}
328347

329-
# Include environment-specific parameters only if not already provided as part of the "AdditionalParameters"
348+
MergeHashes $ArmTemplateParameters $(Get-Variable templateParameters)
349+
MergeHashes $AdditionalParameters $(Get-Variable templateParameters)
350+
351+
# Include environment-specific parameters only if not already provided as part of the "ArmTemplateParameters"
330352
if (($context.Environment.StorageEndpointSuffix) -and (-not ($templateParameters.ContainsKey('storageEndpointSuffix')))) {
331353
$templateParameters.Add('storageEndpointSuffix', $context.Environment.StorageEndpointSuffix)
332354
}
@@ -388,6 +410,8 @@ foreach ($templateFile in $templateFiles) {
388410
"$($serviceDirectoryPrefix)STORAGE_ENDPOINT_SUFFIX" = $context.Environment.StorageEndpointSuffix;
389411
}
390412

413+
MergeHashes $EnvironmentVariables $(Get-Variable deploymentOutputs)
414+
391415
foreach ($key in $deployment.Outputs.Keys) {
392416
$variable = $deployment.Outputs[$key]
393417

@@ -422,7 +446,7 @@ foreach ($templateFile in $templateFiles) {
422446

423447
foreach ($key in $deploymentOutputs.Keys) {
424448
$value = $deploymentOutputs[$key]
425-
$environmentVariables[$key] = $value
449+
$EnvironmentVariables[$key] = $value
426450

427451
if ($CI) {
428452
# Treat all ARM template output variables as secrets since "SecureString" variables do not set values.
@@ -453,7 +477,7 @@ $exitActions.Invoke()
453477

454478
# Suppress output locally
455479
if ($CI) {
456-
return $environmentVariables
480+
return $EnvironmentVariables
457481
}
458482

459483
<#
@@ -571,8 +595,14 @@ Name of the cloud environment. The default is the Azure Public Cloud
571595
('AzureCloud')
572596
573597
.PARAMETER AdditionalParameters
598+
Optional key-value pairs of parameters to pass to the ARM template(s) and pre-post scripts.
599+
600+
.PARAMETER ArmTemplateParameters
574601
Optional key-value pairs of parameters to pass to the ARM template(s).
575602
603+
.PARAMETER EnvironmentVariables
604+
Optional key-value pairs of parameters to set as environment variables to the shell.
605+
576606
.PARAMETER CI
577607
Indicates the script is run as part of a Continuous Integration / Continuous
578608
Deployment (CI/CD) build (only Azure Pipelines is currently supported).
@@ -617,6 +647,4 @@ Run this in an Azure DevOps CI (with approrpiate variables configured) before
617647
executing live tests. The script will output variables as secrets (to enable
618648
log redaction).
619649
620-
.LINK
621-
Remove-TestResources.ps1
622650
#>

eng/common/TestResources/New-TestResources.ps1.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ Deploys live test resources defined for a service directory to Azure.
1616
```
1717
New-TestResources.ps1 [-BaseName <String>] [-ResourceGroupName <String>] [-ServiceDirectory] <String>
1818
[-TestApplicationId <String>] [-TestApplicationSecret <String>] [-TestApplicationOid <String>]
19-
[-DeleteAfterHours <Int32>] [-Location <String>] [-Environment <String>] [-AdditionalParameters <Hashtable>]
20-
[-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] [<CommonParameters>]
19+
[-DeleteAfterHours <Int32>] [-Location <String>] [-Environment <String>] [-ArmTemplateParameters <Hashtable>]
20+
[-AdditionalParameters <Hashtable>] [-EnvironmentVariables <Hashtable>] [-CI] [-Force] [-OutFile] [-WhatIf]
21+
[-Confirm] [<CommonParameters>]
2122
```
2223

2324
### Provisioner
@@ -26,8 +27,8 @@ New-TestResources.ps1 [-BaseName <String>] [-ResourceGroupName <String>] [-Servi
2627
[-TestApplicationId <String>] [-TestApplicationSecret <String>] [-TestApplicationOid <String>]
2728
-TenantId <String> [-SubscriptionId <String>] -ProvisionerApplicationId <String>
2829
-ProvisionerApplicationSecret <String> [-DeleteAfterHours <Int32>] [-Location <String>]
29-
[-Environment <String>] [-AdditionalParameters <Hashtable>] [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm]
30-
[<CommonParameters>]
30+
[-Environment <String>] [-ArmTemplateParameters <Hashtable>] [-AdditionalParameters <Hashtable>]
31+
[-EnvironmentVariables <Hashtable>] [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] [<CommonParameters>]
3132
```
3233

3334
## DESCRIPTION
@@ -339,7 +340,7 @@ Accept wildcard characters: False
339340
### -Environment
340341
Name of the cloud environment.
341342
The default is the Azure Public Cloud
342-
('PublicCloud')
343+
('AzureCloud')
343344
344345
```yaml
345346
Type: String
@@ -353,7 +354,7 @@ Accept pipeline input: False
353354
Accept wildcard characters: False
354355
```
355356
356-
### -AdditionalParameters
357+
### -ArmTemplateParameters
357358
Optional key-value pairs of parameters to pass to the ARM template(s).
358359
359360
```yaml
@@ -368,6 +369,36 @@ Accept pipeline input: False
368369
Accept wildcard characters: False
369370
```
370371
372+
### -AdditionalParameters
373+
Optional key-value pairs of parameters to pass to the ARM template(s) and pre-post scripts.
374+
375+
```yaml
376+
Type: Hashtable
377+
Parameter Sets: (All)
378+
Aliases:
379+
380+
Required: False
381+
Position: Named
382+
Default value: None
383+
Accept pipeline input: False
384+
Accept wildcard characters: False
385+
```
386+
387+
### -EnvironmentVariables
388+
Optional key-value pairs of parameters to set as environment variables to the shell.
389+
390+
```yaml
391+
Type: Hashtable
392+
Parameter Sets: (All)
393+
Aliases:
394+
395+
Required: False
396+
Position: Named
397+
Default value: @{}
398+
Accept pipeline input: False
399+
Accept wildcard characters: False
400+
```
401+
371402
### -CI
372403
Indicates the script is run as part of a Continuous Integration / Continuous
373404
Deployment (CI/CD) build (only Azure Pipelines is currently supported).

eng/common/TestResources/Remove-TestResources.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,4 @@ Remove-TestResources.ps1 `
214214
When run in the context of an Azure DevOps pipeline, this script removes the
215215
resource group whose name is stored in the environment variable
216216
AZURE_RESOURCEGROUP_NAME.
217-
.LINK
218-
New-TestResources.ps1
219217
#>

eng/common/TestResources/deploy-test-resources.yml

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ parameters:
55
Location: ''
66
SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources)
77

8-
# SubscriptionConfiguration will be splat into the parameters of the test
8+
# SubscriptionConfiguration will be splatted into the parameters of the test
99
# resources script. It should be JSON in the form:
1010
# {
1111
# "SubscriptionId": "<subscription id>",
@@ -15,25 +15,43 @@ parameters:
1515
# "ProvisionerApplicationId": "<provisioner app id>",
1616
# "ProvisionerApplicationSecret": "<provisioner app secret>",
1717
# "Environment": "AzureCloud | AzureGov | AzureChina | <other environment>"
18+
# "EnvironmentVariables": {
19+
# "SERVICE_MANAGEMENT_URL": "<service management url>",
20+
# "STORAGE_ENDPOINT_SUFFIX": "<storage endpoint suffix>",
21+
# "RESOURCE_MANAGER_URL": "<resource manager url>",
22+
# "SEARCH_ENDPOINT_SUFFIX": "<search endpoint suffix>",
23+
# "COSMOS_TABLES_ENDPOINT_SUFFIX": "<cosmos tables endpoint suffix>"
24+
# },
25+
# "ArmTemplateParameters": {
26+
# "keyVaultDomainSuffix": "<keyVaultDomainSuffix>",
27+
# "storageEndpointSuffix": "<storageEndpointSuffix>",
28+
# "endpointSuffix": "<endpointSuffix>",
29+
# "azureAuthorityHost": "<azureAuthorityHost>",
30+
# "keyVaultEndpointSuffix": "<keyVaultEndpointSuffix>"
31+
# }
1832
# }
1933

34+
2035
steps:
2136
- template: /eng/common/TestResources/setup-az-modules.yml
2237

2338
- pwsh: |
2439
eng/common/TestResources/Import-AzModules.ps1
2540
26-
$subscriptionConfiguration = @"
41+
$subscriptionConfiguration = @'
2742
${{ parameters.SubscriptionConfiguration }}
28-
"@ | ConvertFrom-Json -AsHashtable;
43+
'@ | ConvertFrom-Json -AsHashtable;
2944
45+
# The subscriptionConfiguration may have ArmTemplateParameters defined, so
46+
# pass those in via the ArmTemplateParameters flag, and handle any
47+
# additional parameters from the pipelines via AdditionalParameters
3048
eng/common/TestResources/New-TestResources.ps1 `
3149
-BaseName 'Generated' `
32-
-ServiceDirectory ${{ parameters.ServiceDirectory }} `
50+
-ServiceDirectory '${{ parameters.ServiceDirectory }}' `
3351
-Location '${{ parameters.Location }}' `
34-
-DeleteAfterHours ${{ parameters.DeleteAfterHours }} `
35-
-AdditionalParameters ${{ parameters.ArmTemplateParameters }} `
52+
-DeleteAfterHours '${{ parameters.DeleteAfterHours }}' `
3653
@subscriptionConfiguration `
54+
-AdditionalParameters ${{ parameters.ArmTemplateParameters }} `
3755
-CI `
3856
-Force `
3957
-Verbose | Out-Null

0 commit comments

Comments
 (0)