Skip to content
Open
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
Expand Up @@ -5,6 +5,7 @@
#pragma warning disable ASPIREAZURE001 // AzureEnvironmentResource.ProvisionInfrastructureStepName for pipeline ordering
#pragma warning disable ASPIREAZURE003 // AzureSubnetResource used in WithSubnet extensions

using System.Diagnostics;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Azure;
using Aspire.Hosting.Azure.Kubernetes;
Expand Down Expand Up @@ -763,7 +764,20 @@ private static void ConfigureAksInfrastructure(AzureResourceInfrastructure infra
{
existingSubnet = SubnetResource.FromExisting(subnetIdentifier);
existingSubnet.Parent = existingVnet;
existingSubnet.Name = subnet.SubnetName;

if (subnet.SubnetName is not null)
{
existingSubnet.Name = subnet.SubnetName;
}
else if (subnet.SubnetNameParameter is not null)
{
existingSubnet.Name = subnet.SubnetNameParameter.AsProvisioningParameter(infrastructure);
}
else
{
throw new UnreachableException("Subnet name must be set.");
}
Comment on lines +768 to +779

infrastructure.Add(existingSubnet);
subnetExistingByKey[subnetIdentifier] = existingSubnet;
}
Expand Down
46 changes: 39 additions & 7 deletions src/Aspire.Hosting.Azure.Network/AzureSubnetResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Aspire.Hosting.Azure;
public class AzureSubnetResource : Resource, IResourceWithParent<AzureVirtualNetworkResource>
{
// Backing field holds either string or ParameterResource
private readonly object _subnetName;
private readonly object _addressPrefix;
Comment on lines 24 to 26

/// <summary>
Expand All @@ -34,7 +35,7 @@ public class AzureSubnetResource : Resource, IResourceWithParent<AzureVirtualNet
public AzureSubnetResource(string name, string subnetName, string addressPrefix, AzureVirtualNetworkResource parent)
: base(name)
{
SubnetName = ThrowIfNullOrEmpty(subnetName);
_subnetName = ThrowIfNullOrEmpty(subnetName);
_addressPrefix = ThrowIfNullOrEmpty(addressPrefix);
Parent = parent ?? throw new ArgumentNullException(nameof(parent));
}
Expand All @@ -49,21 +50,41 @@ public AzureSubnetResource(string name, string subnetName, string addressPrefix,
public AzureSubnetResource(string name, string subnetName, ParameterResource addressPrefix, AzureVirtualNetworkResource parent)
: base(name)
{
SubnetName = ThrowIfNullOrEmpty(subnetName);
_subnetName = ThrowIfNullOrEmpty(subnetName);
_addressPrefix = addressPrefix ?? throw new ArgumentNullException(nameof(addressPrefix));
Parent = parent ?? throw new ArgumentNullException(nameof(parent));
}

/// <summary>
/// Gets the subnet name.
/// Initializes a new instance of the <see cref="AzureSubnetResource"/> class with parameterized subnet name and address prefix.
/// </summary>
public string SubnetName { get; }
/// <param name="name">The name of the resource.</param>
/// <param name="subnetName">The parameter resource containing the subnet name.</param>
/// <param name="addressPrefix">The parameter resource containing the address prefix for the subnet.</param>
/// <param name="parent">The parent Virtual Network resource.</param>
public AzureSubnetResource(string name, ParameterResource subnetName, ParameterResource addressPrefix, AzureVirtualNetworkResource parent)
: base(name)
{
_subnetName = subnetName ?? throw new ArgumentNullException(nameof(subnetName));
_addressPrefix = addressPrefix ?? throw new ArgumentNullException(nameof(addressPrefix));
Parent = parent ?? throw new ArgumentNullException(nameof(parent));
}

/// <summary>
/// Gets the subnet name, or <c>null</c> if the subnet name is provided via a <see cref="ParameterResource"/>.
/// </summary>
public string? SubnetName => _subnetName as string;
Comment on lines +74 to +76

/// <summary>
/// Gets the address prefix for the subnet (e.g., "10.0.1.0/24"), or <c>null</c> if the address prefix is provided via a <see cref="ParameterResource"/>.
/// </summary>
public string? AddressPrefix => _addressPrefix as string;

/// <summary>
/// Gets the parameter resource containing the subnet name, or <c>null</c> if the subnet name is provided as a literal string.
/// </summary>
public ParameterResource? SubnetNameParameter => _subnetName as ParameterResource;

/// <summary>
/// Gets the parameter resource containing the address prefix for the subnet, or <c>null</c> if the address prefix is provided as a literal string.
/// </summary>
Expand Down Expand Up @@ -97,10 +118,21 @@ private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgu
/// </summary>
internal SubnetResource ToProvisioningEntity(AzureResourceInfrastructure infra, ProvisionableResource? dependsOn)
{
var subnet = new SubnetResource(Infrastructure.NormalizeBicepIdentifier(Name))
var subnet = new SubnetResource(Infrastructure.NormalizeBicepIdentifier(Name));

// Set the subnet name from either the literal string or the parameter
if (_subnetName is string subnetName)
{
subnet.Name = subnetName;
}
else if (_subnetName is ParameterResource subnetNameParameter)
{
Name = SubnetName,
};
subnet.Name = subnetNameParameter.AsProvisioningParameter(infra);
}
else
{
throw new UnreachableException("SubnetName must be set either as a string or a ParameterResource.");
}

// Set the address prefix from either the literal string or the parameter
if (_addressPrefix is string addressPrefix)
Expand Down
34 changes: 34 additions & 0 deletions src/Aspire.Hosting.Azure.Network/AzureVirtualNetworkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,40 @@ public static IResourceBuilder<AzureSubnetResource> AddSubnet(
return AddSubnetCore(builder, subnet);
}

/// <summary>
/// Adds an Azure Subnet to the Virtual Network with parameterized address prefix and subnet name.
/// </summary>
/// <param name="builder">The Virtual Network resource builder.</param>
/// <param name="name">The name of the subnet resource.</param>
/// <param name="addressPrefix">The parameter resource containing the address prefix for the subnet (e.g., "10.0.1.0/24").</param>
/// <param name="subnetName">The parameter resource containing the subnet name in Azure.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{AzureSubnetResource}"/>.</returns>
/// <example>
/// This example adds a subnet with parameterized address prefix and subnet name:
/// <code>
/// var subnetName = builder.AddParameter("subnetName");
/// var subnetPrefix = builder.AddParameter("subnetPrefix");
/// var vnet = builder.AddAzureVirtualNetwork("vnet");
/// var subnet = vnet.AddSubnet("my-subnet", subnetPrefix, subnetName);
/// </code>
/// </example>
[AspireExportIgnore(Reason = "Polyglot app hosts use the internal addSubnet dispatcher export.")]
public static IResourceBuilder<AzureSubnetResource> AddSubnet(
this IResourceBuilder<AzureVirtualNetworkResource> builder,
[ResourceName] string name,
IResourceBuilder<ParameterResource> addressPrefix,
IResourceBuilder<ParameterResource> subnetName)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(name);
ArgumentNullException.ThrowIfNull(addressPrefix);
ArgumentNullException.ThrowIfNull(subnetName);

var subnet = new AzureSubnetResource(name, subnetName.Resource, addressPrefix.Resource, builder.Resource);

return AddSubnetCore(builder, subnet);
}

/// <summary>
/// Adds an Azure subnet resource to an Azure Virtual Network resource.
/// </summary>
Expand Down
Loading