Skip to content
Merged
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
46 changes: 28 additions & 18 deletions src/Aspire.Hosting.Azure/AzurePublishingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,39 @@ private Task WriteAzureArtifactsOutputAsync(DistributedApplicationModel model, C

var parameterMap = new Dictionary<ParameterResource, ProvisioningParameter>();

foreach (var resource in model.Resources.OfType<AzureBicepResource>())
void MapParameter(object candidate)
{
foreach (var parameter in resource.Parameters)
if (candidate is ParameterResource p && !parameterMap.ContainsKey(p))
{
Visit(parameter.Value, v =>
var pid = Infrastructure.NormalizeBicepIdentifier(p.Name);

var pp = new ProvisioningParameter(pid, typeof(string))
{
if (v is ParameterResource p && !parameterMap.ContainsKey(p))
{
var pid = Infrastructure.NormalizeBicepIdentifier(p.Name);
IsSecure = p.Secret
};

var pp = new ProvisioningParameter(pid, typeof(string))
{
IsSecure = p.Secret
};
if (!p.Secret && p.Default is not null)
{
pp.Value = p.Value;
}

if (!p.Secret && p.Default is not null)
{
pp.Value = p.Value;
}
parameterMap[p] = pp;
}
}

parameterMap[p] = pp;
}
});
foreach (var resource in model.Resources.OfType<AzureBicepResource>())
{
// Map parameters from existing resources
if (resource.TryGetLastAnnotation<ExistingAzureResourceAnnotation>(out var existingAnnotation))
{
Visit(existingAnnotation.ResourceGroup, MapParameter);
Visit(existingAnnotation.Name, MapParameter);
}

// Map parameters for the resource itself
foreach (var parameter in resource.Parameters)
{
Visit(parameter.Value, MapParameter);
}
}

Expand Down Expand Up @@ -183,7 +193,7 @@ static BicepValue<string> ResolveValue(object val)
BicepValue<string> scope = resource.Scope?.ResourceGroup switch
{
string rgName => new FunctionCallExpression(new IdentifierExpression("resourceGroup"), new StringLiteralExpression(rgName)),
ParameterResource p => parameterMap[p],
ParameterResource p => new FunctionCallExpression(new IdentifierExpression("resourceGroup"), parameterMap[p].Value.Compile()),
_ => new IdentifierExpression(rg.BicepIdentifier)
};

Expand Down
76 changes: 52 additions & 24 deletions tests/Aspire.Hosting.Azure.Tests/AzurePublisherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public async Task PublishAsync_GeneratesMainBicep(bool useContext)
var description = builder.AddParameter("skuDescription", "The sku is ", publishValueAsDefault: true);
var skuDescriptionExpr = ReferenceExpression.Create($"{description} {storageSku}");

var kvName = builder.AddParameter("kvName");
var kvRg = builder.AddParameter("kvRg", "rg-shared");

builder.AddAzureKeyVault("kv").AsExisting(kvName, kvRg);

builder.AddAzureStorage("existing-storage").PublishAsExisting("images", "rg-shared");

var pgdb = builder.AddAzurePostgresFlexibleServer("pg").AddDatabase("pgdb");
var cosmos = builder.AddAzureCosmosDB("account").AddCosmosDatabase("db");
var blobs = builder.AddAzureStorage("storage")
Expand Down Expand Up @@ -103,25 +110,29 @@ public async Task PublishAsync_GeneratesMainBicep(bool useContext)
targetScope = 'subscription'

param environmentName string

param location string

param principalId string


param kvRg string

param kvName string

param storageSku string = 'Standard_LRS'

param skuDescription string = 'The sku is '

var tags = {
'aspire-env-name': environmentName
}

resource rg 'Microsoft.Resources/resourceGroups@2023-07-01' = {
name: 'rg-${environmentName}'
location: location
tags: tags
}

module acaEnv 'acaEnv/acaEnv.bicep' = {
name: 'acaEnv'
scope: rg
Expand All @@ -130,23 +141,40 @@ param principalId string
userPrincipalId: principalId
}
}


module kv 'kv/kv.bicep' = {
name: 'kv'
scope: resourceGroup(kvRg)
params: {
location: location
kvName: kvName
}
}

module existing_storage 'existing-storage/existing-storage.bicep' = {
name: 'existing-storage'
scope: resourceGroup('rg-shared')
params: {
location: location
}
}

module pg 'pg/pg.bicep' = {
name: 'pg'
scope: rg
params: {
location: location
}
}

module account 'account/account.bicep' = {
name: 'account'
scope: rg
params: {
location: location
}
}

module storage 'storage/storage.bicep' = {
name: 'storage'
scope: rg
Expand All @@ -156,7 +184,7 @@ param principalId string
sku_description: '${skuDescription} ${storageSku}'
}
}

module mod 'mod/mod.bicep' = {
name: 'mod'
scope: rg
Expand All @@ -165,15 +193,15 @@ param principalId string
pgdb: '${pg.outputs.connectionString};Database=pgdb'
}
}

module myapp_identity 'myapp-identity/myapp-identity.bicep' = {
name: 'myapp-identity'
scope: rg
params: {
location: location
}
}

module myapp_roles_account 'myapp-roles-account/myapp-roles-account.bicep' = {
name: 'myapp-roles-account'
scope: rg
Expand All @@ -183,15 +211,15 @@ param principalId string
principalId: myapp_identity.outputs.principalId
}
}

module fe_identity 'fe-identity/fe-identity.bicep' = {
name: 'fe-identity'
scope: rg
params: {
location: location
}
}

module fe_roles_storage 'fe-roles-storage/fe-roles-storage.bicep' = {
name: 'fe-roles-storage'
scope: rg
Expand All @@ -201,25 +229,25 @@ param principalId string
principalId: fe_identity.outputs.principalId
}
}

output myapp_identity_id string = myapp_identity.outputs.id

output myapp_identity_clientId string = myapp_identity.outputs.clientId

output account_connectionString string = account.outputs.connectionString

output acaEnv_AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN string = acaEnv.outputs.AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN

output acaEnv_AZURE_CONTAINER_APPS_ENVIRONMENT_ID string = acaEnv.outputs.AZURE_CONTAINER_APPS_ENVIRONMENT_ID

output fe_identity_id string = fe_identity.outputs.id

output fe_identity_clientId string = fe_identity.outputs.clientId

output storage_blobEndpoint string = storage.outputs.blobEndpoint

output acaEnv_AZURE_CONTAINER_REGISTRY_ENDPOINT string = acaEnv.outputs.AZURE_CONTAINER_REGISTRY_ENDPOINT

output acaEnv_AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID string = acaEnv.outputs.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID
""";
output.WriteLine(content);
Expand Down
Loading