Permalink
Branch: master
Find file Copy path
3c5f4df Jan 23, 2019
1 contributor

Users who have contributed to this file

82 lines (79 sloc) 4.65 KB
<!--
Forward Azure Event Grid Event
This policy will get the first event in a list of events and forward it to a downstream system.
If you want to support more, then you need to remove the choose that returns a 400 and implement this capability or simply pass the full list downstream.
In order to successfully subscribe to an Azure Event Grid Topic, this policy also handles the initial validation handshake to prove ownership.
-->
<policies>
<inbound>
<base />
<set-variable name="Request" value="@(context.Request.Body.As<JArray>())" />
<choose>
<when condition="@(context.Variables.GetValueOrDefault<JArray>("Request").Count != 1)">
<return-response>
<set-status code="400" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{"error":"We only support one event"}</set-body>
</return-response>
</when>
<otherwise>
<set-variable name="Event" value="@(context.Variables.GetValueOrDefault<JArray>("Request")[0])" />
<choose>
<when condition="@(context.Variables.GetValueOrDefault<JObject>("Event")["eventType"].ToString() == "Microsoft.EventGrid.SubscriptionValidationEvent")">
<return-response>
<set-status code="200" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var validationResponse = new JObject(new JProperty("validationResponse",context.Variables.GetValueOrDefault<JObject>("Event")["data"]["validationCode"].ToString()));
return validationResponse.ToString();
}</set-body>
</return-response>
</when>
<otherwise>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-header name="X-Event-Id" exists-action="override">
<value>@(context.Variables.GetValueOrDefault<JObject>("Event")["id"].ToString())</value>
</set-header>
<set-header name="X-Event-Subject" exists-action="override">
<value>@(context.Variables.GetValueOrDefault<JObject>("Event")["subject"].ToString())</value>
</set-header>
<set-header name="X-Event-Type" exists-action="override">
<value>@(context.Variables.GetValueOrDefault<JObject>("Event")["eventType"].ToString())</value>
</set-header>
<set-header name="X-Event-Time" exists-action="override">
<value>@(context.Variables.GetValueOrDefault<JObject>("Event")["eventTime"].ToString())</value>
</set-header>
<set-header name="X-Event-Data-Version" exists-action="override">
<value>@(context.Variables.GetValueOrDefault<JObject>("Event")["dataVersion"].ToString())</value>
</set-header>
<set-header name="X-Event-Metadata-Version" exists-action="override">
<value>@(context.Variables.GetValueOrDefault<JObject>("Event")["metadataVersion"].ToString())</value>
</set-header>
<set-header name="X-Event-Topic" exists-action="override">
<value>@(context.Variables.GetValueOrDefault<JObject>("Event")["topic"].ToString())</value>
</set-header>
<set-body>@(context.Variables.GetValueOrDefault<JObject>("Event")["data"].ToString())</set-body>
<!-- Your custom logic here, for example forwarding to a downstream system -->
<!-- can be browsed at https://pipedream.com/r/en4co4ars2s5v/ -->
<set-backend-service base-url="https://en4co4ars2s5v.x.pipedream.net" />
</otherwise>
</choose>
</otherwise>
</choose>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>