Skip to content

Commit f3a6382

Browse files
committed
Merge pull request #1715 from Microsoft/users/ersciple/m100cherryppickazps
Users/ersciple/m100cherryppickazps
2 parents 04993cf + f89df18 commit f3a6382

22 files changed

+359
-112
lines changed

Tasks/AzurePowerShell/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"version": {
1414
"Major": 1,
1515
"Minor": 0,
16-
"Patch": 23
16+
"Patch": 24
1717
},
1818
"demands": [
1919
"azureps"

Tasks/AzurePowerShell/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"version": {
1414
"Major": 1,
1515
"Minor": 0,
16-
"Patch": 23
16+
"Patch": 24
1717
},
1818
"demands": [
1919
"azureps"

Tasks/Common/VstsAzureHelpers_/ImportFunctions.ps1

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
11
function Import-AzureModule {
22
[CmdletBinding()]
3-
param([switch]$PreferAzureRM)
3+
param(
4+
[Parameter(Mandatory = $true)]
5+
[ValidateSet('Azure', 'AzureRM')]
6+
[string[]]$PreferredModule)
47

58
Trace-VstsEnteringInvocation $MyInvocation
69
try {
710
Write-Verbose "Env:PSModulePath: '$env:PSMODULEPATH'"
8-
if ($PreferAzureRM) {
9-
if (!(Import-FromModulePath -Classic:$false) -and
10-
!(Import-FromSdkPath -Classic:$false) -and
11-
!(Import-FromModulePath -Classic:$true) -and
12-
!(Import-FromSdkPath -Classic:$true))
13-
{
11+
if ($PreferredModule -contains 'Azure' -and $PreferredModule -contains 'AzureRM') {
12+
# Attempt to import Azure and AzureRM.
13+
$azure = (Import-FromModulePath -Classic:$true) -or (Import-FromSdkPath -Classic:$true)
14+
$azureRM = (Import-FromModulePath -Classic:$false) -or (Import-FromSdkPath -Classic:$false)
15+
if (!$azure -and !$azureRM) {
1416
throw (Get-VstsLocString -Key AZ_ModuleNotFound)
1517
}
16-
} else {
18+
} elseif ($PreferredModule -contains 'Azure') {
19+
# Attempt to import Azure but fallback to AzureRM.
1720
if (!(Import-FromModulePath -Classic:$true) -and
1821
!(Import-FromSdkPath -Classic:$true) -and
1922
!(Import-FromModulePath -Classic:$false) -and
2023
!(Import-FromSdkPath -Classic:$false))
2124
{
2225
throw (Get-VstsLocString -Key AZ_ModuleNotFound)
2326
}
27+
} else {
28+
# Attempt to import AzureRM but fallback to Azure.
29+
if (!(Import-FromModulePath -Classic:$false) -and
30+
!(Import-FromSdkPath -Classic:$false) -and
31+
!(Import-FromModulePath -Classic:$true) -and
32+
!(Import-FromSdkPath -Classic:$true))
33+
{
34+
throw (Get-VstsLocString -Key AZ_ModuleNotFound)
35+
}
2436
}
2537

2638
# Validate the Classic version.
2739
$minimumVersion = [version]'0.8.10.1'
28-
if ($script:isClassic -and $script:classicVersion -lt $minimumVersion) {
40+
if ($script:azureModule -and $script:azureModule.Version -lt $minimumVersion) {
2941
throw (Get-VstsLocString -Key AZ_RequiresMinVersion0 -ArgumentList $minimumVersion)
3042
}
3143
} finally {
@@ -59,12 +71,9 @@ function Import-FromModulePath {
5971
$module = Import-Module -Name $module.Path -Global -PassThru
6072
Write-Verbose "Imported module version: $($module.Version)"
6173

62-
# Store the mode.
63-
$script:isClassic = $Classic.IsPresent
64-
65-
if ($script:isClassic) {
66-
# The Azure module was imported.
67-
$script:classicVersion = $module.Version
74+
if ($Classic) {
75+
# Store the imported Azure module.
76+
$script:azureModule = $module
6877
} else {
6978
# The AzureRM module was imported.
7079

@@ -74,10 +83,10 @@ function Import-FromModulePath {
7483
throw (Get-VstsLocString -Key AZ_AzureRMProfileModuleNotFound)
7584
}
7685

77-
# Import the AzureRM.profile module.
86+
# Import and then store the AzureRM.profile module.
7887
Write-Host "##[command]Import-Module -Name $($profileModule.Path) -Global"
79-
$profileModule = Import-Module -Name $profileModule.Path -Global -PassThru
80-
Write-Verbose "Imported module version: $($profileModule.Version)"
88+
$script:azureRMProfileModule = Import-Module -Name $profileModule.Path -Global -PassThru
89+
Write-Verbose "Imported module version: $($script:azureRMProfileModule.Version)"
8190
}
8291

8392
return $true
@@ -111,12 +120,11 @@ function Import-FromSdkPath {
111120
$module = Import-Module -Name $path -Global -PassThru
112121
Write-Verbose "Imported module version: $($module.Version)"
113122

114-
# Store the mode.
115-
$script:isClassic = $Classic.IsPresent
116-
123+
# Store the imported module.
117124
if ($Classic) {
118-
# The Azure module was imported.
119-
$script:classicVersion = $module.Version
125+
$script:azureModule = $module
126+
} else {
127+
$script:azureRMProfileModule = $module
120128
}
121129

122130
return $true

Tasks/Common/VstsAzureHelpers_/InitializeFunctions.ps1

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
$script:isClassic = $null
2-
$script:classicVersion = $null
3-
4-
function Add-Certificate {
1+
function Add-Certificate {
52
[CmdletBinding()]
63
param([Parameter(Mandatory=$true)]$Endpoint)
74

@@ -28,7 +25,7 @@ function Initialize-AzureSubscription {
2825

2926
if ($Endpoint.Auth.Scheme -eq 'Certificate') {
3027
# Certificate is only supported for the Azure module.
31-
if (!$script:isClassic) {
28+
if (!$script:azureModule) {
3229
throw (Get-VstsLocString -Key AZ_CertificateAuthNotSupported)
3330
}
3431

@@ -49,7 +46,9 @@ function Initialize-AzureSubscription {
4946
$psCredential = New-Object System.Management.Automation.PSCredential(
5047
$Endpoint.Auth.Parameters.UserName,
5148
(ConvertTo-SecureString $Endpoint.Auth.Parameters.Password -AsPlainText -Force))
52-
if ($script:isClassic) {
49+
50+
# Add account (Azure).
51+
if ($script:azureModule) {
5352
try {
5453
Write-Host "##[command]Add-AzureAccount -Credential $psCredential"
5554
$null = Add-AzureAccount -Credential $psCredential
@@ -58,9 +57,10 @@ function Initialize-AzureSubscription {
5857
Write-VstsTaskError -Message $_.Exception.Message
5958
throw (New-Object System.Exception((Get-VstsLocString -Key AZ_CredentialsError), $_.Exception))
6059
}
60+
}
6161

62-
Set-CurrentAzureSubscription -SubscriptionId $Endpoint.Data.SubscriptionId -StorageAccount $StorageAccount
63-
} else {
62+
# Add account (AzureRM).
63+
if ($script:azureRMProfileModule) {
6464
try {
6565
Write-Host "##[command]Add-AzureRMAccount -Credential $psCredential"
6666
$null = Add-AzureRMAccount -Credential $psCredential
@@ -69,14 +69,22 @@ function Initialize-AzureSubscription {
6969
Write-VstsTaskError -Message $_.Exception.Message
7070
throw (New-Object System.Exception((Get-VstsLocString -Key AZ_CredentialsError), $_.Exception))
7171
}
72+
}
73+
74+
# Select subscription (Azure).
75+
if ($script:azureModule) {
76+
Set-CurrentAzureSubscription -SubscriptionId $Endpoint.Data.SubscriptionId -StorageAccount $StorageAccount
77+
}
7278

79+
# Select subscription (AzureRM).
80+
if ($script:azureRMProfileModule) {
7381
Set-CurrentAzureRMSubscription -SubscriptionId $Endpoint.Data.SubscriptionId
7482
}
7583
} elseif ($Endpoint.Auth.Scheme -eq 'ServicePrincipal') {
7684
$psCredential = New-Object System.Management.Automation.PSCredential(
7785
$Endpoint.Auth.Parameters.ServicePrincipalId,
7886
(ConvertTo-SecureString $Endpoint.Auth.Parameters.ServicePrincipalKey -AsPlainText -Force))
79-
if ($script:isClassic -and $script:classicVersion -lt ([version]'0.9.9')) {
87+
if ($script:azureModule -and $script:azureModule.Version -lt ([version]'0.9.9')) {
8088
# Service principals arent supported from 0.9.9 and greater in the Azure module.
8189
try {
8290
Write-Host "##[command]Add-AzureAccount -ServicePrincipal -Tenant $($Endpoint.Auth.Parameters.TenantId) -Credential $psCredential"
@@ -88,9 +96,9 @@ function Initialize-AzureSubscription {
8896
}
8997

9098
Set-CurrentAzureSubscription -SubscriptionId $Endpoint.Data.SubscriptionId -StorageAccount $StorageAccount
91-
} elseif ($script:isClassic) {
99+
} elseif ($script:azureModule) {
92100
# Throw if >=0.9.9 Azure.
93-
throw (Get-VstsLocString -Key "AZ_ServicePrincipalAuthNotSupportedAzureVersion0" -ArgumentList $script:classicVersion)
101+
throw (Get-VstsLocString -Key "AZ_ServicePrincipalAuthNotSupportedAzureVersion0" -ArgumentList $script:azureModule.Version)
94102
} else {
95103
# Else, this is AzureRM.
96104
try {
@@ -117,7 +125,7 @@ function Set-CurrentAzureSubscription {
117125
[string]$StorageAccount)
118126

119127
$additional = @{ }
120-
if ($script:isClassic -and $script:classicVersion -lt ([version]'0.8.15')) {
128+
if ($script:azureModule.Version -lt ([version]'0.8.15')) {
121129
$additional['Default'] = $true # The Default switch is required prior to 0.8.15.
122130
}
123131

Tasks/Common/VstsAzureHelpers_/VstsAzureHelpers_.psm1

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Private module-scope variables.
2-
$script:isClassic = $null
3-
$script:classicVersion = $null
2+
$script:azureModule = $null
3+
$script:azureRMProfileModule = $null
44

55
# Override the DebugPreference.
66
if ($global:DebugPreference -eq 'Continue') {
@@ -32,8 +32,19 @@ function Initialize-Azure {
3232
$endpoint = Get-VstsEndpoint -Name $serviceName -Require
3333
$storageAccount = Get-VstsInput -Name StorageAccount
3434

35+
# Determine which modules are preferred.
36+
$preferredModules = @( )
37+
if ($endpoint.Auth.Scheme -eq 'ServicePrincipal') {
38+
$preferredModules += 'AzureRM'
39+
} elseif ($endpoint.Auth.Scheme -eq 'UserNamePassword') {
40+
$preferredModules += 'Azure'
41+
$preferredModules += 'AzureRM'
42+
} else {
43+
$preferredModules += 'Azure'
44+
}
45+
3546
# Import/initialize the Azure module.
36-
Import-AzureModule -PreferAzureRM:($serviceNameInput -eq 'ConnectedServiceNameARM')
47+
Import-AzureModule -PreferredModule $preferredModules
3748
Initialize-AzureSubscription -Endpoint $endpoint -StorageAccount $storageAccount
3849
} finally {
3950
Trace-VstsLeavingInvocation $MyInvocation
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
# Arrange.
5+
. $PSScriptRoot/../../lib/Initialize-Test.ps1
6+
$module = Microsoft.PowerShell.Core\Import-Module $PSScriptRoot/../../../Tasks/AzurePowerShell/ps_modules/VstsAzureHelpers_ -PassThru
7+
$variableSets = @(
8+
@{
9+
ClassicModulePathResult = $true
10+
ClassicSdkPathResult = $null
11+
RMModulePathResult = $null
12+
RMSdkPathResult = $null
13+
}
14+
@{
15+
ClassicModulePathResult = $false
16+
ClassicSdkPathResult = $true
17+
RMModulePathResult = $null
18+
RMSdkPathResult = $null
19+
}
20+
@{
21+
ClassicModulePathResult = $false
22+
ClassicSdkPathResult = $false
23+
RMModulePathResult = $true
24+
RMSdkPathResult = $null
25+
}
26+
@{
27+
ClassicModulePathResult = $false
28+
ClassicSdkPathResult = $false
29+
RMModulePathResult = $false
30+
RMSdkPathResult = $true
31+
}
32+
)
33+
foreach ($variableSet in $variableSets) {
34+
Write-Verbose ('-' * 80)
35+
Unregister-Mock Import-FromModulePath
36+
Unregister-Mock Import-FromSdkPath
37+
Register-Mock Import-FromModulePath
38+
Register-Mock Import-FromSdkPath
39+
if ($variableSet.RMModulePathResult -ne $null) {
40+
Register-Mock Import-FromModulePath { $variableSet.RMModulePathResult } -- -Classic: $false
41+
}
42+
43+
if ($variableSet.RMSdkPathResult -ne $null) {
44+
Register-Mock Import-FromSdkPath { $variableSet.RMSdkPathResult } -- -Classic: $false
45+
}
46+
47+
if ($variableSet.ClassicModulePathResult -ne $null) {
48+
Register-Mock Import-FromModulePath { $variableSet.ClassicModulePathResult } -- -Classic: $true
49+
}
50+
51+
if ($variableSet.ClassicSdkPathResult -ne $null) {
52+
Register-Mock Import-FromSdkPath { $variableSet.ClassicSdkPathResult } -- -Classic: $true
53+
}
54+
55+
# Act.
56+
& $module Import-AzureModule -PreferredModule 'Azure'
57+
58+
# Assert.
59+
Assert-WasCalled Import-FromModulePath -Times $(if ($variableSet.RMModulePathResult -eq $null) { 0 } else { 1 }) -- -Classic: $false
60+
Assert-WasCalled Import-FromSdkPath -Times $(if ($variableSet.RMSdkPathResult -eq $null) { 0 } else { 1 }) -- -Classic: $false
61+
Assert-WasCalled Import-FromModulePath -Times $(if ($variableSet.ClassicModulePathResult -eq $null) { 0 } else { 1 }) -- -Classic: $true
62+
Assert-WasCalled Import-FromSdkPath -Times $(if ($variableSet.ClassicSdkPathResult -eq $null) { 0 } else { 1 }) -- -Classic: $true
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
# Arrange.
5+
. $PSScriptRoot/../../lib/Initialize-Test.ps1
6+
$module = Microsoft.PowerShell.Core\Import-Module $PSScriptRoot/../../../Tasks/AzurePowerShell/ps_modules/VstsAzureHelpers_ -PassThru
7+
$variableSets = @(
8+
@{
9+
RMModulePathResult = $true
10+
RMSdkPathResult = $null
11+
ClassicModulePathResult = $null
12+
ClassicSdkPathResult = $null
13+
}
14+
@{
15+
RMModulePathResult = $false
16+
RMSdkPathResult = $true
17+
ClassicModulePathResult = $null
18+
ClassicSdkPathResult = $null
19+
}
20+
@{
21+
RMModulePathResult = $false
22+
RMSdkPathResult = $false
23+
ClassicModulePathResult = $true
24+
ClassicSdkPathResult = $null
25+
}
26+
@{
27+
RMModulePathResult = $false
28+
RMSdkPathResult = $false
29+
ClassicModulePathResult = $false
30+
ClassicSdkPathResult = $true
31+
}
32+
)
33+
foreach ($variableSet in $variableSets) {
34+
Write-Verbose ('-' * 80)
35+
Unregister-Mock Import-FromModulePath
36+
Unregister-Mock Import-FromSdkPath
37+
Register-Mock Import-FromModulePath
38+
Register-Mock Import-FromSdkPath
39+
if ($variableSet.RMModulePathResult -ne $null) {
40+
Register-Mock Import-FromModulePath { $variableSet.RMModulePathResult } -- -Classic: $false
41+
}
42+
43+
if ($variableSet.RMSdkPathResult -ne $null) {
44+
Register-Mock Import-FromSdkPath { $variableSet.RMSdkPathResult } -- -Classic: $false
45+
}
46+
47+
if ($variableSet.ClassicModulePathResult -ne $null) {
48+
Register-Mock Import-FromModulePath { $variableSet.ClassicModulePathResult } -- -Classic: $true
49+
}
50+
51+
if ($variableSet.ClassicSdkPathResult -ne $null) {
52+
Register-Mock Import-FromSdkPath { $variableSet.ClassicSdkPathResult } -- -Classic: $true
53+
}
54+
55+
# Act.
56+
& $module Import-AzureModule -PreferredModule 'AzureRM'
57+
58+
# Assert.
59+
Assert-WasCalled Import-FromModulePath -Times $(if ($variableSet.RMModulePathResult -eq $null) { 0 } else { 1 }) -- -Classic: $false
60+
Assert-WasCalled Import-FromSdkPath -Times $(if ($variableSet.RMSdkPathResult -eq $null) { 0 } else { 1 }) -- -Classic: $false
61+
Assert-WasCalled Import-FromModulePath -Times $(if ($variableSet.ClassicModulePathResult -eq $null) { 0 } else { 1 }) -- -Classic: $true
62+
Assert-WasCalled Import-FromSdkPath -Times $(if ($variableSet.ClassicSdkPathResult -eq $null) { 0 } else { 1 }) -- -Classic: $true
63+
}

0 commit comments

Comments
 (0)