Skip to content

Commit ddcea73

Browse files
Azure Template Update
1 parent f06bb0d commit ddcea73

File tree

13 files changed

+438
-27
lines changed

13 files changed

+438
-27
lines changed

templates/PSFModule/þnameþ.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
# Script module or binary module file associated with this manifest
3-
ModuleToProcess = 'þnameþ.psm1'
3+
RootModule = 'þnameþ.psm1'
44

55
# Version number of this module.
66
ModuleVersion = '1.0.0'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function %functionname%
2+
{
3+
%parameter%
4+
5+
process
6+
{
7+
$invokeParameters = Get-InternalConnectionData -Method '%method%' -Parameter $PSBoundParameters -FunctionName '%condensedname%'
8+
Invoke-RestMethod @invokeParameters
9+
}
10+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function Connect-þnameþ
2+
{
3+
<#
4+
.SYNOPSIS
5+
Configures the connection to the þnameþ Azure Function.
6+
7+
.DESCRIPTION
8+
Configures the connection to the þnameþ Azure Function.
9+
10+
.PARAMETER Uri
11+
Url to connect to the þnameþ Azure function.
12+
13+
.PARAMETER UnprotectedToken
14+
The unencrypted access token to the þnameþ Azure function. ONLY use this from secure locations or non-sensitive functions!
15+
16+
.PARAMETER ProtectedToken
17+
An encrypted access token to the þnameþ Azure function. Use this to persist an access token in a way only the current user on the current system can access.
18+
19+
.PARAMETER Register
20+
Using this command, the module will remember the connection settings persistently across PowerShell sessions.
21+
CAUTION: When using unencrypted token data (such as specified through the -UnprotectedToken parameter), the authenticating token will be stored in clear-text!
22+
23+
.EXAMPLE
24+
PS C:\> Connect-þnameþ -Uri 'https://demofunctionapp.azurewebsites.net/api/'
25+
26+
Establishes a connection to þnameþ
27+
#>
28+
[CmdletBinding()]
29+
param (
30+
[string]
31+
$Uri,
32+
33+
[string]
34+
$UnprotectedToken,
35+
36+
[System.Management.Automation.PSCredential]
37+
$ProtectedToken,
38+
39+
[switch]
40+
$Register
41+
)
42+
43+
process
44+
{
45+
if (Test-PSFParameterBinding -ParameterName UnprotectedToken)
46+
{
47+
Set-PSFConfig -Module 'þnameþ' -Name 'Client.UnprotectedToken' -Value $UnprotectedToken
48+
if ($Register) { Register-PSFConfig -Module 'þnameþ' -Name 'Client.UnprotectedToken' }
49+
}
50+
if (Test-PSFParameterBinding -ParameterName Url)
51+
{
52+
Set-PSFConfig -Module 'þnameþ' -Name 'Client.Uri' -Value $Uri
53+
if ($Register) { Register-PSFConfig -Module 'þnameþ' -Name 'Client.Uri' }
54+
}
55+
if (Test-PSFParameterBinding -ParameterName ProtectedToken)
56+
{
57+
Set-PSFConfig -Module 'þnameþ' -Name 'Client.ProtectedToken' -Value $ProtectedToken
58+
if ($Register) { Register-PSFConfig -Module 'þnameþ' -Name 'Client.ProtectedToken' }
59+
}
60+
61+
}
62+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Set-PSFConfig -Module 'þnameþ' -Name 'Client.Uri' -Value $null -Initialize -Validation 'string' -Description "Url to connect to the þnameþ Azure function"
2+
Set-PSFConfig -Module 'þnameþ' -Name 'Client.UnprotectedToken' -Value '' -Initialize -Validation 'string' -Description "The unencrypted access token to the þnameþ Azure function. ONLY use this from secure locations or non-sensitive functions!"
3+
Set-PSFConfig -Module 'þnameþ' -Name 'Client.ProtectedToken' -Value $null -Initialize -Validation 'credential' -Description "An encrypted access token to the þnameþ Azure function. Use this to persist an access token in a way only the current user on the current system can access."
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
function Get-InternalConnectionData
2+
{
3+
<#
4+
.SYNOPSIS
5+
Creates parameter hashtables for Invoke-RestMethod calls.
6+
7+
.DESCRIPTION
8+
Creates parameter hashtables for Invoke-RestMethod calls.
9+
This is the main abstraction layer for public functions.
10+
11+
.PARAMETER Method
12+
The Rest Method to use when calling this function.
13+
14+
.PARAMETER Parameters
15+
The PSBoundParameters object. Will be passed online using PowerShell Serialization.
16+
17+
.PARAMETER FunctionName
18+
The name of the Azure Function to call.
19+
This should always be the condensed name of the function.
20+
#>
21+
[CmdletBinding()]
22+
param (
23+
[string]
24+
$Method,
25+
26+
$Parameters,
27+
28+
[string]
29+
$FunctionName
30+
)
31+
32+
process
33+
{
34+
try { $uri = '{0}{1}' -f (Get-PSFConfigValue -FullName 'þnameþ.Client.Uri' -NotNull), $FunctionName }
35+
catch { $PSCmdlet.ThrowTerminatingError($_) }
36+
$header = @{ }
37+
38+
#region Authentication
39+
$unprotectedToken = Get-PSFConfigValue -FullName 'þnameþ.Client.UnprotectedToken'
40+
$protectedToken = Get-PSFConfigValue -FullName 'þnameþ.Client.ProtectedToken'
41+
42+
$authenticationDone = $false
43+
if ($protectedToken -and -not $authenticationDone)
44+
{
45+
$uri += '?code={0}' -f $protectedToken.GetNetworkCredential().Password
46+
$authenticationDone = $true
47+
}
48+
if ($unprotectedToken -and -not $authenticationDone)
49+
{
50+
$uri += '?code={0}' -f $unprotectedToken
51+
$authenticationDone = $true
52+
}
53+
if (-not $authenticationDone)
54+
{
55+
throw "No Authentication configured!"
56+
}
57+
#endregion Authentication
58+
59+
60+
@{
61+
Method = $Method
62+
Uri = $uri
63+
Headers = $header
64+
Body = @{
65+
__SerializedParameters = ($Parameters | ConvertTo-PSFHashtable | ConvertTo-PSFClixml)
66+
__PSSerialize = $true
67+
}
68+
}
69+
}
70+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
$script:ModuleRoot = $PSScriptRoot
2+
3+
foreach ($file in (Get-ChildItem -Path "$script:ModuleRoot\internal\configurations" -Recurse -Filter '*.ps1'))
4+
{
5+
. $file.FullName
6+
}
7+
foreach ($file in (Get-ChildItem -Path "$script:ModuleRoot\internal\functions" -Recurse -Filter '*.ps1'))
8+
{
9+
. $file.FullName
10+
}
11+
foreach ($file in (Get-ChildItem -Path "$script:ModuleRoot\functions" -Recurse -Filter '*.ps1'))
12+
{
13+
. $file.FullName
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function Get-Example
2+
{
3+
<#
4+
.NOTES
5+
This file will be used to override the auto-generated CLIENT MODULE function implementation.
6+
7+
Using the vsts-createFunctionClientModule.ps1 task script, you can autogenerate a client module.
8+
That module can be used to connect to the published Azure Function Module.
9+
However sometimes you may want to override the default client function for a given command,
10+
in order to better customize the way it behaves.
11+
12+
Creating a ps1 file with the name of the specific function in this folder will use this file,
13+
rather than creating a default copy.
14+
15+
NOTE:
16+
There will be no further automatic change detection!
17+
If you later update the Azure Function, you need to manually update the client function as well.
18+
#>
19+
[CmdletBinding()]
20+
Param (
21+
22+
)
23+
24+
begin
25+
{
26+
27+
}
28+
process
29+
{
30+
31+
}
32+
end
33+
{
34+
35+
}
36+
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
@{
2-
# Setting this to $true will cause the azure function to use Json serialization, rather than PowerShell Serialization
3-
'NoSerialize' = $true
2+
# Override the rest methods used for the API endpoint
3+
# RestMethods = 'delete'
4+
5+
# Override inclusion into client module
6+
# NoClientFunction
47
}

templates/PSFProject/azFunctionResources/profileFunctions/Convert-AzureFunctionParameter.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
Converts the $request object into a regular hashtable.
1818
#>
19+
[OutputType([System.Collections.Hashtable])]
1920
[CmdletBinding()]
2021
param (
2122
$Request

templates/PSFProject/azFunctionResources/run.ps1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
)
66

77
$parameters = Convert-AzureFunctionParameter -Request $Request
8+
if ($parameters.__PSSerialize)
9+
{
10+
$usePSSerialize = $true
11+
$parameters.Remove('__PSSerialize')
12+
}
13+
else { $usePSSerialize = $false }
814

915
try { $data = %functionname% @parameters }
1016
catch
@@ -13,7 +19,4 @@ catch
1319
return
1420
}
1521

16-
# This is automatically updated by the build script if there is a custom override for function
17-
$serialize = $true
18-
19-
Write-AzureFunctionOutput -Value $data -Serialize:$serialize
22+
Write-AzureFunctionOutput -Value $data -Serialize:$usePSSerialize

0 commit comments

Comments
 (0)