Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools repository (#1460)
Browse files Browse the repository at this point in the history
  • Loading branch information
azure-sdk committed Oct 27, 2020
1 parent d4dd3e2 commit b5ae76c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
73 changes: 35 additions & 38 deletions eng/common/TestResources/New-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,9 @@ if (!$BaseName) {
}

# Try detecting repos that support OutFile and defaulting to it
if (!$CI -and !$PSBoundParameters.ContainsKey('OutFile') -and $IsWindows)
{
if (!$CI -and !$PSBoundParameters.ContainsKey('OutFile') -and $IsWindows) {
# TODO: find a better way to detect the language
if (Test-Path "$repositoryRoot/eng/service.proj")
{
if (Test-Path "$repositoryRoot/eng/service.proj") {
$OutFile = $true
Log "Detected .NET repository. Defaulting OutFile to true. Test environment settings would be stored into the file so you don't need to set environment variables manually."
}
Expand All @@ -169,7 +167,32 @@ if (!$Location) {
Write-Verbose "Location was not set. Using default location for environment: '$Location'"
}

# Log in if requested; otherwise, try to login into playground subscription.
# If no test application ID is specified during an interactive session, create a new service principal.
if (!$CI -and !$TestApplicationId) {

# Make sure the user is logged in to create a service principal.
$context = Get-AzContext;
if (!$context) {
Log "You are not logged in; connecting to 'Azure SDK Developer Playground'"
$context = (Connect-AzAccount -Subscription $defaultSubscription).Context
}

Log "TestApplicationId was not specified; creating a new service principal"
$servicePrincipal = New-AzADServicePrincipal -Role Owner

$TestApplicationId = $servicePrincipal.ApplicationId
$TestApplicationSecret = (ConvertFrom-SecureString $servicePrincipal.Secret -AsPlainText);

Log "Created service principal '$TestApplicationId'"

if (!$ProvisionerApplicationId) {
$ProvisionerApplicationId = $TestApplicationId
$ProvisionerApplicationSecret = $TestApplicationSecret
$TenantId = $context.Tenant.Id
}
}

# Log in as and run pre- and post-scripts as the provisioner service principal.
if ($ProvisionerApplicationId) {
$null = Disable-AzContextAutosave -Scope Process

Expand All @@ -180,8 +203,7 @@ if ($ProvisionerApplicationId) {
# Use the given subscription ID if provided.
$subscriptionArgs = if ($SubscriptionId) {
@{SubscriptionId = $SubscriptionId}
}
else {
} else {
@{}
}

Expand All @@ -192,31 +214,12 @@ if ($ProvisionerApplicationId) {
$exitActions += {
Write-Verbose "Logging out of service principal '$($provisionerAccount.Context.Account)'"

# Only attempt to disconnect if the -WhatIf flag was not set. Otherwise, this call is not necessary and will fail.
# 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
}
}
}
elseif (!$CI)
{
# check if user is logged in and login into
$context = Get-AzContext;
if (!$context)
{
Log "You are not logged in, connecting to 'Azure SDK Developer Playground'"
Connect-AzAccount -Subscription $defaultSubscription
}

# If no test application id is specified create a new service principal
if (!$TestApplicationId) {
Log "TestApplicationId was not specified, creating a new service principal."
$servicePrincipal = New-AzADServicePrincipal -Role Owner

$TestApplicationId = $servicePrincipal.ApplicationId
$TestApplicationSecret = (ConvertFrom-SecureString $servicePrincipal.Secret -AsPlainText);
}
}

# Get test application OID from ID if not already provided.
if ($TestApplicationId -and !$TestApplicationOid) {
Expand Down Expand Up @@ -387,10 +390,8 @@ foreach ($templateFile in $templateFiles) {
}
}

if ($OutFile)
{
if (!$IsWindows)
{
if ($OutFile) {
if (!$IsWindows) {
Write-Host "File option is supported only on Windows"
}

Expand All @@ -403,17 +404,14 @@ foreach ($templateFile in $templateFiles) {
Set-Content $outputFile -Value $protectedBytes -AsByteStream -Force

Write-Host "Test environment settings`n $environmentText`nstored into encrypted $outputFile"
}
else
{
} 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"
}

foreach ($key in $deploymentOutputs.Keys)
{
foreach ($key in $deploymentOutputs.Keys) {
$value = $deploymentOutputs[$key]
$environmentVariables[$key] = $value

Expand Down Expand Up @@ -445,8 +443,7 @@ foreach ($templateFile in $templateFiles) {
$exitActions.Invoke()

# Suppress output locally
if ($CI)
{
if ($CI) {
return $environmentVariables
}

Expand Down
2 changes: 1 addition & 1 deletion eng/common/TestResources/Remove-TestResources.ps1.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Accept wildcard characters: False
### -Environment
Name of the cloud environment.
The default is the Azure Public Cloud
('PublicCloud')
('AzureCloud')

```yaml
Type: String
Expand Down

0 comments on commit b5ae76c

Please sign in to comment.