Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions PSSwagger/AssemblyGenerationHelpers.Resources.psd1
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
#########################################################################################
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# Licensed under the MIT license.
#
#########################################################################################

ConvertFrom-StringData @'
###PSLOC

Expand Down
8 changes: 0 additions & 8 deletions PSSwagger/AssemblyGenerationHelpers.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
#########################################################################################
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# Licensed under the MIT license.
#
#########################################################################################

Microsoft.PowerShell.Core\Set-StrictMode -Version Latest
Microsoft.PowerShell.Utility\Import-LocalizedData LocalizedData -FileName AssemblyGenerationHelpers.Resources.psd1

Expand Down
76 changes: 0 additions & 76 deletions PSSwagger/GeneratedHelpers.ps1
Original file line number Diff line number Diff line change
@@ -1,81 +1,5 @@
#########################################################################################
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# Licensed under the MIT license.
#
#########################################################################################
Microsoft.PowerShell.Core\Set-StrictMode -Version Latest

<#
.DESCRIPTION
Creates AutoRest ServiceClientCredentials for Microsoft Azure using the logged in AzureRM context.
#>
function Get-AzServiceCredential
{
[CmdletBinding()]
param()

$AzureContext = & "Get-AzureRmContext" -ErrorAction Stop
$authenticationFactory = New-Object -TypeName Microsoft.Azure.Commands.Common.Authentication.Factories.AuthenticationFactory
if ((Get-Variable -Name PSEdition -ErrorAction Ignore) -and ('Core' -eq $PSEdition)) {
[Action[string]]$stringAction = {param($s)}
$serviceCredentials = $authenticationFactory.GetServiceClientCredentials($AzureContext, $stringAction)
} else {
$serviceCredentials = $authenticationFactory.GetServiceClientCredentials($AzureContext)
}

$serviceCredentials
}

<#
.DESCRIPTION
Creates delegating handlers for Microsoft Azure generated modules.
#>
function Get-AzDelegatingHandler
{
[CmdletBinding()]
param()

New-Object -TypeName System.Net.Http.DelegatingHandler[] 0
}

<#
.DESCRIPTION
Gets the Azure subscription ID from the logged in AzureRM context.
#>
function Get-AzSubscriptionId
{
[CmdletBinding()]
param()

$AzureContext = & "Get-AzureRmContext" -ErrorAction Stop
if($AzureContext)
{
if(Get-Member -InputObject $AzureContext.Subscription -Name SubscriptionId)
{
return $AzureContext.Subscription.SubscriptionId
}
else
{
return $AzureContext.Subscription.Id
}
}
}

<#
.DESCRIPTION
Gets the resource manager URL from the logged in AzureRM context.
#>
function Get-AzResourceManagerUrl
{
[CmdletBinding()]
param()

$AzureContext = & "Get-AzureRmContext" -ErrorAction Stop
$AzureContext.Environment.ResourceManagerUrl
}

<#
.DESCRIPTION
Creates a System.Net.Http.HttpClientHandler for the given credentials and sets preauthentication to true.
Expand Down
49 changes: 49 additions & 0 deletions PSSwagger/New-ArmServiceClient.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Microsoft.PowerShell.Core\Set-StrictMode -Version Latest

<#
.DESCRIPTION
Creates Service Client object.

.PARAMETER FullClientTypeName
Client type full name.

.PARAMETER GlobalParameterHashtable
Global parameters to be set on client object.
#>
function New-ServiceClient {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]
$FullClientTypeName,

[Parameter(Mandatory = $false)]
[PSCustomObject]
$GlobalParameterHashtable
)

# Azure Powershell way
[Microsoft.Azure.Commands.Common.Authentication.Abstractions.IAzureContext]$Context = Get-AzureRmContext
if (-not $Context -or -not $Context.Account) {
Write-Error -Message 'Run Login-AzureRmAccount to login.' -ErrorId 'AzureRmContextError'
return
}

$Factory = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.ClientFactory
[System.Type[]]$Types = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.IAzureContext], [string]
$CreateArmClientMethod = [Microsoft.Azure.Commands.Common.Authentication.IClientFactory].GetMethod('CreateArmClient', $Types)
$ClientType = $FullClientTypeName -as [Type]
$ClosedMethod = $CreateArmClientMethod.MakeGenericMethod($ClientType)
$Arguments = $Context, [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureEnvironment+Endpoint]::ResourceManager
$Client = $closedMethod.Invoke($Factory, $Arguments)

if ($GlobalParameterHashtable) {
$GlobalParameterHashtable.GetEnumerator() | ForEach-Object {
if ($_.Value -and (Get-Member -InputObject $Client -Name $_.Key -MemberType Property)) {
$Client."$($_.Key)" = $_.Value
}
}
}

return $Client
}
21 changes: 2 additions & 19 deletions PSSwagger/New-ServiceClient.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ Microsoft.PowerShell.Core\Set-StrictMode -Version Latest
Command should return a custom hostname string.
Overrides the default host in the specification.

.PARAMETER SubscriptionIdCommand
Custom command get SubscriptionId value.

.PARAMETER GlobalParameterHashtable
Global parameters to be set on client object.
#>
Expand Down Expand Up @@ -56,10 +53,6 @@ function New-ServiceClient {
[string]
$HostOverrideCommand,

[Parameter(Mandatory = $false)]
[string]
$SubscriptionIdCommand,

[Parameter(Mandatory = $false)]
[PSCustomObject]
$GlobalParameterHashtable
Expand Down Expand Up @@ -90,18 +83,8 @@ function New-ServiceClient {

if ($GlobalParameterHashtable) {
$GlobalParameterHashtable.GetEnumerator() | ForEach-Object {
if (Get-Member -InputObject $Client -Name $_.Key -MemberType Property) {
if ((-not $_.Value) -and ($_.Key -eq 'SubscriptionId')) {
if($SubscriptionIdCommand) {
$Client.SubscriptionId = Invoke-Command -ScriptBlock [scriptblock]::Create($SubscriptionIdCommand)
}
else {
$Client.SubscriptionId = Get-AzSubscriptionId
}
}
else {
$Client."$($_.Key)" = $_.Value
}
if ($_.Value -and (Get-Member -InputObject $Client -Name $_.Key -MemberType Property)) {
$Client."$($_.Key)" = $_.Value
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions PSSwagger/PSSwagger.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,15 @@ function New-PSSwaggerModule

$CopyFilesMap = [ordered]@{
'GeneratedHelpers.ps1' = 'GeneratedHelpers.ps1'
'Test-CoreRequirements.ps1' = 'Test-CoreRequirements.ps1'
'Test-FullRequirements.ps1' = 'Test-FullRequirements.ps1'
'New-ServiceClient.ps1' = 'New-ServiceClient.ps1'
}

if($UseAzureCsharpGenerator) {
$CopyFilesMap['New-ArmServiceClient.ps1'] = 'New-ServiceClient.ps1'
$CopyFilesMap['Test-FullRequirements.ps1'] = 'Test-FullRequirements.ps1'
$CopyFilesMap['Test-CoreRequirements.ps1'] = 'Test-CoreRequirements.ps1'
}
else {
$CopyFilesMap['New-ServiceClient.ps1'] = 'New-ServiceClient.ps1'
}

if (-not $AssemblyFileName) {
Expand Down
38 changes: 22 additions & 16 deletions PSSwagger/Paths.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -616,24 +616,25 @@ function New-SwaggerPath
}

# Process security section
$SubscriptionIdCommand = ""
$AuthenticationCommand = ""
$AuthenticationCommandArgumentName = ''
$hostOverrideCommand = ''
$AddHttpClientHandler = $false
$securityParametersToAdd = @()
$PowerShellCodeGen = $SwaggerMetaDict['PowerShellCodeGen']
if (($PowerShellCodeGen['ServiceType'] -eq 'azure') -or ($PowerShellCodeGen['ServiceType'] -eq 'azure_stack')) {
$SubscriptionIdCommand = 'Get-AzSubscriptionId'
}
if ($PowerShellCodeGen['CustomAuthCommand']) {
$AuthenticationCommand = $PowerShellCodeGen['CustomAuthCommand']
}
if ($PowerShellCodeGen['HostOverrideCommand']) {
$hostOverrideCommand = $PowerShellCodeGen['HostOverrideCommand']

# CustomAuthCommand and HostOverrideCommand are not required for Arm Services
if (($PowerShellCodeGen['ServiceType'] -ne 'azure') -and ($PowerShellCodeGen['ServiceType'] -eq 'azure_stack')) {
if ($PowerShellCodeGen['CustomAuthCommand']) {
$AuthenticationCommand = $PowerShellCodeGen['CustomAuthCommand']
}
if ($PowerShellCodeGen['HostOverrideCommand']) {
$hostOverrideCommand = $PowerShellCodeGen['HostOverrideCommand']
}
}

# If the auth function hasn't been set by metadata, try to discover it from the security and securityDefinition objects in the spec
if (-not $AuthenticationCommand) {
if (-not $AuthenticationCommand -and -not $UseAzureCsharpGenerator) {
if ($FunctionDetails.ContainsKey('Security')) {
# For now, just take the first security object
if ($FunctionDetails.Security.Count -gt 1) {
Expand Down Expand Up @@ -729,7 +730,7 @@ function New-SwaggerPath
}
}

if (-not $AuthenticationCommand) {
if (-not $AuthenticationCommand -and -not $UseAzureCsharpGenerator) {
# At this point, there was no supported security object or overridden auth function, so assume no auth
$AuthenticationCommand = 'Get-AutoRestCredential'
}
Expand Down Expand Up @@ -973,13 +974,18 @@ function New-SwaggerPath
ParameterGroupsExpressionBlock = $parameterGroupsExpressionBlock
SwaggerDict = $SwaggerDict
SwaggerMetaDict = $SwaggerMetaDict
AddHttpClientHandler = $AddHttpClientHandler
HostOverrideCommand = $hostOverrideCommand
AuthenticationCommand = $AuthenticationCommand
AuthenticationCommandArgumentName = $AuthenticationCommandArgumentName
SubscriptionIdCommand = $SubscriptionIdCommand
FlattenedParametersOnPSCmdlet = $flattenedParametersOnPSCmdlet
}
if($AuthenticationCommand) {
$functionBodyParams['AuthenticationCommand'] = $AuthenticationCommand
$functionBodyParams['AuthenticationCommandArgumentName'] = $AuthenticationCommandArgumentName
}
if($AddHttpClientHandler) {
$functionBodyParams['AddHttpClientHandler'] = $AddHttpClientHandler
}
if($hostOverrideCommand) {
$functionBodyParams['hostOverrideCommand'] = $hostOverrideCommand
}
if($globalParameters) {
$functionBodyParams['GlobalParameters'] = $globalParameters
}
Expand Down
4 changes: 1 addition & 3 deletions PSSwagger/ServiceTypes/azure.PSMeta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"info": {
"x-ps-code-generation-settings": {
"customAuthCommand": "Get-AzServiceCredential"
}
"x-ps-code-generation-settings": {}
}
}
5 changes: 1 addition & 4 deletions PSSwagger/ServiceTypes/azure_stack.PSMeta.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"info": {
"x-ps-code-generation-settings": {
"customAuthCommand": "Get-AzServiceCredential",
"hostOverrideCommand": "Get-AzResourceManagerUrl"
}
"x-ps-code-generation-settings": {}
}
}
6 changes: 1 addition & 5 deletions PSSwagger/SwaggerUtils.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1471,18 +1471,14 @@ function Get-PathFunctionBody
[string]
$HostOverrideCommand,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used anywhere in this file. Should it be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed offline, this variable will be used for non-Azure services in below line.

$body = $executionContext.InvokeCommand.ExpandString($functionBodyStr)


[Parameter(Mandatory=$true)]
[Parameter(Mandatory=$false)]
[string]
$AuthenticationCommand,

[Parameter(Mandatory=$false)]
[string]
$AuthenticationCommandArgumentName,

[Parameter(Mandatory=$false)]
[string]
$SubscriptionIdCommand,

[Parameter(Mandatory=$true)]
[PSCustomObject]
$FlattenedParametersOnPSCmdlet
Expand Down