Skip to content

Commit 24b0cd8

Browse files
azure-sdkheaths
andauthored
Sync eng/common directory with azure-sdk-tools for PR 1525 (#14481)
* Use SubscriptionId throughout TestResources Fixes #1454 * Resolve PR feedback * Default DeleteAfterHours to 48 for SDK team Also makes a few other adjustments for subscriptions, like restoring the previous one if available and another was specified. * Resolve PR feedback * Change deployment mode to Complete Also fixes an issue where if the user opted not to deploy to the same resource group, the script would continue execution anyway. * Use consistent aka links to satisfy link checker Only need it for the new Update-TestResources.ps1 script, but I wanted them to look consistent. Co-authored-by: Heath Stewart <heaths@microsoft.com>
1 parent 47e0b1d commit 24b0cd8

8 files changed

+659
-109
lines changed

eng/common/TestResources/New-TestResources.ps1

Lines changed: 89 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ param (
3939
# Azure SDK Developer Playground subscription
4040
[Parameter()]
4141
[ValidatePattern('^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$')]
42-
[string] $SubscriptionId = 'faa080af-c1d8-40ad-9cce-e1a450ca5b57',
42+
[string] $SubscriptionId,
4343

4444
[Parameter(ParameterSetName = 'Provisioner', Mandatory = $true)]
4545
[ValidatePattern('^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$')]
@@ -49,8 +49,8 @@ param (
4949
[string] $ProvisionerApplicationSecret,
5050

5151
[Parameter()]
52-
[ValidateRange(0, [int]::MaxValue)]
53-
[int] $DeleteAfterHours,
52+
[ValidateRange(1, [int]::MaxValue)]
53+
[int] $DeleteAfterHours = 48,
5454

5555
[Parameter()]
5656
[string] $Location = '',
@@ -166,7 +166,7 @@ try {
166166
Log "Generated base name '$BaseName' for CI build"
167167
} elseif (!$BaseName) {
168168
$BaseName = "$UserName$ServiceDirectory"
169-
Log "BaseName was not set. Using default base name: '$BaseName'"
169+
Log "BaseName was not set. Using default base name '$BaseName'"
170170
}
171171

172172
# Make sure pre- and post-scripts are passed formerly required arguments.
@@ -200,35 +200,74 @@ try {
200200
# Make sure the user is logged in to create a service principal.
201201
$context = Get-AzContext;
202202
if (!$context) {
203-
$subscriptionName = $SubscriptionId
203+
Log 'User not logged in. Logging in now...'
204+
$context = (Connect-AzAccount).Context
205+
}
204206

205-
# Use cache of well-known team subs without having to be authenticated.
206-
$wellKnownSubscriptions = @{
207-
'faa080af-c1d8-40ad-9cce-e1a450ca5b57' = 'Azure SDK Developer Playground'
208-
'a18897a6-7e44-457d-9260-f2854c0aca42' = 'Azure SDK Engineering System'
209-
'2cd617ea-1866-46b1-90e3-fffb087ebf9b' = 'Azure SDK Test Resources'
207+
$currentSubcriptionId = $context.Subscription.Id
208+
209+
# If no subscription was specified, try to select the Azure SDK Developer Playground subscription.
210+
# Ignore errors to leave the automatically selected subscription.
211+
if ($SubscriptionId) {
212+
if ($currentSubcriptionId -ne $SubscriptionId) {
213+
Log "Selecting subscription '$SubscriptionId'"
214+
$null = Select-AzSubscription -Subscription $SubscriptionId
215+
216+
$exitActions += {
217+
Log "Selecting previous subscription '$currentSubcriptionId'"
218+
$null = Select-AzSubscription -Subscription $currentSubcriptionId
219+
}
220+
221+
# Update the context.
222+
$context = Get-AzContext
210223
}
224+
} else {
225+
if ($currentSubcriptionId -ne 'faa080af-c1d8-40ad-9cce-e1a450ca5b57') {
226+
Log "Attempting to select subscription 'Azure SDK Developer Playground (faa080af-c1d8-40ad-9cce-e1a450ca5b57)'"
227+
$null = Select-AzSubscription -Subscription 'faa080af-c1d8-40ad-9cce-e1a450ca5b57' -ErrorAction Ignore
211228

212-
if ($wellKnownSubscriptions.ContainsKey($SubscriptionId)) {
213-
$subscriptionName = '{0} ({1})' -f $wellKnownSubscriptions[$SubscriptionId], $SubscriptionId
229+
# Update the context.
230+
$context = Get-AzContext
214231
}
215232

216-
Log "You are not logged in; connecting to $subscriptionName"
217-
$context = (Connect-AzAccount -Subscription $SubscriptionId).Context
233+
$SubscriptionId = $context.Subscription.Id
234+
$PSBoundParameters['SubscriptionId'] = $SubscriptionId
235+
}
236+
237+
# Use cache of well-known team subs without having to be authenticated.
238+
$wellKnownSubscriptions = @{
239+
'faa080af-c1d8-40ad-9cce-e1a450ca5b57' = 'Azure SDK Developer Playground'
240+
'a18897a6-7e44-457d-9260-f2854c0aca42' = 'Azure SDK Engineering System'
241+
'2cd617ea-1866-46b1-90e3-fffb087ebf9b' = 'Azure SDK Test Resources'
242+
}
243+
244+
# Print which subscription is currently selected.
245+
$subscriptionName = $context.Subscription.Id
246+
if ($wellKnownSubscriptions.ContainsKey($subscriptionName)) {
247+
$subscriptionName = '{0} ({1})' -f $wellKnownSubscriptions[$subscriptionName], $subscriptionName
248+
}
249+
250+
Log "Using subscription '$subscriptionName'"
251+
252+
# Make sure the TenantId is also updated from the current context.
253+
# PSBoundParameters is not updated to avoid confusing parameter sets.
254+
if (!$TenantId) {
255+
$TenantId = $context.Subscription.TenantId
218256
}
219257

220258
# If no test application ID is specified during an interactive session, create a new service principal.
221259
if (!$TestApplicationId) {
222260

223261
# Cache the created service principal in this session for frequent reuse.
224-
$servicePrincipal = if ($AzureTestPrincipal -and (Get-AzADServicePrincipal -ApplicationId $AzureTestPrincipal.ApplicationId)) {
262+
$servicePrincipal = if ($AzureTestPrincipal -and (Get-AzADServicePrincipal -ApplicationId $AzureTestPrincipal.ApplicationId) -and $AzureTestSubscription -eq $SubscriptionId) {
225263
Log "TestApplicationId was not specified; loading cached service principal '$($AzureTestPrincipal.ApplicationId)'"
226264
$AzureTestPrincipal
227265
} else {
228-
Log 'TestApplicationId was not specified; creating a new service principal'
229-
$global:AzureTestPrincipal = New-AzADServicePrincipal -Role Owner
266+
Log "TestApplicationId was not specified; creating a new service principal in subscription '$SubscriptionId'"
267+
$global:AzureTestPrincipal = New-AzADServicePrincipal -Role Owner -Scope "/subscriptions/$SubscriptionId"
268+
$global:AzureTestSubscription = $SubscriptionId
230269

231-
Log "Created service principal '$AzureTestPrincipal'"
270+
Log "Created service principal '$($AzureTestPrincipal.ApplicationId)'"
232271
$AzureTestPrincipal
233272
}
234273

@@ -251,13 +290,15 @@ try {
251290
if ($ProvisionerApplicationId) {
252291
$null = Disable-AzContextAutosave -Scope Process
253292

254-
Log "Logging into service principal '$ProvisionerApplicationId'"
293+
Log "Logging into service principal '$ProvisionerApplicationId'."
294+
Write-Warning 'Logging into service principal may fail until the principal is fully propagated.'
295+
255296
$provisionerSecret = ConvertTo-SecureString -String $ProvisionerApplicationSecret -AsPlainText -Force
256297
$provisionerCredential = [System.Management.Automation.PSCredential]::new($ProvisionerApplicationId, $provisionerSecret)
257298

258299
# Use the given subscription ID if provided.
259300
$subscriptionArgs = if ($SubscriptionId) {
260-
@{SubscriptionId = $SubscriptionId}
301+
@{Subscription = $SubscriptionId}
261302
} else {
262303
@{}
263304
}
@@ -292,7 +333,7 @@ try {
292333

293334
# If the ServiceDirectory is an absolute path use the last directory name
294335
# (e.g. D:\foo\bar\ -> bar)
295-
$serviceName = if (Split-Path -IsAbsolute $ServiceDirectory) {
336+
$serviceName = if (Split-Path -IsAbsolute $ServiceDirectory) {
296337
Split-Path -Leaf $ServiceDirectory
297338
} else {
298339
$ServiceDirectory
@@ -307,16 +348,15 @@ try {
307348
"rg-$BaseName"
308349
}
309350

310-
# Tag the resource group to be deleted after a certain number of hours if specified.
311351
$tags = @{
312352
Creator = $UserName
313353
ServiceDirectory = $ServiceDirectory
314354
}
315355

316-
if ($PSBoundParameters.ContainsKey('DeleteAfterHours')) {
317-
$deleteAfter = [DateTime]::UtcNow.AddHours($DeleteAfterHours)
318-
$tags.Add('DeleteAfter', $deleteAfter.ToString('o'))
319-
}
356+
# Tag the resource group to be deleted after a certain number of hours.
357+
Write-Warning "Any clean-up scripts running against subscription '$SubscriptionId' may delete resource group '$ResourceGroupName' after $DeleteAfterHours hours."
358+
$deleteAfter = [DateTime]::UtcNow.AddHours($DeleteAfterHours).ToString('o')
359+
$tags['DeleteAfter'] = $deleteAfter
320360

321361
if ($CI) {
322362
# Add tags for the current CI job.
@@ -348,11 +388,15 @@ try {
348388
# New-AzResourceGroup would've written an error and stopped the pipeline by default anyway.
349389
Write-Verbose "Successfully created resource group '$($resourceGroup.ResourceGroupName)'"
350390
}
351-
elseif (!$resourceGroup -and !$PSCmdlet.ShouldProcess($resourceGroupName)) {
352-
# If the -WhatIf flag was passed, there will be no resource group created. Fake it.
353-
$resourceGroup = [PSCustomObject]@{
354-
ResourceGroupName = $resourceGroupName
355-
Location = $Location
391+
elseif (!$resourceGroup) {
392+
if (!$PSCmdlet.ShouldProcess($resourceGroupName)) {
393+
# If the -WhatIf flag was passed, there will be no resource group created. Fake it.
394+
$resourceGroup = [PSCustomObject]@{
395+
ResourceGroupName = $resourceGroupName
396+
Location = $Location
397+
}
398+
} else {
399+
Write-Error "Resource group '$ResourceGroupName' already exists." -Category ResourceExists -RecommendedAction "Delete resource group '$ResourceGroupName', or overwrite it when redeploying."
356400
}
357401
}
358402

@@ -413,16 +457,16 @@ try {
413457
$lastDebugPreference = $DebugPreference
414458
try {
415459
if ($CI) {
416-
$DebugPreference = "Continue"
460+
$DebugPreference = 'Continue'
417461
}
418-
New-AzResourceGroupDeployment -Name $BaseName -ResourceGroupName $resourceGroup.ResourceGroupName -TemplateFile $templateFile -TemplateParameterObject $templateFileParameters
462+
New-AzResourceGroupDeployment -Name $BaseName -ResourceGroupName $resourceGroup.ResourceGroupName -TemplateFile $templateFile -TemplateParameterObject $templateFileParameters -Mode Complete -Force:$Force
419463
} catch {
420-
Write-Output @"
464+
Write-Output @'
421465
#####################################################
422466
# For help debugging live test provisioning issues, #
423467
# see http://aka.ms/azsdk/engsys/live-test-help, #
424468
#####################################################
425-
"@
469+
'@
426470
throw
427471
} finally {
428472
$DebugPreference = $lastDebugPreference
@@ -466,7 +510,7 @@ try {
466510

467511
if ($OutFile) {
468512
if (!$IsWindows) {
469-
Write-Host "File option is supported only on Windows"
513+
Write-Host 'File option is supported only on Windows'
470514
}
471515

472516
$outputFile = "$templateFile.env"
@@ -595,7 +639,11 @@ is passed to the ARM template as 'tenantId'.
595639
Optional subscription ID to use for new resources when logging in as a
596640
provisioner. You can also use Set-AzContext if not provisioning.
597641
598-
The default is the Azure SDK Developer Playground subscription ID.
642+
If you do not specify a SubscriptionId and are not logged in, one will be
643+
automatically selected for you by the Connect-AzAccount cmdlet.
644+
645+
Once you are logged in (or were previously), the selected SubscriptionId
646+
will be used for subsequent operations that are specific to a subscription.
599647
600648
.PARAMETER ProvisionerApplicationId
601649
The AAD Application ID used to provision test resources when a provisioner is
@@ -614,17 +662,14 @@ If none is specified New-TestResources.ps1 uses the TestApplicationSecret.
614662
This value is not passed to the ARM template.
615663
616664
.PARAMETER DeleteAfterHours
617-
Optional. Positive integer number of hours from the current time to set the
665+
Positive integer number of hours from the current time to set the
618666
'DeleteAfter' tag on the created resource group. The computed value is a
619667
timestamp of the form "2020-03-04T09:07:04.3083910Z".
620668
621-
If this value is not specified no 'DeleteAfter' tag will be assigned to the
622-
created resource group.
623-
624669
An optional cleanup process can delete resource groups whose "DeleteAfter"
625670
timestamp is less than the current time.
626671
627-
This isused for CI automation.
672+
This is used for CI automation.
628673
629674
.PARAMETER Location
630675
Optional location where resources should be created. If left empty, the default
@@ -660,8 +705,8 @@ Save test environment settings into a test-resources.json.env file next to test-
660705
The environment file would be scoped to the current repository directory.
661706
662707
.EXAMPLE
663-
Connect-AzAccount -Subscription "REPLACE_WITH_SUBSCRIPTION_ID"
664-
New-TestResources.ps1 -ServiceDirectory 'keyvault'
708+
Connect-AzAccount -Subscription 'REPLACE_WITH_SUBSCRIPTION_ID'
709+
New-TestResources.ps1 keyvault
665710
666711
Run this in a desktop environment to create new AAD apps and Service Principals
667712
that can be used to provision resources and run live tests.

eng/common/TestResources/New-TestResources.ps1.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Deploys live test resources defined for a service directory to Azure.
1616
```
1717
New-TestResources.ps1 [-BaseName <String>] [-ResourceGroupName <String>] [-ServiceDirectory] <String>
1818
[-TestApplicationId <String>] [-TestApplicationSecret <String>] [-TestApplicationOid <String>]
19-
[-DeleteAfterHours <Int32>] [-Location <String>] [-Environment <String>] [-ArmTemplateParameters <Hashtable>]
20-
[-AdditionalParameters <Hashtable>] [-EnvironmentVariables <Hashtable>] [-CI] [-Force] [-OutFile] [-WhatIf]
21-
[-Confirm] [<CommonParameters>]
19+
[-SubscriptionId <String>] [-DeleteAfterHours <Int32>] [-Location <String>] [-Environment <String>]
20+
[-ArmTemplateParameters <Hashtable>] [-AdditionalParameters <Hashtable>] [-EnvironmentVariables <Hashtable>]
21+
[-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] [<CommonParameters>]
2222
```
2323

2424
### Provisioner
@@ -55,8 +55,8 @@ specified in $ProvisionerApplicationId and $ProvisionerApplicationSecret.
5555

5656
### EXAMPLE 1
5757
```
58-
Connect-AzAccount -Subscription "REPLACE_WITH_SUBSCRIPTION_ID"
59-
New-TestResources.ps1 -ServiceDirectory 'keyvault'
58+
Connect-AzAccount -Subscription 'REPLACE_WITH_SUBSCRIPTION_ID'
59+
New-TestResources.ps1 keyvault
6060
```
6161

6262
Run this in a desktop environment to create new AAD apps and Service Principals
@@ -231,16 +231,20 @@ Optional subscription ID to use for new resources when logging in as a
231231
provisioner.
232232
You can also use Set-AzContext if not provisioning.
233233
234-
The default is the Azure SDK Developer Playground subscription ID.
234+
If you do not specify a SubscriptionId and are not logged in, one will be
235+
automatically selected for you by the Connect-AzAccount cmdlet.
236+
237+
Once you are logged in (or were previously), the selected SubscriptionId
238+
will be used for subsequent operations that are specific to a subscription.
235239
236240
```yaml
237241
Type: String
238-
Parameter Sets: Provisioner
242+
Parameter Sets: (All)
239243
Aliases:
240244

241245
Required: False
242246
Position: Named
243-
Default value: faa080af-c1d8-40ad-9cce-e1a450ca5b57
247+
Default value: None
244248
Accept pipeline input: False
245249
Accept wildcard characters: False
246250
```
@@ -286,19 +290,15 @@ Accept wildcard characters: False
286290
```
287291
288292
### -DeleteAfterHours
289-
Optional.
290293
Positive integer number of hours from the current time to set the
291294
'DeleteAfter' tag on the created resource group.
292295
The computed value is a
293296
timestamp of the form "2020-03-04T09:07:04.3083910Z".
294297
295-
If this value is not specified no 'DeleteAfter' tag will be assigned to the
296-
created resource group.
297-
298298
An optional cleanup process can delete resource groups whose "DeleteAfter"
299299
timestamp is less than the current time.
300300
301-
This isused for CI automation.
301+
This is used for CI automation.
302302
303303
```yaml
304304
Type: Int32
@@ -307,7 +307,7 @@ Aliases:
307307

308308
Required: False
309309
Position: Named
310-
Default value: 0
310+
Default value: 48
311311
Accept pipeline input: False
312312
Accept wildcard characters: False
313313
```

0 commit comments

Comments
 (0)