-
Notifications
You must be signed in to change notification settings - Fork 621
Expand file tree
/
Copy pathEventGridLibrary.cs
More file actions
207 lines (179 loc) · 9.79 KB
/
EventGridLibrary.cs
File metadata and controls
207 lines (179 loc) · 9.79 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text.Json;
using System.Globalization;
using Azure;
using Azure.Messaging;
using Azure.Messaging.EventGrid.Namespaces;
using Azure.ResourceManager.EventGrid;
using ServiceBusExplorer.Utilities.Helpers;
namespace EventGridExplorerLibrary
{
public class EventGridLibrary
{
#region Private Constants
private const string DefaultApiVersion = "2023-06-01-preview";
private const string ExceptionFormat = "Exception: {0}";
// Minimum wait time value for receive operation in Seconds
private const int MinWaitTimeInSeconds = 10;
// Maximum wait time value for receive operation in Seconds
private const int MaxWaitTimeInSeconds = 120;
#endregion
#region Private Fields
private EventGridControlPlaneClient eventGridControlPlaneClient;
private Dictionary<string, EventGridClient> dataPlaneClients = new Dictionary<string, EventGridClient>();
private int maxWaitTime;
private readonly WriteToLogDelegate writeToLog = default;
#endregion
public EventGridLibrary(string subscriptionId, string apiVersion, int maxWaitTime, string customId, WriteToLogDelegate writeToLog)
{
string tenantId = customId == string.Empty ? null : customId;
var apiVersionToUse = string.IsNullOrEmpty(apiVersion) ? DefaultApiVersion : apiVersion;
eventGridControlPlaneClient = new EventGridControlPlaneClient(subscriptionId, tenantId);
this.maxWaitTime = maxWaitTime;
this.writeToLog = writeToLog;
}
public async Task<Response<EventGridNamespaceResource>> GetNamespacesAsync(string resourceGroupName, string namespaceName)
{
return await eventGridControlPlaneClient.GetNamespaceResource(resourceGroupName, namespaceName).GetAsync();
}
public async Task<AsyncPageable<NamespaceTopicResource>> GetTopicsAsync(string resourceGroupName, string namespaceName, string hostname)
{
NamespaceTopicCollection namespaceTopicCollection = eventGridControlPlaneClient.GetNamespaceResource(resourceGroupName, namespaceName).GetNamespaceTopics();
var pages = namespaceTopicCollection.GetAllAsync();
var enumerator = pages.GetAsyncEnumerator();
try
{
while (await enumerator.MoveNextAsync())
{
NamespaceTopicResource namespaceTopicResource = (await eventGridControlPlaneClient.GetNamespaceResource(resourceGroupName, namespaceName).GetNamespaceTopicAsync(enumerator.Current.Data.Name)).Value;
var key = (await namespaceTopicResource.GetSharedAccessKeysAsync()).Value;
dataPlaneClients[enumerator.Current.Data.Name] = new EventGridClient(new Uri(hostname), new AzureKeyCredential(key.Key1));
}
}
finally
{
await enumerator.DisposeAsync();
}
return pages;
}
public async Task<AsyncPageable<NamespaceTopicEventSubscriptionResource>> GetEventSubscriptionsAsync(string resourceGroupName, string namespaceName, string topicName)
{
NamespaceTopicResource namespaceTopicResource = (await eventGridControlPlaneClient.GetNamespaceResource(resourceGroupName, namespaceName).GetNamespaceTopicAsync(topicName)).Value;
NamespaceTopicEventSubscriptionCollection namespaceTopicEventSubscriptionCollection = namespaceTopicResource.GetNamespaceTopicEventSubscriptions();
var pages = namespaceTopicEventSubscriptionCollection.GetAllAsync();
return pages;
}
public async Task CreateTopicAsync(string resourceGroupName, string namespaceName, string topicName)
{
await eventGridControlPlaneClient.CreateNamespaceTopicAsync(resourceGroupName, namespaceName, topicName);
}
public async Task DeleteTopicAsync(string resourceGroupName, string namespaceName, string topicName)
{
await eventGridControlPlaneClient.DeleteNamespaceTopicAsync(resourceGroupName, namespaceName, topicName);
}
public async Task CreateSubscriptionAsync(string resourceGroupName, string namespaceName, string topicName, string subscriptionName, string deliveryMode, List<Dictionary<string,string>> filters, List<string> eventTypes)
{
await eventGridControlPlaneClient.CreateNamespaceTopicEventSubscriptionAsync(resourceGroupName, namespaceName, topicName, subscriptionName, deliveryMode, filters, eventTypes);
}
public async Task DeleteSubscriptionAsync(string resourceGroupName, string namespaceName, string topicName, string subscriptionName)
{
await eventGridControlPlaneClient.DeleteNamespaceTopicEventSubscriptionAsync(resourceGroupName, namespaceName, topicName, subscriptionName);
}
public async Task PublishEventAsync(string topicName, string eventSource, string eventType, string eventDataJson)
{
var eventData = JsonSerializer.Deserialize<object>(eventDataJson);
var cloudEvent = new CloudEvent(eventSource, eventType, eventData);
await dataPlaneClients[topicName].PublishCloudEventAsync(topicName, cloudEvent);
}
public async Task PublishEventsAsync(string topicName, string eventSource, string eventType, List<string> publishEvents)
{
var cloudEvents = new CloudEvent[publishEvents.Count];
for (int i = 0; i < cloudEvents.Length; i++)
{
string eventModel = publishEvents[i];
cloudEvents[i] = new CloudEvent(eventSource, eventType, eventModel);
}
await dataPlaneClients[topicName].PublishCloudEventsAsync(topicName, cloudEvents);
}
public async Task<ReceiveResult> ReceiveEventsAsync(string topicName, string subscriptionName, int maxEventNum)
{
try
{
if (this.maxWaitTime < MinWaitTimeInSeconds)
{
return await dataPlaneClients[topicName].ReceiveCloudEventsAsync(topicName, subscriptionName, maxEvents: maxEventNum, maxWaitTime: TimeSpan.FromSeconds(MinWaitTimeInSeconds));
}
else if (this.maxWaitTime > MaxWaitTimeInSeconds)
{
return await dataPlaneClients[topicName].ReceiveCloudEventsAsync(topicName, subscriptionName, maxEvents: maxEventNum, maxWaitTime: TimeSpan.FromSeconds(MaxWaitTimeInSeconds));
}
else
{
return await dataPlaneClients[topicName].ReceiveCloudEventsAsync(topicName, subscriptionName, maxEvents: maxEventNum, maxWaitTime: TimeSpan.FromSeconds(this.maxWaitTime));
}
}
catch (Exception ex)
{
HandleException(ex);
return null;
}
}
public async Task<bool> EventActionsAsync(string action, List<string> lockTokens, string topicName, string subscriptionName, WriteToLogDelegate logAction)
{
if (lockTokens.Count > 0)
{
IReadOnlyList<string> succeededLockTokens = null;
IReadOnlyList<FailedLockToken> failedLockTokens = null;
switch (action)
{
case "Acknowledge":
var acknowledgeResult = (await dataPlaneClients[topicName].AcknowledgeCloudEventsAsync(topicName, subscriptionName, new AcknowledgeOptions(lockTokens))).Value;
succeededLockTokens = acknowledgeResult.SucceededLockTokens;
failedLockTokens = acknowledgeResult.FailedLockTokens;
break;
case "Release":
var releaseResult = (await dataPlaneClients[topicName].ReleaseCloudEventsAsync(topicName, subscriptionName, new ReleaseOptions(lockTokens))).Value;
succeededLockTokens = releaseResult.SucceededLockTokens;
failedLockTokens = releaseResult.FailedLockTokens;
break;
case "Reject":
var rejectResult = (await dataPlaneClients[topicName].RejectCloudEventsAsync(topicName, subscriptionName, new RejectOptions(lockTokens))).Value;
succeededLockTokens = rejectResult.SucceededLockTokens;
failedLockTokens = rejectResult.FailedLockTokens;
break;
}
logAction($"Event Action: {action}");
if (succeededLockTokens?.Count > 0)
{
logAction($"Success Count: {succeededLockTokens.Count}");
foreach (var lockToken in succeededLockTokens)
{
logAction($"Lock Token: {lockToken}");
}
}
if (failedLockTokens?.Count > 0)
{
logAction($"Failed Count: {failedLockTokens.Count}");
foreach (var lockToken in failedLockTokens)
{
logAction($"Lock Token: {lockToken.LockToken}");
logAction($"Error Code: {lockToken.Error.Code}");
logAction($"Error Description: {lockToken.Error.Message}");
}
}
return failedLockTokens?.Count == 0;
}
return false;
}
private void HandleException(Exception ex)
{
if (string.IsNullOrWhiteSpace(ex?.Message))
{
return;
}
writeToLog(string.Format(CultureInfo.CurrentCulture, ExceptionFormat, ex.Message));
}
}
}