azure-sdk-for-cpp/eng/common/scripts/Add-RetentionLease.ps1
Azure SDK Bot 18fb2de089
Sync eng/common directory with azure-sdk-tools for PR 5951 (#4537)
* Switch to using standard PAT tokens instead of base 64

For most of these we can use the standard System.AccessToken given to
the build instead of maintaining a specific token. However that
token isn't base 64 encoded so we need to encode it.

With this we can stop explicitly passing PAT's unless we need to
access another DevOps org and we also don't have to remember
to keep the PAT's in KV base 64 encoded.

Add error detection for queue build script to fail if we get login response.

* PR Feedback

---------

Co-authored-by: Wes Haggard <Wes.Haggard@microsoft.com>
2023-04-11 18:32:39 -07:00

46 lines
1.5 KiB
PowerShell

[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
[string]$Organization,
[Parameter(Mandatory = $true)]
[string]$Project,
[Parameter(Mandatory = $true)]
[int]$DefinitionId,
[Parameter(Mandatory = $true)]
[int]$RunId,
[Parameter(Mandatory = $true)]
[int]$DaysValid,
[Parameter(Mandatory = $false)]
[string]$OwnerId = "azure-sdk-pipeline-automation",
[Parameter(Mandatory = $false)]
[string]$AccessToken = $env:DEVOPS_PAT
)
Set-StrictMode -Version 3
. (Join-Path $PSScriptRoot common.ps1)
$encodedAuthToken = Get-Base64EncodedToken $AccessToken
LogDebug "Checking for existing leases on run: $RunId"
$existingLeases = Get-RetentionLeases -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -Base64EncodedAuthToken $encodedAuthToken
if ($existingLeases.count -ne 0) {
LogDebug "Found $($existingLeases.count) leases, will delete them first."
foreach ($lease in $existingLeases.value) {
LogDebug "Deleting lease: $($lease.leaseId)"
Delete-RetentionLease -Organization $Organization -Project $Project -LeaseId $lease.leaseId -Base64EncodedAuthToken $encodedAuthToken
}
}
LogDebug "Creating new lease on run: $RunId"
$lease = Add-RetentionLease -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -DaysValid $DaysValid -Base64EncodedAuthToken $encodedAuthToken
LogDebug "Lease ID is: $($lease.value.leaseId)"