Skip to content

Commit 3922a0e

Browse files
Merge pull request #99 from PowershellFrameworkCollective/development
Upgraded azure function user experience
2 parents 753732e + aa92235 commit 3922a0e

File tree

12 files changed

+214
-159
lines changed

12 files changed

+214
-159
lines changed

PSModuleDevelopment/PSModuleDevelopment.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
RootModule = 'PSModuleDevelopment.psm1'
55

66
# Version number of this module.
7-
ModuleVersion = '2.2.6.67'
7+
ModuleVersion = '2.2.6.68'
88

99
# ID used to uniquely identify this module
1010
GUID = '37dd5fce-e7b5-4d57-ac37-832055ce49d6'

PSModuleDevelopment/changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
## 2.2.6.68 (May 3rd, 2019)
3+
- Upd: Template PSFProject - Improved Azure Functions creation experience
4+
25
## 2.2.6.67 (May 2nd, 2019)
36
- Upd: Invoke-PSMDTemplate adding tab completion
47
- Fix: Invoke-PSMDTemplate fails to create templates
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "function",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "Request",
8+
"methods": [
9+
"delete"
10+
]
11+
},
12+
{
13+
"type": "http",
14+
"direction": "out",
15+
"name": "Response"
16+
}
17+
],
18+
"disabled": true
19+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@{
2+
# Setting this to $true will cause the azure function to use Json serialization, rather than PowerShell Serialization
3+
'NoSerialize' = $true
4+
}
Lines changed: 2 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,8 @@
11
<#
2-
This is the globl profile file for the Azure Function.
2+
This is the globl profile file for the Azure Function App.
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.
66
#>
77

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-
}
8+
Set-PSFConfig -FullName 'AutogeneratedAzureFunction.Function.StatusCode' -Value ([System.Net.HttpStatusCode]::OK)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function Convert-AzureFunctionParameter
2+
{
3+
<#
4+
.SYNOPSIS
5+
Extracts the parameters passed into the rest method.
6+
7+
.DESCRIPTION
8+
Extracts the parameters passed into the rest method of an Azure Function.
9+
Returns a hashtable, similar to what would be found on a $PSBoundParameters variable.
10+
11+
.PARAMETER Request
12+
The request to process
13+
14+
.EXAMPLE
15+
PS C:\> Convert-AzureFunctionParameter -Request $request
16+
17+
Converts the $request object into a regular hashtable.
18+
#>
19+
[CmdletBinding()]
20+
param (
21+
$Request
22+
)
23+
24+
$parameters = @{ }
25+
26+
foreach ($key in $Request.Query.Keys)
27+
{
28+
# Do NOT include the authentication key
29+
if ($key -eq 'code') { continue }
30+
$parameters[$key] = $Request.Query.$key
31+
}
32+
foreach ($key in $Request.Body.Keys)
33+
{
34+
$parameters[$key] = $Request.Body.$key
35+
}
36+
37+
if (($parameters.Count -eq 1) -and ($parameters.__SerializedParameters))
38+
{
39+
return $parameters.__SerializedParameters | ConvertFrom-PSFClixml
40+
}
41+
42+
$parameters
43+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function Set-AzureFunctionStatus
2+
{
3+
<#
4+
.SYNOPSIS
5+
Sets the return status of the function.
6+
7+
.DESCRIPTION
8+
Sets the return status of the function.
9+
By default, the status is "OK"
10+
11+
.PARAMETER Status
12+
Set the HTTP status for the return from Azure Functions.
13+
Any status other than OK will cause a terminating error if run outside of Azure Functions.
14+
15+
.EXAMPLE
16+
PS C:\> Set-AzureFunctionStatus -Status BadRequest
17+
18+
Updates the status to say "BadRequest"
19+
#>
20+
[CmdletBinding()]
21+
param (
22+
[Parameter(Mandatory = $true)]
23+
[System.Net.HttpStatusCode]
24+
$Status
25+
)
26+
27+
Set-PSFConfig -FullName 'AutogeneratedAzureFunction.Function.StatusCode' -Value $Status
28+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function Write-AzureFunctionOutput
2+
{
3+
<#
4+
.SYNOPSIS
5+
Write output equally well from Azure Functions or locally.
6+
7+
.DESCRIPTION
8+
Write output equally well from Azure Functions or locally.
9+
When calling this command, call return straight after it.
10+
Use Write-AzureFunctionStatus first if an error should be returned, then specify an error text here.
11+
12+
.PARAMETER Value
13+
The value data to return.
14+
Either an error message
15+
16+
.PARAMETER Serialize
17+
Return the output object as compressed clixml string.
18+
You can use ConvertFrom-PSFClixml to restore the object on the recipient-side.
19+
20+
.EXAMPLE
21+
PS C:\> Write-AzureFunctionOutput -Value $result
22+
23+
Writes the content of $result as output.
24+
25+
.EXAMPLE
26+
PS C:\> Write-AzureFunctionOutput -Value $result -Serialize
27+
28+
Writes the content of $result as output.
29+
If called from Azure Functions, it will convert the output as compressed clixml string.
30+
31+
#>
32+
[CmdletBinding()]
33+
param (
34+
[Parameter(Mandatory = $true)]
35+
$Value,
36+
37+
[switch]
38+
$Serialize,
39+
40+
[System.Net.HttpStatusCode]
41+
$Status
42+
)
43+
44+
if ($PSBoundParameters.ContainsKey('Status'))
45+
{
46+
Set-AzureFunctionStatus -Status $Status
47+
}
48+
49+
if ($Serialize)
50+
{
51+
$Value = $Value | ConvertTo-PSFClixml
52+
}
53+
54+
Push-OutputBinding -Name Response -Value (
55+
[HttpResponseContext]@{
56+
StatusCode = (Get-PSFConfigValue -FullName 'AutogeneratedAzureFunction.Function.StatusCode' -Fallback ([System.Net.HttpStatusCode]::OK))
57+
Body = $Value
58+
}
59+
)
60+
}

templates/PSFProject/azFunctionResources/root.psm1

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
param (
2+
$Request,
3+
4+
$TriggerMetadata
5+
)
6+
7+
$parameters = Convert-AzureFunctionParameter -Request $Request
8+
9+
try { $data = %functionname% @parameters }
10+
catch
11+
{
12+
Write-AzureFunctionOutput -Value "Failed to execute: $_" -Status InternalServerError
13+
return
14+
}
15+
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

0 commit comments

Comments
 (0)