|
| 1 | +#!/bin/bash |
| 2 | +# Passed validation in Cloud Shell on 3/28/2022 |
| 3 | + |
| 4 | +# <FullScript> |
| 5 | +# Create Event Grid custom topic |
| 6 | + |
| 7 | +# Variable block |
| 8 | +let "randomIdentifier=$RANDOM*$RANDOM" |
| 9 | +location="East US" |
| 10 | +subscriptionId="$(az account show --query id -o tsv)" |
| 11 | +resourceGroup="msdocs-event-grid-rg-$randomIdentifier" |
| 12 | +tags="event-grid" |
| 13 | +topic="msdocs-event-grid-topic-$randomIdentifier" |
| 14 | +site="msdocs-event-grid-site-$randomIdentifier" |
| 15 | +eventSubscription="msdocs-event-subscription-$randomIdentifier" |
| 16 | +webappEndpoint="https://$site.azurewebsites.net/api/updates" |
| 17 | +storage="msdocsstorage$randomIdentifier" |
| 18 | + |
| 19 | +# Create a resource group |
| 20 | +echo "Creating in "$location"..." |
| 21 | +az group create --name $resourceGroup --location "$location" --tag $tag |
| 22 | + |
| 23 | +# Enable and then show the Event Grid resource provider |
| 24 | +az provider register --namespace Microsoft.EventGrid |
| 25 | +az provider show --namespace Microsoft.EventGrid --query "registrationState" |
| 26 | + |
| 27 | +# Create custom topic |
| 28 | +echo "Creating $topic" |
| 29 | +az eventgrid topic create \ |
| 30 | +--resource-group $resourceGroup \ |
| 31 | +--name $topic \ |
| 32 | +--location "$location" |
| 33 | + |
| 34 | +# Create a message endpoint |
| 35 | +echo "Creating $site" |
| 36 | +az deployment group create \ |
| 37 | + --resource-group gridResourceGroup \ |
| 38 | + --template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" \ |
| 39 | + --parameters siteName=$site hostingPlanName=viewerhost |
| 40 | + |
| 41 | +# To view your web app, navigate to https://<your-site-name>.azurewebsites.net |
| 42 | + |
| 43 | +# Subscribe to custom topic |
| 44 | +az eventgrid event-subscription create \ |
| 45 | + --source-resource-id "/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.EventGrid/topics/$topic" \ |
| 46 | + --name demoViewerSub \ |
| 47 | + --endpoint $webappEndpoint |
| 48 | + |
| 49 | +# View your web app again to see the subscription validation event. |
| 50 | +# Select the eye icon to expand the event data |
| 51 | + |
| 52 | +# Send an event to your custom topic |
| 53 | +url=$(az eventgrid topic show --name $topic -g $resourceGroup --query "endpoint" --output tsv) |
| 54 | +key=$(az eventgrid topic key list --name $topic -g $resourceGroup --query "key1" --output tsv) |
| 55 | +echo $url |
| 56 | +echo $key |
| 57 | +event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/motorcycles", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "make": "Ducati", "model": "Monster"},"dataVersion": "1.0"} ]' |
| 58 | +curl -X POST -H "aeg-sas-key: $key" -d "$event" $url |
| 59 | + |
| 60 | +# View your web app again to see the event that you just sent |
| 61 | +# </FullScript> |
| 62 | + |
| 63 | +# echo "Deleting all resources" |
| 64 | +# az group delete --name $resourceGroup -y |
0 commit comments