Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EventGrid samples and readme #15461

Merged
merged 6 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 2 additions & 0 deletions eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@
<PackageReference Update="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Update="Microsoft.AspNetCore.Server.WebListener" Version="1.0.2" />

<PackageReference Update="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.2" />

<PackageReference Update="Microsoft.Extensions.Azure" Version="1.0.0" />
<PackageReference Update="Microsoft.Extensions.Configuration" Version="2.1.0" />
<PackageReference Update="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Release History

## 3.0.0-beta.1 (Unreleased)

- The initial release of Microsoft.Azure.WebJobs.Extensions.EventGrid 3.0.0
Original file line number Diff line number Diff line change
@@ -1,34 +1,113 @@
# Azure WebJobs EventGrid client library for .NET

TODO
This extension provides functionality for receiving Event Grid webhook calls in Azure Functions, allowing you to easily write functions that respond to any event published to Event Grid.

## Getting started

### Install the package

Install the Event Grid extension with [NuGet][nuget]:

```Powershell
dotnet add package Microsoft.Azure.WebJobs.Extensions.EventGrid
```

### Prerequisites

You must have an [Azure subscription](https://azure.microsoft.com/free/) and an Azure resource group with a custom Event Grid topic or domain. Follow this [step-by-step tutorial](https://docs.microsoft.com/azure/event-grid/custom-event-quickstart-portal) to register the Event Grid resource provider and create Event Grid topics using the [Azure portal](https://portal.azure.com/). There is a [similar tutorial](https://docs.microsoft.com/azure/event-grid/custom-event-quickstart) using [Azure CLI](https://docs.microsoft.com/cli/azure).

### Authenticate the Client

In order for the extension publish events, you will need the `endpoint` of the Event Grid topic and a `credential`, which can be created using the topic's access key.

### Authenticate the client
You can find the endpoint for your Event Grid topic either in the [Azure Portal](https://portal.azure.com/) or by using the [Azure CLI](https://docs.microsoft.com/cli/azure) snippet below.

```bash
az eventgrid topic show --name <your-resource-name> --resource-group <your-resource-group-name> --query "endpoint"
```

The access key can also be found through the [portal](https://docs.microsoft.com/azure/event-grid/get-access-keys), or by using the Azure CLI snippet below:
```bash
az eventgrid topic key list --name <your-resource-name> --resource-group <your-resource-group-name> --query "key1"
```

## Key concepts

TODO
### Using Event Grid binding
ellismg marked this conversation as resolved.
Show resolved Hide resolved
pakrym marked this conversation as resolved.
Show resolved Hide resolved

Please follow the [binding tutorial](https://docs.microsoft.com/azure/azure-functions/functions-bindings-event-grid-trigger?tabs) to lean about using this extension for publishing EventGrid events.
pakrym marked this conversation as resolved.
Show resolved Hide resolved
pakrym marked this conversation as resolved.
Show resolved Hide resolved

### Using Event Grid trigger

Please follow the [tutorial](https://docs.microsoft.com/azure/azure-functions/functions-bindings-event-grid-trigger?tabs=csharp) to learn about triggering an Azure Function when an event is published.

## Examples

TODO
### Functions that uses Event Grid binding

```C# Snippet:EventGridBindingFunction
public static class EventGridBindingFunction
{
[FunctionName("EventGridBindingFunction")]
public static async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[EventGrid(TopicEndpointUri = "EventGridEndpoint", TopicKeySetting = "EventGridKey")] IAsyncCollector<EventGridEvent> eventCollector)
{
EventGridEvent e = new EventGridEvent(await req.ReadAsStringAsync(), "IncomingRequest", "IncomingRequest", "1.0.0");
await eventCollector.AddAsync(e);
return new OkResult();
}
}
```

### Functions that uses Event Grid trigger

```C# Snippet:EventGridTriggerFunction
public static class EventGridTriggerFunction
{
[FunctionName("EventGridTriggerFunction")]
public static void Run(
ILogger logger,
[EventGridTrigger] EventGridEvent e)
{
logger.LogInformation("Event received {type} {subject}", e.EventType, e.Subject);
}
}
```

## Troubleshooting

TODO
Please refer to [Monitor Azure Functions](https://docs.microsoft.com/azure/azure-functions/functions-monitoring) for troubleshooting guidance.

## Next steps

TODO
Read the [introduction to Azure Function](https://docs.microsoft.com/azure/azure-functions/functions-overview) or [creating an Azure Function guide](https://docs.microsoft.com/azure/azure-functions/functions-create-first-azure-function).

## Contributing

TODO
See our [CONTRIBUTING.md][contrib] for details on building,
testing, and contributing to this library.

This project welcomes contributions and suggestions. Most contributions require
you to agree to a Contributor License Agreement (CLA) declaring that you have
the right to, and actually do, grant us the rights to use your contribution. For
details, visit [cla.microsoft.com][cla].

This project has adopted the [Microsoft Open Source Code of Conduct][coc].
For more information see the [Code of Conduct FAQ][coc_faq]
or contact [opencode@microsoft.com][coc_contact] with any
additional questions or comments.

![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-net%2Fsdk%2Fsearch%2FMicrosoft.Azure.WebJobs.Extensions.EventGrid%2FREADME.png)

<!-- LINKS -->
[source]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/search/Microsoft.Azure.WebJobs.Extensions.EventGrid/src
[package]: https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.EventGrid/
[docs]: https://docs.microsoft.com/dotnet/api/Microsoft.Azure.WebJobs.Extensions.EventGrid
[nuget]: https://www.nuget.org/

[contrib]: https://github.com/Azure/azure-sdk-for-net/tree/master/CONTRIBUTING.md
[cla]: https://cla.microsoft.com
[coc]: https://opensource.microsoft.com/codeofconduct/
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/
[coc_contact]: mailto:opencode@microsoft.com
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Azure;
using Azure.Messaging.EventGrid;
using Microsoft.Azure.WebJobs.Description;
using Microsoft.Azure.WebJobs.Host.Bindings;
Expand Down Expand Up @@ -42,7 +43,7 @@ internal EventGridExtensionConfigProvider(Func<EventGridAttribute, IAsyncCollect
// default constructor
public EventGridExtensionConfigProvider(ILoggerFactory loggerFactory)
{
_converter = (attr => new EventGridAsyncCollector(new EventGridPublisherClient(new Uri(attr.TopicEndpointUri), new EventGridSharedAccessSignatureCredential(attr.TopicKeySetting))));
_converter = (attr => new EventGridAsyncCollector(new EventGridPublisherClient(new Uri(attr.TopicEndpointUri), new AzureKeyCredential(attr.TopicKeySetting))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR: Is there something we could have done in the EventGrid SDK Documentation itself to make it clear which of these you needed to use?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a comment from ApiView about a potential rename of the type to include SAS in the name. Not sure if that would have helped.

_loggerFactory = loggerFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Microsoft.Azure.WebJobs" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading.Tasks;
using Azure.Messaging.EventGrid;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.EventGrid;

namespace Azure.Extensions.WebJobs.Sample
{
#region Snippet:EventGridBindingFunction
public static class EventGridBindingFunction
{
[FunctionName("EventGridBindingFunction")]
public static async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[EventGrid(TopicEndpointUri = "EventGridEndpoint", TopicKeySetting = "EventGridKey")] IAsyncCollector<EventGridEvent> eventCollector)
{
EventGridEvent e = new EventGridEvent(await req.ReadAsStringAsync(), "IncomingRequest", "IncomingRequest", "1.0.0");
await eventCollector.AddAsync(e);
return new OkResult();
}
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Azure.Messaging.EventGrid;
using Microsoft.Extensions.Logging;

namespace Azure.Extensions.WebJobs.Sample
{
#region Snippet:EventGridTriggerFunction
public static class EventGridTriggerFunction
{
[FunctionName("EventGridTriggerFunction")]
public static void Run(
ILogger logger,
[EventGridTrigger] EventGridEvent e)
{
logger.LogInformation("Event received {type} {subject}", e.EventType, e.Subject);
}
}
#endregion
}