-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProgram.cs
236 lines (211 loc) · 12.6 KB
/
Program.cs
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Samples.Common;
using Azure.ResourceManager.ServiceBus;
using Azure.ResourceManager.ServiceBus.Models;
namespace ServiceBusPublishSubscribeAdvanceFeatures
{
public class Program
{
/**
* Azure Service Bus basic scenario sample.
* - Create namespace.
* - Create a service bus subscription in the topic with session and dead-letter enabled.
* - Create another subscription in the topic with auto deletion of idle entities.
* - Create second topic with new Send Authorization rule, partitioning enabled and a new Service bus Subscription.
* - Update second topic to change time for AutoDeleteOnIdle time, without Send rule and with a new manage authorization rule.
* - Get the keys from default authorization rule to connect to topic.
* - Delete a topic
* - Delete namespace
*/
private static ResourceIdentifier? _resourceGroupId = null;
public static async Task RunSample(ArmClient client)
{
try
{
//============================================================
// Create a namespace.
// Get default subscription
SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
// Create a resource group in the USCentral region
var rgName = Utilities.CreateRandomName("rgSB04_");
Utilities.Log($"creating resource group with name : {rgName} ...");
var rgLro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(AzureLocation.WestUS));
var resourceGroup = rgLro.Value;
_resourceGroupId = resourceGroup.Id;
Utilities.Log("Created resource group with name: " + resourceGroup.Data.Name + "...");
//create namespace and wait for completion
var nameSpaceName = Utilities.CreateRandomName("nameSpace");
Utilities.Log("Creating namespace " + nameSpaceName + " in resource group " + rgName + "...");
var namespaceCollection = resourceGroup.GetServiceBusNamespaces();
var data = new ServiceBusNamespaceData(AzureLocation.WestUS)
{
Sku = new ServiceBusSku(ServiceBusSkuName.Standard),
Location = AzureLocation.WestUS
};
var serviceBusNamespace = (await namespaceCollection.CreateOrUpdateAsync(WaitUntil.Completed,nameSpaceName,data)).Value;
Utilities.Log("Created service bus " + serviceBusNamespace.Data.Name);
//Create a topic
Utilities.Log("Creating topic1 in namespace...");
var topicName = Utilities.CreateRandomName("topic1_");
var topicCollection = serviceBusNamespace.GetServiceBusTopics();
var topiccData = new ServiceBusTopicData()
{
MaxSizeInMegabytes = 1024,
};
var topic1 = (await topicCollection.CreateOrUpdateAsync(WaitUntil.Completed,topicName,topiccData)).Value;
Utilities.Log("Created a topic" + topic1.Data.Name);
Utilities.Log("Created topic following topic along with namespace : " + nameSpaceName);
var firstTopic = (serviceBusNamespace.GetServiceBusTopic(topicName)).Value;
//============================================================
// Create a service bus subscription in the topic with session and dead-letter enabled.
var subscription1Name = Utilities.CreateRandomName("subs1_");
Utilities.Log("Creating subscription " + subscription1Name + " in topic " + topic1.Data.Name + "...");
var subscription1Collection = firstTopic.GetServiceBusSubscriptions();
var subscription1Data = new ServiceBusSubscriptionData()
{
RequiresSession = true,
DefaultMessageTimeToLive = TimeSpan.FromMinutes(20),
MaxDeliveryCount = 20,
DeadLetteringOnMessageExpiration = true,
DeadLetteringOnFilterEvaluationExceptions = true,
};
var firstSubscription = (await subscription1Collection.CreateOrUpdateAsync(WaitUntil.Completed,subscription1Name, subscription1Data)).Value;
Utilities.Log("Created subscription " + firstSubscription.Data.Name + " in topic " + topic1.Data.Name + "...");
//============================================================
// Create another subscription in the topic with auto deletion of idle entities.
var subscription2Name = Utilities.CreateRandomName("subs2_");
Utilities.Log("Creating another subscription" + subscription2Name + " in topic " + topic1.Data.Name + "...");
var subscription2Collection = topic1.GetServiceBusSubscriptions();
var subscription2Data = new ServiceBusSubscriptionData()
{
RequiresSession = true,
AutoDeleteOnIdle = TimeSpan.FromMinutes(20),
};
var subscription2 = (await subscription2Collection.CreateOrUpdateAsync(WaitUntil.Completed, subscription2Name, subscription2Data)).Value;
Utilities.Log("Created another subscription" + subscription2.Data.Name + " in topic " + topic1.Data.Name + "...");
//============================================================
// Create second topic with new Send Authorization rule, partitioning enabled and a new Service bus Subscription.
var topic2Name = Utilities.CreateRandomName("topic2_");
Utilities.Log("Creating second topic " + topic2Name + ", with De-duplication and AutoDeleteOnIdle features...");
var topic2Collection = serviceBusNamespace.GetServiceBusTopics();
var topic2Data = new ServiceBusTopicData()
{
EnablePartitioning = true,
};
var topic2 = (await topic2Collection.CreateOrUpdateAsync(WaitUntil.Completed, topic2Name, topic2Data)).Value;
Utilities.Log("Created second topic :" + topic2.Data.Name);
//Create rule
var sendRuleName = Utilities.CreateRandomName("SendRule");
Utilities.Log("Creating rule : " + sendRuleName + " in topic " + topic2.Data.Name + "...");
var ruleCollection = topic2.GetServiceBusTopicAuthorizationRules();
var sendRuleData = new ServiceBusAuthorizationRuleData()
{
Rights =
{
ServiceBusAccessRight.Send
}
};
var sendRule = (await ruleCollection.CreateOrUpdateAsync(WaitUntil.Completed, sendRuleName, sendRuleData)).Value;
Utilities.Log("Created sendrule :" + sendRule.Data.Name);
//Create third subscription
var subscription3Name = Utilities.CreateRandomName("subs3_");
Utilities.Log("Creating third subscription" + subscription3Name + " in topic " + topic2.Data.Name + "...");
var subscription3Collection = topic2.GetServiceBusSubscriptions();
var subscription3Data = new ServiceBusSubscriptionData()
{
RequiresSession = true,
};
var subscription3 = (await subscription3Collection.CreateOrUpdateAsync(WaitUntil.Completed, subscription3Name, subscription3Data)).Value;
Utilities.Log("Created third subscription" + subscription3.Data.Name);
//============================================================
// Update second topic to change time for AutoDeleteOnIdle time, without Send rule and with a new manage authorization rule.
Utilities.Log("Updating second topic " + topic2Name + "...");
var updateData = new ServiceBusTopicData()
{
AutoDeleteOnIdle = TimeSpan.FromMinutes(5),
};
//Delete sendRule
Utilities.Log("Deleting sendRule...");
_ = await sendRule.DeleteAsync(WaitUntil.Completed);
Utilities.Log("Deleted sendRule...");
//Create rule
var manageRuleName = Utilities.CreateRandomName("ManageRule");
Utilities.Log("Creating rule" + manageRuleName + " in topic " + topic2.Data.Name + "...");
var manageRuleData = new ServiceBusAuthorizationRuleData()
{
Rights =
{
ServiceBusAccessRight.Manage,
ServiceBusAccessRight.Send,
ServiceBusAccessRight.Listen
}
};
var manageRule = (await ruleCollection.CreateOrUpdateAsync(WaitUntil.Completed, manageRuleName, manageRuleData)).Value;
Utilities.Log("Created manageRule : " + manageRule.Data.Name);
_ = topic2.UpdateAsync(WaitUntil.Completed,updateData);
Utilities.Log("Updated second topic to change its auto deletion time");
//=============================================================
// Get connection string for default authorization rule of namespace
var namespaceAuthorizationRules = serviceBusNamespace.GetServiceBusNamespaceAuthorizationRules().ToList();
Utilities.Log("Number of authorization rule for namespace :" + namespaceAuthorizationRules.Count());
//=============================================================
// Delete a topic and namespace
Utilities.Log("Deleting topic " + topicName + "in namespace : " + serviceBusNamespace.Data.Name + "...");
await topic1.DeleteAsync(WaitUntil.Completed);
Utilities.Log("Deleted topic " + topicName + "...");
Utilities.Log("Deleting namespace : " + serviceBusNamespace.Data.Name + "...");
try
{
await serviceBusNamespace.DeleteAsync(WaitUntil.Completed);
}
catch (Exception)
{
}
Utilities.Log("Deleted namespace : " + nameSpaceName + "...");
}
finally
{
try
{
if (_resourceGroupId is not null)
{
Utilities.Log($"Deleting Resource Group: {_resourceGroupId}");
await client.GetResourceGroupResource(_resourceGroupId).DeleteAsync(WaitUntil.Completed);
Utilities.Log($"Deleted Resource Group: {_resourceGroupId}");
}
}
catch (NullReferenceException)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
catch (Exception g)
{
Utilities.Log(g);
}
}
}
public static async Task Main(string[] args)
{
try
{
var clientId = Environment.GetEnvironmentVariable("CLIENT_ID");
var clientSecret = Environment.GetEnvironmentVariable("CLIENT_SECRET");
var tenantId = Environment.GetEnvironmentVariable("TENANT_ID");
var subscription = Environment.GetEnvironmentVariable("SUBSCRIPTION_ID");
ClientSecretCredential credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
ArmClient client = new ArmClient(credential, subscription);
await RunSample(client);
}
catch (Exception e)
{
Utilities.Log(e);
}
}
}
}