diff --git a/PSSwagger/AssemblyGenerationHelpers.Resources.psd1 b/PSSwagger/AssemblyGenerationHelpers.Resources.psd1 index 9b00b0c..4ff8f77 100644 --- a/PSSwagger/AssemblyGenerationHelpers.Resources.psd1 +++ b/PSSwagger/AssemblyGenerationHelpers.Resources.psd1 @@ -1,11 +1,3 @@ -######################################################################################### -# -# Copyright (c) Microsoft Corporation. All rights reserved. -# -# Licensed under the MIT license. -# -######################################################################################### - ConvertFrom-StringData @' ###PSLOC diff --git a/PSSwagger/AssemblyGenerationHelpers.ps1 b/PSSwagger/AssemblyGenerationHelpers.ps1 index 5bbbc01..33dcfb3 100644 --- a/PSSwagger/AssemblyGenerationHelpers.ps1 +++ b/PSSwagger/AssemblyGenerationHelpers.ps1 @@ -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 diff --git a/PSSwagger/GeneratedHelpers.ps1 b/PSSwagger/GeneratedHelpers.ps1 index 27cc989..ca5fc69 100644 --- a/PSSwagger/GeneratedHelpers.ps1 +++ b/PSSwagger/GeneratedHelpers.ps1 @@ -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. diff --git a/PSSwagger/New-ArmServiceClient.ps1 b/PSSwagger/New-ArmServiceClient.ps1 new file mode 100644 index 0000000..d21496a --- /dev/null +++ b/PSSwagger/New-ArmServiceClient.ps1 @@ -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 +} \ No newline at end of file diff --git a/PSSwagger/New-ServiceClient.ps1 b/PSSwagger/New-ServiceClient.ps1 index 1b84c90..276e214 100644 --- a/PSSwagger/New-ServiceClient.ps1 +++ b/PSSwagger/New-ServiceClient.ps1 @@ -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. #> @@ -56,10 +53,6 @@ function New-ServiceClient { [string] $HostOverrideCommand, - [Parameter(Mandatory = $false)] - [string] - $SubscriptionIdCommand, - [Parameter(Mandatory = $false)] [PSCustomObject] $GlobalParameterHashtable @@ -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 } } } diff --git a/PSSwagger/PSSwagger.psm1 b/PSSwagger/PSSwagger.psm1 index d0163e4..c4308d4 100644 --- a/PSSwagger/PSSwagger.psm1 +++ b/PSSwagger/PSSwagger.psm1 @@ -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) { diff --git a/PSSwagger/Paths.psm1 b/PSSwagger/Paths.psm1 index a771d0e..208a82d 100644 --- a/PSSwagger/Paths.psm1 +++ b/PSSwagger/Paths.psm1 @@ -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) { @@ -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' } @@ -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 } diff --git a/PSSwagger/ServiceTypes/azure.PSMeta.json b/PSSwagger/ServiceTypes/azure.PSMeta.json index c3de351..ffc184b 100644 --- a/PSSwagger/ServiceTypes/azure.PSMeta.json +++ b/PSSwagger/ServiceTypes/azure.PSMeta.json @@ -1,7 +1,5 @@ { "info": { - "x-ps-code-generation-settings": { - "customAuthCommand": "Get-AzServiceCredential" - } + "x-ps-code-generation-settings": {} } } \ No newline at end of file diff --git a/PSSwagger/ServiceTypes/azure_stack.PSMeta.json b/PSSwagger/ServiceTypes/azure_stack.PSMeta.json index 8bcd7a5..ffc184b 100644 --- a/PSSwagger/ServiceTypes/azure_stack.PSMeta.json +++ b/PSSwagger/ServiceTypes/azure_stack.PSMeta.json @@ -1,8 +1,5 @@ { "info": { - "x-ps-code-generation-settings": { - "customAuthCommand": "Get-AzServiceCredential", - "hostOverrideCommand": "Get-AzResourceManagerUrl" - } + "x-ps-code-generation-settings": {} } } \ No newline at end of file diff --git a/PSSwagger/SwaggerUtils.psm1 b/PSSwagger/SwaggerUtils.psm1 index 58f7971..972e810 100644 --- a/PSSwagger/SwaggerUtils.psm1 +++ b/PSSwagger/SwaggerUtils.psm1 @@ -1471,7 +1471,7 @@ function Get-PathFunctionBody [string] $HostOverrideCommand, - [Parameter(Mandatory=$true)] + [Parameter(Mandatory=$false)] [string] $AuthenticationCommand, @@ -1479,10 +1479,6 @@ function Get-PathFunctionBody [string] $AuthenticationCommandArgumentName, - [Parameter(Mandatory=$false)] - [string] - $SubscriptionIdCommand, - [Parameter(Mandatory=$true)] [PSCustomObject] $FlattenedParametersOnPSCmdlet