Skip to content

Commit

Permalink
Move Partition retrieval functions to separate class (#827)
Browse files Browse the repository at this point in the history
Co-authored-by: supereddie <>
  • Loading branch information
supereddie authored Feb 5, 2025
1 parent 416f0a8 commit 9dcf317
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 68 deletions.
83 changes: 15 additions & 68 deletions src/Common/Helpers/ServiceBusHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ public class ServiceBusHelper
private const string ServiceBusIssuerSecretArgumentCannotBeNull = "The issuerSecret argument cannot be null.";
private const string QueueDescriptionCannotBeNull = "The queue description argument cannot be null.";
private const string SubscriptionDescriptionCannotBeNull = "The subscription description argument cannot be null.";
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 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.";
Expand Down Expand Up @@ -177,6 +174,7 @@ public class ServiceBusHelper
private IServiceBusNotificationHub serviceBusNotificationHub;
private IServiceBusEventHub serviceBusEventHub;
private IServiceBusConsumerGroup serviceBusConsumerGroup;
private IServiceBusPartition serviceBusPartition;
#endregion

#region Private Static Fields
Expand Down Expand Up @@ -244,6 +242,7 @@ public ServiceBusHelper(WriteToLogDelegate writeToLog, ServiceBusHelper serviceB
serviceBusNotificationHub = serviceBusHelper.serviceBusNotificationHub;
serviceBusEventHub = serviceBusHelper.serviceBusEventHub;
serviceBusConsumerGroup = serviceBusHelper.serviceBusConsumerGroup;
serviceBusPartition = serviceBusHelper.serviceBusPartition;
}
#endregion

Expand Down Expand Up @@ -368,6 +367,11 @@ public string Scheme
{
serviceBusConsumerGroup.Scheme = scheme;
}

if (serviceBusPartition != null)
{
serviceBusPartition.Scheme = scheme;
}
}
}
}
Expand Down Expand Up @@ -807,6 +811,7 @@ public bool Connect(ServiceBusNamespace serviceBusNamespace)
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));
serviceBusPartition = CreateServiceBusEntity(static (sbn, nsmgr) => new ServiceBusPartition(sbn, nsmgr));

WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ServiceBusIsConnected, namespaceManager.Address.AbsoluteUri));
namespaceUri = namespaceManager.Address;
Expand Down Expand Up @@ -975,15 +980,7 @@ public Uri GetEventHubUri(string eventHubPath)
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public PartitionDescription GetPartition(PartitionDescription partitionDescription)
{
if (partitionDescription == null)
{
throw new ArgumentException(PartitionDescriptionCannotBeNull);
}
if (namespaceManager != null)
{
return RetryHelper.RetryFunc(() => namespaceManager.GetEventHubPartition(partitionDescription.EventHubPath, partitionDescription.ConsumerGroupName, partitionDescription.PartitionId), writeToLog);
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusPartition.GetPartition(partitionDescription);
}

/// <summary>
Expand All @@ -995,15 +992,7 @@ public PartitionDescription GetPartition(PartitionDescription partitionDescripti
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public PartitionDescription GetPartition(string path, string consumerGroupName, string name)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException(PathCannotBeNull);
}
if (namespaceManager != null)
{
return RetryHelper.RetryFunc(() => namespaceManager.GetEventHubPartition(path, consumerGroupName, name), writeToLog);
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusPartition.GetPartition(path, consumerGroupName, name);
}

/// <summary>
Expand All @@ -1013,16 +1002,7 @@ public PartitionDescription GetPartition(string path, string consumerGroupName,
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public IEnumerable<PartitionDescription> GetPartitions(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException(PathCannotBeNull);
}
if (namespaceManager == null)
{
throw new ApplicationException(ServiceBusIsDisconnected);
}
var description = namespaceManager.GetEventHub(path);
return description.PartitionIds.Select((t, i) => i).Select(index => RetryHelper.RetryFunc(() => namespaceManager.GetEventHubPartition(description.Path, description.PartitionIds[index]), writeToLog)).ToList();
return serviceBusPartition.GetPartitions(path);
}

/// <summary>
Expand All @@ -1032,15 +1012,7 @@ public IEnumerable<PartitionDescription> GetPartitions(string path)
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public IEnumerable<PartitionDescription> GetPartitions(EventHubDescription description)
{
if (description == null)
{
throw new ArgumentException(EventHubDescriptionCannotBeNull);
}
if (namespaceManager != null)
{
return description.PartitionIds.Select((t, i) => i).Select(index => RetryHelper.RetryFunc(() => namespaceManager.GetEventHubPartition(description.Path, description.PartitionIds[index]), writeToLog)).ToList();
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusPartition.GetPartitions(description);
}

/// <summary>
Expand All @@ -1051,19 +1023,7 @@ public IEnumerable<PartitionDescription> GetPartitions(EventHubDescription descr
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public IEnumerable<PartitionDescription> GetPartitions(EventHubDescription description, string consumerGroupName)
{
if (description == null)
{
throw new ArgumentException(EventHubDescriptionCannotBeNull);
}
if (string.IsNullOrWhiteSpace(consumerGroupName))
{
throw new ArgumentException(ConsumerGroupCannotBeNull);
}
if (namespaceManager != null)
{
return description.PartitionIds.Select((t, i) => i).Select(index => RetryHelper.RetryFunc(() => namespaceManager.GetEventHubPartition(description.Path, consumerGroupName, description.PartitionIds[index]), writeToLog)).ToList();
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusPartition.GetPartitions(description, consumerGroupName);
}

/// <summary>
Expand All @@ -1074,20 +1034,7 @@ public IEnumerable<PartitionDescription> GetPartitions(EventHubDescription descr
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public IEnumerable<PartitionDescription> GetPartitions(string path, string consumerGroupName)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException(PathCannotBeNull);
}
if (string.IsNullOrWhiteSpace(consumerGroupName))
{
throw new ArgumentException(ConsumerGroupCannotBeNull);
}
if (namespaceManager != null)
{
var description = namespaceManager.GetEventHub(path);
return description.PartitionIds.Select((t, i) => i).Select(index => RetryHelper.RetryFunc(() => namespaceManager.GetEventHubPartition(description.Path, consumerGroupName, description.PartitionIds[index]), writeToLog)).ToList();
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusPartition.GetPartitions(path, consumerGroupName);
}

/// <summary>
Expand All @@ -1099,7 +1046,7 @@ public IEnumerable<PartitionDescription> GetPartitions(string path, string consu
/// <returns>The absolute uri of the partition.</returns>
public Uri GetPartitionUri(string eventHubName, string consumerGroupName, string partitionId)
{
return Microsoft.ServiceBus.ServiceBusEnvironment.CreateServiceUri(scheme, Namespace, string.Concat(ServicePath, eventHubName, "/", consumerGroupName, "/", partitionId));
return serviceBusPartition.GetPartitionUri(eventHubName, consumerGroupName, partitionId);
}

/// <summary>
Expand Down
23 changes: 23 additions & 0 deletions src/Common/WindowsAzure/IServiceBusPartition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using Microsoft.ServiceBus.Messaging;

namespace ServiceBusExplorer.WindowsAzure
{
internal interface IServiceBusPartition : IServiceBusEntity
{
PartitionDescription GetPartition(PartitionDescription partitionDescription);

PartitionDescription GetPartition(string path, string consumerGroupName, string name);

IEnumerable<PartitionDescription> GetPartitions(EventHubDescription description);

IEnumerable<PartitionDescription> GetPartitions(EventHubDescription description, string consumerGroupName);

IEnumerable<PartitionDescription> GetPartitions(string path);

IEnumerable<PartitionDescription> GetPartitions(string path, string consumerGroupName);

Uri GetPartitionUri(string eventHubName, string consumerGroupName, string partitionId);
}
}
159 changes: 159 additions & 0 deletions src/Common/WindowsAzure/ServiceBusPartition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using ServiceBusExplorer.Enums;
using ServiceBusExplorer.Helpers;

namespace ServiceBusExplorer.WindowsAzure
{
internal class ServiceBusPartition : ServiceBusEntity, IServiceBusPartition
{
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 readonly string servicePath = string.Empty;

public ServiceBusPartition(ServiceBusNamespace serviceBusNamespace, NamespaceManager namespaceManager) : base(serviceBusNamespace, namespaceManager)
{
}

protected override EntityType EntityType => EntityType.All; // Partitions are not included in the EntityType enum

/// <summary>
/// Retrieves a partition.
/// </summary>
/// <param name="partitionDescription">A PartitionDescription object.</param>
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public PartitionDescription GetPartition(PartitionDescription partitionDescription)
{
if (partitionDescription == null)
{
throw new ArgumentException(PartitionDescriptionCannotBeNull);
}
if (NamespaceManager != null)
{
return RetryHelper.RetryFunc(() => NamespaceManager.GetEventHubPartition(partitionDescription.EventHubPath, partitionDescription.ConsumerGroupName, partitionDescription.PartitionId), WriteToLog);
}
throw new ApplicationException(ServiceBusIsDisconnected);
}

/// <summary>
/// Retrieves a partition.
/// </summary>
/// <param name="path">Path of the event hub relative to the service namespace base address.</param>
/// <param name="consumerGroupName">The consumer group name.</param>
/// <param name="name">Partition name.</param>
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public PartitionDescription GetPartition(string path, string consumerGroupName, string name)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException(PathCannotBeNull);
}
if (NamespaceManager != null)
{
return RetryHelper.RetryFunc(() => NamespaceManager.GetEventHubPartition(path, consumerGroupName, name), WriteToLog);
}
throw new ApplicationException(ServiceBusIsDisconnected);
}

/// <summary>
/// Retrieves the collection of partitions of the event hub passed as a parameter.
/// </summary>
/// <param name="path">Path of the event hub relative to the service namespace base address.</param>
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public IEnumerable<PartitionDescription> GetPartitions(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException(PathCannotBeNull);
}
if (NamespaceManager == null)
{
throw new ApplicationException(ServiceBusIsDisconnected);
}
var description = NamespaceManager.GetEventHub(path);
return description.PartitionIds.Select((t, i) => i).Select(index => RetryHelper.RetryFunc(() => NamespaceManager.GetEventHubPartition(description.Path, description.PartitionIds[index]), WriteToLog)).ToList();
}

/// <summary>
/// Retrieves the collection of partitions of the event hub passed as a parameter.
/// </summary>
/// <param name="description">A event hub belonging to the current service namespace base.</param>
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public IEnumerable<PartitionDescription> GetPartitions(EventHubDescription description)
{
if (description == null)
{
throw new ArgumentException(EventHubDescriptionCannotBeNull);
}
if (NamespaceManager != null)
{
return description.PartitionIds.Select((t, i) => i).Select(index => RetryHelper.RetryFunc(() => NamespaceManager.GetEventHubPartition(description.Path, description.PartitionIds[index]), WriteToLog)).ToList();
}
throw new ApplicationException(ServiceBusIsDisconnected);
}

/// <summary>
/// Retrieves the collection of partitions of the event hub passed as a parameter.
/// </summary>
/// <param name="description">A event hub belonging to the current service namespace base.</param>
/// <param name="consumerGroupName">The consumer group name.</param>
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public IEnumerable<PartitionDescription> GetPartitions(EventHubDescription description, string consumerGroupName)
{
if (description == null)
{
throw new ArgumentException(EventHubDescriptionCannotBeNull);
}
if (string.IsNullOrWhiteSpace(consumerGroupName))
{
throw new ArgumentException(ConsumerGroupCannotBeNull);
}
if (NamespaceManager != null)
{
return description.PartitionIds.Select((t, i) => i).Select(index => RetryHelper.RetryFunc(() => NamespaceManager.GetEventHubPartition(description.Path, consumerGroupName, description.PartitionIds[index]), WriteToLog)).ToList();
}
throw new ApplicationException(ServiceBusIsDisconnected);
}

/// <summary>
/// Retrieves the collection of partitions of the event hub passed as a parameter.
/// </summary>
/// <param name="path">Path of the event hub relative to the service namespace base address.</param>
/// <param name="consumerGroupName">The consumer group name.</param>
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of partitions attached to the event hub passed as a parameter.</returns>
public IEnumerable<PartitionDescription> GetPartitions(string path, string consumerGroupName)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException(PathCannotBeNull);
}
if (string.IsNullOrWhiteSpace(consumerGroupName))
{
throw new ArgumentException(ConsumerGroupCannotBeNull);
}
if (NamespaceManager != null)
{
var description = NamespaceManager.GetEventHub(path);
return description.PartitionIds.Select((t, i) => i).Select(index => RetryHelper.RetryFunc(() => NamespaceManager.GetEventHubPartition(description.Path, consumerGroupName, description.PartitionIds[index]), WriteToLog)).ToList();
}
throw new ApplicationException(ServiceBusIsDisconnected);
}

/// <summary>
/// Gets the uri of a partition.
/// </summary>
/// <param name="eventHubName">Name of the event hub.</param>
/// <param name="consumerGroupName">Name of the partition.</param>
/// <param name="partitionId">The partition id.</param>
/// <returns>The absolute uri of the partition.</returns>
public Uri GetPartitionUri(string eventHubName, string consumerGroupName, string partitionId)
{
return Microsoft.ServiceBus.ServiceBusEnvironment.CreateServiceUri(Scheme, Namespace, string.Concat(servicePath, eventHubName, "/", consumerGroupName, "/", partitionId));
}
}
}

0 comments on commit 9dcf317

Please sign in to comment.