Skip to content

Commit ec868c3

Browse files
committed
fixup! AzureStorage auto create blob containers
1 parent 1c88e47 commit ec868c3

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Runtime.CompilerServices;
46
using Aspire.Hosting.ApplicationModel;
57
using Aspire.Hosting.Azure;
68
using Azure.Provisioning;
@@ -11,12 +13,19 @@ namespace Aspire.Hosting;
1113
/// A resource that represents an Azure Blob Storage container.
1214
/// </summary>
1315
/// <param name="name">The name of the resource.</param>
14-
/// <param name="blobStorage">The <see cref="AzureBlobStorageResource"/> that the resource is stored in.</param>
15-
public class AzureBlobStorageContainerResource(string name, AzureBlobStorageResource blobStorage) : Resource(name),
16+
/// <param name="blobContainerName">The name of the blob container.</param>
17+
/// <param name="parent">The <see cref="AzureBlobStorageResource"/> that the resource is stored in.</param>
18+
public class AzureBlobStorageContainerResource(string name, string blobContainerName, AzureBlobStorageResource parent) : Resource(name),
1619
IResourceWithConnectionString,
1720
IResourceWithParent<AzureBlobStorageResource>,
1821
IResourceWithAzureFunctionsConfig
1922
{
23+
24+
/// <summary>
25+
/// Gets the blob container name.
26+
/// </summary>
27+
public string BlobContainerName { get; } = ThrowIfNullOrEmpty(blobContainerName);
28+
2029
/// <summary>
2130
/// Gets the connection string template for the manifest for the Azure Blob Storage container resource.
2231
/// </summary>
@@ -25,7 +34,7 @@ public class AzureBlobStorageContainerResource(string name, AzureBlobStorageReso
2534
/// <summary>
2635
/// Gets the parent <see cref="AzureBlobStorageResource"/> of this <see cref="AzureBlobStorageContainerResource"/>.
2736
/// </summary>
28-
public AzureBlobStorageResource Parent => blobStorage ?? throw new ArgumentNullException(nameof(blobStorage));
37+
public AzureBlobStorageResource Parent => parent ?? throw new ArgumentNullException(nameof(parent));
2938

3039
internal void ApplyAzureFunctionsConfiguration(IDictionary<string, object> target, string connectionName)
3140
=> Parent.ApplyAzureFunctionsConfiguration(target, connectionName, Name);
@@ -36,16 +45,20 @@ internal void ApplyAzureFunctionsConfiguration(IDictionary<string, object> targe
3645
/// <returns>A <see cref="global::Azure.Provisioning.Storage.BlobContainer"/> instance.</returns>
3746
internal global::Azure.Provisioning.Storage.BlobContainer ToProvisioningEntity()
3847
{
39-
global::Azure.Provisioning.Storage.BlobContainer blobContainer = new(Infrastructure.NormalizeBicepIdentifier(Name));
40-
41-
if (Name is not null)
48+
global::Azure.Provisioning.Storage.BlobContainer blobContainer = new(Infrastructure.NormalizeBicepIdentifier(Name))
4249
{
43-
blobContainer.Name = Name;
44-
}
50+
Name = BlobContainerName
51+
};
4552

4653
return blobContainer;
4754
}
4855

56+
private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
57+
{
58+
ArgumentException.ThrowIfNullOrEmpty(argument, paramName);
59+
return argument;
60+
}
61+
4962
void IResourceWithAzureFunctionsConfig.ApplyAzureFunctionsConfiguration(IDictionary<string, object> target, string connectionName)
5063
=> ApplyAzureFunctionsConfiguration(target, connectionName);
5164
}

src/Aspire.Hosting.Azure.Storage/AzureStorageExtensions.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,16 @@ public static IResourceBuilder<AzureBlobStorageResource> AddBlobs(this IResource
324324
/// </summary>
325325
/// <param name="builder">The <see cref="IResourceBuilder{T}"/> for <see cref="AzureBlobStorageResource"/>/</param>
326326
/// <param name="name">The name of the resource.</param>
327+
/// <param name="blobContainerName">The name of the blob container.</param>
327328
/// <returns>An <see cref="IResourceBuilder{T}"/> for the <see cref="AzureBlobStorageContainerResource"/>.</returns>
328-
public static IResourceBuilder<AzureBlobStorageContainerResource> AddBlobContainer(this IResourceBuilder<AzureBlobStorageResource> builder, [ResourceName] string name)
329+
public static IResourceBuilder<AzureBlobStorageContainerResource> AddBlobContainer(this IResourceBuilder<AzureBlobStorageResource> builder, [ResourceName] string name, string? blobContainerName = null)
329330
{
330331
ArgumentNullException.ThrowIfNull(builder);
331332
ArgumentException.ThrowIfNullOrEmpty(name);
332333

333-
AzureBlobStorageContainerResource resource = new(name, builder.Resource);
334+
blobContainerName ??= name;
335+
336+
AzureBlobStorageContainerResource resource = new(name, blobContainerName, builder.Resource);
334337

335338
builder.Resource.BlobContainers.Add(resource);
336339

tests/Aspire.Hosting.Azure.Tests/AzureStorageExtensionsTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public async Task ResourceNamesBicepValid()
177177

178178
var manifest = await AzureManifestUtils.GetManifestWithBicep(storage.Resource);
179179

180-
await Verifier.Verify(manifest.BicepText, extension: "bicep");
180+
await Verifier.Verify(manifest.BicepText, extension: "bicep")
181+
.UseDirectory("Snapshots");
181182
}
182183
}

0 commit comments

Comments
 (0)