Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit bdb849f

Browse files
authored
[Azure and AzureStack] Use IClientFactory to create ARM Client in Azure PowerShell way. (#348)
This takes care of both Azure and AzureStack services including setting the proper values for SubscriptionId and BaseUri, etc., This also ensure that the generated cmdlets remain compatible over changes to the Profile cmdlets and Azure authentication. Additional minor update - Removed 'MIT license' fixed header from the generated module helper script files.
1 parent 2b2782e commit bdb849f

10 files changed

+85
-142
lines changed

PSSwagger/AssemblyGenerationHelpers.Resources.psd1

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
#########################################################################################
2-
#
3-
# Copyright (c) Microsoft Corporation. All rights reserved.
4-
#
5-
# Licensed under the MIT license.
6-
#
7-
#########################################################################################
8-
91
ConvertFrom-StringData @'
102
###PSLOC
113

PSSwagger/AssemblyGenerationHelpers.ps1

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
#########################################################################################
2-
#
3-
# Copyright (c) Microsoft Corporation. All rights reserved.
4-
#
5-
# Licensed under the MIT license.
6-
#
7-
#########################################################################################
8-
91
Microsoft.PowerShell.Core\Set-StrictMode -Version Latest
102
Microsoft.PowerShell.Utility\Import-LocalizedData LocalizedData -FileName AssemblyGenerationHelpers.Resources.psd1
113

PSSwagger/GeneratedHelpers.ps1

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,5 @@
1-
#########################################################################################
2-
#
3-
# Copyright (c) Microsoft Corporation. All rights reserved.
4-
#
5-
# Licensed under the MIT license.
6-
#
7-
#########################################################################################
81
Microsoft.PowerShell.Core\Set-StrictMode -Version Latest
92

10-
<#
11-
.DESCRIPTION
12-
Creates AutoRest ServiceClientCredentials for Microsoft Azure using the logged in AzureRM context.
13-
#>
14-
function Get-AzServiceCredential
15-
{
16-
[CmdletBinding()]
17-
param()
18-
19-
$AzureContext = & "Get-AzureRmContext" -ErrorAction Stop
20-
$authenticationFactory = New-Object -TypeName Microsoft.Azure.Commands.Common.Authentication.Factories.AuthenticationFactory
21-
if ((Get-Variable -Name PSEdition -ErrorAction Ignore) -and ('Core' -eq $PSEdition)) {
22-
[Action[string]]$stringAction = {param($s)}
23-
$serviceCredentials = $authenticationFactory.GetServiceClientCredentials($AzureContext, $stringAction)
24-
} else {
25-
$serviceCredentials = $authenticationFactory.GetServiceClientCredentials($AzureContext)
26-
}
27-
28-
$serviceCredentials
29-
}
30-
31-
<#
32-
.DESCRIPTION
33-
Creates delegating handlers for Microsoft Azure generated modules.
34-
#>
35-
function Get-AzDelegatingHandler
36-
{
37-
[CmdletBinding()]
38-
param()
39-
40-
New-Object -TypeName System.Net.Http.DelegatingHandler[] 0
41-
}
42-
43-
<#
44-
.DESCRIPTION
45-
Gets the Azure subscription ID from the logged in AzureRM context.
46-
#>
47-
function Get-AzSubscriptionId
48-
{
49-
[CmdletBinding()]
50-
param()
51-
52-
$AzureContext = & "Get-AzureRmContext" -ErrorAction Stop
53-
if($AzureContext)
54-
{
55-
if(Get-Member -InputObject $AzureContext.Subscription -Name SubscriptionId)
56-
{
57-
return $AzureContext.Subscription.SubscriptionId
58-
}
59-
else
60-
{
61-
return $AzureContext.Subscription.Id
62-
}
63-
}
64-
}
65-
66-
<#
67-
.DESCRIPTION
68-
Gets the resource manager URL from the logged in AzureRM context.
69-
#>
70-
function Get-AzResourceManagerUrl
71-
{
72-
[CmdletBinding()]
73-
param()
74-
75-
$AzureContext = & "Get-AzureRmContext" -ErrorAction Stop
76-
$AzureContext.Environment.ResourceManagerUrl
77-
}
78-
793
<#
804
.DESCRIPTION
815
Creates a System.Net.Http.HttpClientHandler for the given credentials and sets preauthentication to true.

PSSwagger/New-ArmServiceClient.ps1

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Microsoft.PowerShell.Core\Set-StrictMode -Version Latest
2+
3+
<#
4+
.DESCRIPTION
5+
Creates Service Client object.
6+
7+
.PARAMETER FullClientTypeName
8+
Client type full name.
9+
10+
.PARAMETER GlobalParameterHashtable
11+
Global parameters to be set on client object.
12+
#>
13+
function New-ServiceClient {
14+
[CmdletBinding()]
15+
param(
16+
[Parameter(Mandatory = $true)]
17+
[string]
18+
$FullClientTypeName,
19+
20+
[Parameter(Mandatory = $false)]
21+
[PSCustomObject]
22+
$GlobalParameterHashtable
23+
)
24+
25+
# Azure Powershell way
26+
[Microsoft.Azure.Commands.Common.Authentication.Abstractions.IAzureContext]$Context = Get-AzureRmContext
27+
if (-not $Context -or -not $Context.Account) {
28+
Write-Error -Message 'Run Login-AzureRmAccount to login.' -ErrorId 'AzureRmContextError'
29+
return
30+
}
31+
32+
$Factory = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.ClientFactory
33+
[System.Type[]]$Types = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.IAzureContext], [string]
34+
$CreateArmClientMethod = [Microsoft.Azure.Commands.Common.Authentication.IClientFactory].GetMethod('CreateArmClient', $Types)
35+
$ClientType = $FullClientTypeName -as [Type]
36+
$ClosedMethod = $CreateArmClientMethod.MakeGenericMethod($ClientType)
37+
$Arguments = $Context, [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureEnvironment+Endpoint]::ResourceManager
38+
$Client = $closedMethod.Invoke($Factory, $Arguments)
39+
40+
if ($GlobalParameterHashtable) {
41+
$GlobalParameterHashtable.GetEnumerator() | ForEach-Object {
42+
if ($_.Value -and (Get-Member -InputObject $Client -Name $_.Key -MemberType Property)) {
43+
$Client."$($_.Key)" = $_.Value
44+
}
45+
}
46+
}
47+
48+
return $Client
49+
}

PSSwagger/New-ServiceClient.ps1

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ Microsoft.PowerShell.Core\Set-StrictMode -Version Latest
2323
Command should return a custom hostname string.
2424
Overrides the default host in the specification.
2525
26-
.PARAMETER SubscriptionIdCommand
27-
Custom command get SubscriptionId value.
28-
2926
.PARAMETER GlobalParameterHashtable
3027
Global parameters to be set on client object.
3128
#>
@@ -56,10 +53,6 @@ function New-ServiceClient {
5653
[string]
5754
$HostOverrideCommand,
5855

59-
[Parameter(Mandatory = $false)]
60-
[string]
61-
$SubscriptionIdCommand,
62-
6356
[Parameter(Mandatory = $false)]
6457
[PSCustomObject]
6558
$GlobalParameterHashtable
@@ -91,18 +84,8 @@ function New-ServiceClient {
9184

9285
if ($GlobalParameterHashtable) {
9386
$GlobalParameterHashtable.GetEnumerator() | ForEach-Object {
94-
if (Get-Member -InputObject $Client -Name $_.Key -MemberType Property) {
95-
if ((-not $_.Value) -and ($_.Key -eq 'SubscriptionId')) {
96-
if($SubscriptionIdCommand) {
97-
$Client.SubscriptionId = Invoke-Command -ScriptBlock [scriptblock]::Create($SubscriptionIdCommand)
98-
}
99-
else {
100-
$Client.SubscriptionId = Get-AzSubscriptionId
101-
}
102-
}
103-
else {
104-
$Client."$($_.Key)" = $_.Value
105-
}
87+
if ($_.Value -and (Get-Member -InputObject $Client -Name $_.Key -MemberType Property)) {
88+
$Client."$($_.Key)" = $_.Value
10689
}
10790
}
10891
}

PSSwagger/PSSwagger.psm1

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,9 +613,15 @@ function New-PSSwaggerModule
613613

614614
$CopyFilesMap = [ordered]@{
615615
'GeneratedHelpers.ps1' = 'GeneratedHelpers.ps1'
616-
'Test-CoreRequirements.ps1' = 'Test-CoreRequirements.ps1'
617-
'Test-FullRequirements.ps1' = 'Test-FullRequirements.ps1'
618-
'New-ServiceClient.ps1' = 'New-ServiceClient.ps1'
616+
}
617+
618+
if($UseAzureCsharpGenerator) {
619+
$CopyFilesMap['New-ArmServiceClient.ps1'] = 'New-ServiceClient.ps1'
620+
$CopyFilesMap['Test-FullRequirements.ps1'] = 'Test-FullRequirements.ps1'
621+
$CopyFilesMap['Test-CoreRequirements.ps1'] = 'Test-CoreRequirements.ps1'
622+
}
623+
else {
624+
$CopyFilesMap['New-ServiceClient.ps1'] = 'New-ServiceClient.ps1'
619625
}
620626

621627
if (-not $AssemblyFileName) {

PSSwagger/Paths.psm1

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -616,24 +616,25 @@ function New-SwaggerPath
616616
}
617617

618618
# Process security section
619-
$SubscriptionIdCommand = ""
620619
$AuthenticationCommand = ""
621620
$AuthenticationCommandArgumentName = ''
622621
$hostOverrideCommand = ''
623622
$AddHttpClientHandler = $false
624623
$securityParametersToAdd = @()
625624
$PowerShellCodeGen = $SwaggerMetaDict['PowerShellCodeGen']
626-
if (($PowerShellCodeGen['ServiceType'] -eq 'azure') -or ($PowerShellCodeGen['ServiceType'] -eq 'azure_stack')) {
627-
$SubscriptionIdCommand = 'Get-AzSubscriptionId'
628-
}
629-
if ($PowerShellCodeGen['CustomAuthCommand']) {
630-
$AuthenticationCommand = $PowerShellCodeGen['CustomAuthCommand']
631-
}
632-
if ($PowerShellCodeGen['HostOverrideCommand']) {
633-
$hostOverrideCommand = $PowerShellCodeGen['HostOverrideCommand']
625+
626+
# CustomAuthCommand and HostOverrideCommand are not required for Arm Services
627+
if (($PowerShellCodeGen['ServiceType'] -ne 'azure') -and ($PowerShellCodeGen['ServiceType'] -eq 'azure_stack')) {
628+
if ($PowerShellCodeGen['CustomAuthCommand']) {
629+
$AuthenticationCommand = $PowerShellCodeGen['CustomAuthCommand']
630+
}
631+
if ($PowerShellCodeGen['HostOverrideCommand']) {
632+
$hostOverrideCommand = $PowerShellCodeGen['HostOverrideCommand']
633+
}
634634
}
635+
635636
# If the auth function hasn't been set by metadata, try to discover it from the security and securityDefinition objects in the spec
636-
if (-not $AuthenticationCommand) {
637+
if (-not $AuthenticationCommand -and -not $UseAzureCsharpGenerator) {
637638
if ($FunctionDetails.ContainsKey('Security')) {
638639
# For now, just take the first security object
639640
if ($FunctionDetails.Security.Count -gt 1) {
@@ -729,7 +730,7 @@ function New-SwaggerPath
729730
}
730731
}
731732

732-
if (-not $AuthenticationCommand) {
733+
if (-not $AuthenticationCommand -and -not $UseAzureCsharpGenerator) {
733734
# At this point, there was no supported security object or overridden auth function, so assume no auth
734735
$AuthenticationCommand = 'Get-AutoRestCredential'
735736
}
@@ -973,13 +974,18 @@ function New-SwaggerPath
973974
ParameterGroupsExpressionBlock = $parameterGroupsExpressionBlock
974975
SwaggerDict = $SwaggerDict
975976
SwaggerMetaDict = $SwaggerMetaDict
976-
AddHttpClientHandler = $AddHttpClientHandler
977-
HostOverrideCommand = $hostOverrideCommand
978-
AuthenticationCommand = $AuthenticationCommand
979-
AuthenticationCommandArgumentName = $AuthenticationCommandArgumentName
980-
SubscriptionIdCommand = $SubscriptionIdCommand
981977
FlattenedParametersOnPSCmdlet = $flattenedParametersOnPSCmdlet
982978
}
979+
if($AuthenticationCommand) {
980+
$functionBodyParams['AuthenticationCommand'] = $AuthenticationCommand
981+
$functionBodyParams['AuthenticationCommandArgumentName'] = $AuthenticationCommandArgumentName
982+
}
983+
if($AddHttpClientHandler) {
984+
$functionBodyParams['AddHttpClientHandler'] = $AddHttpClientHandler
985+
}
986+
if($hostOverrideCommand) {
987+
$functionBodyParams['hostOverrideCommand'] = $hostOverrideCommand
988+
}
983989
if($globalParameters) {
984990
$functionBodyParams['GlobalParameters'] = $globalParameters
985991
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
22
"info": {
3-
"x-ps-code-generation-settings": {
4-
"customAuthCommand": "Get-AzServiceCredential"
5-
}
3+
"x-ps-code-generation-settings": {}
64
}
75
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
{
22
"info": {
3-
"x-ps-code-generation-settings": {
4-
"customAuthCommand": "Get-AzServiceCredential",
5-
"hostOverrideCommand": "Get-AzResourceManagerUrl"
6-
}
3+
"x-ps-code-generation-settings": {}
74
}
85
}

PSSwagger/SwaggerUtils.psm1

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,18 +1471,14 @@ function Get-PathFunctionBody
14711471
[string]
14721472
$HostOverrideCommand,
14731473

1474-
[Parameter(Mandatory=$true)]
1474+
[Parameter(Mandatory=$false)]
14751475
[string]
14761476
$AuthenticationCommand,
14771477

14781478
[Parameter(Mandatory=$false)]
14791479
[string]
14801480
$AuthenticationCommandArgumentName,
14811481

1482-
[Parameter(Mandatory=$false)]
1483-
[string]
1484-
$SubscriptionIdCommand,
1485-
14861482
[Parameter(Mandatory=$true)]
14871483
[PSCustomObject]
14881484
$FlattenedParametersOnPSCmdlet

0 commit comments

Comments
 (0)