1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . Diagnostics ;
4
5
using Aspire . Hosting . ApplicationModel ;
5
6
using Aspire . Hosting . Azure ;
6
7
using Aspire . Hosting . Azure . AppContainers ;
@@ -64,6 +65,7 @@ public static IResourceBuilder<AzureContainerAppEnvironmentResource> AddAzureCon
64
65
65
66
var containerAppEnvResource = new AzureContainerAppEnvironmentResource ( name , static infra =>
66
67
{
68
+ var appEnvResource = ( AzureContainerAppEnvironmentResource ) infra . AspireResource ;
67
69
var userPrincipalId = new ProvisioningParameter ( "userPrincipalId" , typeof ( string ) ) ;
68
70
69
71
infra . Add ( userPrincipalId ) ;
@@ -75,14 +77,24 @@ public static IResourceBuilder<AzureContainerAppEnvironmentResource> AddAzureCon
75
77
76
78
infra . Add ( tags ) ;
77
79
78
- var identity = new UserAssignedIdentity ( "mi" )
80
+ ProvisioningVariable ? resourceToken = null ;
81
+ if ( appEnvResource . UseAzdNamingConvention )
82
+ {
83
+ resourceToken = new ProvisioningVariable ( "resourceToken" , typeof ( string ) )
84
+ {
85
+ Value = BicepFunction . GetUniqueString ( BicepFunction . GetResourceGroup ( ) . Id )
86
+ } ;
87
+ infra . Add ( resourceToken ) ;
88
+ }
89
+
90
+ var identity = new UserAssignedIdentity ( Infrastructure . NormalizeBicepIdentifier ( $ "{ appEnvResource . Name } _mi") )
79
91
{
80
92
Tags = tags
81
93
} ;
82
94
83
95
infra . Add ( identity ) ;
84
96
85
- var containerRegistry = new ContainerRegistryService ( "acr" )
97
+ var containerRegistry = new ContainerRegistryService ( Infrastructure . NormalizeBicepIdentifier ( $ " { appEnvResource . Name } _acr" ) )
86
98
{
87
99
Sku = new ( ) { Name = ContainerRegistrySkuName . Basic } ,
88
100
Tags = tags
@@ -96,15 +108,15 @@ public static IResourceBuilder<AzureContainerAppEnvironmentResource> AddAzureCon
96
108
pullRa . Name = BicepFunction . CreateGuid ( containerRegistry . Id , identity . Id , pullRa . RoleDefinitionId ) ;
97
109
infra . Add ( pullRa ) ;
98
110
99
- var laWorkspace = new OperationalInsightsWorkspace ( "law" )
111
+ var laWorkspace = new OperationalInsightsWorkspace ( Infrastructure . NormalizeBicepIdentifier ( $ " { appEnvResource . Name } _law" ) )
100
112
{
101
113
Sku = new ( ) { Name = OperationalInsightsWorkspaceSkuName . PerGB2018 } ,
102
114
Tags = tags
103
115
} ;
104
116
105
117
infra . Add ( laWorkspace ) ;
106
118
107
- var containerAppEnvironment = new ContainerAppManagedEnvironment ( "cae" )
119
+ var containerAppEnvironment = new ContainerAppManagedEnvironment ( appEnvResource . GetBicepIdentifier ( ) )
108
120
{
109
121
WorkloadProfiles = [
110
122
new ContainerAppWorkloadProfile ( )
@@ -149,9 +161,10 @@ public static IResourceBuilder<AzureContainerAppEnvironmentResource> AddAzureCon
149
161
150
162
var resource = ( AzureContainerAppEnvironmentResource ) infra . AspireResource ;
151
163
164
+ StorageAccount ? storageVolume = null ;
152
165
if ( resource . VolumeNames . Count > 0 )
153
166
{
154
- var storageVolume = new StorageAccount ( "storageVolume" )
167
+ storageVolume = new StorageAccount ( Infrastructure . NormalizeBicepIdentifier ( $ " { appEnvResource . Name } _storageVolume" ) )
155
168
{
156
169
Tags = tags ,
157
170
Sku = new StorageSku ( ) { Name = StorageSkuName . StandardLrs } ,
@@ -200,6 +213,26 @@ public static IResourceBuilder<AzureContainerAppEnvironmentResource> AddAzureCon
200
213
infra . Add ( containerAppStorage ) ;
201
214
202
215
managedStorages [ outputName ] = containerAppStorage ;
216
+
217
+ if ( appEnvResource . UseAzdNamingConvention )
218
+ {
219
+ var volumeName = output . volume . Type switch
220
+ {
221
+ ContainerMountType . BindMount => $ "bm{ output . index } ",
222
+ ContainerMountType . Volume => output . volume . Source ?? $ "v{ output . index } ",
223
+ _ => throw new NotSupportedException ( )
224
+ } ;
225
+
226
+ share . Name = BicepFunction . Take (
227
+ BicepFunction . Interpolate (
228
+ $ "{ BicepFunction . ToLower ( output . resource . Name ) } -{ BicepFunction . ToLower ( volumeName ) } ") ,
229
+ 60 ) ;
230
+
231
+ containerAppStorage . Name = BicepFunction . Take (
232
+ BicepFunction . Interpolate (
233
+ $ "{ BicepFunction . ToLower ( output . resource . Name ) } -{ BicepFunction . ToLower ( volumeName ) } ") ,
234
+ 32 ) ;
235
+ }
203
236
}
204
237
}
205
238
@@ -208,10 +241,34 @@ public static IResourceBuilder<AzureContainerAppEnvironmentResource> AddAzureCon
208
241
{
209
242
infra . Add ( new ProvisioningOutput ( key , typeof ( string ) )
210
243
{
211
- Value = value . Name
244
+ // use an expression here in case the resource's Name was set to a function expression above
245
+ Value = new MemberExpression ( new IdentifierExpression ( value . BicepIdentifier ) , "name" )
212
246
} ) ;
213
247
}
214
248
249
+ if ( appEnvResource . UseAzdNamingConvention )
250
+ {
251
+ Debug . Assert ( resourceToken is not null ) ;
252
+
253
+ identity . Name = BicepFunction . Interpolate ( $ "mi-{ resourceToken } ") ;
254
+ containerRegistry . Name = new FunctionCallExpression (
255
+ new IdentifierExpression ( "replace" ) ,
256
+ new InterpolatedStringExpression (
257
+ [
258
+ new StringLiteralExpression ( "acr-" ) ,
259
+ new IdentifierExpression ( resourceToken . BicepIdentifier )
260
+ ] ) ,
261
+ new StringLiteralExpression ( "-" ) ,
262
+ new StringLiteralExpression ( "" ) ) ;
263
+ laWorkspace . Name = BicepFunction . Interpolate ( $ "law-{ resourceToken } ") ;
264
+ containerAppEnvironment . Name = BicepFunction . Interpolate ( $ "cae-{ resourceToken } ") ;
265
+
266
+ if ( storageVolume is not null )
267
+ {
268
+ storageVolume . Name = BicepFunction . Interpolate ( $ "vol{ resourceToken } ") ;
269
+ }
270
+ }
271
+
215
272
infra . Add ( new ProvisioningOutput ( "MANAGED_IDENTITY_NAME" , typeof ( string ) )
216
273
{
217
274
Value = identity . Name
@@ -272,4 +329,21 @@ public static IResourceBuilder<AzureContainerAppEnvironmentResource> AddAzureCon
272
329
273
330
return builder . AddResource ( containerAppEnvResource ) ;
274
331
}
332
+
333
+ /// <summary>
334
+ /// Configures the container app environment resources to use the same naming conventions as azd.
335
+ /// </summary>
336
+ /// <param name="builder">The AzureContainerAppEnvironmentResource to configure.</param>
337
+ /// <returns><see cref="IResourceBuilder{T}"/></returns>
338
+ /// <remarks>
339
+ /// By default, the container app environment resources use a different naming convention than azd.
340
+ ///
341
+ /// This method allows for reusing the previously deployed resources if the application was deployed using
342
+ /// azd without calling <see cref="AddAzureContainerAppEnvironment"/>
343
+ /// </remarks>
344
+ public static IResourceBuilder < AzureContainerAppEnvironmentResource > WithAzdResourceNaming ( this IResourceBuilder < AzureContainerAppEnvironmentResource > builder )
345
+ {
346
+ builder . Resource . UseAzdNamingConvention = true ;
347
+ return builder ;
348
+ }
275
349
}
0 commit comments