From 44fad218934e1dde2181ef9e4048e7cdeadef85e Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Mon, 24 Aug 2020 14:12:02 -0700 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools repository for Tools PR https://github.com/Azure/azure-sdk-tools/pull/850 (#1077) --- .../TestResources/New-TestResources.ps1 | 40 +++++-- .../TestResources/New-TestResources.ps1.md | 106 +++++++++--------- .../TestResources/Remove-TestResources.ps1.md | 77 ++++++------- .../TestResources/remove-test-resources.yml | 2 +- 4 files changed, 115 insertions(+), 110 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 3201f517548..0552f9dd5e7 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -15,6 +15,9 @@ param ( [ValidatePattern('^[-a-zA-Z0-9\.\(\)_]{0,80}(?<=[a-zA-Z0-9\(\)])$')] [string] $BaseName, + [ValidatePattern('^[-\w\._\(\)]+$')] + [string] $ResourceGroupName, + [Parameter(Mandatory = $true)] [string] $ServiceDirectory, @@ -115,6 +118,7 @@ $repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path $root = [System.IO.Path]::Combine($repositoryRoot, "sdk", $ServiceDirectory) | Resolve-Path $templateFileName = 'test-resources.json' $templateFiles = @() +$environmentVariables = @{} Write-Verbose "Checking for '$templateFileName' files under '$root'" Get-ChildItem -Path $root -Filter $templateFileName -Recurse | ForEach-Object { @@ -194,14 +198,18 @@ $serviceName = if (Split-Path -IsAbsolute $ServiceDirectory) { $ServiceDirectory } -# Format the resource group name based on resource group naming recommendations and limitations. -$resourceGroupName = if ($CI) { - $BaseName = 't' + (New-Guid).ToString('n').Substring(0, 16) - Write-Verbose "Generated base name '$BaseName' for CI build" +if ($CI) { + $BaseName = 't' + (New-Guid).ToString('n').Substring(0, 16) + Write-Verbose "Generated base name '$BaseName' for CI build" +} - "rg-{0}-$BaseName" -f ($serviceName -replace '[\\\/:]', '-').Substring(0, [Math]::Min($serviceName.Length, 90 - $BaseName.Length - 4)).Trim('-') +$ResourceGroupName = if ($ResourceGroupName) { + $ResourceGroupName +} elseif ($CI) { + # Format the resource group name based on resource group naming recommendations and limitations. + "rg-{0}-$BaseName" -f ($serviceName -replace '[\\\/:]', '-').Substring(0, [Math]::Min($serviceName.Length, 90 - $BaseName.Length - 4)).Trim('-') } else { - "rg-$BaseName" + "rg-$BaseName" } # Tag the resource group to be deleted after a certain number of hours if specified. @@ -225,13 +233,14 @@ 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" + Write-Host "Setting variable 'AZURE_RESOURCEGROUP_NAME': $ResourceGroupName" + Write-Host "##vso[task.setvariable variable=AZURE_RESOURCEGROUP_NAME;]$ResourceGroupName" + $environmentVariables['AZURE_RESOURCEGROUP_NAME'] = $ResourceGroupName } -Log "Creating resource group '$resourceGroupName' in location '$Location'" +Log "Creating resource group '$ResourceGroupName' in location '$Location'" $resourceGroup = Retry { - New-AzResourceGroup -Name "$resourceGroupName" -Location $Location -Tag $tags -Force:$Force + New-AzResourceGroup -Name "$ResourceGroupName" -Location $Location -Tag $tags -Force:$Force } if ($resourceGroup.ProvisioningState -eq 'Succeeded') { @@ -295,7 +304,7 @@ foreach ($templateFile in $templateFiles) { $preDeploymentScript = $templateFile | Split-Path | Join-Path -ChildPath 'test-resources-pre.ps1' if (Test-Path $preDeploymentScript) { Log "Invoking pre-deployment script '$preDeploymentScript'" - &$preDeploymentScript -ResourceGroupName $resourceGroupName @PSBoundParameters + &$preDeploymentScript -ResourceGroupName $ResourceGroupName @PSBoundParameters } Log "Deploying template '$templateFile' to resource group '$($resourceGroup.ResourceGroupName)'" @@ -364,6 +373,7 @@ foreach ($templateFile in $templateFiles) { foreach ($key in $deploymentOutputs.Keys) { $value = $deploymentOutputs[$key] + $environmentVariables[$key] = $value if ($CI) { # Treat all ARM template output variables as secrets since "SecureString" variables do not set values. @@ -386,12 +396,14 @@ foreach ($templateFile in $templateFiles) { $postDeploymentScript = $templateFile | Split-Path | Join-Path -ChildPath 'test-resources-post.ps1' if (Test-Path $postDeploymentScript) { Log "Invoking post-deployment script '$postDeploymentScript'" - &$postDeploymentScript -ResourceGroupName $resourceGroupName -DeploymentOutputs $deploymentOutputs @PSBoundParameters + &$postDeploymentScript -ResourceGroupName $ResourceGroupName -DeploymentOutputs $deploymentOutputs @PSBoundParameters } } $exitActions.Invoke() +return $environmentVariables + <# .SYNOPSIS Deploys live test resources defined for a service directory to Azure. @@ -422,6 +434,10 @@ the ARM template. See also https://docs.microsoft.com/azure/architecture/best-pr Note: The value specified for this parameter will be overriden and generated by New-TestResources.ps1 if $CI is specified. +.PARAMETER ResourceGroupName +Set this value to deploy directly to a Resource Group that has already been +created. + .PARAMETER ServiceDirectory A directory under 'sdk' in the repository root - optionally with subdirectories specified - in which to discover ARM templates named 'test-resources.json'. diff --git a/eng/common/TestResources/New-TestResources.ps1.md b/eng/common/TestResources/New-TestResources.ps1.md index e12f0a30dc0..d4479294d38 100644 --- a/eng/common/TestResources/New-TestResources.ps1.md +++ b/eng/common/TestResources/New-TestResources.ps1.md @@ -8,33 +8,30 @@ schema: 2.0.0 # New-TestResources.ps1 ## SYNOPSIS - Deploys live test resources defined for a service directory to Azure. ## SYNTAX ### Default (Default) - -```text -New-TestResources.ps1 [-BaseName] -ServiceDirectory -TestApplicationId - [-TestApplicationSecret ] [-TestApplicationOid ] [-DeleteAfterHours ] - [-Location ] [-Environment ] [-AdditionalParameters ] [-CI] [-Force] [-WhatIf] - [-Confirm] [] +``` +New-TestResources.ps1 [-BaseName] [-ResourceGroupName ] -ServiceDirectory + -TestApplicationId [-TestApplicationSecret ] [-TestApplicationOid ] + [-DeleteAfterHours ] [-Location ] [-Environment ] [-AdditionalParameters ] + [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] [] ``` ### Provisioner - -```text -New-TestResources.ps1 [-BaseName] -ServiceDirectory -TestApplicationId - [-TestApplicationSecret ] [-TestApplicationOid ] -TenantId [-SubscriptionId ] - -ProvisionerApplicationId -ProvisionerApplicationSecret [-DeleteAfterHours ] - [-Location ] [-Environment ] [-AdditionalParameters ] [-CI] [-Force] [-WhatIf] - [-Confirm] [] +``` +New-TestResources.ps1 [-BaseName] [-ResourceGroupName ] -ServiceDirectory + -TestApplicationId [-TestApplicationSecret ] [-TestApplicationOid ] + -TenantId [-SubscriptionId ] -ProvisionerApplicationId + -ProvisionerApplicationSecret [-DeleteAfterHours ] [-Location ] + [-Environment ] [-AdditionalParameters ] [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] + [] ``` ## DESCRIPTION - -Deploys live test resources specified in test-resources.json files to a resource +Deploys live test resouces specified in test-resources.json files to a resource group. This script searches the directory specified in $ServiceDirectory recursively @@ -56,8 +53,7 @@ specified in $ProvisionerApplicationId and $ProvisionerApplicationSecret. ## EXAMPLES ### EXAMPLE 1 - -```text +``` Connect-AzAccount -Subscription "REPLACE_WITH_SUBSCRIPTION_ID" $testAadApp = New-AzADServicePrincipal -Role Owner -DisplayName 'azure-sdk-live-test-app' New-TestResources.ps1 ` @@ -74,8 +70,7 @@ Requires PowerShell 7 to use ConvertFrom-SecureString -AsPlainText or convert the SecureString to plaintext by another means. ### EXAMPLE 2 - -```text +``` New-TestResources.ps1 ` -BaseName 'Generated' ` -ServiceDirectory '$(ServiceDirectory)' ` @@ -90,7 +85,7 @@ New-TestResources.ps1 ` -Verbose ``` -Run this in an Azure DevOps CI (with appropriate variables configured) before +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). @@ -98,13 +93,12 @@ log redaction). ## PARAMETERS ### -BaseName - A name to use in the resource group and passed to the ARM template as 'baseName'. Limit $BaseName to enough characters to be under limit plus prefixes specified in the ARM template. See also https://docs.microsoft.com/azure/architecture/best-practices/resource-naming -Note: The value specified for this parameter will be overridden and generated +Note: The value specified for this parameter will be overriden and generated by New-TestResources.ps1 if $CI is specified. ```yaml @@ -119,8 +113,23 @@ Accept pipeline input: False Accept wildcard characters: False ``` -### -ServiceDirectory +### -ResourceGroupName +Set this value to deploy directly to a Resource Group that has already been +created. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` +### -ServiceDirectory A directory under 'sdk' in the repository root - optionally with subdirectories specified - in which to discover ARM templates named 'test-resources.json'. This can also be an absolute path or specify parent directories. @@ -138,7 +147,6 @@ Accept wildcard characters: False ``` ### -TestApplicationId - The AAD Application ID to authenticate the test runner against deployed resources. Passed to the ARM template as 'testApplicationId'. @@ -159,7 +167,6 @@ Accept wildcard characters: False ``` ### -TestApplicationSecret - Optional service principal secret (password) to authenticate the test runner against deployed resources. Passed to the ARM template as @@ -181,7 +188,6 @@ Accept wildcard characters: False ``` ### -TestApplicationOid - Service Principal Object ID of the AAD Test application. This is used to assign permissions to the AAD application so it can access tested features on the live @@ -206,7 +212,6 @@ Accept wildcard characters: False ``` ### -TenantId - The tenant ID of a service principal when a provisioner is specified. The same Tenant ID is used for Test Application and Provisioner Application. @@ -226,7 +231,6 @@ Accept wildcard characters: False ``` ### -SubscriptionId - Optional subscription ID to use for new resources when logging in as a provisioner. You can also use Set-AzContext if not provisioning. @@ -244,7 +248,6 @@ Accept wildcard characters: False ``` ### -ProvisionerApplicationId - The AAD Application ID used to provision test resources when a provisioner is specified. @@ -265,7 +268,6 @@ Accept wildcard characters: False ``` ### -ProvisionerApplicationSecret - A service principal secret (password) used to provision test resources when a provisioner is specified. @@ -286,7 +288,6 @@ Accept wildcard characters: False ``` ### -DeleteAfterHours - Optional. Positive integer number of hours from the current time to set the 'DeleteAfter' tag on the created resource group. @@ -299,7 +300,7 @@ created resource group. An optional cleanup process can delete resource groups whose "DeleteAfter" timestamp is less than the current time. -This is used for CI automation. +This isused for CI automation. ```yaml Type: Int32 @@ -314,7 +315,6 @@ Accept wildcard characters: False ``` ### -Location - Optional location where resources should be created. If left empty, the default is based on the cloud to which the template is being deployed: @@ -336,7 +336,6 @@ Accept wildcard characters: False ``` ### -Environment - Name of the cloud environment. The default is the Azure Public Cloud ('PublicCloud') @@ -354,7 +353,6 @@ Accept wildcard characters: False ``` ### -AdditionalParameters - Optional key-value pairs of parameters to pass to the ARM template(s). ```yaml @@ -370,7 +368,6 @@ 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). @@ -387,7 +384,6 @@ Accept wildcard characters: False ``` ### -Force - Force creation of resources instead of being prompted. ```yaml @@ -402,31 +398,32 @@ Accept pipeline input: False Accept wildcard characters: False ``` -### -WhatIf - -Shows what would happen if the cmdlet runs. -The cmdlet is not run. +### -OutFile +Save test environment settings into a test-resources.json.env file next to test-resources.json. +File is protected via DPAPI. +Supported only on windows. +The environment file would be scoped to the current repository directory. ```yaml Type: SwitchParameter Parameter Sets: (All) -Aliases: wi +Aliases: Required: False Position: Named -Default value: None +Default value: False Accept pipeline input: False Accept wildcard characters: False ``` -### -Confirm - -Prompts you for confirmation before running the cmdlet. +### -WhatIf +Shows what would happen if the cmdlet runs. +The cmdlet is not run. ```yaml Type: SwitchParameter Parameter Sets: (All) -Aliases: cf +Aliases: wi Required: False Position: Named @@ -435,16 +432,13 @@ Accept pipeline input: False Accept wildcard characters: False ``` -### -OutFile - -save test environment settings into a test-resources.json.env file next to test-resources.json. -The file is protected via DPAPI. The environment file would be scoped to the current repository directory. -Note: Supported only on Windows. +### -Confirm +Prompts you for confirmation before running the cmdlet. ```yaml Type: SwitchParameter Parameter Sets: (All) -Aliases: +Aliases: cf Required: False Position: Named @@ -454,7 +448,6 @@ 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). ## INPUTS @@ -465,4 +458,5 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ## RELATED LINKS -[Remove-TestResources.ps1](./Remove-TestResources.ps1.md) +[Remove-TestResources.ps1]() + diff --git a/eng/common/TestResources/Remove-TestResources.ps1.md b/eng/common/TestResources/Remove-TestResources.ps1.md index f8f1c001df1..7a70923dcd0 100644 --- a/eng/common/TestResources/Remove-TestResources.ps1.md +++ b/eng/common/TestResources/Remove-TestResources.ps1.md @@ -8,46 +8,41 @@ schema: 2.0.0 # Remove-TestResources.ps1 ## SYNOPSIS - Deletes the resource group deployed for a service directory from Azure. ## SYNTAX ### Default (Default) - -```text -Remove-TestResources.ps1 [-BaseName] [-Environment ] [-Force] [-WhatIf] [-Confirm] - [] +``` +Remove-TestResources.ps1 [-BaseName] [-ServiceDirectory ] [-Environment ] [-Force] + [-RemoveTestResourcesRemainingArguments ] [-WhatIf] [-Confirm] [] ``` ### Default+Provisioner - -```text +``` Remove-TestResources.ps1 [-BaseName] -TenantId [-SubscriptionId ] - -ProvisionerApplicationId -ProvisionerApplicationSecret [-Environment ] [-Force] - [-WhatIf] [-Confirm] [] + -ProvisionerApplicationId -ProvisionerApplicationSecret [-ServiceDirectory ] + [-Environment ] [-Force] [-RemoveTestResourcesRemainingArguments ] [-WhatIf] [-Confirm] + [] ``` ### ResourceGroup+Provisioner - -```text +``` Remove-TestResources.ps1 -ResourceGroupName -TenantId [-SubscriptionId ] - -ProvisionerApplicationId -ProvisionerApplicationSecret [-Environment ] [-Force] - [-WhatIf] [-Confirm] [] + -ProvisionerApplicationId -ProvisionerApplicationSecret [-ServiceDirectory ] + [-Environment ] [-Force] [-RemoveTestResourcesRemainingArguments ] [-WhatIf] [-Confirm] + [] ``` ### ResourceGroup - -```text -Remove-TestResources.ps1 -ResourceGroupName [-Environment ] [-Force] [-WhatIf] [-Confirm] - [] +``` +Remove-TestResources.ps1 -ResourceGroupName [-ServiceDirectory ] [-Environment ] + [-Force] [-RemoveTestResourcesRemainingArguments ] [-WhatIf] [-Confirm] [] ``` ## DESCRIPTION - Removes a resource group and all its resources previously deployed using New-TestResources.ps1. - If you are not currently logged into an account in the Az PowerShell module, you will be asked to log in with Connect-AzAccount. Alternatively, you (or a @@ -58,17 +53,14 @@ create resources. ## EXAMPLES ### EXAMPLE 1 - -```text -Remove-TestResources.ps1 -BaseName 'uuid123' -Force ``` - +Remove-TestResources.ps1 -BaseName 'uuid123' -Force Use the currently logged-in account to delete the resource group by the name of 'rg-uuid123' +``` ### EXAMPLE 2 - -```text +``` Remove-TestResources.ps1 ` -ResourceGroupName "${env:AZURE_RESOURCEGROUP_NAME}" ` -TenantId '$(TenantId)' ` @@ -76,16 +68,14 @@ Remove-TestResources.ps1 ` -ProvisionerApplicationSecret '$(AppSecret)' ` -Force ` -Verbose ` -``` - 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. +``` ## PARAMETERS ### -BaseName - A name to use in the resource group and passed to the ARM template as 'baseName'. This will delete the resource group named 'rg-\' @@ -102,7 +92,6 @@ Accept wildcard characters: False ``` ### -ResourceGroupName - The name of the resource group to delete. ```yaml @@ -118,7 +107,6 @@ Accept wildcard characters: False ``` ### -TenantId - The tenant ID of a service principal when a provisioner is specified. ```yaml @@ -134,7 +122,6 @@ Accept wildcard characters: False ``` ### -SubscriptionId - Optional subscription ID to use for new resources when logging in as a provisioner. You can also use Set-AzContext if not provisioning. @@ -152,7 +139,6 @@ Accept wildcard characters: False ``` ### -ProvisionerApplicationId - A service principal ID to provision test resources when a provisioner is specified. ```yaml @@ -168,7 +154,6 @@ Accept wildcard characters: False ``` ### -ProvisionerApplicationSecret - A service principal secret (password) to provision test resources when a provisioner is specified. ```yaml @@ -184,9 +169,8 @@ Accept wildcard characters: False ``` ### -ServiceDirectory - A directory under 'sdk' in the repository root - optionally with subdirectories -specified - specified - in which to discover pre removal script named 'remove-test-resources-pre.json'. +specified - in which to discover pre removal script named 'remove-test-resources-pre.json'. ```yaml Type: String @@ -201,7 +185,6 @@ Accept wildcard characters: False ``` ### -Environment - Name of the cloud environment. The default is the Azure Public Cloud ('PublicCloud') @@ -219,7 +202,6 @@ Accept wildcard characters: False ``` ### -Force - Force removal of resource group without asking for user confirmation ```yaml @@ -234,8 +216,22 @@ Accept pipeline input: False Accept wildcard characters: False ``` -### -WhatIf +### -RemoveTestResourcesRemainingArguments +Captures any arguments not declared here (no parameter errors) +```yaml +Type: Object +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -WhatIf Shows what would happen if the cmdlet runs. The cmdlet is not run. @@ -252,7 +248,6 @@ Accept wildcard characters: False ``` ### -Confirm - Prompts you for confirmation before running the cmdlet. ```yaml @@ -268,7 +263,6 @@ 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). ## INPUTS @@ -279,4 +273,5 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ## RELATED LINKS -[New-TestResources.ps1](./New-TestResources.ps1.md) +[New-TestResources.ps1]() + diff --git a/eng/common/TestResources/remove-test-resources.yml b/eng/common/TestResources/remove-test-resources.yml index 34beecd3cb8..01e9f64d551 100644 --- a/eng/common/TestResources/remove-test-resources.yml +++ b/eng/common/TestResources/remove-test-resources.yml @@ -27,7 +27,7 @@ steps: eng/common/TestResources/Remove-TestResources.ps1 ` -ResourceGroupName "${env:AZURE_RESOURCEGROUP_NAME}" ` - -ServiceDirectory ${{ parameters.ServiceDirectory }} ` + -ServiceDirectory "${{ parameters.ServiceDirectory }}" ` @subscriptionConfiguration ` -Force ` -Verbose