From a67dfdf795f4739be9619ca63e787e1d4a498cdd Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Mon, 2 Nov 2020 15:27:58 -0500 Subject: [PATCH 01/13] Update subscription configuration schema to include new parameters --- .../TestResources/deploy-test-resources.yml | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/eng/common/TestResources/deploy-test-resources.yml b/eng/common/TestResources/deploy-test-resources.yml index 6f0db25d61a8..98c3201197ef 100644 --- a/eng/common/TestResources/deploy-test-resources.yml +++ b/eng/common/TestResources/deploy-test-resources.yml @@ -15,8 +15,23 @@ parameters: # "ProvisionerApplicationId": "", # "ProvisionerApplicationSecret": "", # "Environment": "AzureCloud | AzureGov | AzureChina | " +# "EnvironmentVariables": { +# "SERVICE_MANAGEMENT_URL": "", +# "STORAGE_ENDPOINT_SUFFIX": "", +# "RESOURCE_MANAGER_URL": "", +# "SEARCH_ENDPOINT_SUFFIX": "", +# "COSMOS_TABLES_ENDPOINT_SUFFIX": "" +# }, +# "ArmTemplateParameters": { +# "keyVaultDomainSuffix": "", +# "storageEndpointSuffix": "", +# "endpointSuffix": "", +# "azureAuthorityHost": "", +# "keyVaultEndpointSuffix": "" +# } # } + steps: - template: /eng/common/TestResources/setup-az-modules.yml @@ -27,13 +42,31 @@ steps: ${{ parameters.SubscriptionConfiguration }} "@ | ConvertFrom-Json -AsHashtable; + if ($subscriptionConfiguration.ArmTemplateParameters) { + # Handle subscription configuration containing arm template parameters + $armTemplateParameters = $subscriptionConfiguration.ArmTemplateParameters + } else { + # Handle legacy subscription configuration, with arm template params passed in by templates + $armTemplateParameters = ${{ parameters.ArmTemplateParameters }} + } + + $accessParameters = @{ + "SubscriptionId" = $subscriptionConfiguration.SubscriptionId; + "TenantId" = $subscriptionConfiguration.TenantId; + "TestApplicationId" = $subscriptionConfiguration.TestApplicationId; + "TestApplicationSecret" = $subscriptionConfiguration.TestApplicationSecret; + "ProvisionerApplicationId" = $subscriptionConfiguration.ProvisionerApplicationId; + "ProvisionerApplicationSecret" = $subscriptionConfiguration.ProvisionerApplicationSecret; + "Environment" = $subscriptionConfiguration.Environment + } + eng/common/TestResources/New-TestResources.ps1 ` -BaseName 'Generated' ` -ServiceDirectory ${{ parameters.ServiceDirectory }} ` -Location '${{ parameters.Location }}' ` -DeleteAfterHours ${{ parameters.DeleteAfterHours }} ` - -AdditionalParameters ${{ parameters.ArmTemplateParameters }} ` - @subscriptionConfiguration ` + -AdditionalParameters $armTemplateParameters ` + @accessParameters ` -CI ` -Force ` -Verbose | Out-Null From 6068b12ee7813365e2f20b181f71e9986787f693 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Tue, 3 Nov 2020 21:11:00 -0500 Subject: [PATCH 02/13] Support platform specific arm template parameters and legacy hashtable format --- .../TestResources/New-TestResources.ps1 | 16 ++++-- .../TestResources/deploy-test-resources.yml | 51 +++++++++++-------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 8de70b50dce7..828d6f1c7d8d 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -59,7 +59,10 @@ param ( [string] $Environment = 'AzureCloud', [Parameter()] - [hashtable] $AdditionalParameters, + [hashtable] $ArmTemplateParameters, + + [Parameter()] + [hashtable] $EnvironmentVariables, [Parameter()] [switch] $CI = ($null -ne $env:SYSTEM_TEAMPROJECTID), @@ -322,11 +325,11 @@ if ($TenantId) { if ($TestApplicationSecret) { $templateParameters.Add('testApplicationSecret', $TestApplicationSecret) } -if ($AdditionalParameters) { - $templateParameters += $AdditionalParameters +if ($ArmTemplateParameters) { + $templateParameters += $ArmTemplateParameters } -# Include environment-specific parameters only if not already provided as part of the "AdditionalParameters" +# Include environment-specific parameters only if not already provided as part of the "ArmTemplateParameters" if (($context.Environment.StorageEndpointSuffix) -and (-not ($templateParameters.ContainsKey('storageEndpointSuffix')))) { $templateParameters.Add('storageEndpointSuffix', $context.Environment.StorageEndpointSuffix) } @@ -570,9 +573,12 @@ is based on the cloud to which the template is being deployed: Name of the cloud environment. The default is the Azure Public Cloud ('AzureCloud') -.PARAMETER AdditionalParameters +.PARAMETER ArmTemplateParameters Optional key-value pairs of parameters to pass to the ARM template(s). +.PARAMETER EnvironmentVariables +Optional key-value pairs of parameters to set as environment variables to the shell. + .PARAMETER CI Indicates the script is run as part of a Continuous Integration / Continuous Deployment (CI/CD) build (only Azure Pipelines is currently supported). diff --git a/eng/common/TestResources/deploy-test-resources.yml b/eng/common/TestResources/deploy-test-resources.yml index 98c3201197ef..dcbffd3be202 100644 --- a/eng/common/TestResources/deploy-test-resources.yml +++ b/eng/common/TestResources/deploy-test-resources.yml @@ -1,9 +1,11 @@ parameters: ServiceDirectory: not-set - ArmTemplateParameters: '@{}' + ArmTemplateParameters: '$subscriptionConfiguration.ArmTemplateParameters' + PlatformArmTemplateParameters: '' DeleteAfterHours: 24 Location: '' SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources) + Platform: '' # SubscriptionConfiguration will be splat into the parameters of the test # resources script. It should be JSON in the form: @@ -38,35 +40,40 @@ steps: - pwsh: | eng/common/TestResources/Import-AzModules.ps1 - $subscriptionConfiguration = @" + $subscriptionConfiguration = @' ${{ parameters.SubscriptionConfiguration }} - "@ | ConvertFrom-Json -AsHashtable; + '@ | ConvertFrom-Json -AsHashtable; - if ($subscriptionConfiguration.ArmTemplateParameters) { - # Handle subscription configuration containing arm template parameters - $armTemplateParameters = $subscriptionConfiguration.ArmTemplateParameters - } else { - # Handle legacy subscription configuration, with arm template params passed in by templates - $armTemplateParameters = ${{ parameters.ArmTemplateParameters }} - } + # Support legacy ArmTemplateParameters passed in directly as a hashmap, otherwise default to subscriptionConfiguration JSON. + # Example legacy input: @{ 'param1' = 'foo' ; 'param2' = 'bar' } + $subscriptionConfiguration.ArmTemplateParameters = ${{ parameters.ArmTemplateParameters }} ? ${{ parameters.ArmTemplateParameters }} : @{} + + # Support additional sdk pipeline and/or platform specific arm template parameters: + # EnableHsm: + # Value: true + # Platforms: + # - Linux_NetCore_Keyring + # TestGlobalParam: + # Value: global foo + # For duplicate keys, prefer the template/script parameter. + $platformArmTemplateParameters = @' + ${{ parameters.PlatformArmTemplateParameters }} + '@ | ConvertFrom-Json -AsHashtable; - $accessParameters = @{ - "SubscriptionId" = $subscriptionConfiguration.SubscriptionId; - "TenantId" = $subscriptionConfiguration.TenantId; - "TestApplicationId" = $subscriptionConfiguration.TestApplicationId; - "TestApplicationSecret" = $subscriptionConfiguration.TestApplicationSecret; - "ProvisionerApplicationId" = $subscriptionConfiguration.ProvisionerApplicationId; - "ProvisionerApplicationSecret" = $subscriptionConfiguration.ProvisionerApplicationSecret; - "Environment" = $subscriptionConfiguration.Environment + if ($platformArmTemplateParameters) { + foreach ($p in $platformArmTemplateParameters.GetEnumerator()) { + if (!$p.Value.Platforms -or '${{ parameters.Platform }}' -in $p.Value.Platforms) { + $subscriptionConfiguration.ArmTemplateParameters[$p.Name] = $p.Value.Value + } + } } eng/common/TestResources/New-TestResources.ps1 ` -BaseName 'Generated' ` - -ServiceDirectory ${{ parameters.ServiceDirectory }} ` + -ServiceDirectory '${{ parameters.ServiceDirectory }}' ` -Location '${{ parameters.Location }}' ` - -DeleteAfterHours ${{ parameters.DeleteAfterHours }} ` - -AdditionalParameters $armTemplateParameters ` - @accessParameters ` + -DeleteAfterHours '${{ parameters.DeleteAfterHours }}' ` + @subscriptionConfiguration ` -CI ` -Force ` -Verbose | Out-Null From e4613f63da1bb59d66582fb0e6ddd7af9035205c Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Wed, 4 Nov 2020 19:48:02 -0500 Subject: [PATCH 03/13] Update arm template parameter comment to include top level key --- eng/common/TestResources/deploy-test-resources.yml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/eng/common/TestResources/deploy-test-resources.yml b/eng/common/TestResources/deploy-test-resources.yml index dcbffd3be202..c8920767cc1f 100644 --- a/eng/common/TestResources/deploy-test-resources.yml +++ b/eng/common/TestResources/deploy-test-resources.yml @@ -49,12 +49,13 @@ steps: $subscriptionConfiguration.ArmTemplateParameters = ${{ parameters.ArmTemplateParameters }} ? ${{ parameters.ArmTemplateParameters }} : @{} # Support additional sdk pipeline and/or platform specific arm template parameters: - # EnableHsm: - # Value: true - # Platforms: - # - Linux_NetCore_Keyring - # TestGlobalParam: - # Value: global foo + # ArmTemplateParameters: + # EnableHsm: + # Value: true + # Platforms: + # - Linux_NetCore_Keyring + # TestGlobalParam: + # Value: global foo # For duplicate keys, prefer the template/script parameter. $platformArmTemplateParameters = @' ${{ parameters.PlatformArmTemplateParameters }} From 68757cf4f77b73e2e68d95bbe224c6f5fec365d8 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Thu, 5 Nov 2020 03:23:03 -0500 Subject: [PATCH 04/13] Restore AdditionalParameters. Merge ArmTemplateParameters from stringified hash literal --- .../TestResources/New-TestResources.ps1 | 21 +++++++++--- .../TestResources/deploy-test-resources.yml | 34 +++++-------------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 828d6f1c7d8d..664f3549b910 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -61,6 +61,9 @@ param ( [Parameter()] [hashtable] $ArmTemplateParameters, + [Parameter()] + [hashtable] $AdditionalParameters, + [Parameter()] [hashtable] $EnvironmentVariables, @@ -121,7 +124,7 @@ $repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path $root = [System.IO.Path]::Combine($repositoryRoot, "sdk", $ServiceDirectory) | Resolve-Path $templateFileName = 'test-resources.json' $templateFiles = @() -$environmentVariables = @{} +$outputEnvironmentVariables = @{} # Azure SDK Developer Playground $defaultSubscription = "faa080af-c1d8-40ad-9cce-e1a450ca5b57" @@ -292,7 +295,7 @@ if ($CI) { # Set the resource group name variable. Write-Host "Setting variable 'AZURE_RESOURCEGROUP_NAME': $ResourceGroupName" Write-Host "##vso[task.setvariable variable=AZURE_RESOURCEGROUP_NAME;]$ResourceGroupName" - $environmentVariables['AZURE_RESOURCEGROUP_NAME'] = $ResourceGroupName + $outputEnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] = $ResourceGroupName } Log "Creating resource group '$ResourceGroupName' in location '$Location'" @@ -328,6 +331,9 @@ if ($TestApplicationSecret) { if ($ArmTemplateParameters) { $templateParameters += $ArmTemplateParameters } +if ($AdditionalParameters) { + $templateParameters += $AdditionalParameters +} # Include environment-specific parameters only if not already provided as part of the "ArmTemplateParameters" if (($context.Environment.StorageEndpointSuffix) -and (-not ($templateParameters.ContainsKey('storageEndpointSuffix')))) { @@ -391,6 +397,10 @@ foreach ($templateFile in $templateFiles) { "$($serviceDirectoryPrefix)STORAGE_ENDPOINT_SUFFIX" = $context.Environment.StorageEndpointSuffix; } + foreach ($ev in $EnvironmentVariables) { + $deploymentOutputs[$ev.Name] = $ev.Value + } + foreach ($key in $deployment.Outputs.Keys) { $variable = $deployment.Outputs[$key] @@ -425,7 +435,7 @@ foreach ($templateFile in $templateFiles) { foreach ($key in $deploymentOutputs.Keys) { $value = $deploymentOutputs[$key] - $environmentVariables[$key] = $value + $outputEnvironmentVariables[$key] = $value if ($CI) { # Treat all ARM template output variables as secrets since "SecureString" variables do not set values. @@ -456,7 +466,7 @@ $exitActions.Invoke() # Suppress output locally if ($CI) { - return $environmentVariables + return $outputEnvironmentVariables } <# @@ -573,6 +583,9 @@ is based on the cloud to which the template is being deployed: Name of the cloud environment. The default is the Azure Public Cloud ('AzureCloud') +.PARAMETER AdditionalParameters +Optional key-value pairs of parameters to pass to the ARM template(s) and pre-post scripts. + .PARAMETER ArmTemplateParameters Optional key-value pairs of parameters to pass to the ARM template(s). diff --git a/eng/common/TestResources/deploy-test-resources.yml b/eng/common/TestResources/deploy-test-resources.yml index c8920767cc1f..47bb7a02ed54 100644 --- a/eng/common/TestResources/deploy-test-resources.yml +++ b/eng/common/TestResources/deploy-test-resources.yml @@ -1,13 +1,11 @@ parameters: ServiceDirectory: not-set - ArmTemplateParameters: '$subscriptionConfiguration.ArmTemplateParameters' - PlatformArmTemplateParameters: '' + ArmTemplateParameters: '@{}' DeleteAfterHours: 24 Location: '' SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources) - Platform: '' -# SubscriptionConfiguration will be splat into the parameters of the test +# SubscriptionConfiguration will be splatted into the parameters of the test # resources script. It should be JSON in the form: # { # "SubscriptionId": "", @@ -44,29 +42,13 @@ steps: ${{ parameters.SubscriptionConfiguration }} '@ | ConvertFrom-Json -AsHashtable; - # Support legacy ArmTemplateParameters passed in directly as a hashmap, otherwise default to subscriptionConfiguration JSON. - # Example legacy input: @{ 'param1' = 'foo' ; 'param2' = 'bar' } - $subscriptionConfiguration.ArmTemplateParameters = ${{ parameters.ArmTemplateParameters }} ? ${{ parameters.ArmTemplateParameters }} : @{} - - # Support additional sdk pipeline and/or platform specific arm template parameters: - # ArmTemplateParameters: - # EnableHsm: - # Value: true - # Platforms: - # - Linux_NetCore_Keyring - # TestGlobalParam: - # Value: global foo - # For duplicate keys, prefer the template/script parameter. - $platformArmTemplateParameters = @' - ${{ parameters.PlatformArmTemplateParameters }} - '@ | ConvertFrom-Json -AsHashtable; + if (!$subscriptionConfiguration.ArmTemplateParameters) { + $subscriptionConfiguration.ArmTemplateParameters = @{} + } - if ($platformArmTemplateParameters) { - foreach ($p in $platformArmTemplateParameters.GetEnumerator()) { - if (!$p.Value.Platforms -or '${{ parameters.Platform }}' -in $p.Value.Platforms) { - $subscriptionConfiguration.ArmTemplateParameters[$p.Name] = $p.Value.Value - } - } + $armTemplateParameters = ${{ parameters.ArmTemplateParameters }} + foreach ($p in $armTemplateParameters.GetEnumerator()) { + $subscriptionConfiguration.ArmTemplateParameters[$p.Name] = $p.Value } eng/common/TestResources/New-TestResources.ps1 ` From b1e7d4a693c5ea32ac6e732327b381632b199972 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Thu, 5 Nov 2020 19:21:20 -0500 Subject: [PATCH 05/13] Handle duplicate keys more explicitly for arm and env vars --- eng/common/TestResources/New-TestResources.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 664f3549b910..a5966ab65ec2 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -331,8 +331,8 @@ if ($TestApplicationSecret) { if ($ArmTemplateParameters) { $templateParameters += $ArmTemplateParameters } -if ($AdditionalParameters) { - $templateParameters += $AdditionalParameters +foreach ($key in $AdditionalParameters.Keys) { + $templateParameters[$key] = $AdditionalParameters[$key] } # Include environment-specific parameters only if not already provided as part of the "ArmTemplateParameters" @@ -397,8 +397,8 @@ foreach ($templateFile in $templateFiles) { "$($serviceDirectoryPrefix)STORAGE_ENDPOINT_SUFFIX" = $context.Environment.StorageEndpointSuffix; } - foreach ($ev in $EnvironmentVariables) { - $deploymentOutputs[$ev.Name] = $ev.Value + foreach ($key in $EnvironmentVariables.Keys) { + $deploymentOutputs.Add($key, $EnvironmentVariables[$key]) } foreach ($key in $deployment.Outputs.Keys) { From de998b5fa157c0cc35b699330d6ff675a1ba0cb0 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Thu, 5 Nov 2020 19:23:15 -0500 Subject: [PATCH 06/13] Regenerate New-TestResources.ps1 markdown --- .../TestResources/New-TestResources.ps1.md | 48 ++++++++++++++++--- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1.md b/eng/common/TestResources/New-TestResources.ps1.md index c41693c87666..2af2f8c96617 100644 --- a/eng/common/TestResources/New-TestResources.ps1.md +++ b/eng/common/TestResources/New-TestResources.ps1.md @@ -16,8 +16,9 @@ Deploys live test resources defined for a service directory to Azure. ``` New-TestResources.ps1 [-BaseName ] [-ResourceGroupName ] [-ServiceDirectory] [-TestApplicationId ] [-TestApplicationSecret ] [-TestApplicationOid ] - [-DeleteAfterHours ] [-Location ] [-Environment ] [-AdditionalParameters ] - [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] [] + [-DeleteAfterHours ] [-Location ] [-Environment ] [-ArmTemplateParameters ] + [-AdditionalParameters ] [-EnvironmentVariables ] [-CI] [-Force] [-OutFile] [-WhatIf] + [-Confirm] [] ``` ### Provisioner @@ -26,8 +27,8 @@ New-TestResources.ps1 [-BaseName ] [-ResourceGroupName ] [-Servi [-TestApplicationId ] [-TestApplicationSecret ] [-TestApplicationOid ] -TenantId [-SubscriptionId ] -ProvisionerApplicationId -ProvisionerApplicationSecret [-DeleteAfterHours ] [-Location ] - [-Environment ] [-AdditionalParameters ] [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] - [] + [-Environment ] [-ArmTemplateParameters ] [-AdditionalParameters ] + [-EnvironmentVariables ] [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] [] ``` ## DESCRIPTION @@ -339,7 +340,7 @@ Accept wildcard characters: False ### -Environment Name of the cloud environment. The default is the Azure Public Cloud -('PublicCloud') +('AzureCloud') ```yaml Type: String @@ -353,7 +354,7 @@ Accept pipeline input: False Accept wildcard characters: False ``` -### -AdditionalParameters +### -ArmTemplateParameters Optional key-value pairs of parameters to pass to the ARM template(s). ```yaml @@ -368,6 +369,36 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -AdditionalParameters +Optional key-value pairs of parameters to pass to the ARM template(s) and pre-post scripts. + +```yaml +Type: Hashtable +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -EnvironmentVariables +Optional key-value pairs of parameters to set as environment variables to the shell. + +```yaml +Type: Hashtable +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -CI Indicates the script is run as part of a Continuous Integration / Continuous Deployment (CI/CD) build (only Azure Pipelines is currently supported). @@ -449,7 +480,7 @@ Accept wildcard characters: False ``` ### CommonParameters -This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216). +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). ## INPUTS @@ -458,3 +489,6 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ## NOTES ## RELATED LINKS + +[Remove-TestResources.ps1]() + From aa0b0704bd24b0b81a32af6bd56495beeccd17e3 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Thu, 5 Nov 2020 20:22:37 -0500 Subject: [PATCH 07/13] revert variable name to environmentVariables to fix post-scripts --- eng/common/TestResources/New-TestResources.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index a5966ab65ec2..6af3c3e52747 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -65,7 +65,8 @@ param ( [hashtable] $AdditionalParameters, [Parameter()] - [hashtable] $EnvironmentVariables, + [ValidateNotNullOrEmpty()] + [hashtable] $EnvironmentVariables = @{}, [Parameter()] [switch] $CI = ($null -ne $env:SYSTEM_TEAMPROJECTID), @@ -124,7 +125,6 @@ $repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path $root = [System.IO.Path]::Combine($repositoryRoot, "sdk", $ServiceDirectory) | Resolve-Path $templateFileName = 'test-resources.json' $templateFiles = @() -$outputEnvironmentVariables = @{} # Azure SDK Developer Playground $defaultSubscription = "faa080af-c1d8-40ad-9cce-e1a450ca5b57" @@ -295,7 +295,7 @@ if ($CI) { # Set the resource group name variable. Write-Host "Setting variable 'AZURE_RESOURCEGROUP_NAME': $ResourceGroupName" Write-Host "##vso[task.setvariable variable=AZURE_RESOURCEGROUP_NAME;]$ResourceGroupName" - $outputEnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] = $ResourceGroupName + $EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] = $ResourceGroupName } Log "Creating resource group '$ResourceGroupName' in location '$Location'" @@ -435,7 +435,7 @@ foreach ($templateFile in $templateFiles) { foreach ($key in $deploymentOutputs.Keys) { $value = $deploymentOutputs[$key] - $outputEnvironmentVariables[$key] = $value + $EnvironmentVariables[$key] = $value if ($CI) { # Treat all ARM template output variables as secrets since "SecureString" variables do not set values. @@ -466,7 +466,7 @@ $exitActions.Invoke() # Suppress output locally if ($CI) { - return $outputEnvironmentVariables + return $EnvironmentVariables } <# From 63789bc3c0215d2814b73646af8db6f8131da06b Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Thu, 5 Nov 2020 21:16:46 -0500 Subject: [PATCH 08/13] Handle empty arm template parameters better --- eng/common/TestResources/deploy-test-resources.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eng/common/TestResources/deploy-test-resources.yml b/eng/common/TestResources/deploy-test-resources.yml index 47bb7a02ed54..f9e8e1fe4fbb 100644 --- a/eng/common/TestResources/deploy-test-resources.yml +++ b/eng/common/TestResources/deploy-test-resources.yml @@ -47,8 +47,8 @@ steps: } $armTemplateParameters = ${{ parameters.ArmTemplateParameters }} - foreach ($p in $armTemplateParameters.GetEnumerator()) { - $subscriptionConfiguration.ArmTemplateParameters[$p.Name] = $p.Value + foreach ($key in $armTemplateParameters.Keys) { + $subscriptionConfiguration.ArmTemplateParameters[$key] = $armTemplateParameters[$key] } eng/common/TestResources/New-TestResources.ps1 ` From 91dab6567410d292190f99dd69f59bd7ad9b37a9 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Thu, 5 Nov 2020 21:49:35 -0500 Subject: [PATCH 09/13] Remove arm template parameter merge logic from deploy template --- eng/common/TestResources/New-TestResources.ps1 | 2 +- eng/common/TestResources/deploy-test-resources.yml | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 6af3c3e52747..ce0e27fec6bb 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -65,7 +65,7 @@ param ( [hashtable] $AdditionalParameters, [Parameter()] - [ValidateNotNullOrEmpty()] + [ValidateNotNull()] [hashtable] $EnvironmentVariables = @{}, [Parameter()] diff --git a/eng/common/TestResources/deploy-test-resources.yml b/eng/common/TestResources/deploy-test-resources.yml index f9e8e1fe4fbb..031c4b37cccd 100644 --- a/eng/common/TestResources/deploy-test-resources.yml +++ b/eng/common/TestResources/deploy-test-resources.yml @@ -42,21 +42,16 @@ steps: ${{ parameters.SubscriptionConfiguration }} '@ | ConvertFrom-Json -AsHashtable; - if (!$subscriptionConfiguration.ArmTemplateParameters) { - $subscriptionConfiguration.ArmTemplateParameters = @{} - } - - $armTemplateParameters = ${{ parameters.ArmTemplateParameters }} - foreach ($key in $armTemplateParameters.Keys) { - $subscriptionConfiguration.ArmTemplateParameters[$key] = $armTemplateParameters[$key] - } - + # The subscriptionConfiguration may have ArmTemplateParameters defined, so + # pass those in via the ArmTemplateParameters flag, and handle any + # additional parameters from the pipelines via AdditionalParameters eng/common/TestResources/New-TestResources.ps1 ` -BaseName 'Generated' ` -ServiceDirectory '${{ parameters.ServiceDirectory }}' ` -Location '${{ parameters.Location }}' ` -DeleteAfterHours '${{ parameters.DeleteAfterHours }}' ` @subscriptionConfiguration ` + -AdditionalParameters ${{ parameters.ArmTemplateParameters }} ` -CI ` -Force ` -Verbose | Out-Null From 18bed2d888fd7b2cf48759cb36653db991489706 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Mon, 9 Nov 2020 15:42:40 -0500 Subject: [PATCH 10/13] Add merge hashes function to New-TestResources.ps1 --- .../TestResources/New-TestResources.ps1 | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index ce0e27fec6bb..c64fdfe64e87 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -108,6 +108,16 @@ function Retry([scriptblock] $Action, [int] $Attempts = 5) { } } +function MergeHashes([System.Collections.HashTable] $source, [psvariable] $dest) { + foreach ($key in $source.Keys) { + if ($dest.Value[$key]) { + Write-Warning ("Overwriting '$($dest.Name).$($key)' with value '$($dest.Value[$key])' " + + "to new value '$($source[$key])'") + } + $dest.Value[$key] = $source[$key] + } +} + # Support actions to invoke on exit. $exitActions = @({ if ($exitActions.Count -gt 1) { @@ -258,7 +268,7 @@ $serviceName = if (Split-Path -IsAbsolute $ServiceDirectory) { $ServiceDirectory } -if ($CI) { +if ($CI) { $BaseName = 't' + (New-Guid).ToString('n').Substring(0, 16) Write-Verbose "Generated base name '$BaseName' for CI build" } @@ -328,12 +338,8 @@ if ($TenantId) { if ($TestApplicationSecret) { $templateParameters.Add('testApplicationSecret', $TestApplicationSecret) } -if ($ArmTemplateParameters) { - $templateParameters += $ArmTemplateParameters -} -foreach ($key in $AdditionalParameters.Keys) { - $templateParameters[$key] = $AdditionalParameters[$key] -} +MergeHashes($ArmTemplateParameters, $(Get-Variable $templateParameters)) +MergeHashes($AdditionalParameters, $(Get-Variable $templateParameters)) # Include environment-specific parameters only if not already provided as part of the "ArmTemplateParameters" if (($context.Environment.StorageEndpointSuffix) -and (-not ($templateParameters.ContainsKey('storageEndpointSuffix')))) { @@ -397,9 +403,7 @@ foreach ($templateFile in $templateFiles) { "$($serviceDirectoryPrefix)STORAGE_ENDPOINT_SUFFIX" = $context.Environment.StorageEndpointSuffix; } - foreach ($key in $EnvironmentVariables.Keys) { - $deploymentOutputs.Add($key, $EnvironmentVariables[$key]) - } + MergeHashes($EnvironmentVariables, $(Get-Variable $deploymentOutputs)) foreach ($key in $deployment.Outputs.Keys) { $variable = $deployment.Outputs[$key] From fe789f74f5ef5820a7cabd4046320d81cde9276a Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Mon, 9 Nov 2020 15:42:40 -0500 Subject: [PATCH 11/13] Add merge hashes function to New-TestResources.ps1 --- eng/common/TestResources/New-TestResources.ps1 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index c64fdfe64e87..587d74151908 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -108,7 +108,7 @@ function Retry([scriptblock] $Action, [int] $Attempts = 5) { } } -function MergeHashes([System.Collections.HashTable] $source, [psvariable] $dest) { +function MergeHashes([hashtable] $source, [psvariable] $dest) { foreach ($key in $source.Keys) { if ($dest.Value[$key]) { Write-Warning ("Overwriting '$($dest.Name).$($key)' with value '$($dest.Value[$key])' " + @@ -338,8 +338,9 @@ if ($TenantId) { if ($TestApplicationSecret) { $templateParameters.Add('testApplicationSecret', $TestApplicationSecret) } -MergeHashes($ArmTemplateParameters, $(Get-Variable $templateParameters)) -MergeHashes($AdditionalParameters, $(Get-Variable $templateParameters)) + +MergeHashes $ArmTemplateParameters $(Get-Variable templateParameters) +MergeHashes $AdditionalParameters $(Get-Variable templateParameters) # Include environment-specific parameters only if not already provided as part of the "ArmTemplateParameters" if (($context.Environment.StorageEndpointSuffix) -and (-not ($templateParameters.ContainsKey('storageEndpointSuffix')))) { @@ -403,7 +404,7 @@ foreach ($templateFile in $templateFiles) { "$($serviceDirectoryPrefix)STORAGE_ENDPOINT_SUFFIX" = $context.Environment.StorageEndpointSuffix; } - MergeHashes($EnvironmentVariables, $(Get-Variable $deploymentOutputs)) + MergeHashes $EnvironmentVariables $(Get-Variable deploymentOutputs) foreach ($key in $deployment.Outputs.Keys) { $variable = $deployment.Outputs[$key] From 9e46e75e92f2ef950bf4e32ce4653e342757ed89 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Mon, 9 Nov 2020 17:57:22 -0500 Subject: [PATCH 12/13] Add env variable overwrite warning. Use ContainsKey checks --- eng/common/TestResources/New-TestResources.ps1 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 587d74151908..5320a988aa85 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -10,7 +10,7 @@ [CmdletBinding(DefaultParameterSetName = 'Default', SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] param ( - # Limit $BaseName to enough characters to be under limit plus prefixes, and https://docs.microsoft.com/azure/architecture/best-practices/resource-naming. + # Limit $BaseName to enough characters to be under limit plus prefixes, and https://docs.microsoft.com/azure/architecture/best-practices/resource-naming [Parameter()] [ValidatePattern('^[-a-zA-Z0-9\.\(\)_]{0,80}(?<=[a-zA-Z0-9\(\)])$')] [string] $BaseName, @@ -110,7 +110,7 @@ function Retry([scriptblock] $Action, [int] $Attempts = 5) { function MergeHashes([hashtable] $source, [psvariable] $dest) { foreach ($key in $source.Keys) { - if ($dest.Value[$key]) { + if ($dest.Value.ContainsKey($key) -and $dest.Value[$key] -ne $source[$key]) { Write-Warning ("Overwriting '$($dest.Name).$($key)' with value '$($dest.Value[$key])' " + "to new value '$($source[$key])'") } @@ -305,6 +305,12 @@ if ($CI) { # Set the resource group name variable. Write-Host "Setting variable 'AZURE_RESOURCEGROUP_NAME': $ResourceGroupName" Write-Host "##vso[task.setvariable variable=AZURE_RESOURCEGROUP_NAME;]$ResourceGroupName" + if ($EnvironmentVariables.ContainsKey('AZURE_RESOURCEGROUP_NAME') -and ` + $EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] -ne $ResourceGroupName) + { + Write-Warning ("Overwriting 'EnvironmentVariables.AZURE_RESOURCEGROUP_NAME' with value " + + "'$($EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'])' " + "to new value '$($ResourceGroupName)'") + } $EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] = $ResourceGroupName } From 9b449d44768d29efa90d3f1bb022f76ee1083344 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Mon, 9 Nov 2020 18:16:14 -0500 Subject: [PATCH 13/13] Temporarily manually fix invalid generated markdown links --- eng/common/TestResources/New-TestResources.ps1 | 2 -- eng/common/TestResources/New-TestResources.ps1.md | 7 ++----- eng/common/TestResources/Remove-TestResources.ps1 | 2 -- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 5320a988aa85..4c03816c1284 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -647,6 +647,4 @@ Run this in an Azure DevOps CI (with approrpiate variables configured) before executing live tests. The script will output variables as secrets (to enable log redaction). -.LINK -Remove-TestResources.ps1 #> diff --git a/eng/common/TestResources/New-TestResources.ps1.md b/eng/common/TestResources/New-TestResources.ps1.md index 2af2f8c96617..515a51bd373c 100644 --- a/eng/common/TestResources/New-TestResources.ps1.md +++ b/eng/common/TestResources/New-TestResources.ps1.md @@ -394,7 +394,7 @@ Aliases: Required: False Position: Named -Default value: None +Default value: @{} Accept pipeline input: False Accept wildcard characters: False ``` @@ -480,7 +480,7 @@ Accept wildcard characters: False ``` ### CommonParameters -This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216). ## INPUTS @@ -489,6 +489,3 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ## NOTES ## RELATED LINKS - -[Remove-TestResources.ps1]() - diff --git a/eng/common/TestResources/Remove-TestResources.ps1 b/eng/common/TestResources/Remove-TestResources.ps1 index 83b1a58c347f..b74ddb42ac23 100644 --- a/eng/common/TestResources/Remove-TestResources.ps1 +++ b/eng/common/TestResources/Remove-TestResources.ps1 @@ -214,6 +214,4 @@ Remove-TestResources.ps1 ` When run in the context of an Azure DevOps pipeline, this script removes the resource group whose name is stored in the environment variable AZURE_RESOURCEGROUP_NAME. -.LINK -New-TestResources.ps1 #>