Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync eng/common directory with azure-sdk-tools repository #10368

Merged
merged 1 commit into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions eng/common/TestResources/New-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ param (
[ValidateNotNullOrEmpty()]
[string] $Location = 'westus2',

[Parameter()]
[ValidateNotNullOrEmpty()]
[string] $Environment = 'AzureCloud',

[Parameter()]
[ValidateNotNullOrEmpty()]
[hashtable] $AdditionalParameters,
Expand Down Expand Up @@ -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 += {
Expand All @@ -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"
}
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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).
Expand Down
25 changes: 21 additions & 4 deletions eng/common/TestResources/New-TestResources.ps1.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ Deploys live test resources defined for a service directory to Azure.
```
New-TestResources.ps1 [-BaseName] <String> -ServiceDirectory <String> -TestApplicationId <String>
[-TestApplicationSecret <String>] [-TestApplicationOid <String>] [-DeleteAfterHours <Int32>]
[-Location <String>] [-AdditionalParameters <Hashtable>] [-CI] [-Force] [-WhatIf] [-Confirm]
[<CommonParameters>]
[-Location <String>] [-Environment <String>] [-AdditionalParameters <Hashtable>] [-CI] [-Force] [-WhatIf]
[-Confirm] [<CommonParameters>]
```

### Provisioner
```
New-TestResources.ps1 [-BaseName] <String> -ServiceDirectory <String> -TestApplicationId <String>
[-TestApplicationSecret <String>] [-TestApplicationOid <String>] -TenantId <String>
-ProvisionerApplicationId <String> -ProvisionerApplicationSecret <String> [-DeleteAfterHours <Int32>]
[-Location <String>] [-AdditionalParameters <Hashtable>] [-CI] [-Force] [-WhatIf] [-Confirm]
[<CommonParameters>]
[-Location <String>] [-Environment <String>] [-AdditionalParameters <Hashtable>] [-CI] [-Force] [-WhatIf]
[-Confirm] [<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -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).

Expand Down
10 changes: 9 additions & 1 deletion eng/common/TestResources/Remove-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ param (
[Parameter(ParameterSetName = 'ResourceGroup+Provisioner', Mandatory = $true)]
[string] $ProvisionerApplicationSecret,

[Parameter()]
[ValidateNotNullOrEmpty()]
[string] $Environment = 'AzureCloud',

[Parameter()]
[switch] $Force
)
Expand Down Expand Up @@ -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 += {
Expand Down Expand Up @@ -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

Expand Down
29 changes: 25 additions & 4 deletions eng/common/TestResources/Remove-TestResources.ps1.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,28 @@ Deletes the resource group deployed for a service directory from Azure.

### Default (Default)
```
Remove-TestResources.ps1 [-BaseName] <String> [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
Remove-TestResources.ps1 [-BaseName] <String> [-Environment <String>] [-Force] [-WhatIf] [-Confirm]
[<CommonParameters>]
```

### Default+Provisioner
```
Remove-TestResources.ps1 [-BaseName] <String> -TenantId <String> -ProvisionerApplicationId <String>
-ProvisionerApplicationSecret <String> [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
-ProvisionerApplicationSecret <String> [-Environment <String>] [-Force] [-WhatIf] [-Confirm]
[<CommonParameters>]
```

### ResourceGroup+Provisioner
```
Remove-TestResources.ps1 -ResourceGroupName <String> -TenantId <String> -ProvisionerApplicationId <String>
-ProvisionerApplicationSecret <String> [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
-ProvisionerApplicationSecret <String> [-Environment <String>] [-Force] [-WhatIf] [-Confirm]
[<CommonParameters>]
```

### ResourceGroup
```
Remove-TestResources.ps1 -ResourceGroupName <String> [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
Remove-TestResources.ps1 -ResourceGroupName <String> [-Environment <String>] [-Force] [-WhatIf] [-Confirm]
[<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion eng/common/TestResources/deploy-test-resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
5 changes: 4 additions & 1 deletion eng/common/TestResources/remove-test-resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ parameters:
TenantId: not-specified
ProvisionerApplicationId: not-specified
ProvisionerApplicationSecret: not-specified
Environment: 'AzureCloud'
Condition: true

steps:
- pwsh: >
Expand All @@ -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