Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync eng/common directory with azure-sdk-tools repository #1460

Merged
merged 1 commit into from
Oct 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 35 additions & 38 deletions eng/common/TestResources/New-TestResources.ps1
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
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