Repository navigation
Expand file tree
/
Copy pathAzureEventViewer.cs
More file actions
68 lines (61 loc) · 2.66 KB
/
Copy pathAzureEventViewer.cs
File metadata and controls
68 lines (61 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.EventGrid.Models;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace AzureEventViewer
{
public static class AzureEventViewer
{
[FunctionName("AzureEventViewer")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req,
[Queue("eventQueue", Connection = "AzureWebJobsStorage")] IAsyncCollector<string> outputQueue,
TraceWriter log)
{
var requestContent = await req.Content.ReadAsStringAsync();
// Retrieve event type value from the header and return a bad
// request code if it is missing.
if (!req.Headers.TryGetValues("Aeg-Event-Type", out var headerValues))
return req.CreateResponse(HttpStatusCode.BadRequest, "Missing event type in the header.");
var eventTypeHeaderValue = headerValues.FirstOrDefault();
// Echo back the validation code if the event type
// is for a subscription validation.
if (eventTypeHeaderValue == "SubscriptionValidation")
{
var events = JsonConvert.DeserializeObject<EventGridEvent[]>(requestContent);
if (events.FirstOrDefault()?.Data is JObject dataObject)
{
return req.CreateResponse(HttpStatusCode.OK,
new {validationResponse = dataObject["validationCode"]});
}
req.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid request");
}
else if (eventTypeHeaderValue == "Notification")
{
// Write the event to a storage queue
log.Info(requestContent);
try
{
await outputQueue.AddAsync(requestContent);
}
catch (Exception e)
{
log.Info(e.ToString());
return req.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error with storage queue");
}
return req.CreateResponse(HttpStatusCode.OK);
}
// Return a bad request if the event type is missing from the header.
return req.CreateResponse(HttpStatusCode.BadRequest, "Missing event type in the header.");
}
}
}