Skip to content
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
16 changes: 16 additions & 0 deletions eng/common/TestResources/Remove-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ param (
[Parameter(ParameterSetName = 'Default+Provisioner', Mandatory = $true)]
[Parameter(ParameterSetName = 'ResourceGroup+Provisioner', Mandatory = $true)]
[string] $ProvisionerApplicationSecret,

[Parameter()]
[string] $ServiceDirectory,

[Parameter()]
[ValidateSet('AzureCloud', 'AzureUSGovernment', 'AzureChinaCloud')]
Expand Down Expand Up @@ -116,6 +119,15 @@ if (!$ResourceGroupName) {
$ResourceGroupName = "rg-$BaseName"
}

if (![string]::IsNullOrWhiteSpace($ServiceDirectory)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is unnecessary. Put [ValidateIsNotNullOrEmpty()] on the parameter as well and just use line 123. IT's better to do most validation in PowerShell parameters because it will validate and return correct errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed you pushed another commit, but please make this change as well. We want PowerShell to validate as much as possible - not the script.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The advantage of allowing an empty value here is that invocation would be more simple for the contributor... but only if they coincidentally didn't have one of these pre- scripts (in the majority of cases today you don't need to run a pre- script for resource removal).

If we do validation here we'll need to update references to remove-test-resources.yml in each repo before the PRs that this merge opens get merged or we'll be broken.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they don't want to specify it, don't specify the parameter. ValidateNullOrEmpty only applies if they specify the parameter. That's the norm with well-written PowerShell scripts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying... I think the correct syntax would take the form of:

...

[ValidateNotNullOrEmpty()]
[string] $ServiceDirectory,

...

if ($ServiceDirectory) { 
  $root = [System.IO.Path]::Combine("$PSScriptRoot/../../../sdk", $ServiceDirectory) | Resolve-Path
  ...
}

I'll start updating the template invocations ahead of this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need the [Parameter()] attribute.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think we should not put ValidateNotNullOrEmpty as that will be a breaking change for all the consumers of our eng\common. So at least initially I'd stick with what we is here right now. We can later decide to fix up all the places and then we can add this if we feel it is interesting.

The more and more dependencies we get on the stuff under eng/common the more we are going to have to be careful about making breaking changes as it will become very difficult to keep things working in all the places if we don't.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a breaking change. It only applies if you specify the -ServiceDirectory. If they didn't (it didn't exist), there's no harm. This is how PowerShell works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combining it with the change to the template which pass empty by default will break pipelines until we can get all the users updated to pass something not-empty. We can come back and fix this later once we have updated all the usages.

$root = [System.IO.Path]::Combine("$PSScriptRoot/../../../sdk", $ServiceDirectory) | Resolve-Path
$preRemovalScript = Join-Path -Path $root -ChildPath 'remove-test-resources-pre.ps1'
if (Test-Path $preRemovalScript) {
Log "Invoking pre resource removal script '$preRemovalScript'"
&$preRemovalScript -ResourceGroupName $ResourceGroupName @PSBoundParameters
}
}

Log "Deleting resource group '$ResourceGroupName'"
if (Retry { Remove-AzResourceGroup -Name "$ResourceGroupName" -Force:$Force }) {
Write-Verbose "Successfully deleted resource group '$ResourceGroupName'"
Expand Down Expand Up @@ -157,6 +169,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 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')
Expand Down
16 changes: 16 additions & 0 deletions eng/common/TestResources/Remove-TestResources.ps1.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ Accept pipeline input: False
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'.

```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Environment
Name of the cloud environment.
The default is the Azure Public Cloud
Expand Down
6 changes: 6 additions & 0 deletions eng/common/TestResources/remove-test-resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# Assumes steps in deploy-test-resources.yml was run previously. Requires
# environment variable: AZURE_RESOURCEGROUP_NAME and Az PowerShell module

parameters:
ServiceDirectory: ''

steps:
- pwsh: >
eng/common/TestResources/Remove-TestResources.ps1
Expand All @@ -15,6 +18,7 @@ steps:
-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
Expand All @@ -29,6 +33,7 @@ steps:
-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
Expand All @@ -43,6 +48,7 @@ steps:
-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
Expand Down