Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update main function template to contain EventGrid
  • Loading branch information
nyanhp committed Feb 13, 2025
commit 48be2a0e9a6e7cf7531cee7bc78f4b7532dc50b8
57 changes: 38 additions & 19 deletions templates/AzureFunction/build/build.config.psd1
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
@{
TimerTrigger = @{
# Default Schedule for timed executions
Schedule = '0 5 * * * *'
TimerTrigger = @{
# Default Schedule for timed executions
Schedule = '0 5 * * * *'

# Different Schedules for specific timed endpoints
ScheduleOverrides = @{
# 'Update-Whatever' = '0 5 12 * * *'
}
}
# Different Schedules for specific timed endpoints
ScheduleOverrides = @{
# 'Update-Whatever' = '0 5 12 * * *'
}
}

HttpTrigger = @{
<#
HttpTrigger = @{
<#
AuthLevels:
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

anonymous: No Token needed (combine with Identity Provider for Entra ID auth without also needing a token)
function: (default) Require a function-endpoint-specific token with the request
admin: Require a Function-App-global admin token (master key) for the request
#>
AuthLevel = 'function'
AuthLevelOverrides = @{
# 'Set-Foo' = 'anonymous'
}
Methods = @('get', 'post')
MethodOverrides = @{
# 'Set-Foo' = 'delete'
}
}
AuthLevel = 'function'
AuthLevelOverrides = @{
# 'Set-Foo' = 'anonymous'
}
Methods = @('get', 'post')
MethodOverrides = @{
# 'Set-Foo' = 'delete'
}
}

EventGridTrigger = @{
<#
AuthLevels:
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

anonymous: No Token needed (combine with Identity Provider for Entra ID auth without also needing a token)
function: (default) Require a function-endpoint-specific token with the request
admin: Require a Function-App-global admin token (master key) for the request
#>
AuthLevel = 'function'
AuthLevelOverrides = @{
# 'Set-Foo' = 'anonymous'
}
Methods = @('get', 'post')
MethodOverrides = @{
# 'Set-Foo' = 'delete'
}
}
}
18 changes: 18 additions & 0 deletions templates/AzureFunction/build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ foreach ($command in Get-ChildItem -Path "$workingDirectory\þnameþ\functions\h
$httpConfig -replace '%AUTHLEVEL%', $authLevel -replace '%METHODS%', ($methods -join '", "') | Set-Content -Path "$($endpointFolder.FullName)\function.json"
}


# Generate Event Grid Trigger
$eventGridCode = Get-Content -Path "$PSScriptRoot\functionEventGrid\run.ps1" | Join-String -Separator "`n"
$eventGridConfig = Get-Content -Path "$PSScriptRoot\functionEventGrid\function.json" | Join-String -Separator "`n"
foreach ($command in Get-ChildItem -Path "$workingDirectory\þnameþ\functions\eventGridTrigger" -Recurse -File -Filter *.ps1) {
$authLevel = $config.EventGridTrigger.AuthLevel
if ($config.EventGridTrigger.AuthLevelOverrides.$($command.BaseName)) {
$authLevel = $config.EventGridTrigger.AuthLevelOverrides.$($command.BaseName)
}
$methods = $config.EventGridTrigger.Methods
if ($config.EventGridTrigger.MethodOverrides.$($command.BaseName)) {
$methods = $config.EventGridTrFigger.MethodOverrides.$($command.BaseName)
}
$endpointFolder = New-Item -Path $buildFolder.FullName -Name $command.BaseName -ItemType Directory
$eventGridCode -replace '%COMMAND%',$command.BaseName | Set-Content -Path "$($endpointFolder.FullName)\run.ps1"
$eventGridConfig -replace '%AUTHLEVEL%', $authLevel -replace '%METHODS%', ($methods -join '", "') | Set-Content -Path "$($endpointFolder.FullName)\function.json"
}

# Generate Timer Trigger
$timerCode = Get-Content -Path "$PSScriptRoot\functionTimer\run.ps1" | Join-String -Separator "`n"
$timerConfig = Get-Content -Path "$PSScriptRoot\functionTimer\function.json" | Join-String -Separator "`n"
Expand Down
19 changes: 19 additions & 0 deletions templates/AzureFunction/build/functionEventGrid/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"bindings": [
{
"authLevel": "%AUTHLEVEL%",
"type": "eventGridTrigger",
"direction": "in",
"name": "EventGridEvent",
"methods": [
"%METHODS%"
]
},
{
"type": "http",
"direction": "out",
"name": "Response"
}
],
"disabled": false
}
24 changes: 24 additions & 0 deletions templates/AzureFunction/build/functionEventGrid/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
param (
$EventGridEvent,

$TriggerMetadata
)


Write-Host "Trigger: %COMMAND% has been invoked"

$parameters = Get-RestParameter -Request $Request -Command %COMMAND%

try {
$results = %COMMAND% @parameters -ErrorAction Stop
}
catch {
$_ | Out-String | ForEach-Object {
foreach ($line in ($_ -split "`n")) {
Write-Warning $line
}
}
Write-FunctionResult -Status InternalServerError -Body "$_"
return
}
Write-FunctionResult -Status OK -Body $results