From 7f321eca637d816cdf003b37347ecf316b9555a2 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 6 Oct 2020 12:04:11 -0700 Subject: [PATCH 1/7] Simplify Net-TestResources usage --- .../TestResources/New-TestResources.ps1 | 55 +++++++++++++++++-- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 59aab61fe728e..33dd55a6c7546 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -11,7 +11,7 @@ [CmdletBinding(DefaultParameterSetName = 'Default', SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] param ( # Limit $BaseName to enough characters to be under limit plus prefixes, and https://docs.microsoft.com/azure/architecture/best-practices/resource-naming. - [Parameter(Mandatory = $true, Position = 0)] + [Parameter(Position = 0)] [ValidatePattern('^[-a-zA-Z0-9\.\(\)_]{0,80}(?<=[a-zA-Z0-9\(\)])$')] [string] $BaseName, @@ -21,7 +21,7 @@ param ( [Parameter(Mandatory = $true)] [string] $ServiceDirectory, - [Parameter(Mandatory = $true)] + [Parameter()] [ValidatePattern('^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$')] [string] $TestApplicationId, @@ -119,6 +119,8 @@ $root = [System.IO.Path]::Combine($repositoryRoot, "sdk", $ServiceDirectory) | R $templateFileName = 'test-resources.json' $templateFiles = @() $environmentVariables = @{} +# Azure SDK Developer Playground +$defaultSubscription = "faa080af-c1d8-40ad-9cce-e1a450ca5b57" Write-Verbose "Checking for '$templateFileName' files under '$root'" Get-ChildItem -Path $root -Filter $templateFileName -Recurse | ForEach-Object { @@ -133,6 +135,26 @@ if (!$templateFiles) { exit } +$UserName = if ($env:USER) { $env:USER } else { "${env:USERNAME}" } + +# If no base name is specified use current user name +if (!$BaseName) { + $BaseName = "$UserName$ServiceDirectory" + + Log "BaseName was not set. Using default base name: '$BaseName'" +} + +# Try detecting repos that support OutFile and defaulting to it +if (!$CI -and !$PSBoundParameters.ContainsKey('OutFile')) +{ + # TODO: find a better way to detect the language + 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, you don't need to set environment variables manually." + } +} + # If no location is specified use safe default locations for the given # environment. If no matching environment is found $Location remains an empty # string. @@ -147,7 +169,7 @@ if (!$Location) { Write-Verbose "Location was not set. Using default location for environment: '$Location'" } -# Log in if requested; otherwise, the user is expected to already be authenticated via Connect-AzAccount. +# Log in if requested; otherwise, try to login into playground subscription. if ($ProvisionerApplicationId) { $null = Disable-AzContextAutosave -Scope Process @@ -176,6 +198,25 @@ if ($ProvisionerApplicationId) { } } } +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) { @@ -215,7 +256,7 @@ $ResourceGroupName = if ($ResourceGroupName) { # Tag the resource group to be deleted after a certain number of hours if specified. $tags = @{ - Creator = if ($env:USER) { $env:USER } else { "${env:USERNAME}" } + Creator = $UserName ServiceDirectory = $ServiceDirectory } @@ -403,7 +444,11 @@ foreach ($templateFile in $templateFiles) { $exitActions.Invoke() -return $environmentVariables +# Suppress output locally +if ($CI) +{ + return $environmentVariables +} <# .SYNOPSIS From 3dd5ac62a6efe2c5122fe21f980f4c939e53123f Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 6 Oct 2020 12:14:49 -0700 Subject: [PATCH 2/7] docs and windows check --- eng/common/TestResources/New-TestResources.ps1 | 2 +- eng/common/TestResources/README.md | 11 ++--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 33dd55a6c7546..096a14410f414 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -145,7 +145,7 @@ if (!$BaseName) { } # Try detecting repos that support OutFile and defaulting to it -if (!$CI -and !$PSBoundParameters.ContainsKey('OutFile')) +if (!$CI -and !$PSBoundParameters.ContainsKey('OutFile') -and $IsWindows) { # TODO: find a better way to detect the language if (Test-Path "$repositoryRoot/eng/service.proj") diff --git a/eng/common/TestResources/README.md b/eng/common/TestResources/README.md index 9cd79fe714bed..6702cc99aa00c 100644 --- a/eng/common/TestResources/README.md +++ b/eng/common/TestResources/README.md @@ -26,17 +26,10 @@ Note that `-Subscription` is an optional parameter but recommended if your accou is a member of multiple subscriptions. ```powershell -Connect-AzAccount -Subscription 'YOUR SUBSCRIPTION ID' -$sp = New-AzADServicePrincipal -Role Owner -eng\common\TestResources\New-TestResources.ps1 ` - -BaseName 'myusername' ` - -ServiceDirectory 'search' ` - -TestApplicationId $sp.ApplicationId ` - -TestApplicationSecret (ConvertFrom-SecureString $sp.Secret -AsPlainText) +eng\common\TestResources\New-TestResources.ps1 -ServiceDirectory 'search' ``` -If you are running this for a .NET project on Windows, the recommended method is to -add the `-OutFile` switch to the above command. This will save test environment settings +The `OutFile` switch would be set if you are running this for a .NET project on Windows. This will save test environment settings into a test-resources.json.env file next to test-resources.json. The file is protected via DPAPI. The environment file would be scoped to the current repository directory and avoids the need to set environment variables or restart your IDE to recognize them. From f190801aeb2db8299018e1b1ac023869082414e4 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 6 Oct 2020 12:54:00 -0700 Subject: [PATCH 3/7] Update eng/common/TestResources/New-TestResources.ps1 Co-authored-by: Heath Stewart --- eng/common/TestResources/New-TestResources.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 096a14410f414..d65defd7c0c6f 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -151,7 +151,7 @@ if (!$CI -and !$PSBoundParameters.ContainsKey('OutFile') -and $IsWindows) 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, you don't need to set environment variables manually." + 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." } } @@ -613,4 +613,4 @@ log redaction). .LINK Remove-TestResources.ps1 -#> \ No newline at end of file +#> From fc80b68a381afae039ee1933e715ef708144feae Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 6 Oct 2020 12:56:20 -0700 Subject: [PATCH 4/7] update markdown --- .../TestResources/New-TestResources.ps1.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1.md b/eng/common/TestResources/New-TestResources.ps1.md index aab44b896d1f7..88024f2c69996 100644 --- a/eng/common/TestResources/New-TestResources.ps1.md +++ b/eng/common/TestResources/New-TestResources.ps1.md @@ -14,16 +14,16 @@ Deploys live test resources defined for a service directory to Azure. ### Default (Default) ``` -New-TestResources.ps1 [-BaseName] [-ResourceGroupName ] -ServiceDirectory - -TestApplicationId [-TestApplicationSecret ] [-TestApplicationOid ] +New-TestResources.ps1 [[-BaseName] ] [-ResourceGroupName ] -ServiceDirectory + [-TestApplicationId ] [-TestApplicationSecret ] [-TestApplicationOid ] [-DeleteAfterHours ] [-Location ] [-Environment ] [-AdditionalParameters ] [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] [] ``` ### Provisioner ``` -New-TestResources.ps1 [-BaseName] [-ResourceGroupName ] -ServiceDirectory - -TestApplicationId [-TestApplicationSecret ] [-TestApplicationOid ] +New-TestResources.ps1 [[-BaseName] ] [-ResourceGroupName ] -ServiceDirectory + [-TestApplicationId ] [-TestApplicationSecret ] [-TestApplicationOid ] -TenantId [-SubscriptionId ] -ProvisionerApplicationId -ProvisionerApplicationSecret [-DeleteAfterHours ] [-Location ] [-Environment ] [-AdditionalParameters ] [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] @@ -106,7 +106,7 @@ Type: String Parameter Sets: (All) Aliases: -Required: True +Required: False Position: 1 Default value: None Accept pipeline input: False @@ -159,7 +159,7 @@ Type: String Parameter Sets: (All) Aliases: -Required: True +Required: False Position: Named Default value: None Accept pipeline input: False @@ -197,7 +197,7 @@ It is passed as to the ARM template as 'testApplicationOid' For more information on the relationship between AAD Applications and Service -Principals see: https://docs.microsoft.com/azure/active-directory/develop/app-objects-and-service-principals +Principals see: https://docs.microsoft.com/en-us/azure/active-directory/develop/app-objects-and-service-principals ```yaml Type: String @@ -458,3 +458,6 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ## NOTES ## RELATED LINKS + +[Remove-TestResources.ps1]() + From ee1f10c64401dbb937ffffe610fe9a77a54bb7a9 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 6 Oct 2020 13:06:12 -0700 Subject: [PATCH 5/7] make service directory the default parameter --- eng/common/TestResources/New-TestResources.ps1 | 4 ++-- eng/common/TestResources/New-TestResources.ps1.md | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index d65defd7c0c6f..f881cdc453c7c 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -11,14 +11,14 @@ [CmdletBinding(DefaultParameterSetName = 'Default', SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] param ( # Limit $BaseName to enough characters to be under limit plus prefixes, and https://docs.microsoft.com/azure/architecture/best-practices/resource-naming. - [Parameter(Position = 0)] + [Parameter()] [ValidatePattern('^[-a-zA-Z0-9\.\(\)_]{0,80}(?<=[a-zA-Z0-9\(\)])$')] [string] $BaseName, [ValidatePattern('^[-\w\._\(\)]+$')] [string] $ResourceGroupName, - [Parameter(Mandatory = $true)] + [Parameter(Mandatory = $true, Position = 0)] [string] $ServiceDirectory, [Parameter()] diff --git a/eng/common/TestResources/New-TestResources.ps1.md b/eng/common/TestResources/New-TestResources.ps1.md index 88024f2c69996..5e7b843c342a9 100644 --- a/eng/common/TestResources/New-TestResources.ps1.md +++ b/eng/common/TestResources/New-TestResources.ps1.md @@ -14,7 +14,7 @@ Deploys live test resources defined for a service directory to Azure. ### Default (Default) ``` -New-TestResources.ps1 [[-BaseName] ] [-ResourceGroupName ] -ServiceDirectory +New-TestResources.ps1 [-BaseName ] [-ResourceGroupName ] [-ServiceDirectory] [-TestApplicationId ] [-TestApplicationSecret ] [-TestApplicationOid ] [-DeleteAfterHours ] [-Location ] [-Environment ] [-AdditionalParameters ] [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] [] @@ -22,7 +22,7 @@ New-TestResources.ps1 [[-BaseName] ] [-ResourceGroupName ] -Serv ### Provisioner ``` -New-TestResources.ps1 [[-BaseName] ] [-ResourceGroupName ] -ServiceDirectory +New-TestResources.ps1 [-BaseName ] [-ResourceGroupName ] [-ServiceDirectory] [-TestApplicationId ] [-TestApplicationSecret ] [-TestApplicationOid ] -TenantId [-SubscriptionId ] -ProvisionerApplicationId -ProvisionerApplicationSecret [-DeleteAfterHours ] [-Location ] @@ -107,7 +107,7 @@ Parameter Sets: (All) Aliases: Required: False -Position: 1 +Position: Named Default value: None Accept pipeline input: False Accept wildcard characters: False @@ -140,7 +140,7 @@ Parameter Sets: (All) Aliases: Required: True -Position: Named +Position: 1 Default value: None Accept pipeline input: False Accept wildcard characters: False From 1f8dacffda02c0e24229afbbda747d97a24c74ce Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Wed, 7 Oct 2020 08:13:09 -0700 Subject: [PATCH 6/7] Fix links --- eng/common/TestResources/New-TestResources.ps1 | 2 +- eng/common/TestResources/New-TestResources.ps1.md | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index f881cdc453c7c..4ce8a76d1a432 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -511,7 +511,7 @@ test resources (e.g. Role Assignments on resources). It is passed as to the ARM template as 'testApplicationOid' For more information on the relationship between AAD Applications and Service -Principals see: https://docs.microsoft.com/en-us/azure/active-directory/develop/app-objects-and-service-principals +Principals see: https://docs.microsoft.com/azure/active-directory/develop/app-objects-and-service-principals .PARAMETER TenantId The tenant ID of a service principal when a provisioner is specified. The same diff --git a/eng/common/TestResources/New-TestResources.ps1.md b/eng/common/TestResources/New-TestResources.ps1.md index 5e7b843c342a9..6fc15eefdb493 100644 --- a/eng/common/TestResources/New-TestResources.ps1.md +++ b/eng/common/TestResources/New-TestResources.ps1.md @@ -197,7 +197,7 @@ It is passed as to the ARM template as 'testApplicationOid' For more information on the relationship between AAD Applications and Service -Principals see: https://docs.microsoft.com/en-us/azure/active-directory/develop/app-objects-and-service-principals +Principals see: https://docs.microsoft.com/azure/active-directory/develop/app-objects-and-service-principals ```yaml Type: String @@ -458,6 +458,3 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ## NOTES ## RELATED LINKS - -[Remove-TestResources.ps1]() - From f8725d06a42512d7eae826f7587a64b098e9966c Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Wed, 7 Oct 2020 10:58:41 -0700 Subject: [PATCH 7/7] Doc change --- eng/common/TestResources/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/eng/common/TestResources/README.md b/eng/common/TestResources/README.md index 6702cc99aa00c..070f2ba49ec57 100644 --- a/eng/common/TestResources/README.md +++ b/eng/common/TestResources/README.md @@ -26,6 +26,7 @@ Note that `-Subscription` is an optional parameter but recommended if your accou is a member of multiple subscriptions. ```powershell +Connect-AzAccount -Subscription 'YOUR SUBSCRIPTION ID' eng\common\TestResources\New-TestResources.ps1 -ServiceDirectory 'search' ```