Skip to content

Commit

Permalink
Add tests and docs for publishing CloudEvent to domain topics (Azure#…
Browse files Browse the repository at this point in the history
…25511)

* Add tests and docs for publishing CloudEvent to domain topics.

* PR fb

* Fix snippets
  • Loading branch information
JoshLove-msft authored Nov 25, 2021
1 parent 136bf36 commit e38b7d0
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 0 deletions.
21 changes: 21 additions & 0 deletions sdk/eventgrid/Azure.Messaging.EventGrid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,27 @@ List<EventGridEvent> eventsList = new List<EventGridEvent>
await client.SendEventsAsync(eventsList);
```

For sending CloudEvents, the CloudEvent source is used as the domain topic:
```C# Snippet:SendCloudEventsToDomain
List<CloudEvent> eventsList = new List<CloudEvent>();

for (int i = 0; i < 10; i++)
{
CloudEvent cloudEvent = new CloudEvent(
// the source is mapped to the domain topic
$"Subject-{i}",
"Microsoft.MockPublisher.TestEvent",
"hello")
{
Id = $"event-{i}",
Time = DateTimeOffset.Now
};
eventsList.Add(cloudEvent);
}

await client.SendEventsAsync(eventsList);
```

### Receiving and Deserializing Events
There are several different Azure services that act as [event handlers](https://docs.microsoft.com/azure/event-grid/event-handlers).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,42 @@ public async Task CanPublishEventToDomain()
await client.SendEventsAsync(eventsList);
}

[RecordedTest]
public async Task CanPublishCloudEventToDomain()
{
EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
EventGridPublisherClient client = InstrumentClient(
new EventGridPublisherClient(
new Uri(TestEnvironment.CloudEventDomainHost),
new AzureKeyCredential(TestEnvironment.CloudEventDomainKey),
options));

#region Snippet:SendCloudEventsToDomain
List<CloudEvent> eventsList = new List<CloudEvent>();

for (int i = 0; i < 10; i++)
{
CloudEvent cloudEvent = new CloudEvent(
// the source is mapped to the domain topic
$"Subject-{i}",
"Microsoft.MockPublisher.TestEvent",
"hello")
{
#if SNIPPET
Id = $"event-{i}",
Time = DateTimeOffset.Now
#else
Id = Recording.Random.NewGuid().ToString(),
Time = Recording.Now
#endif
};
eventsList.Add(cloudEvent);
}

await client.SendEventsAsync(eventsList);
#endregion
}

[RecordedTest]
public async Task CanPublishEventToDomainAAD()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class EventGridTestEnvironment : TestEnvironment
public const string DomainKeyEnvironmentVariableName = "EVENT_GRID_DOMAIN_KEY";
public const string DomainEndpointEnvironmentVariableName = "EVENT_GRID_DOMAIN_ENDPOINT";

public const string CloudEventDomainKeyEnvironmentVariableName = "EVENT_GRID_CLOUD_EVENT_DOMAIN_KEY";
public const string CloudEventDomainEndpointEnvironmentVariableName = "EVENT_GRID_CLOUD_EVENT_DOMAIN_ENDPOINT";

public const string CloudEventTopicKeyEnvironmentVariableName = "EVENT_GRID_CLOUD_EVENT_TOPIC_KEY";
public const string CloudEventTopicEndpointEnvironmentVariableName = "EVENT_GRID_CLOUD_EVENT_TOPIC_ENDPOINT";

Expand All @@ -26,6 +29,9 @@ public class EventGridTestEnvironment : TestEnvironment
public string DomainHost => GetRecordedVariable(DomainEndpointEnvironmentVariableName);
public string DomainKey => GetRecordedVariable(DomainKeyEnvironmentVariableName, options => options.IsSecret(SanitizedValue.Base64));

public string CloudEventDomainHost => GetRecordedVariable(CloudEventDomainEndpointEnvironmentVariableName);
public string CloudEventDomainKey => GetRecordedVariable(CloudEventDomainKeyEnvironmentVariableName, options => options.IsSecret(SanitizedValue.Base64));

public string CloudEventTopicHost => GetRecordedVariable(CloudEventTopicEndpointEnvironmentVariableName);
public string CloudEventTopicKey => GetRecordedVariable(CloudEventTopicKeyEnvironmentVariableName, options => options.IsSecret(SanitizedValue.Base64));

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ For detailed information about the Event Grid client library concepts: [Event Gr

## Examples

### Publish to an Event Grid topic
```C# Snippet:CloudNativePublish
EventGridPublisherClient client = new EventGridPublisherClient(
new Uri(TestEnvironment.CloudEventTopicHost),
Expand All @@ -64,6 +65,23 @@ var cloudEvent =
await client.SendCloudNativeCloudEventAsync(cloudEvent);
```

### Publish to an Event Grid Domain
When publishing to an Event Grid Domain with CloudEvents, the CloudEvent `source` is used as the domain topic. The Event Grid service doesn't support using an absolute URI for a domain topic, so you would need to do something like the following to integrate with the CloudNative CloudEvents:
```C# Snippet:CloudNativePublishToDomain
CloudEvent cloudEvent =
new CloudEvent
{
Type = "record",
// Event Grid does not allow absolute URIs as the domain topic
Source = new Uri("test", UriKind.Relative),
Id = "eventId",
Time = DateTimeOffset.Now,
Data = new TestPayload("name", 0)
};

await client.SendCloudNativeCloudEventAsync(cloudEvent);
```

## Troubleshooting

For troubleshooting information, see the [Event Grid Client Library documentation](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/eventgrid/Azure.Messaging.EventGrid#troubleshooting).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,37 @@ public async Task CanPublishSingleCloudEvent()
await client.SendCloudNativeCloudEventAsync(cloudEvent);
}

[RecordedTest]
public async Task CanPublishCloudEventToDomain()
{
EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
EventGridPublisherClient client = InstrumentClient(
new EventGridPublisherClient(
new Uri(TestEnvironment.CloudEventDomainHost),
new AzureKeyCredential(TestEnvironment.CloudEventDomainKey),
options));

#region Snippet:CloudNativePublishToDomain
CloudEvent cloudEvent =
new CloudEvent
{
Type = "record",
// Event Grid does not allow absolute URIs as the domain topic
Source = new Uri("test", UriKind.Relative),
#if SNIPPET
Id = "eventId",
Time = DateTimeOffset.Now,
#else
Id = Recording.Random.NewGuid().ToString(),
Time = Recording.Now,
#endif
Data = new TestPayload("name", 0)
};

await client.SendCloudNativeCloudEventAsync(cloudEvent);
#endregion
}

private class TestPayload
{
public TestPayload(string name, int age)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e38b7d0

Please sign in to comment.