-
Notifications
You must be signed in to change notification settings - Fork 621
Expand file tree
/
Copy pathEventGridControlPlaneClient.cs
More file actions
206 lines (175 loc) · 9.54 KB
/
EventGridControlPlaneClient.cs
File metadata and controls
206 lines (175 loc) · 9.54 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
// <copyright file="EventGridClient.cs" company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
namespace EventGridExplorerLibrary
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using global::Azure;
using global::Azure.Core;
using global::Azure.Identity;
using global::Azure.Messaging.EventGrid.Namespaces;
using global::Azure.ResourceManager;
using global::Azure.ResourceManager.EventGrid;
using global::Azure.ResourceManager.EventGrid.Models;
/// <summary>
/// Implementation of the event grid client interface.
/// </summary>
public class EventGridControlPlaneClient : IEventGridControlPlaneClient
{
private const string AadScope = "https://management.core.windows.net";
private const string AuthorityHostUri = "https://login.microsoftonline.com/";
private const string NamespaceTopicEventSubscriptionsApiVersion = "2023-12-15-preview";
private const string NamespaceTopicEventSubscriptionsResourceType = "Microsoft.EventGrid/namespaces/topics/eventSubscriptions";
private readonly string subscriptionId;
private readonly string tenantId;
public ArmClient armclient;
/// <summary>
/// Initializes a new instance of the <see cref="EventGridClient"/> class.
/// </summary>
public EventGridControlPlaneClient(string subscriptionId, string tenantId)
{
this.subscriptionId = subscriptionId;
this.tenantId = tenantId;
this.armclient = new ArmClient(this.GetTokenCredential());
// Setting this API version is needed to support name space topic event subscriptions with webhook as a destination.
ArmClientOptions options = new ArmClientOptions();
options.SetApiVersion(NamespaceTopicEventSubscriptionsResourceType, NamespaceTopicEventSubscriptionsApiVersion);
}
/// <inheritdoc/>
public async Task<string> CreateNamespaceTopicAsync(string resourceGroupName, string namespaceName, string namespaceTopicName)
{
EventGridNamespaceResource namespaceResource = GetNamespaceResource(resourceGroupName, namespaceName);
NamespaceTopicCollection collection = namespaceResource.GetNamespaceTopics();
// check if exists
if (await collection.ExistsAsync(namespaceTopicName))
{
return $"{namespaceTopicName} exists already";
}
NamespaceTopicData namespaceTopicData = new NamespaceTopicData
{
PublisherType = PublisherType.Custom,
InputSchema = EventInputSchema.CloudEventSchemaV10,
};
var azureOperation = await collection.CreateOrUpdateAsync(WaitUntil.Completed, namespaceTopicName, namespaceTopicData);
NamespaceTopicResource result = azureOperation.Value;
return result.Id;
}
/// <inheritdoc/>
public async Task<string> DeleteNamespaceTopicAsync(string resourceGroupName, string namespaceName, string namespaceTopicName)
{
EventGridNamespaceResource namespaceResource = GetNamespaceResource(resourceGroupName, namespaceName);
NamespaceTopicResource namespaceTopicResource = (await namespaceResource.GetNamespaceTopicAsync(namespaceTopicName)).Value;
NamespaceTopicCollection collection = namespaceResource.GetNamespaceTopics();
// check if exists
if (!(await collection.ExistsAsync(namespaceTopicName)).Value)
{
return $"{namespaceTopicName} does not exist";
}
ArmOperation azureOperation = await namespaceTopicResource.DeleteAsync(WaitUntil.Completed);
return string.Empty;
}
/// <inheritdoc/>
public async Task<string> DeleteNamespaceTopicEventSubscriptionAsync(string resourceGroupName, string namespaceName, string namespaceTopicName, string subscriptionName)
{
EventGridNamespaceResource namespaceResource = GetNamespaceResource(resourceGroupName, namespaceName);
NamespaceTopicResource namespaceTopicResource = (await namespaceResource.GetNamespaceTopicAsync(namespaceTopicName)).Value;
NamespaceTopicEventSubscriptionResource namespaceTopicEventSubscriptionResource = (await namespaceTopicResource.GetNamespaceTopicEventSubscriptionAsync(subscriptionName)).Value;
NamespaceTopicEventSubscriptionCollection collection = namespaceTopicResource.GetNamespaceTopicEventSubscriptions();
// check if exists
if (!(await collection.ExistsAsync(subscriptionName)).Value)
{
return $"{subscriptionName} for topic {namespaceTopicName} does not exist";
}
ArmOperation azureOperation = await namespaceTopicEventSubscriptionResource.DeleteAsync(WaitUntil.Completed);
return string.Empty;
}
/// <inheritdoc/>
public async Task<string> CreateNamespaceTopicEventSubscriptionAsync(string resourceGroupName, string namespaceName, string namespaceTopicName, string subscriptionName, string deliveryMode, List<Dictionary<string,string>> filters, List<string> eventTypes)
{
EventGridNamespaceResource namespaceResource = GetNamespaceResource(resourceGroupName, namespaceName);
NamespaceTopicResource namespaceTopicResource = (await namespaceResource.GetNamespaceTopicAsync(namespaceTopicName)).Value;
NamespaceTopicEventSubscriptionCollection collection = namespaceTopicResource.GetNamespaceTopicEventSubscriptions();
NamespaceTopicEventSubscriptionData namespaceTopicEventSubscriptionData = new NamespaceTopicEventSubscriptionData();
FiltersConfiguration filtersConfiguration = GetFiltersConfiguration(filters, eventTypes);
if (filtersConfiguration.IncludedEventTypes.Count > 0 || filtersConfiguration.Filters.Count > 0)
{
namespaceTopicEventSubscriptionData.DeliveryConfiguration = new DeliveryConfiguration
{
DeliveryMode = deliveryMode
};
namespaceTopicEventSubscriptionData.FiltersConfiguration = filtersConfiguration;
namespaceTopicEventSubscriptionData.EventDeliverySchema = new DeliverySchema("CloudEventSchemaV1_0");
}
else
{
namespaceTopicEventSubscriptionData.DeliveryConfiguration = new DeliveryConfiguration
{
DeliveryMode = deliveryMode
};
namespaceTopicEventSubscriptionData.EventDeliverySchema = new DeliverySchema("CloudEventSchemaV1_0");
}
// check if exists
if ((await collection.ExistsAsync(subscriptionName)).Value)
{
return $"{subscriptionName} for topic {namespaceTopicName} already exists";
}
var azureOperation = collection.CreateOrUpdate(WaitUntil.Completed, subscriptionName, namespaceTopicEventSubscriptionData);
return azureOperation.Value.Id;
}
/// <inheritdoc/>
public EventGridNamespaceResource GetNamespaceResource(string resourceGroupName, string namespaceName)
{
var namespaceArmId = CreateArmId(this.subscriptionId, resourceGroupName) + namespaceName;
ArmClient client = GetArmClient();
ResourceIdentifier namespaceIdentifier = new ResourceIdentifier(namespaceArmId);
EventGridNamespaceResource namespaceResource = client.GetEventGridNamespaceResource(namespaceIdentifier);
return namespaceResource;
}
private ArmClient GetArmClient()
{
// TODO: handle recreation of client if token expires
if (armclient == null)
{
armclient = new ArmClient(GetTokenCredential());
}
return armclient;
}
private InteractiveBrowserCredential GetTokenCredential()
{
string[] scope = { AadScope + "/.default" };
var credentialOption = new InteractiveBrowserCredentialOptions()
{
TenantId = tenantId,
AuthorityHost = new Uri(AuthorityHostUri)
};
InteractiveBrowserCredential credential = new InteractiveBrowserCredential(credentialOption);
return credential;
}
private string CreateArmId(string subscriptionId, string resourceGroupName)
{
return $"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.EventGrid/namespaces/";
}
private FiltersConfiguration GetFiltersConfiguration(List<Dictionary<string, string>> filters, List<string> eventTypes)
{
FiltersConfiguration filtersConfiguration = new FiltersConfiguration();
EventGridFilterFactory eventGridFilterFactory = new EventGridFilterFactory(filtersConfiguration);
foreach (var i in filters)
{
eventGridFilterFactory.Key = i["Key"];
eventGridFilterFactory.Value = i["Value"];
eventGridFilterFactory.OperatorType = i["Operator"];
eventGridFilterFactory.FilterSelection();
}
if (eventTypes.Count > 0)
{
foreach (string eventType in eventTypes)
{
filtersConfiguration.IncludedEventTypes.Add(eventType);
}
}
return filtersConfiguration;
}
}
}