-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
ServiceBusClient.cs
653 lines (532 loc) · 31.5 KB
/
ServiceBusClient.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Management.ServiceBus;
using Microsoft.Azure.Management.ServiceBus.Models;
using Microsoft.Azure.Commands.ServiceBus.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Xml.Linq;
using System.Security.Cryptography;
namespace Microsoft.Azure.Commands.Servicebus
{
public class ServiceBusClient
{
// Azure SDK requires a request parameter to be specified for a few Backup API calls, but
// the request is actually optional unless an update is needed
// private static readonly BackupRequest EmptyRequest = new BackupRequest(location: "");
public Action<string> VerboseLogger { get; set; }
public Action<string> ErrorLogger { get; set; }
public Action<string> WarningLogger { get; set; }
public ServiceBusClient(AzureContext context)
{
this.Client = AzureSession.ClientFactory.CreateArmClient<ServiceBusManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager);
}
public ServiceBusManagementClient Client
{
get;
private set;
}
#region Namespace
public NamespaceAttributes GetNamespace(string resourceGroupName, string namespaceName)
{
NamespaceResource response = Client.Namespaces.Get(resourceGroupName, namespaceName);
return new NamespaceAttributes(response);
}
public IEnumerable<NamespaceAttributes> ListNamespaces(string resourceGroupName)
{
Rest.Azure.IPage<NamespaceResource> response = Client.Namespaces.ListByResourceGroup(resourceGroupName);
//IEnumerable<NamespaceAttributes> resourceList = response.Select(resource => new NamespaceAttributes(resourceGroupName, resource));
IEnumerable<NamespaceAttributes> resourceList = response.Select(resource => new NamespaceAttributes(resource));
return resourceList;
}
public IEnumerable<NamespaceAttributes> ListAllNamespaces()
{
Rest.Azure.IPage<NamespaceResource> response = Client.Namespaces.ListBySubscription();
//var resourceList = response.Select(resource => new NamespaceAttributes(null, resource));
var resourceList = response.Select(resource => new NamespaceAttributes(resource));
return resourceList;
}
public NamespaceAttributes BeginCreateNamespace(string resourceGroupName, string namespaceName, string location, string skuName, Dictionary<string, string> tags)
{
NamespaceCreateOrUpdateParameters parameter = new NamespaceCreateOrUpdateParameters();
parameter.Location = location;
if (tags != null)
{
parameter.Tags = new Dictionary<string, string>(tags);
}
if (skuName != null)
{
parameter.Sku = new Sku
{
Name = skuName,
Tier = skuName
};
}
NamespaceResource response = Client.Namespaces.CreateOrUpdate(resourceGroupName, namespaceName, parameter);
return new NamespaceAttributes(response);
}
public NamespaceAttributes UpdateNamespace(string resourceGroupName, string namespaceName, string location, string skuName, int? skuCapacity, Dictionary<string, string> tags)
{
var parameter = new NamespaceCreateOrUpdateParameters()
{
Location = location
};
if (tags != null)
{
parameter.Tags = new Dictionary<string, string>(tags);
}
Sku tempSku = new Sku();
if (skuName != null)
{
tempSku.Name = skuName;
tempSku.Tier = skuName;
}
if (skuCapacity != null)
{
tempSku.Capacity = skuCapacity;
}
parameter.Sku = tempSku;
NamespaceResource response = Client.Namespaces.CreateOrUpdate(resourceGroupName, namespaceName, parameter);
return new NamespaceAttributes(response);
}
public void BeginDeleteNamespace(string resourceGroupName, string namespaceName)
{
Client.Namespaces.Delete(resourceGroupName, namespaceName);
}
//internal NamespaceLongRunningOperation GetLongRunningOperationStatus(NamespaceLongRunningOperation longRunningOperation)
//{
// var response = Client.Namespaces.l(longRunningOperation.OperationLink);
// RetryAfter(response, Client.LongRunningOperationRetryTimeout);
// var result = NamespaceLongRunningOperation.CreateLongRunningOperation(longRunningOperation.OperationName, response as NamespaceLongRunningResponse);
// return result;
//}
private static void RetryAfter(NamespaceLongRunningOperation longrunningResponse, int longRunningOperationInitialTimeout)
{
if (longRunningOperationInitialTimeout >= 0)
{
//longrunningResponse.RetryAfter = longRunningOperationInitialTimeout;
}
}
#endregion
#region NameSpace AuthorizationRules
public SharedAccessAuthorizationRuleAttributes GetNamespaceAuthorizationRules(string resourceGroupName, string namespaceName, string authRuleName)
{
SharedAccessAuthorizationRuleResource response = Client.Namespaces.GetAuthorizationRule(resourceGroupName, namespaceName, authRuleName);
return new SharedAccessAuthorizationRuleAttributes(response);
}
public IEnumerable<SharedAccessAuthorizationRuleAttributes> ListNamespaceAuthorizationRules(string resourceGroupName, string namespaceName)
{
Rest.Azure.IPage<SharedAccessAuthorizationRuleResource> response = Client.Namespaces.ListAuthorizationRules(resourceGroupName, namespaceName);
IEnumerable<SharedAccessAuthorizationRuleAttributes> resourceList = response.Select(resource => new SharedAccessAuthorizationRuleAttributes(resource));
return resourceList;
}
public SharedAccessAuthorizationRuleAttributes CreateOrUpdateNamespaceAuthorizationRules(string resourceGroupName, string namespaceName, string authorizationRuleName, SharedAccessAuthorizationRuleAttributes parameters)
{
var parameter1 = new SharedAccessAuthorizationRuleCreateOrUpdateParameters()
{
Name = parameters.Name,
Rights = parameters.Rights.ToList()
};
var response = Client.Namespaces.CreateOrUpdateAuthorizationRule(resourceGroupName, namespaceName, authorizationRuleName, parameter1);
return new SharedAccessAuthorizationRuleAttributes(response);
}
public bool DeleteNamespaceAuthorizationRules(string resourceGroupName, string namespaceName, string authRuleName)
{
if (string.Equals(SharedAccessAuthorizationRuleAttributes.DefaultNamespaceAuthorizationRule, authRuleName, StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
Client.Namespaces.DeleteAuthorizationRule(resourceGroupName, namespaceName, authRuleName);
return true;
}
public ResourceListKeys GetNamespaceListKeys(string resourceGroupName, string namespaceName, string authRuleName)
{
var listKeys = Client.Namespaces.ListKeys(resourceGroupName, namespaceName, authRuleName);
return listKeys;
}
public ResourceListKeys SetRegenerateKeys(string resourceGroupName, string namespaceName, string authRuleName, RegenerateKeysParameters regenerateKeys)
{
var regenerateKeyslistKeys = Client.Namespaces.RegenerateKeys(resourceGroupName, namespaceName, authRuleName, regenerateKeys);
return regenerateKeyslistKeys;
}
#endregion
#region Queues
public QueueAttributes CreateUpdateQueue(string resourceGroupName, string namespaceName, string queueName, QueueAttributes queue)
{
var parameters = new QueueCreateOrUpdateParameters()
{
Name = queue.Name,
Location = queue.Location,
LockDuration = queue.LockDuration,
AccessedAt = queue.AccessedAt,
AutoDeleteOnIdle = queue.AutoDeleteOnIdle,
EntityAvailabilityStatus = queue.EntityAvailabilityStatus,
CreatedAt = queue.CreatedAt,
DefaultMessageTimeToLive = queue.DefaultMessageTimeToLive,
DuplicateDetectionHistoryTimeWindow = queue.DuplicateDetectionHistoryTimeWindow,
EnableBatchedOperations = queue.EnableBatchedOperations,
DeadLetteringOnMessageExpiration = queue.DeadLetteringOnMessageExpiration,
EnableExpress = queue.EnableExpress,
EnablePartitioning = queue.EnablePartitioning,
IsAnonymousAccessible = queue.IsAnonymousAccessible,
MaxDeliveryCount = queue.MaxDeliveryCount,
MaxSizeInMegabytes = queue.MaxSizeInMegabytes,
MessageCount = queue.MessageCount,
CountDetails = queue.CountDetails,
RequiresDuplicateDetection = queue.RequiresDuplicateDetection,
RequiresSession = queue.RequiresSession,
SizeInBytes = queue.SizeInBytes,
Status = queue.Status,
SupportOrdering = queue.SupportOrdering,
UpdatedAt = queue.UpdatedAt,
};
var response = Client.Queues.CreateOrUpdate(resourceGroupName, namespaceName, queueName, parameters);
return new QueueAttributes(response);
}
public QueueResource GetQueue(string resourceGroupName, string namespaceName, string queueName)
{
QueueResource response = Client.Queues.Get(resourceGroupName, namespaceName, queueName);
return response;
}
public QueueAttributes UpdateQueue(string resourceGroupName, string namespaceName, string queueName, string location, bool enableExpress, bool isAnonymousAccessible)
{
QueueCreateOrUpdateParameters parameters = new QueueCreateOrUpdateParameters()
{
Location = location,
EnableExpress = enableExpress,
IsAnonymousAccessible = isAnonymousAccessible
};
var response = Client.Queues.CreateOrUpdate(resourceGroupName, namespaceName, queueName, parameters);
return new QueueAttributes(response);
}
public IEnumerable<QueueAttributes> ListQueues(string resourceGroupName, string namespaceName)
{
Rest.Azure.IPage<QueueResource> response = Client.Queues.ListAll(resourceGroupName, namespaceName);
//IEnumerable<NamespaceAttributes> resourceList = response.Select(resource => new NamespaceAttributes(resourceGroupName, resource));
IEnumerable<QueueAttributes> resourceList = response.Select(resource => new QueueAttributes(resource));
return resourceList;
}
public bool DeleteQueue(string resourceGroupName, string namespaceName, string queueName)
{
Client.Queues.Delete(resourceGroupName, namespaceName, queueName);
return true;
}
public SharedAccessAuthorizationRuleAttributes CreateOrUpdateServiceBusQueueAuthorizationRules(string resourceGroupName, string namespaceName, string queueName, string authorizationRuleName, SharedAccessAuthorizationRuleAttributes parameters)
{
var parameter1 = new SharedAccessAuthorizationRuleCreateOrUpdateParameters()
{
Name = parameters.Name,
Rights = parameters.Rights.ToList()
};
var response = Client.Queues.CreateOrUpdateAuthorizationRule(resourceGroupName, namespaceName, queueName, authorizationRuleName, parameter1);
return new SharedAccessAuthorizationRuleAttributes(response);
}
public SharedAccessAuthorizationRuleAttributes GetServiceBusQueueAuthorizationRules(string resourceGroupName, string namespaceName, string queueName, string authRuleName)
{
SharedAccessAuthorizationRuleResource response = Client.Queues.GetAuthorizationRule(resourceGroupName, namespaceName, queueName, authRuleName);
return new SharedAccessAuthorizationRuleAttributes(response);
}
public IEnumerable<SharedAccessAuthorizationRuleAttributes> ListServiceBusQueueAuthorizationRules(string resourceGroupName, string namespaceName, string queueName)
{
Rest.Azure.IPage<SharedAccessAuthorizationRuleResource> response = Client.Queues.ListAuthorizationRules(resourceGroupName, namespaceName,queueName);
IEnumerable<SharedAccessAuthorizationRuleAttributes> resourceList = response.Select(resource => new SharedAccessAuthorizationRuleAttributes(resource));
return resourceList;
}
public bool DeleteServiceBusQueueAuthorizationRules(string resourceGroupName, string namespaceName, string queueName, string authRuleName)
{
if (string.Equals(SharedAccessAuthorizationRuleAttributes.DefaultNamespaceAuthorizationRule, authRuleName, StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
Client.Queues.DeleteAuthorizationRule(resourceGroupName, namespaceName, queueName, authRuleName);
return true;
}
public ListKeysAttributes GetQueueKey(string resourceGroupName, string namespaceName, string queueName, string authRuleName)
{
var listKeys = Client.Topics.ListKeys(resourceGroupName, namespaceName, queueName, authRuleName);
return new ListKeysAttributes(listKeys);
}
public ListKeysAttributes NewQueueKey(string resourceGroupName, string namespaceName, string queueName, string authRuleName, string regenerateKeys)
{
ResourceListKeys regenerateKeyslistKeys;
if (regenerateKeys == "PrimaryKey")
regenerateKeyslistKeys = Client.Queues.RegenerateKeys(resourceGroupName, namespaceName, queueName, authRuleName, new RegenerateKeysParameters(Policykey.PrimaryKey));
else
regenerateKeyslistKeys = Client.Queues.RegenerateKeys(resourceGroupName, namespaceName, queueName, authRuleName, new RegenerateKeysParameters(Policykey.SecondaryKey));
return new ListKeysAttributes(regenerateKeyslistKeys);
}
#endregion Queues
#region Topics
public TopicAttributes CreateUpdateTopic(string resourceGroupName, string namespaceName, string topicName, TopicAttributes topic)
{
var parameters = new TopicCreateOrUpdateParameters()
{
AccessedAt = topic.AccessedAt,
AutoDeleteOnIdle = topic.AutoDeleteOnIdle,
EntityAvailabilityStatus = topic.EntityAvailabilityStatus,
CreatedAt = topic.CreatedAt,
CountDetails = topic.CountDetails,
DefaultMessageTimeToLive = topic.DefaultMessageTimeToLive,
DuplicateDetectionHistoryTimeWindow = topic.DuplicateDetectionHistoryTimeWindow,
EnableBatchedOperations = topic.EnableBatchedOperations,
EnableExpress = topic.EnableExpress,
EnablePartitioning = topic.EnablePartitioning,
EnableSubscriptionPartitioning = topic.EnableSubscriptionPartitioning,
FilteringMessagesBeforePublishing = topic.FilteringMessagesBeforePublishing,
IsAnonymousAccessible = topic.IsAnonymousAccessible,
IsExpress = topic.IsExpress,
MaxSizeInMegabytes = topic.MaxSizeInMegabytes,
RequiresDuplicateDetection = topic.RequiresDuplicateDetection,
SizeInBytes = topic.SizeInBytes,
Status = topic.Status,
SubscriptionCount = topic.SubscriptionCount,
SupportOrdering = topic.SupportOrdering,
UpdatedAt = topic.UpdatedAt,
Location = topic.Location
};
var response = Client.Topics.CreateOrUpdate(resourceGroupName, namespaceName, topicName, parameters);
return new TopicAttributes(response);
}
public TopicAttributes GetTopic(string resourceGroupName, string namespaceName, string topicName)
{
TopicResource response = Client.Topics.Get(resourceGroupName, namespaceName, topicName);
return new TopicAttributes(response);
}
public TopicAttributes UpdateTopic(string resourceGroupName, string namespaceName, string topicName, string location, bool enableExpress, bool isAnonymousAccessible)
{
TopicCreateOrUpdateParameters parameters = new TopicCreateOrUpdateParameters()
{
Location = location,
EnableExpress = enableExpress,
IsAnonymousAccessible = isAnonymousAccessible
};
var response = Client.Topics.CreateOrUpdate(resourceGroupName, namespaceName, topicName, parameters);
return new TopicAttributes(response);
}
public IEnumerable<TopicAttributes> ListTopics(string resourceGroupName, string namespaceName)
{
Rest.Azure.IPage<TopicResource> response = Client.Topics.ListAll(resourceGroupName, namespaceName);
//IEnumerable<NamespaceAttributes> resourceList = response.Select(resource => new NamespaceAttributes(resourceGroupName, resource));
IEnumerable<TopicAttributes> resourceList = response.Select(resource => new TopicAttributes(resource));
return resourceList;
}
public bool DeleteTopic(string resourceGroupName, string namespaceName, string topicName)
{
Client.Topics.Delete(resourceGroupName, namespaceName, topicName);
return true;
}
public SharedAccessAuthorizationRuleAttributes CreateOrUpdateServiceBusTopicAuthorizationRules(string resourceGroupName, string namespaceName, string topicName, string authorizationRuleName, SharedAccessAuthorizationRuleAttributes parameters)
{
var parameter1 = new SharedAccessAuthorizationRuleCreateOrUpdateParameters()
{
Name = parameters.Name,
Rights = parameters.Rights.ToList()
};
var response = Client.Topics.CreateOrUpdateAuthorizationRule(resourceGroupName, namespaceName, topicName, authorizationRuleName, parameter1);
return new SharedAccessAuthorizationRuleAttributes(response);
}
public SharedAccessAuthorizationRuleAttributes GetServiceBusTopicAuthorizationRules(string resourceGroupName, string namespaceName, string topicName, string authRuleName)
{
SharedAccessAuthorizationRuleResource response = Client.Topics.GetAuthorizationRule(resourceGroupName, namespaceName, topicName, authRuleName);
return new SharedAccessAuthorizationRuleAttributes(response);
}
public IEnumerable<SharedAccessAuthorizationRuleAttributes> ListServiceBusTopicAuthorizationRules(string resourceGroupName, string namespaceName, string topicName)
{
Rest.Azure.IPage<SharedAccessAuthorizationRuleResource> response = Client.Topics.ListAuthorizationRules(resourceGroupName, namespaceName, topicName);
IEnumerable<SharedAccessAuthorizationRuleAttributes> resourceList = response.Select(resource => new SharedAccessAuthorizationRuleAttributes(resource));
return resourceList;
}
public bool DeleteServiceBusTopicAuthorizationRule(string resourceGroupName, string namespaceName, string topicName, string authRuleName)
{
if (string.Equals(SharedAccessAuthorizationRuleAttributes.DefaultNamespaceAuthorizationRule, authRuleName, StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
Client.Topics.DeleteAuthorizationRule(resourceGroupName, namespaceName, topicName, authRuleName);
return true;
}
public ListKeysAttributes GetTopicKey(string resourceGroupName, string namespaceName, string topicName, string authRuleName)
{
var listKeys = Client.Topics.ListKeys(resourceGroupName, namespaceName, topicName, authRuleName);
return new ListKeysAttributes(listKeys);
}
public ListKeysAttributes NewTopicKey(string resourceGroupName, string namespaceName, string topicName, string authRuleName, string regenerateKeys)
{
ResourceListKeys regenerateKeyslistKeys;
if (regenerateKeys == "PrimaryKey")
regenerateKeyslistKeys = Client.Topics.RegenerateKeys(resourceGroupName, namespaceName, topicName, authRuleName, new RegenerateKeysParameters(Policykey.PrimaryKey));
else
regenerateKeyslistKeys = Client.Topics.RegenerateKeys(resourceGroupName, namespaceName, topicName, authRuleName, new RegenerateKeysParameters(Policykey.SecondaryKey));
return new ListKeysAttributes(regenerateKeyslistKeys);
}
#endregion Topics
#region Subscription
public SubscriptionAttributes CreateUpdateSubscription(string resourceGroupName, string namespaceName, string topicName, string subscriptionName, SubscriptionAttributes subscription)
{
var parameters = new SubscriptionCreateOrUpdateParameters()
{
AccessedAt = subscription.AccessedAt,
AutoDeleteOnIdle = subscription.AutoDeleteOnIdle,
CountDetails = subscription.CountDetails,
CreatedAt = subscription.CreatedAt,
DefaultMessageTimeToLive = subscription.DefaultMessageTimeToLive,
DeadLetteringOnFilterEvaluationExceptions = subscription.DeadLetteringOnFilterEvaluationExceptions,
DeadLetteringOnMessageExpiration = subscription.DeadLetteringOnMessageExpiration,
EnableBatchedOperations = subscription.EnableBatchedOperations,
EntityAvailabilityStatus = subscription.EntityAvailabilityStatus,
IsReadOnly = subscription.IsReadOnly,
LockDuration = subscription.LockDuration,
MaxDeliveryCount = subscription.MaxDeliveryCount,
MessageCount = subscription.MessageCount,
RequiresSession = subscription.RequiresSession,
Status = subscription.Status,
UpdatedAt = subscription.UpdatedAt,
Location = subscription.Location
};
var response = Client.Subscriptions.CreateOrUpdate(resourceGroupName, namespaceName, topicName, subscriptionName, parameters);
return new SubscriptionAttributes(response);
}
public SubscriptionAttributes GetSubscription(string resourceGroupName, string namespaceName, string topicName, string subscriptionName)
{
SubscriptionResource response = Client.Subscriptions.Get(resourceGroupName, namespaceName, topicName, subscriptionName);
return new SubscriptionAttributes(response);
}
public SubscriptionAttributes UpdateSubscription(string resourceGroupName, string namespaceName, string topicName, string subscriptionName, string location, bool enableExpress, bool isAnonymousAccessible)
{
SubscriptionCreateOrUpdateParameters parameters = new SubscriptionCreateOrUpdateParameters()
{
Location = location,
//EnableExpress = enableExpress,
//IsAnonymousAccessible = isAnonymousAccessible
};
var response = Client.Subscriptions.CreateOrUpdate(resourceGroupName, namespaceName, topicName, subscriptionName, parameters);
return new SubscriptionAttributes(response);
}
public IEnumerable<SubscriptionAttributes> ListSubscriptions(string resourceGroupName, string namespaceName, string topicName)
{
Rest.Azure.IPage<SubscriptionResource> response = Client.Subscriptions.ListAll(resourceGroupName, namespaceName,topicName);
//IEnumerable<NamespaceAttributes> resourceList = response.Select(resource => new NamespaceAttributes(resourceGroupName, resource));
IEnumerable<SubscriptionAttributes> resourceList = response.Select(resource => new SubscriptionAttributes(resource));
return resourceList;
}
public bool DeleteSubscription(string resourceGroupName, string namespaceName, string topicName, string subscriptionName)
{
Client.Subscriptions.Delete(resourceGroupName, namespaceName, topicName, subscriptionName);
return true;
}
#endregion Subscription
//#region EventHub
//public EventHubResource GetEventnHub(string resourceGroupName, string namespaceName, string eventHubName)
//{
// EventHubResource response = Client.ServiceBus.Get(resourceGroupName, namespaceName, eventHubName);
// return response;
//}
////public EventHubAttributes GetEventHubPNSCredentials(string resourceGroupName, string namespaceName, string eventHubName)
////{
//// IPage<EventHubResource> response = Client.ServiceBus.GetPnsCredentials(resourceGroupName, namespaceName, eventHubName);
//// return new EventHubAttributes(response);
////}
//public IEnumerable<EventHubAttributes> ListServiceBus(string resourceGroupName, string namespaceName)
//{
// Rest.Azure.IPage<EventHubResource> response = Client.ServiceBus.ListAll(resourceGroupName, namespaceName);
// IEnumerable<EventHubAttributes> resourceList = response.Select(resource => new EventHubAttributes(resource));
// return resourceList;
//}
//public EventHubAttributes CreateEventHub(string resourceGroupName, string namespaceName, string eventHubName, EventHubCreateOrUpdateParameters parameters)
//{
// var parameter = new EventHubCreateOrUpdateParameters()
// {
// //Location = location
// };
// var response = Client.ServiceBus.CreateOrUpdate(resourceGroupName, namespaceName, eventHubName, parameter);
// return new EventHubAttributes(response);
//}
//public EventHubAttributes UpdateEventHub(string resourceGroupName, string namespaceName, string eventHubName, EventHubCreateOrUpdateParameters parameters)
//{
// var parameter = new EventHubCreateOrUpdateParameters()
// {
// // Location = location,
// Name = eventHubName
// };
// var response = Client.ServiceBus.CreateOrUpdate(resourceGroupName, namespaceName, eventHubName, parameter);
// return new EventHubAttributes(response);
//}
//public bool DeleteEventHub(string resourceGroupName, string namespaceName, string eventHubName)
//{
// Client.ServiceBus.Delete(resourceGroupName, namespaceName, eventHubName);
// return true;
//}
//public SharedAccessAuthorizationRuleAttributes GetEventHubAuthorizationRules(string resourceGroupName, string namespaceName, string eventHubName, string authRuleName)
//{
// SharedAccessAuthorizationRuleResource response = Client.ServiceBus.GetAuthorizationRule(resourceGroupName, namespaceName,
// eventHubName, authRuleName);
// return new SharedAccessAuthorizationRuleAttributes(response);
//}
//public IEnumerable<SharedAccessAuthorizationRuleAttributes> ListEventHubAuthorizationRules(string resourceGroupName, string namespaceName,
// string eventHubName)
//{
// Rest.Azure.IPage<SharedAccessAuthorizationRuleResource> response = Client.ServiceBus.ListAuthorizationRules(resourceGroupName, namespaceName, eventHubName);
// IEnumerable<SharedAccessAuthorizationRuleAttributes> resourceList = response.Select(resource => new SharedAccessAuthorizationRuleAttributes(resource));
// return resourceList;
//}
//public SharedAccessAuthorizationRuleAttributes CreateOrUpdateEventHubAuthorizationRules(string resourceGroupName, string namespaceName, string eventHubName, string authorizationRuleName, SharedAccessAuthorizationRuleCreateOrUpdateParameters parameters)
//{
// var parameter = new SharedAccessAuthorizationRuleCreateOrUpdateParameters()
// {
// //Name = authRuleName,
// //Properties = new SharedAccessAuthorizationRuleProperties()
// //{
// // KeyName = authRuleName,
// // Rights = new List<AccessRights>(rights),
// // PrimaryKey = primarykey,
// // ClaimType = SharedAccessAuthorizationRuleAttributes.DefaultClaimType,
// // ClaimValue = SharedAccessAuthorizationRuleAttributes.DefaultClaimValue
// //}
// Rights = parameters.Rights
// };
// //parameter.Properties.SecondaryKey = string.IsNullOrEmpty(secondaryKey) ? GenerateRandomKey() : secondaryKey;
// var response = Client.ServiceBus.CreateOrUpdateAuthorizationRule(resourceGroupName, namespaceName, eventHubName, authorizationRuleName, parameters);
// return new SharedAccessAuthorizationRuleAttributes(response);
//}
//public bool DeleteEventHubAuthorizationRules(string resourceGroupName, string namespaceName, string eventHubName, string authRuleName)
//{
// if (string.Equals(SharedAccessAuthorizationRuleAttributes.DefaultNamespaceAuthorizationRule, authRuleName, StringComparison.InvariantCultureIgnoreCase))
// {
// return false;
// }
// //var response = Client.ServiceBus.DeleteAuthorizationRule(resourceGroupName, namespaceName, eventHubName, authRuleName);
// return true;
//}
//public ResourceListKeys GetEventHubListKeys(string resourceGroupName, string namespaceName, string eventHubName, string authRuleName)
//{
// ResourceListKeys listKeys = Client.ServiceBus.ListKeys(resourceGroupName, namespaceName,
// eventHubName, authRuleName);
// return listKeys;
//}
//#endregion
public static string GenerateRandomKey()
{
byte[] key256 = new byte[32];
using (var rngCryptoServiceProvider = new RNGCryptoServiceProvider())
{
rngCryptoServiceProvider.GetBytes(key256);
}
return Convert.ToBase64String(key256);
}
}
}