Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to create a Service Bus Topic via Topic Manager #134

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading;
using System.Threading.Tasks;

namespace KnightBus.Azure.ServiceBus.Management;

public interface IServiceBusTopicCreator
{
Task<bool> CreateIfNotExists(string path, CancellationToken ct);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ namespace KnightBus.Azure.ServiceBus.Management;

public static class ServiceBusExtensions
{
internal static QueueProperties ToQueueProperties(this QueueRuntimeProperties properties, IQueueManager manager)
internal static QueueProperties ToQueueProperties(
this QueueRuntimeProperties properties,
IQueueManager manager
)
{
return new QueueProperties(properties.Name, manager, true)
{
Expand All @@ -23,7 +26,12 @@ internal static QueueProperties ToQueueProperties(this QueueRuntimeProperties pr
UpdatedAt = properties.UpdatedAt
};
}
internal static SubscriptionQueueProperties ToQueueProperties(this SubscriptionRuntimeProperties properties, IQueueManager manager, string topic)

internal static SubscriptionQueueProperties ToQueueProperties(
this SubscriptionRuntimeProperties properties,
IQueueManager manager,
string topic
)
{
return new SubscriptionQueueProperties(properties.SubscriptionName, manager, topic, false)
{
Expand All @@ -38,7 +46,10 @@ internal static SubscriptionQueueProperties ToQueueProperties(this SubscriptionR
};
}

internal static QueueProperties ToQueueProperties(this TopicRuntimeProperties properties, IQueueManager manager)
internal static QueueProperties ToQueueProperties(
this TopicRuntimeProperties properties,
IQueueManager manager
)
{
return new QueueProperties(properties.Name, manager, false, QueueType.Topic)
{
Expand All @@ -50,7 +61,10 @@ internal static QueueProperties ToQueueProperties(this TopicRuntimeProperties pr
};
}

public static IServiceCollection AddServiceBusManagement(this IServiceCollection services, string connectionString)
public static IServiceCollection AddServiceBusManagement(
this IServiceCollection services,
string connectionString
)
{
return services
.AddScoped<IQueueMessageSender, ServiceBusQueueManager>()
Expand All @@ -60,4 +74,9 @@ public static IServiceCollection AddServiceBusManagement(this IServiceCollection
.AddScoped<ServiceBusTopicManager>()
.UseServiceBus(c => c.ConnectionString = connectionString);
}

public static IServiceCollection AddTopicCreator(this IServiceCollection services)
{
return services.AddScoped<IServiceBusTopicCreator, ServiceBusTopicManager>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace KnightBus.Azure.ServiceBus.Management;

public class ServiceBusTopicManager : IQueueManager
public class ServiceBusTopicManager : IQueueManager, IServiceBusTopicCreator
{
private readonly ServiceBusAdministrationClient _adminClient;
private readonly ServiceBusClient _client;
Expand All @@ -19,20 +19,29 @@ public ServiceBusTopicManager(IServiceBusConfiguration configuration)
_adminClient = new ServiceBusAdministrationClient(configuration.ConnectionString);
_client = new ServiceBusClient(configuration.ConnectionString);
}

public async Task<IEnumerable<QueueProperties>> List(CancellationToken ct)
{
var queues = _adminClient.GetTopicsRuntimePropertiesAsync((ct));
var properties = new List<QueueProperties>();
await foreach (var q in queues)
properties.Add(q.ToQueueProperties(new ServiceBusSubscriptionManager(q.Name, _client, _adminClient)));
properties.Add(
q.ToQueueProperties(
new ServiceBusSubscriptionManager(q.Name, _client, _adminClient)
)
);

return properties;
}

public async Task<QueueProperties> Get(string path, CancellationToken ct)
{
var topic = await _adminClient.GetTopicRuntimePropertiesAsync(path, ct).ConfigureAwait(false);
return topic.Value.ToQueueProperties(new ServiceBusSubscriptionManager(path, _client, _adminClient));
var topic = await _adminClient
.GetTopicRuntimePropertiesAsync(path, ct)
.ConfigureAwait(false);
return topic.Value.ToQueueProperties(
new ServiceBusSubscriptionManager(path, _client, _adminClient)
);
}

public Task Delete(string path, CancellationToken ct)
Expand All @@ -45,12 +54,20 @@ public Task<IReadOnlyList<QueueMessage>> Peek(string name, int count, Cancellati
throw new NotImplementedException();
}

public Task<IReadOnlyList<QueueMessage>> PeekDeadLetter(string path, int count, CancellationToken ct)
public Task<IReadOnlyList<QueueMessage>> PeekDeadLetter(
string path,
int count,
CancellationToken ct
)
{
throw new NotImplementedException();
}

public Task<IReadOnlyList<QueueMessage>> ReadDeadLetter(string path, int count, CancellationToken ct)
public Task<IReadOnlyList<QueueMessage>> ReadDeadLetter(
string path,
int count,
CancellationToken ct
)
{
throw new NotImplementedException();
}
Expand All @@ -59,5 +76,31 @@ public Task<int> MoveDeadLetters(string path, int count, CancellationToken ct)
{
throw new NotImplementedException();
}

/// <summary>
///Will try and get the topic and create one if one isn't found
/// </summary>
/// <param name="path"></param>
/// <param name="ct"></param>
/// <returns>True if the topic was actually created, otherwise false, this does not neccesarily indicate that the Topic exists</returns>
public async Task<bool> CreateIfNotExists(string path, CancellationToken ct)
{
try
{
var topic = await _adminClient.GetTopicRuntimePropertiesAsync(path, ct);
return false;
}
catch (ServiceBusException ex)
{
if (ex.Reason == ServiceBusFailureReason.MessageNotFound)
{
var createdTopic = await _adminClient.CreateTopicAsync(path, ct);
if (createdTopic.HasValue)
return true;
}
}
return false;
}

public QueueType QueueType => QueueType.Topic;
}
Loading