diff --git a/src/Common/Helpers/ServiceBusHelper.cs b/src/Common/Helpers/ServiceBusHelper.cs index 3fb1ccc8..fd134bca 100644 --- a/src/Common/Helpers/ServiceBusHelper.cs +++ b/src/Common/Helpers/ServiceBusHelper.cs @@ -97,15 +97,11 @@ public class ServiceBusHelper private const string EventHubDescriptionCannotBeNull = "The event hub description argument cannot be null."; private const string ConsumerGroupCannotBeNull = "The consumerGroup argument cannot be null or empty."; private const string PartitionDescriptionCannotBeNull = "The partition description argument cannot be null."; - private const string ConsumerGroupDescriptionCannotBeNull = "The consumer group description argument cannot be null."; private const string PathCannotBeNull = "The path argument cannot be null or empty."; private const string NameCannotBeNull = "The name argument cannot be null or empty."; private const string DescriptionCannotBeNull = "The description argument cannot be null."; private const string ServiceBusIsDisconnected = "The application is now disconnected from any service bus namespace."; private const string ServiceBusIsConnected = "The application is now connected to the {0} service bus namespace."; - private const string ConsumerGroupCreated = "The consumer group {0} has been successfully created."; - private const string ConsumerGroupDeleted = "The consumer group {0} has been successfully deleted."; - private const string ConsumerGroupUpdated = "The consumer group {0} has been successfully updated."; private const string WarningHeader = "The following validations failed:"; private const string WarningFormat = "\n\r - {0}"; private const string PropertyConversionError = "{0} property conversion error: {1}"; @@ -180,6 +176,7 @@ public class ServiceBusHelper private IServiceBusRelay serviceBusRelay; private IServiceBusNotificationHub serviceBusNotificationHub; private IServiceBusEventHub serviceBusEventHub; + private IServiceBusConsumerGroup serviceBusConsumerGroup; #endregion #region Private Static Fields @@ -246,6 +243,7 @@ public ServiceBusHelper(WriteToLogDelegate writeToLog, ServiceBusHelper serviceB serviceBusRelay = serviceBusHelper.serviceBusRelay; serviceBusNotificationHub = serviceBusHelper.serviceBusNotificationHub; serviceBusEventHub = serviceBusHelper.serviceBusEventHub; + serviceBusConsumerGroup = serviceBusHelper.serviceBusConsumerGroup; } #endregion @@ -365,6 +363,11 @@ public string Scheme { serviceBusEventHub.Scheme = scheme; } + + if (serviceBusConsumerGroup != null) + { + serviceBusConsumerGroup.Scheme = scheme; + } } } } @@ -803,6 +806,7 @@ public bool Connect(ServiceBusNamespace serviceBusNamespace) serviceBusRelay = CreateServiceBusEntity(static (sbn, nsmgr) => new ServiceBusRelay(sbn, nsmgr)); serviceBusNotificationHub = CreateServiceBusEntity((sbn, nsmgr) => new ServiceBusNotificationHub(sbn, nsmgr, notificationHubNamespaceManager)); serviceBusEventHub = CreateServiceBusEntity(static (sbn, nsmgr) => new ServiceBusEventHub(sbn, nsmgr)); + serviceBusConsumerGroup = CreateServiceBusEntity(static (sbn, nsmgr) => new ServiceBusConsumerGroup(sbn, nsmgr)); WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ServiceBusIsConnected, namespaceManager.Address.AbsoluteUri)); namespaceUri = namespaceManager.Address; @@ -1106,19 +1110,7 @@ public Uri GetPartitionUri(string eventHubName, string consumerGroupName, string /// Returns an IEnumerable collection of consumer groups attached to the event hub passed as a parameter. public ConsumerGroupDescription GetConsumerGroup(string eventHubPath, string name) { - if (string.IsNullOrWhiteSpace(eventHubPath)) - { - throw new ArgumentException(PathCannotBeNull); - } - if (string.IsNullOrWhiteSpace(name)) - { - throw new ArgumentException(NameCannotBeNull); - } - if (namespaceManager != null) - { - return RetryHelper.RetryFunc(() => namespaceManager.GetConsumerGroup(eventHubPath, name), writeToLog); - } - throw new ApplicationException(ServiceBusIsDisconnected); + return serviceBusConsumerGroup.GetConsumerGroup(eventHubPath, name); } /// @@ -1128,15 +1120,7 @@ public ConsumerGroupDescription GetConsumerGroup(string eventHubPath, string nam /// Returns an IEnumerable collection of consumer groups attached to the event hub passed as a parameter. public IEnumerable GetConsumerGroups(string path) { - if (string.IsNullOrWhiteSpace(path)) - { - throw new ArgumentException(PathCannotBeNull); - } - if (namespaceManager != null) - { - return RetryHelper.RetryFunc(() => namespaceManager.GetConsumerGroups(path), writeToLog); - } - throw new ApplicationException(ServiceBusIsDisconnected); + return serviceBusConsumerGroup.GetConsumerGroups(path); } /// @@ -1146,15 +1130,7 @@ public IEnumerable GetConsumerGroups(string path) /// Returns an IEnumerable collection of consumer groups attached to the event hub passed as a parameter. public IEnumerable GetConsumerGroups(EventHubDescription description) { - if (description == null) - { - throw new ArgumentException(EventHubDescriptionCannotBeNull); - } - if (namespaceManager != null) - { - return RetryHelper.RetryFunc(() => namespaceManager.GetConsumerGroups(description.Path), writeToLog); - } - throw new ApplicationException(ServiceBusIsDisconnected); + return serviceBusConsumerGroup.GetConsumerGroups(description); } /// @@ -1164,18 +1140,7 @@ public IEnumerable GetConsumerGroups(EventHubDescripti /// Returns a newly-created ConsumerGroupDescription object. public ConsumerGroupDescription CreateConsumerGroup(ConsumerGroupDescription description) { - if (description == null) - { - throw new ArgumentException(DescriptionCannotBeNull); - } - if (namespaceManager != null) - { - var consumerGroup = RetryHelper.RetryFunc(() => namespaceManager.CreateConsumerGroup(description), writeToLog); - WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ConsumerGroupCreated, description.Name)); - OnCreate?.Invoke(new ServiceBusHelperEventArgs(consumerGroup, EntityType.ConsumerGroup)); - return consumerGroup; - } - throw new ApplicationException(ServiceBusIsDisconnected); + return serviceBusConsumerGroup.CreateConsumerGroup(description); } /// @@ -1185,20 +1150,7 @@ public ConsumerGroupDescription CreateConsumerGroup(ConsumerGroupDescription des /// Name of the consumer group. public void DeleteConsumerGroup(string eventHubName, string name) { - if (string.IsNullOrWhiteSpace(name)) - { - throw new ArgumentException(PathCannotBeNull); - } - if (namespaceManager != null) - { - RetryHelper.RetryAction(() => namespaceManager.DeleteConsumerGroup(eventHubName, name), writeToLog); - WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ConsumerGroupDeleted, name)); - OnDelete?.Invoke(new ServiceBusHelperEventArgs(new ConsumerGroupDescription(eventHubName, name), EntityType.ConsumerGroup)); - } - else - { - throw new ApplicationException(ServiceBusIsDisconnected); - } + serviceBusConsumerGroup.DeleteConsumerGroup(eventHubName, name); } /// @@ -1207,20 +1159,7 @@ public void DeleteConsumerGroup(string eventHubName, string name) /// The consumer group to delete. public void DeleteConsumerGroup(ConsumerGroupDescription consumerGroupDescription) { - if (string.IsNullOrWhiteSpace(consumerGroupDescription?.Name)) - { - throw new ArgumentException(ConsumerGroupDescriptionCannotBeNull); - } - if (namespaceManager != null) - { - RetryHelper.RetryAction(() => namespaceManager.DeleteConsumerGroup(consumerGroupDescription.EventHubPath, consumerGroupDescription.Name), writeToLog); - WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ConsumerGroupDeleted, consumerGroupDescription.Name)); - OnDelete?.Invoke(new ServiceBusHelperEventArgs(consumerGroupDescription, EntityType.ConsumerGroup)); - } - else - { - throw new ApplicationException(ServiceBusIsDisconnected); - } + serviceBusConsumerGroup.DeleteConsumerGroup(consumerGroupDescription); } /// @@ -1230,14 +1169,7 @@ public void DeleteConsumerGroup(ConsumerGroupDescription consumerGroupDescriptio /// public void DeleteConsumerGroups(string eventHubName, IEnumerable consumerGroups) { - if (consumerGroups == null) - { - return; - } - foreach (var consumerGroup in consumerGroups) - { - DeleteConsumerGroup(eventHubName, consumerGroup); - } + serviceBusConsumerGroup.DeleteConsumerGroups(eventHubName, consumerGroups); } /// @@ -1247,18 +1179,7 @@ public void DeleteConsumerGroups(string eventHubName, IEnumerable consum /// Returns an updated ConsumerGroupDescription object. public ConsumerGroupDescription UpdateConsumerGroup(ConsumerGroupDescription description) { - if (description == null) - { - throw new ArgumentException(DescriptionCannotBeNull); - } - if (namespaceManager != null) - { - var consumerGroup = RetryHelper.RetryFunc(() => namespaceManager.UpdateConsumerGroup(description), writeToLog); - WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ConsumerGroupUpdated, description.Name)); - OnCreate?.Invoke(new ServiceBusHelperEventArgs(consumerGroup, EntityType.ConsumerGroup)); - return consumerGroup; - } - throw new ApplicationException(ServiceBusIsDisconnected); + return serviceBusConsumerGroup.UpdateConsumerGroup(description); } /// @@ -1269,7 +1190,7 @@ public ConsumerGroupDescription UpdateConsumerGroup(ConsumerGroupDescription des /// The absolute uri of the consumer group. public Uri GetConsumerGroupUri(string eventHubName, string consumerGroupPath) { - return Microsoft.ServiceBus.ServiceBusEnvironment.CreateServiceUri(scheme, Namespace, string.Concat(ServicePath, eventHubName, "/", consumerGroupPath)); + return serviceBusConsumerGroup.GetConsumerGroupUri(eventHubName, consumerGroupPath); } /// diff --git a/src/Common/WindowsAzure/IServiceBusConsumerGroup.cs b/src/Common/WindowsAzure/IServiceBusConsumerGroup.cs new file mode 100644 index 00000000..9a29ceb2 --- /dev/null +++ b/src/Common/WindowsAzure/IServiceBusConsumerGroup.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using Microsoft.ServiceBus.Messaging; + +namespace ServiceBusExplorer.WindowsAzure +{ + internal interface IServiceBusConsumerGroup : IServiceBusEntity + { + ConsumerGroupDescription CreateConsumerGroup(ConsumerGroupDescription description); + + void DeleteConsumerGroup(ConsumerGroupDescription consumerGroupDescription); + + void DeleteConsumerGroup(string eventHubName, string name); + + void DeleteConsumerGroups(string eventHubName, IEnumerable consumerGroups); + + ConsumerGroupDescription GetConsumerGroup(string eventHubPath, string name); + + IEnumerable GetConsumerGroups(EventHubDescription description); + + IEnumerable GetConsumerGroups(string path); + + Uri GetConsumerGroupUri(string eventHubName, string consumerGroupPath); + + ConsumerGroupDescription UpdateConsumerGroup(ConsumerGroupDescription description); + } +} diff --git a/src/Common/WindowsAzure/ServiceBusConsumerGroup.cs b/src/Common/WindowsAzure/ServiceBusConsumerGroup.cs new file mode 100644 index 00000000..7613a14b --- /dev/null +++ b/src/Common/WindowsAzure/ServiceBusConsumerGroup.cs @@ -0,0 +1,204 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.ServiceBus; +using Microsoft.ServiceBus.Messaging; +using ServiceBusExplorer.Enums; +using ServiceBusExplorer.Helpers; + +namespace ServiceBusExplorer.WindowsAzure +{ + internal class ServiceBusConsumerGroup : ServiceBusEntity, IServiceBusConsumerGroup + { + private const string EventHubDescriptionCannotBeNull = "The event hub description argument cannot be null."; + private const string ConsumerGroupDescriptionCannotBeNull = "The consumer group description argument cannot be null."; + private const string ConsumerGroupCreated = "The consumer group {0} has been successfully created."; + private const string ConsumerGroupDeleted = "The consumer group {0} has been successfully deleted."; + private const string ConsumerGroupUpdated = "The consumer group {0} has been successfully updated."; + + private readonly string servicePath = string.Empty; + + public ServiceBusConsumerGroup(ServiceBusNamespace serviceBusNamespace, NamespaceManager namespaceManager) : base(serviceBusNamespace, namespaceManager) + { + } + + protected override EntityType EntityType => EntityType.ConsumerGroup; + + /// + /// Retrieves the collection of consumer groups of the event hub passed as a parameter. + /// + /// The path of a event hub. + /// The name of a consumer group. + /// Returns an IEnumerable collection of consumer groups attached to the event hub passed as a parameter. + public ConsumerGroupDescription GetConsumerGroup(string eventHubPath, string name) + { + if (string.IsNullOrWhiteSpace(eventHubPath)) + { + throw new ArgumentException(PathCannotBeNull); + } + if (string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentException(NameCannotBeNull); + } + if (NamespaceManager != null) + { + return RetryHelper.RetryFunc(() => NamespaceManager.GetConsumerGroup(eventHubPath, name), WriteToLog); + } + throw new ApplicationException(ServiceBusIsDisconnected); + } + + /// + /// Retrieves the collection of consumer groups of the event hub passed as a parameter. + /// + /// Path of the event hub relative to the service namespace base address. + /// Returns an IEnumerable collection of consumer groups attached to the event hub passed as a parameter. + public IEnumerable GetConsumerGroups(string path) + { + if (string.IsNullOrWhiteSpace(path)) + { + throw new ArgumentException(PathCannotBeNull); + } + if (NamespaceManager != null) + { + return RetryHelper.RetryFunc(() => NamespaceManager.GetConsumerGroups(path), WriteToLog); + } + throw new ApplicationException(ServiceBusIsDisconnected); + } + + /// + /// Retrieves the collection of consumer groups of the event hub passed as a parameter. + /// + /// A event hub belonging to the current service namespace base. + /// Returns an IEnumerable collection of consumer groups attached to the event hub passed as a parameter. + public IEnumerable GetConsumerGroups(EventHubDescription description) + { + if (description == null) + { + throw new ArgumentException(EventHubDescriptionCannotBeNull); + } + if (NamespaceManager != null) + { + return RetryHelper.RetryFunc(() => NamespaceManager.GetConsumerGroups(description.Path), WriteToLog); + } + throw new ApplicationException(ServiceBusIsDisconnected); + } + + /// + /// Creates a new consumer group in the service namespace with the given name. + /// + /// A ConsumerGroupDescription object describing the attributes with which the new consumer group will be created. + /// Returns a newly-created ConsumerGroupDescription object. + public ConsumerGroupDescription CreateConsumerGroup(ConsumerGroupDescription description) + { + if (description == null) + { + throw new ArgumentException(DescriptionCannotBeNull); + } + if (NamespaceManager != null) + { + var consumerGroup = RetryHelper.RetryFunc(() => NamespaceManager.CreateConsumerGroup(description), WriteToLog); + Log(string.Format(CultureInfo.CurrentCulture, ConsumerGroupCreated, description.Name)); + OnCreated(consumerGroup); + return consumerGroup; + } + throw new ApplicationException(ServiceBusIsDisconnected); + } + + /// + /// Deletes the consumer group described by the relative name of the service namespace base address. + /// + /// Name of the event hub. + /// Name of the consumer group. + public void DeleteConsumerGroup(string eventHubName, string name) + { + if (string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentException(PathCannotBeNull); + } + if (NamespaceManager != null) + { + RetryHelper.RetryAction(() => NamespaceManager.DeleteConsumerGroup(eventHubName, name), WriteToLog); + Log(string.Format(CultureInfo.CurrentCulture, ConsumerGroupDeleted, name)); + OnDeleted(new ConsumerGroupDescription(eventHubName, name)); + } + else + { + throw new ApplicationException(ServiceBusIsDisconnected); + } + } + + /// + /// Deletes the consumer group described by the relative name of the service namespace base address. + /// + /// The consumer group to delete. + public void DeleteConsumerGroup(ConsumerGroupDescription consumerGroupDescription) + { + if (string.IsNullOrWhiteSpace(consumerGroupDescription?.Name)) + { + throw new ArgumentException(ConsumerGroupDescriptionCannotBeNull); + } + if (NamespaceManager != null) + { + RetryHelper.RetryAction(() => NamespaceManager.DeleteConsumerGroup(consumerGroupDescription.EventHubPath, consumerGroupDescription.Name), WriteToLog); + Log(string.Format(CultureInfo.CurrentCulture, ConsumerGroupDeleted, consumerGroupDescription.Name)); + OnDeleted(consumerGroupDescription); + } + else + { + throw new ApplicationException(ServiceBusIsDisconnected); + } + } + + /// + /// Deletes all the consumer groups in the list. + /// Name of the event hub. + /// A list of consumer groups to delete. + /// + public void DeleteConsumerGroups(string eventHubName, IEnumerable consumerGroups) + { + if (consumerGroups == null) + { + return; + } + foreach (var consumerGroup in consumerGroups) + { + DeleteConsumerGroup(eventHubName, consumerGroup); + } + } + + /// + /// Updates a consumer group in the service namespace with the given name. + /// + /// A ConsumerGroupDescription object describing the attributes with which the new consumer group will be updated. + /// Returns an updated ConsumerGroupDescription object. + public ConsumerGroupDescription UpdateConsumerGroup(ConsumerGroupDescription description) + { + if (description == null) + { + throw new ArgumentException(DescriptionCannotBeNull); + } + if (NamespaceManager != null) + { + var consumerGroup = RetryHelper.RetryFunc(() => NamespaceManager.UpdateConsumerGroup(description), WriteToLog); + Log(string.Format(CultureInfo.CurrentCulture, ConsumerGroupUpdated, description.Name)); + OnCreated(consumerGroup); + return consumerGroup; + } + throw new ApplicationException(ServiceBusIsDisconnected); + } + + /// + /// Gets the uri of a consumer group. + /// + /// Name of the event hub. + /// The name of a consumer group. + /// The absolute uri of the consumer group. + public Uri GetConsumerGroupUri(string eventHubName, string consumerGroupPath) + { + return Microsoft.ServiceBus.ServiceBusEnvironment.CreateServiceUri(Scheme, Namespace, string.Concat(servicePath, eventHubName, "/", consumerGroupPath)); + } + } +}