diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 9e281d04e..3201f5175 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -154,6 +154,9 @@ if ($ProvisionerApplicationId) { $subscriptionArgs = if ($SubscriptionId) { @{SubscriptionId = $SubscriptionId} } + else { + @{} + } $provisionerAccount = Retry { Connect-AzAccount -Force:$Force -Tenant $TenantId -Credential $provisionerCredential -ServicePrincipal -Environment $Environment @subscriptionArgs @@ -161,7 +164,11 @@ if ($ProvisionerApplicationId) { $exitActions += { Write-Verbose "Logging out of service principal '$($provisionerAccount.Context.Account)'" - $null = Disconnect-AzAccount -AzureContext $provisionerAccount.Context + + # Only attempt to disconnect if the -WhatIf flag was not set. Otherwise, this call is not necessary and will fail. + if ($PSCmdlet.ShouldProcess($ProvisionerApplicationId)) { + $null = Disconnect-AzAccount -AzureContext $provisionerAccount.Context + } } } @@ -176,6 +183,8 @@ if ($TestApplicationId -and !$TestApplicationOid) { } } +# Determine the Azure context that the script is running in. +$context = Get-AzContext; # If the ServiceDirectory is an absolute path use the last directory name # (e.g. D:\foo\bar\ -> bar) @@ -229,6 +238,13 @@ if ($resourceGroup.ProvisioningState -eq 'Succeeded') { # New-AzResourceGroup would've written an error and stopped the pipeline by default anyway. Write-Verbose "Successfully created resource group '$($resourceGroup.ResourceGroupName)'" } +elseif (($resourceGroup -eq $null) -and (-not $PSCmdlet.ShouldProcess($resourceGroupName))) { + # If the -WhatIf flag was passed, there will be no resource group created. Fake it. + $resourceGroup = [PSCustomObject]@{ + ResourceGroupName = $resourceGroupName + Location = $Location + } +} # Populate the template parameters and merge any additional specified. $templateParameters = @{ @@ -247,6 +263,11 @@ if ($AdditionalParameters) { $templateParameters += $AdditionalParameters } +# Include environment-specific parameters only if not already provided as part of the "AdditionalParameters" +if (($context.Environment.StorageEndpointSuffix) -and (-not ($templateParameters.ContainsKey('storageEndpointSuffix')))) { + $templateParameters.Add('storageEndpointSuffix', $context.Environment.StorageEndpointSuffix) +} + # Try to detect the shell based on the parent process name (e.g. launch via shebang). $shell, $shellExportFormat = if (($parentProcessName = (Get-Process -Id $PID).Parent.ProcessName) -and $parentProcessName -eq 'cmd') { 'cmd', 'set {0}={1}' @@ -256,6 +277,7 @@ $shell, $shellExportFormat = if (($parentProcessName = (Get-Process -Id $PID).Pa 'PowerShell', '$env:{0} = ''{1}''' } +# Deploy the templates foreach ($templateFile in $templateFiles) { # Deployment fails if we pass in more parameters than are defined. Write-Verbose "Removing unnecessary parameters from template '$templateFile'" @@ -288,8 +310,6 @@ foreach ($templateFile in $templateFiles) { $serviceDirectoryPrefix = $serviceName.ToUpperInvariant() + "_" - $context = Get-AzContext; - # Add default values $deploymentOutputs = @{ "$($serviceDirectoryPrefix)CLIENT_ID" = $TestApplicationId; @@ -299,6 +319,10 @@ foreach ($templateFile in $templateFiles) { "$($serviceDirectoryPrefix)RESOURCE_GROUP" = $resourceGroup.ResourceGroupName; "$($serviceDirectoryPrefix)LOCATION" = $resourceGroup.Location; "$($serviceDirectoryPrefix)ENVIRONMENT" = $context.Environment.Name; + "$($serviceDirectoryPrefix)AZURE_AUTHORITY_HOST" = $context.Environment.ActiveDirectoryAuthority; + "$($serviceDirectoryPrefix)RESOURCE_MANAGER_URL" = $context.Environment.ResourceManagerUrl; + "$($serviceDirectoryPrefix)SERVICE_MANAGEMENT_URL" = $context.Environment.ServiceManagementUrl; + "$($serviceDirectoryPrefix)STORAGE_ENDPOINT_SUFFIX" = $context.Environment.StorageEndpointSuffix; } foreach ($key in $deployment.Outputs.Keys) { @@ -331,7 +355,7 @@ foreach ($templateFile in $templateFiles) { } else { - + if (!$CI) { # Write an extra new line to isolate the environment variables for easy reading. Log "Persist the following environment variables based on your detected shell ($shell):`n" @@ -340,7 +364,7 @@ foreach ($templateFile in $templateFiles) { foreach ($key in $deploymentOutputs.Keys) { $value = $deploymentOutputs[$key] - + if ($CI) { # Treat all ARM template output variables as secrets since "SecureString" variables do not set values. # In order to mask secrets but set environment variables for any given ARM template, we set variables twice as shown below. @@ -526,4 +550,4 @@ log redaction). .LINK Remove-TestResources.ps1 -#> +#> \ No newline at end of file diff --git a/eng/common/scripts/artifact-metadata-parsing.ps1 b/eng/common/scripts/artifact-metadata-parsing.ps1 index af7c9401e..92017cc7b 100644 --- a/eng/common/scripts/artifact-metadata-parsing.ps1 +++ b/eng/common/scripts/artifact-metadata-parsing.ps1 @@ -36,47 +36,7 @@ function CreateReleases($pkgList, $releaseApiUrl, $releaseSha) { "Authorization" = "token $($env:GH_TOKEN)" } - Invoke-WebRequest-WithHandling -url $url -body $body -headers $headers -method "Post" - } -} - -function Invoke-WebRequest-WithHandling($url, $method, $body = $null, $headers = $null) { - $attempts = 1 - - while ($attempts -le 3) { - try { - return Invoke-RestMethod -Method $method -Uri $url -Body $body -Headers $headers - } - catch { - $response = $_.Exception.Response - - $statusCode = $response.StatusCode.value__ - $statusDescription = $response.StatusDescription - - if ($statusCode) { - Write-Host "API request attempt number $attempts to $url failed with statuscode $statusCode" - Write-Host $statusDescription - - Write-Host "Rate Limit Details:" - Write-Host "Total: $($response.Headers.GetValues("X-RateLimit-Limit"))" - Write-Host "Remaining: $($response.Headers.GetValues("X-RateLimit-Remaining"))" - Write-Host "Reset Epoch: $($response.Headers.GetValues("X-RateLimit-Reset"))" - } - else { - Write-Host "API request attempt number $attempts to $url failed with no statuscode present, exception follows:" - Write-Host $_.Exception.Response - Write-Host $_.Exception - } - - if ($attempts -ge 3) { - Write-Host "Abandoning Request $url after 3 attempts." - exit(1) - } - - Start-Sleep -s 10 - } - - $attempts += 1 + Invoke-RestMethod -Uri $url -Body $body -Headers $headers -Method "Post" -MaximumRetryCount 3 -RetryIntervalSec 10 } } @@ -119,7 +79,7 @@ function IsMavenPackageVersionPublished($pkgId, $pkgVersion, $groupId) { try { $uri = "https://oss.sonatype.org/content/repositories/releases/$groupId/$pkgId/$pkgVersion/$pkgId-$pkgVersion.pom" - $pomContent = Invoke-RestMethod -MaximumRetryCount 3 -Method "GET" -uri $uri + $pomContent = Invoke-RestMethod -MaximumRetryCount 3 -RetryIntervalSec 10 -Method "GET" -uri $uri if ($pomContent -ne $null -or $pomContent.Length -eq 0) { return $true @@ -382,7 +342,7 @@ function ParseCppArtifact($pkg, $workingDirectory) { # Returns the pypi publish status of a package id and version. function IsPythonPackageVersionPublished($pkgId, $pkgVersion) { try { - $existingVersion = (Invoke-RestMethod -MaximumRetryCount 3 -Method "Get" -uri "https://pypi.org/pypi/$pkgId/$pkgVersion/json").info.version + $existingVersion = (Invoke-RestMethod -MaximumRetryCount 3 -RetryIntervalSec 10 -Method "Get" -uri "https://pypi.org/pypi/$pkgId/$pkgVersion/json").info.version # if existingVersion exists, then it's already been published return $True @@ -406,7 +366,7 @@ function IsPythonPackageVersionPublished($pkgId, $pkgVersion) { # Retrieves the list of all tags that exist on the target repository function GetExistingTags($apiUrl) { try { - return (Invoke-RestMethod -Method "GET" -Uri "$apiUrl/git/refs/tags" -MaximumRetryCount 3 -RetryIntervalSec 10) | % { $_.ref.Replace("refs/tags/", "") } + return (Invoke-RestMethod -Method "GET" -Uri "$apiUrl/git/refs/tags" -MaximumRetryCount 3 -RetryIntervalSec 10) | % { $_.ref.Replace("refs/tags/", "") } } catch { Write-Host $_ @@ -531,7 +491,7 @@ function CheckArtifactShaAgainstTagsList($priorExistingTagList, $releaseSha, $ap $unmatchedTags = @() foreach ($tag in $priorExistingTagList) { - $tagSha = (Invoke-WebRequest-WithHandling -Method "Get" -Url "$apiUrl/git/refs/tags/$tag" -Headers $headers)."object".sha + $tagSha = (Invoke-RestMethod -Method "Get" -Uri "$apiUrl/git/refs/tags/$tag" -Headers $headers -MaximumRetryCount 3 -RetryIntervalSec 10)."object".sha if ($tagSha -eq $releaseSha) { Write-Host "This package has already been released. The existing tag commit SHA $releaseSha matches the artifact SHA being processed. Skipping release step for this tag." diff --git a/eng/common/scripts/update-docs-metadata.ps1 b/eng/common/scripts/update-docs-metadata.ps1 index a858078f5..b96ea284b 100644 --- a/eng/common/scripts/update-docs-metadata.ps1 +++ b/eng/common/scripts/update-docs-metadata.ps1 @@ -43,7 +43,7 @@ function GetMetaData($lang){ } } - $metadataResponse = Invoke-WebRequest-WithHandling -url $metadataUri -method "GET" | ConvertFrom-Csv + $metadataResponse = Invoke-RestMethod -Uri $metadataUri -method "GET" -MaximumRetryCount 3 -RetryIntervalSec 10 | ConvertFrom-Csv return $metadataResponse }