Sync eng/common directory with azure-sdk-tools repository for Tools PR https://github.com/Azure/azure-sdk-tools/pull/850 (#490)

This commit is contained in:
Azure SDK Bot 2020-08-24 14:11:57 -07:00 committed by GitHub
parent d2e2c8db1b
commit 4e9efce5f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 125 additions and 120 deletions

View File

@ -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'.

View File

@ -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] <String> -ServiceDirectory <String> -TestApplicationId <String>
[-TestApplicationSecret <String>] [-TestApplicationOid <String>] [-DeleteAfterHours <Int32>]
[-Location <String>] [-Environment <String>] [-AdditionalParameters <Hashtable>] [-CI] [-Force] [-WhatIf]
[-Confirm] [<CommonParameters>]
```
New-TestResources.ps1 [-BaseName] <String> [-ResourceGroupName <String>] -ServiceDirectory <String>
-TestApplicationId <String> [-TestApplicationSecret <String>] [-TestApplicationOid <String>]
[-DeleteAfterHours <Int32>] [-Location <String>] [-Environment <String>] [-AdditionalParameters <Hashtable>]
[-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] [<CommonParameters>]
```
### Provisioner
```text
New-TestResources.ps1 [-BaseName] <String> -ServiceDirectory <String> -TestApplicationId <String>
[-TestApplicationSecret <String>] [-TestApplicationOid <String>] -TenantId <String> [-SubscriptionId <String>]
-ProvisionerApplicationId <String> -ProvisionerApplicationSecret <String> [-DeleteAfterHours <Int32>]
[-Location <String>] [-Environment <String>] [-AdditionalParameters <Hashtable>] [-CI] [-Force] [-WhatIf]
[-Confirm] [<CommonParameters>]
```
New-TestResources.ps1 [-BaseName] <String> [-ResourceGroupName <String>] -ServiceDirectory <String>
-TestApplicationId <String> [-TestApplicationSecret <String>] [-TestApplicationOid <String>]
-TenantId <String> [-SubscriptionId <String>] -ProvisionerApplicationId <String>
-ProvisionerApplicationSecret <String> [-DeleteAfterHours <Int32>] [-Location <String>]
[-Environment <String>] [-AdditionalParameters <Hashtable>] [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm]
[<CommonParameters>]
```
## 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,8 +398,25 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -WhatIf
### -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:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```
### -WhatIf
Shows what would happen if the cmdlet runs.
The cmdlet is not run.
@ -420,7 +433,6 @@ Accept wildcard characters: False
```
### -Confirm
Prompts you for confirmation before running the cmdlet.
```yaml
@ -435,26 +447,7 @@ 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.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
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]()

View File

@ -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] <String> [-Environment <String>] [-Force] [-WhatIf] [-Confirm]
[<CommonParameters>]
```
Remove-TestResources.ps1 [-BaseName] <String> [-ServiceDirectory <String>] [-Environment <String>] [-Force]
[-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm] [<CommonParameters>]
```
### Default+Provisioner
```text
```
Remove-TestResources.ps1 [-BaseName] <String> -TenantId <String> [-SubscriptionId <String>]
-ProvisionerApplicationId <String> -ProvisionerApplicationSecret <String> [-Environment <String>] [-Force]
[-WhatIf] [-Confirm] [<CommonParameters>]
```
### ResourceGroup+Provisioner
```text
Remove-TestResources.ps1 -ResourceGroupName <String> -TenantId <String> [-SubscriptionId <String>]
-ProvisionerApplicationId <String> -ProvisionerApplicationSecret <String> [-Environment <String>] [-Force]
[-WhatIf] [-Confirm] [<CommonParameters>]
```
### ResourceGroup
```text
Remove-TestResources.ps1 -ResourceGroupName <String> [-Environment <String>] [-Force] [-WhatIf] [-Confirm]
-ProvisionerApplicationId <String> -ProvisionerApplicationSecret <String> [-ServiceDirectory <String>]
[-Environment <String>] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
[<CommonParameters>]
```
## DESCRIPTION
### ResourceGroup+Provisioner
```
Remove-TestResources.ps1 -ResourceGroupName <String> -TenantId <String> [-SubscriptionId <String>]
-ProvisionerApplicationId <String> -ProvisionerApplicationSecret <String> [-ServiceDirectory <String>]
[-Environment <String>] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
[<CommonParameters>]
```
### ResourceGroup
```
Remove-TestResources.ps1 -ResourceGroupName <String> [-ServiceDirectory <String>] [-Environment <String>]
[-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm] [<CommonParameters>]
```
## 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-\<baseName\>'
@ -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]()

View File

@ -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