|
6 | 6 | using System.Runtime.CompilerServices; |
7 | 7 | using Aspire.Hosting.ApplicationModel; |
8 | 8 | using Aspire.Hosting.Utils; |
| 9 | +using Azure.Provisioning; |
9 | 10 | using Azure.Provisioning.AppContainers; |
| 11 | +using Azure.Provisioning.Primitives; |
10 | 12 | using Microsoft.Extensions.DependencyInjection; |
11 | 13 | using Xunit; |
12 | 14 | using Xunit.Abstractions; |
@@ -1273,6 +1275,94 @@ param outputs_azure_container_apps_environment_id string |
1273 | 1275 | Assert.Equal(expectedBicep, bicep); |
1274 | 1276 | } |
1275 | 1277 |
|
| 1278 | + [Fact] |
| 1279 | + public async Task CanCustomizeWithProvisioningBuildOptions() |
| 1280 | + { |
| 1281 | + var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); |
| 1282 | + |
| 1283 | + builder.Services.Configure<AzureProvisioningOptions>(options => options.ProvisioningBuildOptions.InfrastructureResolvers.Insert(0, new MyResourceNamePropertyResolver())); |
| 1284 | + builder.AddAzureContainerAppsInfrastructure(); |
| 1285 | + |
| 1286 | + builder.AddContainer("api1", "myimage"); |
| 1287 | + |
| 1288 | + using var app = builder.Build(); |
| 1289 | + |
| 1290 | + await ExecuteBeforeStartHooksAsync(app, default); |
| 1291 | + |
| 1292 | + var model = app.Services.GetRequiredService<DistributedApplicationModel>(); |
| 1293 | + |
| 1294 | + var container = Assert.Single(model.GetContainerResources()); |
| 1295 | + |
| 1296 | + container.TryGetLastAnnotation<DeploymentTargetAnnotation>(out var target); |
| 1297 | + |
| 1298 | + var resource = target?.DeploymentTarget as AzureProvisioningResource; |
| 1299 | + |
| 1300 | + Assert.NotNull(resource); |
| 1301 | + |
| 1302 | + var (_, bicep) = await ManifestUtils.GetManifestWithBicep(resource); |
| 1303 | + |
| 1304 | + var expectedBicep = |
| 1305 | + """ |
| 1306 | + @description('The location for the resource(s) to be deployed.') |
| 1307 | + param location string = resourceGroup().location |
| 1308 | +
|
| 1309 | + param outputs_azure_container_registry_managed_identity_id string |
| 1310 | +
|
| 1311 | + param outputs_managed_identity_client_id string |
| 1312 | +
|
| 1313 | + param outputs_azure_container_apps_environment_id string |
| 1314 | +
|
| 1315 | + resource api1 'Microsoft.App/containerApps@2024-03-01' = { |
| 1316 | + name: 'api1-my' |
| 1317 | + location: location |
| 1318 | + properties: { |
| 1319 | + configuration: { |
| 1320 | + activeRevisionsMode: 'Single' |
| 1321 | + } |
| 1322 | + environmentId: outputs_azure_container_apps_environment_id |
| 1323 | + template: { |
| 1324 | + containers: [ |
| 1325 | + { |
| 1326 | + image: 'myimage:latest' |
| 1327 | + name: 'api1' |
| 1328 | + env: [ |
| 1329 | + { |
| 1330 | + name: 'AZURE_CLIENT_ID' |
| 1331 | + value: outputs_managed_identity_client_id |
| 1332 | + } |
| 1333 | + ] |
| 1334 | + } |
| 1335 | + ] |
| 1336 | + scale: { |
| 1337 | + minReplicas: 1 |
| 1338 | + } |
| 1339 | + } |
| 1340 | + } |
| 1341 | + identity: { |
| 1342 | + type: 'UserAssigned' |
| 1343 | + userAssignedIdentities: { |
| 1344 | + '${outputs_azure_container_registry_managed_identity_id}': { } |
| 1345 | + } |
| 1346 | + } |
| 1347 | + } |
| 1348 | + """; |
| 1349 | + output.WriteLine(bicep); |
| 1350 | + Assert.Equal(expectedBicep, bicep); |
| 1351 | + } |
| 1352 | + |
| 1353 | + private sealed class MyResourceNamePropertyResolver : DynamicResourceNamePropertyResolver |
| 1354 | + { |
| 1355 | + public override void ResolveProperties(ProvisionableConstruct construct, ProvisioningBuildOptions options) |
| 1356 | + { |
| 1357 | + if (construct is ContainerApp app) |
| 1358 | + { |
| 1359 | + app.Name = app.Name.Value + "-my"; |
| 1360 | + } |
| 1361 | + |
| 1362 | + base.ResolveProperties(construct, options); |
| 1363 | + } |
| 1364 | + } |
| 1365 | + |
1276 | 1366 | [Fact] |
1277 | 1367 | public async Task ExternalEndpointBecomesIngress() |
1278 | 1368 | { |
|
0 commit comments