Skip to content

Commit 2c1949f

Browse files
Azure Function Integration Template
1 parent a4127d8 commit 2c1949f

File tree

4 files changed

+199
-2
lines changed

4 files changed

+199
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
param (
2+
$Path
3+
)
4+
5+
New-PSMDTemplate -FilePath "$PSScriptRoot\þnameþ.ps1" -TemplateName AzureFunctionRest -OutPath $Path -Description "Template for an Azure Function with Rest Trigger" -Author "Friedrich Weinmann" -Tags 'function', 'file', 'azure'
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function þnameþ
2+
{
3+
<#
4+
.SYNOPSIS
5+
Insert Synopsis
6+
7+
.DESCRIPTION
8+
Insert Description
9+
10+
.PARAMETER Request
11+
Contains the Request Information the Azure Function was called with.
12+
Effectively the parameters.
13+
14+
.PARAMETER TriggerMetadata
15+
Azure Functions Specific Vodoo. Don't touch unless you know what you do.
16+
17+
.EXAMPLE
18+
PS C:\> Invoke-RestMethod '<insert function uri>'
19+
20+
Invokes the Azure function without any parameter.
21+
#>
22+
[CmdletBinding()]
23+
param (
24+
$Request,
25+
26+
$TriggerMetadata
27+
)
28+
29+
begin
30+
{
31+
#region Convert input parameters from Azure Functions
32+
if ($env:Functions_EXTENSION_VERSION)
33+
{
34+
$PSBoundParameters.Clear()
35+
$PSBoundParameters = Convert-AzureFunctionParameter -Request $Request
36+
}
37+
#endregion Convert input parameters from Azure Functions
38+
}
39+
process
40+
{
41+
if ($failed)
42+
{
43+
Write-AzureFunctionOutput -Value 'Failed to execute successfully!' -Status InternalServerError
44+
return
45+
}
46+
}
47+
end
48+
{
49+
Write-AzureFunctionOutput -Value $results -Serialize
50+
return
51+
}
52+
}

templates/PSFProject/PSMDTemplate.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
TemplateName = 'PSFProject'
3-
Version = "1.1.1.0"
3+
Version = "1.2.1.0"
44
AutoIncrementVersion = $true
55
Tags = 'module','psframework'
66
Author = 'Friedrich Weinmann'

templates/PSFProject/azFunctionResources/profile.ps1

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,144 @@ This is the globl profile file for the Azure Function.
33
This file will have been executed first, before any function runs.
44
Use this to create a common execution environment,
55
but keep in mind that the profile execution time is added to the function startup time for ALL functions.
6-
#>
6+
#>
7+
8+
$global:functionStatusCode = [System.Net.HttpStatusCode]::OK
9+
function Convert-AzureFunctionParameter
10+
{
11+
<#
12+
.SYNOPSIS
13+
Extracts the parameters passed into the rest method.
14+
15+
.DESCRIPTION
16+
Extracts the parameters passed into the rest method of an Azure Function.
17+
Returns a hashtable, similar to what would be found on a $PSBoundParameters variable.
18+
19+
.PARAMETER Request
20+
The request to process
21+
22+
.EXAMPLE
23+
PS C:\> Convert-AzureFunctionParameter -Request $request
24+
25+
Converts the $request object into a regular hashtable.
26+
#>
27+
[CmdletBinding()]
28+
param (
29+
$Request
30+
)
31+
32+
$parameters = @{ }
33+
34+
foreach ($key in $Request.Query.Keys)
35+
{
36+
# Do NOT include the authentication key
37+
if ($key -eq 'code') { continue }
38+
$parameters[$key] = $Request.Query.$key
39+
}
40+
foreach ($key in $Request.Body.Keys)
41+
{
42+
$parameters[$key] = $Request.Body.$key
43+
}
44+
45+
$parameters
46+
}
47+
48+
function Set-AzureFunctionStatus
49+
{
50+
<#
51+
.SYNOPSIS
52+
Sets the return status of the function.
53+
54+
.DESCRIPTION
55+
Sets the return status of the function.
56+
By default, the status is "OK"
57+
58+
.PARAMETER Status
59+
Set the HTTP status for the return from Azure Functions.
60+
Any status other than OK will cause a terminating error if run outside of Azure Functions.
61+
62+
.EXAMPLE
63+
PS C:\> Set-AzureFunctionStatus -Status BadRequest
64+
65+
Updates the status to say "BadRequest"
66+
#>
67+
[CmdletBinding()]
68+
param (
69+
[Parameter(Mandatory = $true)]
70+
[System.Net.HttpStatusCode]
71+
$Status
72+
)
73+
74+
$global:functionStatusCode = $Status
75+
}
76+
77+
function Write-AzureFunctionOutput
78+
{
79+
<#
80+
.SYNOPSIS
81+
Write output equally well from Azure Functions or locally.
82+
83+
.DESCRIPTION
84+
Write output equally well from Azure Functions or locally.
85+
When calling this command, call return straight after it.
86+
Use Write-AzureFunctionStatus first if an error should be returned, then specify an error text here.
87+
88+
.PARAMETER Value
89+
The value data to return.
90+
Either an error message
91+
92+
.PARAMETER Serialize
93+
Return the output object as compressed clixml string.
94+
You can use ConvertFrom-PSFClixml to restore the object on the recipient-side.
95+
96+
.EXAMPLE
97+
PS C:\> Write-AzureFunctionOutput -Value $result
98+
99+
Writes the content of $result as output.
100+
101+
.EXAMPLE
102+
PS C:\> Write-AzureFunctionOutput -Value $result -Serialize
103+
104+
Writes the content of $result as output.
105+
If called from Azure Functions, it will convert the output as compressed clixml string.
106+
107+
#>
108+
[CmdletBinding()]
109+
param (
110+
[Parameter(Mandatory = $true)]
111+
$Value,
112+
113+
[switch]
114+
$Serialize,
115+
116+
[System.Net.HttpStatusCode]
117+
$Status
118+
)
119+
120+
if ($PSBoundParameters.ContainsKey('Status'))
121+
{
122+
Set-AzureFunctionStatus -Status $Status
123+
}
124+
125+
# If not in function, just return value
126+
if (-not $env:Functions_EXTENSION_VERSION)
127+
{
128+
if ($global:functionStatusCode -ne [System.Net.HttpStatusCode]::OK)
129+
{
130+
throw $Value
131+
}
132+
return $Value
133+
}
134+
135+
if ($Serialize)
136+
{
137+
$Value = $Value | ConvertTo-PSFClixml
138+
}
139+
140+
Push-OutputBinding -Name Response -Value (
141+
[HttpResponseContext]@{
142+
StatusCode = $global:functionStatusCode
143+
Body = $Value
144+
}
145+
)
146+
}

0 commit comments

Comments
 (0)