Skip to content

Commit e9d7f77

Browse files
Merge pull request #211 from nyanhp/feature/event_grid_trigger
Add template for Event Grid trigger
2 parents f4d6915 + 48be2a0 commit e9d7f77

File tree

8 files changed

+151
-19
lines changed

8 files changed

+151
-19
lines changed
Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,49 @@
11
@{
2-
TimerTrigger = @{
3-
# Default Schedule for timed executions
4-
Schedule = '0 5 * * * *'
2+
TimerTrigger = @{
3+
# Default Schedule for timed executions
4+
Schedule = '0 5 * * * *'
55

6-
# Different Schedules for specific timed endpoints
7-
ScheduleOverrides = @{
8-
# 'Update-Whatever' = '0 5 12 * * *'
9-
}
10-
}
6+
# Different Schedules for specific timed endpoints
7+
ScheduleOverrides = @{
8+
# 'Update-Whatever' = '0 5 12 * * *'
9+
}
10+
}
1111

12-
HttpTrigger = @{
13-
<#
12+
HttpTrigger = @{
13+
<#
1414
AuthLevels:
1515
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cfunctionsv2&pivots=programming-language-csharp#http-auth
1616
1717
anonymous: No Token needed (combine with Identity Provider for Entra ID auth without also needing a token)
1818
function: (default) Require a function-endpoint-specific token with the request
1919
admin: Require a Function-App-global admin token (master key) for the request
2020
#>
21-
AuthLevel = 'function'
22-
AuthLevelOverrides = @{
23-
# 'Set-Foo' = 'anonymous'
24-
}
25-
Methods = @('get', 'post')
26-
MethodOverrides = @{
27-
# 'Set-Foo' = 'delete'
28-
}
29-
}
21+
AuthLevel = 'function'
22+
AuthLevelOverrides = @{
23+
# 'Set-Foo' = 'anonymous'
24+
}
25+
Methods = @('get', 'post')
26+
MethodOverrides = @{
27+
# 'Set-Foo' = 'delete'
28+
}
29+
}
30+
31+
EventGridTrigger = @{
32+
<#
33+
AuthLevels:
34+
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cfunctionsv2&pivots=programming-language-csharp#http-auth
35+
36+
anonymous: No Token needed (combine with Identity Provider for Entra ID auth without also needing a token)
37+
function: (default) Require a function-endpoint-specific token with the request
38+
admin: Require a Function-App-global admin token (master key) for the request
39+
#>
40+
AuthLevel = 'function'
41+
AuthLevelOverrides = @{
42+
# 'Set-Foo' = 'anonymous'
43+
}
44+
Methods = @('get', 'post')
45+
MethodOverrides = @{
46+
# 'Set-Foo' = 'delete'
47+
}
48+
}
3049
}

templates/AzureFunction/build/build.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ foreach ($command in Get-ChildItem -Path "$workingDirectory\þnameþ\functions\h
4444
$httpConfig -replace '%AUTHLEVEL%', $authLevel -replace '%METHODS%', ($methods -join '", "') | Set-Content -Path "$($endpointFolder.FullName)\function.json"
4545
}
4646

47+
48+
# Generate Event Grid Trigger
49+
$eventGridCode = Get-Content -Path "$PSScriptRoot\functionEventGrid\run.ps1" | Join-String -Separator "`n"
50+
$eventGridConfig = Get-Content -Path "$PSScriptRoot\functionEventGrid\function.json" | Join-String -Separator "`n"
51+
foreach ($command in Get-ChildItem -Path "$workingDirectory\þnameþ\functions\eventGridTrigger" -Recurse -File -Filter *.ps1) {
52+
$authLevel = $config.EventGridTrigger.AuthLevel
53+
if ($config.EventGridTrigger.AuthLevelOverrides.$($command.BaseName)) {
54+
$authLevel = $config.EventGridTrigger.AuthLevelOverrides.$($command.BaseName)
55+
}
56+
$methods = $config.EventGridTrigger.Methods
57+
if ($config.EventGridTrigger.MethodOverrides.$($command.BaseName)) {
58+
$methods = $config.EventGridTrFigger.MethodOverrides.$($command.BaseName)
59+
}
60+
$endpointFolder = New-Item -Path $buildFolder.FullName -Name $command.BaseName -ItemType Directory
61+
$eventGridCode -replace '%COMMAND%',$command.BaseName | Set-Content -Path "$($endpointFolder.FullName)\run.ps1"
62+
$eventGridConfig -replace '%AUTHLEVEL%', $authLevel -replace '%METHODS%', ($methods -join '", "') | Set-Content -Path "$($endpointFolder.FullName)\function.json"
63+
}
64+
4765
# Generate Timer Trigger
4866
$timerCode = Get-Content -Path "$PSScriptRoot\functionTimer\run.ps1" | Join-String -Separator "`n"
4967
$timerConfig = Get-Content -Path "$PSScriptRoot\functionTimer\function.json" | Join-String -Separator "`n"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "%AUTHLEVEL%",
5+
"type": "eventGridTrigger",
6+
"direction": "in",
7+
"name": "EventGridEvent",
8+
"methods": [
9+
"%METHODS%"
10+
]
11+
},
12+
{
13+
"type": "http",
14+
"direction": "out",
15+
"name": "Response"
16+
}
17+
],
18+
"disabled": false
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
param (
2+
$EventGridEvent,
3+
4+
$TriggerMetadata
5+
)
6+
7+
8+
Write-Host "Trigger: %COMMAND% has been invoked"
9+
10+
$parameters = Get-RestParameter -Request $Request -Command %COMMAND%
11+
12+
try {
13+
$results = %COMMAND% @parameters -ErrorAction Stop
14+
}
15+
catch {
16+
$_ | Out-String | ForEach-Object {
17+
foreach ($line in ($_ -split "`n")) {
18+
Write-Warning $line
19+
}
20+
}
21+
Write-FunctionResult -Status InternalServerError -Body "$_"
22+
return
23+
}
24+
Write-FunctionResult -Status OK -Body $results
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
param (
2+
$Path
3+
)
4+
New-PSMDTemplate -ReferencePath "$PSScriptRoot" -OutPath $Path
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@{
2+
TemplateName = 'AzureFunctionEventGrid'
3+
Version = "1.0.0.0"
4+
AutoIncrementVersion = $true
5+
Tags = 'azure', 'function', 'eventgrid'
6+
Author = 'Jan-Hendrik Peters'
7+
Description = 'Event Grid trigger endpoint for the basic Azure Function Template'
8+
Exclusions = @("PSMDInvoke.ps1", ".PSMDDependency") # Contains list of files - relative path to root - to ignore when building the template
9+
Scripts = @{ }
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"bindings": [
3+
{
4+
"type": "eventGridTrigger",
5+
"name": "EventGridEvent",
6+
"direction": "in"
7+
},
8+
{
9+
"type": "http",
10+
"direction": "out",
11+
"name": "Response"
12+
}
13+
],
14+
"disabled": false
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
param (
2+
$EventGridEvent,
3+
4+
$TriggerMetadata
5+
)
6+
7+
Write-Host "Trigger: %COMMAND% has been invoked"
8+
9+
$parameters = Get-RestParameter -Request $EventGridEvent -Command %COMMAND%
10+
11+
try {
12+
$results = %COMMAND% @parameters -ErrorAction Stop
13+
}
14+
catch {
15+
$_ | Out-String | ForEach-Object {
16+
foreach ($line in ($_ -split "`n")) {
17+
Write-Warning $line
18+
}
19+
}
20+
Write-FunctionResult -Status InternalServerError -Body "$_"
21+
return
22+
}
23+
Write-FunctionResult -Status OK -Body $results

0 commit comments

Comments
 (0)