|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Azure;
using Azure.Provisioning;
using Azure.Provisioning.ServiceBus;
namespace Aspire.Hosting;
/// <summary>
/// Provides extension methods for adding the Azure Service Bus resources to the application model.
/// </summary>
public static class AzureServiceBusExtensions
{
/// <summary>
/// Adds an Azure Service Bus Namespace resource to the application model. This resource can be used to create queue, topic, and subscription resources.
/// </summary>
/// <param name="builder">The builder for the distributed application.</param>
/// <param name="name">The name of the resource.</param>
/// <returns></returns>
public static IResourceBuilder<AzureServiceBusResource> AddAzureServiceBus(this IDistributedApplicationBuilder builder, [ResourceName] string name)
{
builder.AddAzureProvisioning();
var configureInfrastructure = static (AzureResourceInfrastructure infrastructure) =>
{
var skuParameter = new ProvisioningParameter("sku", typeof(string))
{
Value = "Standard"
};
infrastructure.Add(skuParameter);
var serviceBusNamespace = new ServiceBusNamespace(infrastructure.AspireResource.GetBicepIdentifier())
{
Sku = new ServiceBusSku()
{
Name = skuParameter
},
DisableLocalAuth = true,
Tags = { { "aspire-resource-name", infrastructure.AspireResource.Name } }
};
infrastructure.Add(serviceBusNamespace);
var principalTypeParameter = new ProvisioningParameter(AzureBicepResource.KnownParameters.PrincipalType, typeof(string));
var principalIdParameter = new ProvisioningParameter(AzureBicepResource.KnownParameters.PrincipalId, typeof(string));
infrastructure.Add(serviceBusNamespace.CreateRoleAssignment(ServiceBusBuiltInRole.AzureServiceBusDataOwner, principalTypeParameter, principalIdParameter));
infrastructure.Add(new ProvisioningOutput("serviceBusEndpoint", typeof(string)) { Value = serviceBusNamespace.ServiceBusEndpoint });
var azureResource = (AzureServiceBusResource)infrastructure.AspireResource;
foreach (var queue in azureResource.Queues)
{
var queueResource = new ServiceBusQueue(Infrastructure.NormalizeBicepIdentifier(queue))
{
Parent = serviceBusNamespace,
Name = queue
};
infrastructure.Add(queueResource);
}
var topicDictionary = new Dictionary<string, ServiceBusTopic>();
foreach (var topic in azureResource.Topics)
{
var topicResource = new ServiceBusTopic(Infrastructure.NormalizeBicepIdentifier(topic))
{
Parent = serviceBusNamespace,
Name = topic
};
infrastructure.Add(topicResource);
topicDictionary.Add(topic, topicResource);
}
foreach (var subscription in azureResource.Subscriptions)
{
var topic = topicDictionary[subscription.TopicName];
var subscriptionResource = new ServiceBusSubscription(Infrastructure.NormalizeBicepIdentifier(subscription.Name))
{
Parent = topic,
Name = subscription.Name
};
infrastructure.Add(subscriptionResource);
}
};
var resource = new AzureServiceBusResource(name, configureInfrastructure);
return builder.AddResource(resource)
// These ambient parameters are only available in development time.
.WithParameter(AzureBicepResource.KnownParameters.PrincipalId)
.WithParameter(AzureBicepResource.KnownParameters.PrincipalType)
.WithManifestPublishingCallback(resource.WriteToManifest);
}
/// <summary>
/// Adds an Azure Service Bus Topic resource to the application model. This resource requires an <see cref="AzureServiceBusResource"/> to be added to the application model.
/// </summary>
/// <param name="builder">The Azure Service Bus resource builder.</param>
/// <param name="name">The name of the topic.</param>
/// <param name="subscriptions">The name of the subscriptions.</param>
public static IResourceBuilder<AzureServiceBusResource> AddTopic(this IResourceBuilder<AzureServiceBusResource> builder, [ResourceName] string name, string[] subscriptions)
{
builder.Resource.Topics.Add(name);
foreach (var subscription in subscriptions)
{
builder.Resource.Subscriptions.Add((name, subscription));
}
return builder;
}
/// <summary>
/// Adds an Azure Service Bus Queue resource to the application model. This resource requires an <see cref="AzureServiceBusResource"/> to be added to the application model.
/// </summary>
/// <param name="builder">The Azure Service Bus resource builder.</param>
/// <param name="name">The name of the queue.</param>
public static IResourceBuilder<AzureServiceBusResource> AddQueue(this IResourceBuilder<AzureServiceBusResource> builder, [ResourceName] string name)
{
builder.Resource.Queues.Add(name);
return builder;
}
/// <summary>
/// Adds an Azure Service Bus Topic resource to the application model. This resource requires an <see cref="AzureServiceBusResource"/> to be added to the application model.
/// </summary>
/// <param name="builder">The Azure Service Bus resource builder.</param>
/// <param name="name">The name of the topic.</param>
public static IResourceBuilder<AzureServiceBusResource> AddTopic(this IResourceBuilder<AzureServiceBusResource> builder, [ResourceName] string name)
{
builder.Resource.Topics.Add(name);
return builder;
}
/// <summary>
/// Adds an Azure Service Bus Topic resource to the application model. This resource requires an <see cref="AzureServiceBusResource"/> to be added to the application model.
/// </summary>
/// <param name="builder">The Azure Service Bus resource builder.</param>
/// <param name="topicName">The name of the topic.</param>
/// <param name="subscriptionName">The name of the subscription.</param>
public static IResourceBuilder<AzureServiceBusResource> AddSubscription(this IResourceBuilder<AzureServiceBusResource> builder, string topicName, string subscriptionName)
{
builder.Resource.Subscriptions.Add((topicName, subscriptionName));
return builder;
}
}
|