Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools repository for Tools PR
Browse files Browse the repository at this point in the history
  • Loading branch information
azure-sdk authored Aug 24, 2020
1 parent 81d4949 commit 44fad21
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 110 deletions.
40 changes: 28 additions & 12 deletions eng/common/TestResources/New-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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') {
Expand Down Expand Up @@ -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)'"
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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'.
Expand Down
Loading

0 comments on commit 44fad21

Please sign in to comment.