From 35aa439cfedd29b65d739b15d3df88766b3f9289 Mon Sep 17 00:00:00 2001 From: azure-sdk Date: Wed, 18 Mar 2020 23:19:51 +0000 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools repository --- .../TestResources/New-TestResources.ps1 | 22 ++++++++++++-- .../TestResources/New-TestResources.ps1.md | 25 +++++++++++++--- .../TestResources/Remove-TestResources.ps1 | 10 ++++++- .../TestResources/Remove-TestResources.ps1.md | 29 ++++++++++++++++--- .../TestResources/deploy-test-resources.yml | 10 ++++++- .../TestResources/remove-test-resources.yml | 5 +++- 6 files changed, 87 insertions(+), 14 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 90b4faa553dc..19b38c42e21a 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -48,6 +48,10 @@ param ( [ValidateNotNullOrEmpty()] [string] $Location = 'westus2', + [Parameter()] + [ValidateNotNullOrEmpty()] + [string] $Environment = 'AzureCloud', + [Parameter()] [ValidateNotNullOrEmpty()] [hashtable] $AdditionalParameters, @@ -128,7 +132,7 @@ if ($ProvisionerApplicationId) { $provisionerCredential = [System.Management.Automation.PSCredential]::new($ProvisionerApplicationId, $provisionerSecret) $provisionerAccount = Retry { - Connect-AzAccount -Tenant $TenantId -Credential $provisionerCredential -ServicePrincipal + Connect-AzAccount -Tenant $TenantId -Credential $provisionerCredential -ServicePrincipal -Environment $Environment } $exitActions += { @@ -153,7 +157,15 @@ $resourceGroupName = if ($CI) { $BaseName = 't' + (New-Guid).ToString('n').Substring(0, 16) Write-Verbose "Generated base name '$BaseName' for CI build" - "rg-{0}-$BaseName" -f ($ServiceDirectory -replace '[\\\/]', '-').Substring(0, [Math]::Min($ServiceDirectory.Length, 90 - $BaseName.Length - 4)).Trim('-') + # If the ServiceDirectory is an absolute path use the last directory name + # (e.g. D:\foo\bar\ -> bar) + $serviceName = if (Split-Path -IsAbsolute $ServiceDirectory) { + Split-Path -Leaf $ServiceDirectory + } else { + $ServiceDirectory + } + + "rg-{0}-$BaseName" -f ($serviceName -replace '[\\\/:]', '-').Substring(0, [Math]::Min($serviceName.Length, 90 - $BaseName.Length - 4)).Trim('-') } else { "rg-$BaseName" } @@ -241,7 +253,7 @@ foreach ($templateFile in $templateFiles) { Log "Deploying template '$templateFile' to resource group '$($resourceGroup.ResourceGroupName)'" $deployment = Retry { - New-AzResourceGroupDeployment -Name $BaseName -ResourceGroupName $resourceGroup.ResourceGroupName -TemplateFile $templateFile -TemplateParameterObject $templateFileParameters + New-AzResourceGroupDeployment -Name $BaseName -ResourceGroupName $resourceGroup.ResourceGroupName -TemplateFile $templateFile -TemplateParameterObject $templateFileParameters -Location $Location } if ($deployment.ProvisioningState -eq 'Succeeded') { @@ -391,6 +403,10 @@ Optional location where resources should be created. By default this is .PARAMETER AdditionalParameters Optional key-value pairs of parameters to pass to the ARM template(s). +.PARAMETER Environment +Name of the cloud environment. The default is the Azure Public Cloud +('PublicCloud') + .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/New-TestResources.ps1.md b/eng/common/TestResources/New-TestResources.ps1.md index b6516d63cace..4c978512519b 100644 --- a/eng/common/TestResources/New-TestResources.ps1.md +++ b/eng/common/TestResources/New-TestResources.ps1.md @@ -16,8 +16,8 @@ Deploys live test resources defined for a service directory to Azure. ``` New-TestResources.ps1 [-BaseName] -ServiceDirectory -TestApplicationId [-TestApplicationSecret ] [-TestApplicationOid ] [-DeleteAfterHours ] - [-Location ] [-AdditionalParameters ] [-CI] [-Force] [-WhatIf] [-Confirm] - [] + [-Location ] [-Environment ] [-AdditionalParameters ] [-CI] [-Force] [-WhatIf] + [-Confirm] [] ``` ### Provisioner @@ -25,8 +25,8 @@ New-TestResources.ps1 [-BaseName] -ServiceDirectory -TestAppli New-TestResources.ps1 [-BaseName] -ServiceDirectory -TestApplicationId [-TestApplicationSecret ] [-TestApplicationOid ] -TenantId -ProvisionerApplicationId -ProvisionerApplicationSecret [-DeleteAfterHours ] - [-Location ] [-AdditionalParameters ] [-CI] [-Force] [-WhatIf] [-Confirm] - [] + [-Location ] [-Environment ] [-AdditionalParameters ] [-CI] [-Force] [-WhatIf] + [-Confirm] [] ``` ## DESCRIPTION @@ -298,6 +298,23 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Environment +Name of the cloud environment. +The default is the Azure Public Cloud +('PublicCloud') + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: AzureCloud +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -AdditionalParameters Optional key-value pairs of parameters to pass to the ARM template(s). diff --git a/eng/common/TestResources/Remove-TestResources.ps1 b/eng/common/TestResources/Remove-TestResources.ps1 index b0db06abd9ed..494fe1c873d1 100644 --- a/eng/common/TestResources/Remove-TestResources.ps1 +++ b/eng/common/TestResources/Remove-TestResources.ps1 @@ -34,6 +34,10 @@ param ( [Parameter(ParameterSetName = 'ResourceGroup+Provisioner', Mandatory = $true)] [string] $ProvisionerApplicationSecret, + [Parameter()] + [ValidateNotNullOrEmpty()] + [string] $Environment = 'AzureCloud', + [Parameter()] [switch] $Force ) @@ -87,7 +91,7 @@ if ($ProvisionerApplicationId) { $provisionerSecret = ConvertTo-SecureString -String $ProvisionerApplicationSecret -AsPlainText -Force $provisionerCredential = [System.Management.Automation.PSCredential]::new($ProvisionerApplicationId, $provisionerSecret) $provisionerAccount = Retry { - Connect-AzAccount -Tenant $TenantId -Credential $provisionerCredential -ServicePrincipal + Connect-AzAccount -Tenant $TenantId -Credential $provisionerCredential -ServicePrincipal -Environment $Environment } $exitActions += { @@ -138,6 +142,10 @@ A service principal ID to provision test resources when a provisioner is specifi .PARAMETER ProvisionerApplicationSecret A service principal secret (password) to provision test resources when a provisioner is specified. +.PARAMETER Environment +Name of the cloud environment. The default is the Azure Public Cloud +('PublicCloud') + .PARAMETER Force Force removal of resource group without asking for user confirmation diff --git a/eng/common/TestResources/Remove-TestResources.ps1.md b/eng/common/TestResources/Remove-TestResources.ps1.md index 8dc48c85df28..03803d8e591c 100644 --- a/eng/common/TestResources/Remove-TestResources.ps1.md +++ b/eng/common/TestResources/Remove-TestResources.ps1.md @@ -14,24 +14,28 @@ Deletes the resource group deployed for a service directory from Azure. ### Default (Default) ``` -Remove-TestResources.ps1 [-BaseName] [-Force] [-WhatIf] [-Confirm] [] +Remove-TestResources.ps1 [-BaseName] [-Environment ] [-Force] [-WhatIf] [-Confirm] + [] ``` ### Default+Provisioner ``` Remove-TestResources.ps1 [-BaseName] -TenantId -ProvisionerApplicationId - -ProvisionerApplicationSecret [-Force] [-WhatIf] [-Confirm] [] + -ProvisionerApplicationSecret [-Environment ] [-Force] [-WhatIf] [-Confirm] + [] ``` ### ResourceGroup+Provisioner ``` Remove-TestResources.ps1 -ResourceGroupName -TenantId -ProvisionerApplicationId - -ProvisionerApplicationSecret [-Force] [-WhatIf] [-Confirm] [] + -ProvisionerApplicationSecret [-Environment ] [-Force] [-WhatIf] [-Confirm] + [] ``` ### ResourceGroup ``` -Remove-TestResources.ps1 -ResourceGroupName [-Force] [-WhatIf] [-Confirm] [] +Remove-TestResources.ps1 -ResourceGroupName [-Environment ] [-Force] [-WhatIf] [-Confirm] + [] ``` ## DESCRIPTION @@ -148,6 +152,23 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Environment +Name of the cloud environment. +The default is the Azure Public Cloud +('PublicCloud') + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: AzureCloud +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Force Force removal of resource group without asking for user confirmation diff --git a/eng/common/TestResources/deploy-test-resources.yml b/eng/common/TestResources/deploy-test-resources.yml index d63c0e1ac161..5844d2d39b06 100644 --- a/eng/common/TestResources/deploy-test-resources.yml +++ b/eng/common/TestResources/deploy-test-resources.yml @@ -6,13 +6,17 @@ parameters: TestApplicationObjectId: not-specified ProvisionerApplicationId: not-specified ProvisionerApplicationSecret: not-specified + AdditionalParameters: '' + Location: 'westus2' + Environment: 'AzureCloud' DeleteAfterHours: 24 - + Condition: succeeded() steps: # New-TestResources command requires Az module - pwsh: Install-Module -Name Az -Scope CurrentUser -AllowClobber -Force -Verbose displayName: Install Azure PowerShell module + condition: ${{ parameters.Condition }} # Command sets an environment variable in the pipeline's context: # AZURE_RESOURCEGROUP_NAME @@ -26,8 +30,12 @@ steps: -TestApplicationOid '${{ parameters.TestApplicationObjectId }}' -ProvisionerApplicationId '${{ parameters.ProvisionerApplicationId }}' -ProvisionerApplicationSecret '${{ parameters.ProvisionerApplicationSecret }}' + -AdditionalParameters ${{ parameters.AdditionalParameters }} -DeleteAfterHours ${{ parameters.DeleteAfterHours }} + -Location '${{ parameters.Location }}' + -Environment '${{ parameters.Environment }}' -CI -Force -Verbose displayName: Deploy test resources + condition: ${{ parameters.Condition }} \ No newline at end of file diff --git a/eng/common/TestResources/remove-test-resources.yml b/eng/common/TestResources/remove-test-resources.yml index 8fb6177a38c3..90fe597e0fa1 100644 --- a/eng/common/TestResources/remove-test-resources.yml +++ b/eng/common/TestResources/remove-test-resources.yml @@ -4,6 +4,8 @@ parameters: TenantId: not-specified ProvisionerApplicationId: not-specified ProvisionerApplicationSecret: not-specified + Environment: 'AzureCloud' + Condition: true steps: - pwsh: > @@ -12,8 +14,9 @@ steps: -TenantId '${{ parameters.TenantId }}' -ProvisionerApplicationId '${{ parameters.ProvisionerApplicationId }}' -ProvisionerApplicationSecret '${{ parameters.ProvisionerApplicationSecret }}' + -Environment '${{ parameters.Environment }}' -Force -Verbose displayName: Remove test resources - condition: ne(variables['AZURE_RESOURCEGROUP_NAME'], '') + condition: and(ne(variables['AZURE_RESOURCEGROUP_NAME'], ''), ${{ parameters.Condition }}) continueOnError: true