diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index e26d1515933a..9e281d04eb66 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -156,7 +156,7 @@ if ($ProvisionerApplicationId) { } $provisionerAccount = Retry { - Connect-AzAccount -Tenant $TenantId -Credential $provisionerCredential -ServicePrincipal -Environment $Environment @subscriptionArgs + Connect-AzAccount -Force:$Force -Tenant $TenantId -Credential $provisionerCredential -ServicePrincipal -Environment $Environment @subscriptionArgs } $exitActions += { diff --git a/eng/common/TestResources/Remove-TestResources.ps1 b/eng/common/TestResources/Remove-TestResources.ps1 index 6d89a967ec03..0c5c464bc870 100644 --- a/eng/common/TestResources/Remove-TestResources.ps1 +++ b/eng/common/TestResources/Remove-TestResources.ps1 @@ -47,7 +47,11 @@ param ( [string] $Environment = 'AzureCloud', [Parameter()] - [switch] $Force + [switch] $Force, + + # Captures any arguments not declared here (no parameter errors) + [Parameter(ValueFromRemainingArguments = $true)] + $RemoveTestResourcesRemainingArguments ) # By default stop for any error. @@ -105,7 +109,7 @@ if ($ProvisionerApplicationId) { } $provisionerAccount = Retry { - Connect-AzAccount -Tenant $TenantId -Credential $provisionerCredential -ServicePrincipal -Environment $Environment @subscriptionArgs + Connect-AzAccount -Force:$Force -Tenant $TenantId -Credential $provisionerCredential -ServicePrincipal -Environment $Environment @subscriptionArgs } $exitActions += { @@ -143,54 +147,40 @@ $exitActions.Invoke() <# .SYNOPSIS Deletes the resource group deployed for a service directory from Azure. - .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 build pipeline) can pass $ProvisionerApplicationId and $ProvisionerApplicationSecret to authenticate a service principal with access to create resources. - .PARAMETER 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-' - .PARAMETER ResourceGroupName The name of the resource group to delete. - .PARAMETER TenantId The tenant ID of a service principal when a provisioner is specified. - .PARAMETER SubscriptionId Optional subscription ID to use for new resources when logging in as a provisioner. You can also use Set-AzContext if not provisioning. - .PARAMETER ProvisionerApplicationId A service principal ID to provision test resources when a provisioner is specified. - .PARAMETER ProvisionerApplicationSecret A service principal secret (password) to provision test resources when a provisioner is specified. - .PARAMETER ServiceDirectory A directory under 'sdk' in the repository root - optionally with subdirectories specified - in which to discover pre removal script named 'remove-test-resources-pre.json'. - .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 - .EXAMPLE Remove-TestResources.ps1 -BaseName 'uuid123' -Force - Use the currently logged-in account to delete the resource group by the name of 'rg-uuid123' - .EXAMPLE Remove-TestResources.ps1 ` -ResourceGroupName "${env:AZURE_RESOURCEGROUP_NAME}" ` @@ -199,11 +189,9 @@ 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. - .LINK New-TestResources.ps1 -#> +#> \ No newline at end of file diff --git a/eng/common/TestResources/deploy-test-resources.yml b/eng/common/TestResources/deploy-test-resources.yml index 00e75a3bfdb0..b580d73f1406 100644 --- a/eng/common/TestResources/deploy-test-resources.yml +++ b/eng/common/TestResources/deploy-test-resources.yml @@ -1,76 +1,40 @@ -# Deploys resources to a cloud type specified by the variable (not parameter) -# 'CloudType'. Use this as part of a matrix to deploy resources to a particular -# cloud instance. Normally we would use template parameters instead of variables -# but matrix variables are not available during template expansion so any -# benefits of parameters are lost. - parameters: ServiceDirectory: not-set ArmTemplateParameters: '@{}' DeleteAfterHours: 24 Location: '' + SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources) + +# SubscriptionConfiguration will be splat into the parameters of the test +# resources script. It should be JSON in the form: +# { +# "SubscriptionId": "", +# "TenantId": "", +# "TestApplicationId": "", +# "TestApplicationSecret": "", +# "ProvisionerApplicationId": "", +# "ProvisoinerApplicationSecret": "", +# "Environment": "AzureCloud | AzureGov | AzureChina | " +# } steps: # New-TestResources command requires Az module - pwsh: Install-Module -Name Az -Scope CurrentUser -AllowClobber -Force -Verbose displayName: Install Azure PowerShell module - - pwsh: > - eng/common/TestResources/New-TestResources.ps1 - -BaseName 'Generated' - -ServiceDirectory '${{ parameters.ServiceDirectory }}' - -TenantId '$(aad-azure-sdk-test-tenant-id)' - -SubscriptionId '$(azure-subscription-id)' - -TestApplicationId '$(aad-azure-sdk-test-client-id)' - -TestApplicationSecret '$(aad-azure-sdk-test-client-secret)' - -ProvisionerApplicationId '$(aad-azure-sdk-test-client-id)' - -ProvisionerApplicationSecret '$(aad-azure-sdk-test-client-secret)' - -AdditionalParameters ${{ parameters.ArmTemplateParameters }} - -DeleteAfterHours ${{ parameters.DeleteAfterHours }} - -Location '${{ parameters.Location }}' - -Environment 'AzureCloud' - -CI - -Force - -Verbose - displayName: Deploy test resources (AzureCloud) - condition: and(succeeded(), eq(variables['CloudType'], 'AzureCloud')) - - - pwsh: > - eng/common/TestResources/New-TestResources.ps1 - -BaseName 'Generated' - -ServiceDirectory '${{ parameters.ServiceDirectory }}' - -TenantId '$(aad-azure-sdk-test-tenant-id-gov)' - -SubscriptionId '$(azure-subscription-id-gov)' - -TestApplicationId '$(aad-azure-sdk-test-client-id-gov)' - -TestApplicationSecret '$(aad-azure-sdk-test-client-secret-gov)' - -ProvisionerApplicationId '$(aad-azure-sdk-test-client-id-gov)' - -ProvisionerApplicationSecret '$(aad-azure-sdk-test-client-secret-gov)' - -AdditionalParameters ${{ parameters.ArmTemplateParameters }} - -DeleteAfterHours ${{ parameters.DeleteAfterHours }} - -Location '${{ parameters.Location }}' - -Environment 'AzureUSGovernment' - -CI - -Force - -Verbose - displayName: Deploy test resources (AzureUSGovernment) - condition: and(succeeded(), eq(variables['CloudType'], 'AzureUSGovernment')) + - pwsh: | + $subscriptionConfiguration = @" + ${{ parameters.SubscriptionConfiguration }} + "@ | ConvertFrom-Json -AsHashtable; - - pwsh: > - eng/common/TestResources/New-TestResources.ps1 - -BaseName 'Generated' - -ServiceDirectory '${{ parameters.ServiceDirectory }}' - -TenantId '$(aad-azure-sdk-test-tenant-id-cn)' - -SubscriptionId '$(azure-subscription-id-cn)' - -TestApplicationId '$(aad-azure-sdk-test-client-id-cn)' - -TestApplicationSecret '$(aad-azure-sdk-test-client-secret-cn)' - -ProvisionerApplicationId '$(aad-azure-sdk-test-client-id-cn)' - -ProvisionerApplicationSecret '$(aad-azure-sdk-test-client-secret-cn)' - -AdditionalParameters ${{ parameters.ArmTemplateParameters }} - -DeleteAfterHours ${{ parameters.DeleteAfterHours }} - -Location '${{ parameters.Location }}' - -Environment 'AzureChinaCloud' - -CI - -Force - -Verbose - displayName: Deploy test resources (AzureChinaCloud) - condition: and(succeeded(), eq(variables['CloudType'], 'AzureChinaCloud')) \ No newline at end of file + eng/common/TestResources/New-TestResources.ps1 ` + -BaseName 'Generated' ` + -ServiceDirectory ${{ parameters.ServiceDirectory }} ` + -Location '${{ parameters.Location }}' ` + -DeleteAfterHours ${{ parameters.DeleteAfterHours }} ` + -AdditionalParameters ${{ parameters.ArmTemplateParameters }} ` + @subscriptionConfiguration ` + -CI ` + -Force ` + -Verbose + displayName: Deploy test resources diff --git a/eng/common/TestResources/remove-test-resources.yml b/eng/common/TestResources/remove-test-resources.yml index e79dfe1267e6..e66933b60cc6 100644 --- a/eng/common/TestResources/remove-test-resources.yml +++ b/eng/common/TestResources/remove-test-resources.yml @@ -1,57 +1,35 @@ -# Removes resources from a cloud type specified by the variable (not parameter) -# 'CloudType'. Use this as part of a matrix to remove resources from a -# particular cloud instance. Normally we would use template variables instead of -# parameters but matrix variables are not available during template expansion -# so any benefits of parameters are lost. - # Assumes steps in deploy-test-resources.yml was run previously. Requires # environment variable: AZURE_RESOURCEGROUP_NAME and Az PowerShell module parameters: ServiceDirectory: '' + SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources) -steps: - - pwsh: > - eng/common/TestResources/Remove-TestResources.ps1 - -ResourceGroupName "${env:AZURE_RESOURCEGROUP_NAME}" - -TenantId '$(aad-azure-sdk-test-tenant-id)' - -SubscriptionId '$(azure-subscription-id)' - -ProvisionerApplicationId '$(aad-azure-sdk-test-client-id)' - -ProvisionerApplicationSecret '$(aad-azure-sdk-test-client-secret)' - -ServiceDirectory '${{ parameters.ServiceDirectory }}' - -Environment 'AzureCloud' - -Force - -Verbose - displayName: Remove test resources (AzureCloud) - condition: and(ne(variables['AZURE_RESOURCEGROUP_NAME'], ''), eq(variables['CloudType'], 'AzureCloud')) - continueOnError: true +# SubscriptionConfiguration will be splat into the parameters of the test +# resources script. It should be JSON in the form: +# { +# "SubscriptionId": "", +# "TenantId": "", +# "TestApplicationId": "", +# "TestApplicationSecret": "", +# "ProvisionerApplicationId": "", +# "ProvisoinerApplicationSecret": "", +# "Environment": "AzureCloud | AzureGov | AzureChina | " +# } +# The Remove-TestResources.ps1 script accommodates extra parameters so it will +# not error when parameters are provided which the script doesn't use. - - pwsh: > - eng/common/TestResources/Remove-TestResources.ps1 - -ResourceGroupName "${env:AZURE_RESOURCEGROUP_NAME}" - -TenantId '$(aad-azure-sdk-test-tenant-id-gov)' - -SubscriptionId '$(azure-subscription-id-gov)' - -ProvisionerApplicationId '$(aad-azure-sdk-test-client-id-gov)' - -ProvisionerApplicationSecret '$(aad-azure-sdk-test-client-secret-gov)' - -ServiceDirectory '${{ parameters.ServiceDirectory }}' - -Environment 'AzureUSGovernment' - -Force - -Verbose - displayName: Remove test resources (AzureUSGovernment) - condition: and(ne(variables['AZURE_RESOURCEGROUP_NAME'], ''), eq(variables['CloudType'], 'AzureUSGovernment')) - continueOnError: true +steps: + - pwsh: | + $subscriptionConfiguration = @" + ${{ parameters.SubscriptionConfiguration }} + "@ | ConvertFrom-Json -AsHashtable; - - pwsh: > - eng/common/TestResources/Remove-TestResources.ps1 - -ResourceGroupName "${env:AZURE_RESOURCEGROUP_NAME}" - -TenantId '$(aad-azure-sdk-test-tenant-id-cn)' - -SubscriptionId '$(azure-subscription-id-cn)' - -ProvisionerApplicationId '$(aad-azure-sdk-test-client-id-cn)' - -ProvisionerApplicationSecret '$(aad-azure-sdk-test-client-secret-cn)' - -ServiceDirectory '${{ parameters.ServiceDirectory }}' - -Environment 'AzureChinaCloud' - -Force - -Verbose - displayName: Remove test resources (AzureChinaCloud) - condition: and(ne(variables['AZURE_RESOURCEGROUP_NAME'], ''), eq(variables['CloudType'], 'AzureChinaCloud')) + eng/common/TestResources/Remove-TestResources.ps1 ` + -ResourceGroupName "${env:AZURE_RESOURCEGROUP_NAME}" ` + -ServiceDirectory ${{ parameters.ServiceDirectory }} ` + @subscriptionConfiguration ` + -Force ` + -Verbose + displayName: Remove test resources continueOnError: true