Last active
April 24, 2019 09:32
-
-
Save olandese/b534226d2a08cf1e64d909c72be399a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
#.SYNOPSIS | |
# Creates Event Grid Subscription. | |
#.DESCRIPTION | |
# Creates an Event Grid Subscription (Topic type: Azure Subscription) to a Function App V2 | |
#.OUTPUTS | |
# Progress messages | |
# | |
Param( | |
[string]$ResourceGroupName, | |
[string]$FunctionAppName, | |
[string]$FunctionName, | |
[string]$EventGridSubscriptionName, | |
[string]$FunctionUrl="azurewebsites.net" | |
) | |
Function Get-AppServicePublishingCredentials($resourceGroupName, $functionAppName) | |
{ | |
$resourceType = "Microsoft.Web/sites/config" | |
$resourceName = "$functionAppName/publishingcredentials" | |
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force | |
return $publishingCredentials | |
} | |
function Get-EncodedCredentials($resourceGroupName, $functionAppName) | |
{ | |
$publishingCredentials = Get-AppServicePublishingCredentials $resourceGroupName $functionAppName | |
$pair = "$($publishingCredentials.Properties.PublishingUserName):$($publishingCredentials.Properties.PublishingPassword)" | |
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair)) | |
return $encodedCreds | |
} | |
function Get-MasterAPIKey($encodedCreds, $functionAppName) | |
{ | |
$result = Invoke-RestMethod -Uri "https://$functionAppName.scm.$FunctionUrl/api/functions/admin/masterkey" -Headers @{Authorization=("Basic {0}" -f $encodedCreds)} | |
return $result.masterKey | |
} | |
function Get-EventGridExtensionSystemKey($functionAppName, $masterKey) | |
{ | |
$result = Invoke-RestMethod ("https://$functionAppName.$FunctionUrl/admin/host/systemkeys/eventgrid_extension?code=" + $masterKey) | |
return $result.value | |
} | |
function Create-EventGridSubscription($functionAppName, $functionName, $systemKey, $eventGridSubscriptionName) | |
{ | |
$functionEndpointURL = "https://" + $functionAppName + ".$FunctionUrl/runtime/webhooks/eventgrid?functionName=" + $functionName + "&code=" + $systemkey +"" | |
$subscriptionID=(Get-AzureRmContext).Subscription.Id | |
New-AzureRmDeployment ` | |
-Name egdeployment ` | |
-Location westeurope ` | |
-TemplateUri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-event-grid-resource-events-to-webhook/azuredeploy.json ` | |
-eventSubName $eventGridSubscriptionName ` | |
-endpoint $functionEndpointURL | |
} | |
# Get the Encoded Publish Credentials, so we can call Kudu API to get master key | |
$encodedCreds = Get-EncodedCredentials $ResourceGroupName $FunctionAppName | |
Write-Output "This is the encodedCreds: $encodedCreds" | |
# Get master key to call Event Grid Extension API | |
$masterKey = Get-MasterAPIKey $encodedCreds $FunctionAppName | |
Write-Output "This is the masterKey: $masterkey" | |
# Get Event Grid SystemKey, so we can create the Event Grid Webhook | |
$systemKey = Get-EventGridExtensionSystemKey $FunctionAppName $masterkey | |
Write-Output "This is the SystemKey: $systemKey" | |
Write-Output "Creating the Event Grid Subscription:" | |
Create-EventGridSubscription $FunctionAppName $FunctionName $systemKey $EventGridSubscriptionName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment