Skip to content

Commit 7804aea

Browse files
authored
Fix existing resources with the azure publisher (#8622)
- Added existing resources to baseline tests
1 parent c29004a commit 7804aea

File tree

2 files changed

+80
-42
lines changed

2 files changed

+80
-42
lines changed

src/Aspire.Hosting.Azure/AzurePublishingContext.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,29 +114,39 @@ private Task WriteAzureArtifactsOutputAsync(DistributedApplicationModel model, C
114114

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

117-
foreach (var resource in model.Resources.OfType<AzureBicepResource>())
117+
void MapParameter(object candidate)
118118
{
119-
foreach (var parameter in resource.Parameters)
119+
if (candidate is ParameterResource p && !parameterMap.ContainsKey(p))
120120
{
121-
Visit(parameter.Value, v =>
121+
var pid = Infrastructure.NormalizeBicepIdentifier(p.Name);
122+
123+
var pp = new ProvisioningParameter(pid, typeof(string))
122124
{
123-
if (v is ParameterResource p && !parameterMap.ContainsKey(p))
124-
{
125-
var pid = Infrastructure.NormalizeBicepIdentifier(p.Name);
125+
IsSecure = p.Secret
126+
};
126127

127-
var pp = new ProvisioningParameter(pid, typeof(string))
128-
{
129-
IsSecure = p.Secret
130-
};
128+
if (!p.Secret && p.Default is not null)
129+
{
130+
pp.Value = p.Value;
131+
}
131132

132-
if (!p.Secret && p.Default is not null)
133-
{
134-
pp.Value = p.Value;
135-
}
133+
parameterMap[p] = pp;
134+
}
135+
}
136136

137-
parameterMap[p] = pp;
138-
}
139-
});
137+
foreach (var resource in model.Resources.OfType<AzureBicepResource>())
138+
{
139+
// Map parameters from existing resources
140+
if (resource.TryGetLastAnnotation<ExistingAzureResourceAnnotation>(out var existingAnnotation))
141+
{
142+
Visit(existingAnnotation.ResourceGroup, MapParameter);
143+
Visit(existingAnnotation.Name, MapParameter);
144+
}
145+
146+
// Map parameters for the resource itself
147+
foreach (var parameter in resource.Parameters)
148+
{
149+
Visit(parameter.Value, MapParameter);
140150
}
141151
}
142152

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

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

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public async Task PublishAsync_GeneratesMainBicep(bool useContext)
3838
var description = builder.AddParameter("skuDescription", "The sku is ", publishValueAsDefault: true);
3939
var skuDescriptionExpr = ReferenceExpression.Create($"{description} {storageSku}");
4040

41+
var kvName = builder.AddParameter("kvName");
42+
var kvRg = builder.AddParameter("kvRg", "rg-shared");
43+
44+
builder.AddAzureKeyVault("kv").AsExisting(kvName, kvRg);
45+
46+
builder.AddAzureStorage("existing-storage").PublishAsExisting("images", "rg-shared");
47+
4148
var pgdb = builder.AddAzurePostgresFlexibleServer("pg").AddDatabase("pgdb");
4249
var cosmos = builder.AddAzureCosmosDB("account").AddCosmosDatabase("db");
4350
var blobs = builder.AddAzureStorage("storage")
@@ -103,25 +110,29 @@ public async Task PublishAsync_GeneratesMainBicep(bool useContext)
103110
targetScope = 'subscription'
104111
105112
param environmentName string
106-
113+
107114
param location string
108-
115+
109116
param principalId string
110-
117+
118+
param kvRg string
119+
120+
param kvName string
121+
111122
param storageSku string = 'Standard_LRS'
112-
123+
113124
param skuDescription string = 'The sku is '
114-
125+
115126
var tags = {
116127
'aspire-env-name': environmentName
117128
}
118-
129+
119130
resource rg 'Microsoft.Resources/resourceGroups@2023-07-01' = {
120131
name: 'rg-${environmentName}'
121132
location: location
122133
tags: tags
123134
}
124-
135+
125136
module acaEnv 'acaEnv/acaEnv.bicep' = {
126137
name: 'acaEnv'
127138
scope: rg
@@ -130,23 +141,40 @@ param principalId string
130141
userPrincipalId: principalId
131142
}
132143
}
133-
144+
145+
module kv 'kv/kv.bicep' = {
146+
name: 'kv'
147+
scope: resourceGroup(kvRg)
148+
params: {
149+
location: location
150+
kvName: kvName
151+
}
152+
}
153+
154+
module existing_storage 'existing-storage/existing-storage.bicep' = {
155+
name: 'existing-storage'
156+
scope: resourceGroup('rg-shared')
157+
params: {
158+
location: location
159+
}
160+
}
161+
134162
module pg 'pg/pg.bicep' = {
135163
name: 'pg'
136164
scope: rg
137165
params: {
138166
location: location
139167
}
140168
}
141-
169+
142170
module account 'account/account.bicep' = {
143171
name: 'account'
144172
scope: rg
145173
params: {
146174
location: location
147175
}
148176
}
149-
177+
150178
module storage 'storage/storage.bicep' = {
151179
name: 'storage'
152180
scope: rg
@@ -156,7 +184,7 @@ param principalId string
156184
sku_description: '${skuDescription} ${storageSku}'
157185
}
158186
}
159-
187+
160188
module mod 'mod/mod.bicep' = {
161189
name: 'mod'
162190
scope: rg
@@ -165,15 +193,15 @@ param principalId string
165193
pgdb: '${pg.outputs.connectionString};Database=pgdb'
166194
}
167195
}
168-
196+
169197
module myapp_identity 'myapp-identity/myapp-identity.bicep' = {
170198
name: 'myapp-identity'
171199
scope: rg
172200
params: {
173201
location: location
174202
}
175203
}
176-
204+
177205
module myapp_roles_account 'myapp-roles-account/myapp-roles-account.bicep' = {
178206
name: 'myapp-roles-account'
179207
scope: rg
@@ -183,15 +211,15 @@ param principalId string
183211
principalId: myapp_identity.outputs.principalId
184212
}
185213
}
186-
214+
187215
module fe_identity 'fe-identity/fe-identity.bicep' = {
188216
name: 'fe-identity'
189217
scope: rg
190218
params: {
191219
location: location
192220
}
193221
}
194-
222+
195223
module fe_roles_storage 'fe-roles-storage/fe-roles-storage.bicep' = {
196224
name: 'fe-roles-storage'
197225
scope: rg
@@ -201,25 +229,25 @@ param principalId string
201229
principalId: fe_identity.outputs.principalId
202230
}
203231
}
204-
232+
205233
output myapp_identity_id string = myapp_identity.outputs.id
206-
234+
207235
output myapp_identity_clientId string = myapp_identity.outputs.clientId
208-
236+
209237
output account_connectionString string = account.outputs.connectionString
210-
238+
211239
output acaEnv_AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN string = acaEnv.outputs.AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN
212240
213241
output acaEnv_AZURE_CONTAINER_APPS_ENVIRONMENT_ID string = acaEnv.outputs.AZURE_CONTAINER_APPS_ENVIRONMENT_ID
214-
242+
215243
output fe_identity_id string = fe_identity.outputs.id
216-
244+
217245
output fe_identity_clientId string = fe_identity.outputs.clientId
218-
246+
219247
output storage_blobEndpoint string = storage.outputs.blobEndpoint
220-
248+
221249
output acaEnv_AZURE_CONTAINER_REGISTRY_ENDPOINT string = acaEnv.outputs.AZURE_CONTAINER_REGISTRY_ENDPOINT
222-
250+
223251
output acaEnv_AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID string = acaEnv.outputs.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID
224252
""";
225253
output.WriteLine(content);

0 commit comments

Comments
 (0)