Skip to content

Commit

Permalink
Add tags to CDK-based resources (#2732)
Browse files Browse the repository at this point in the history
* Add tags to top level resources.

* Use construct instead of builder.
  • Loading branch information
mitchdenny authored Mar 11, 2024
1 parent 0759519 commit c236e90
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions playground/cdk/CdkSample.AppHost/cache.module.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ resource keyVault_IeF8jZvXV 'Microsoft.KeyVault/vaults@2023-02-01' existing = {
resource redisCache_p9fE6TK3F 'Microsoft.Cache/Redis@2020-06-01' = {
name: toLower(take(concat('cache', uniqueString(resourceGroup().id)), 24))
location: location
tags: {
'aspire-resource-name': 'cache'
}
properties: {
enableNonSslPort: false
minimumTlsVersion: '1.2'
Expand Down
3 changes: 3 additions & 0 deletions playground/cdk/CdkSample.AppHost/cosmos.module.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ resource keyVault_IeF8jZvXV 'Microsoft.KeyVault/vaults@2023-02-01' existing = {
resource cosmosDBAccount_5pKmb8KAZ 'Microsoft.DocumentDB/databaseAccounts@2023-04-15' = {
name: toLower(take(concat('cosmos', uniqueString(resourceGroup().id)), 24))
location: location
tags: {
'aspire-resource-name': 'cosmos'
}
kind: 'GlobalDocumentDB'
properties: {
databaseAccountOfferType: 'Standard'
Expand Down
3 changes: 3 additions & 0 deletions playground/cdk/CdkSample.AppHost/mykv.module.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ param signaturesecret string
resource keyVault_IKWI2x0B5 'Microsoft.KeyVault/vaults@2023-02-01' = {
name: toLower(take(concat('mykv', uniqueString(resourceGroup().id)), 24))
location: location
tags: {
'aspire-resource-name': 'mykv'
}
properties: {
tenantId: tenant().tenantId
sku: {
Expand Down
3 changes: 3 additions & 0 deletions playground/cdk/CdkSample.AppHost/pgsql.module.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ resource keyVault_IeF8jZvXV 'Microsoft.KeyVault/vaults@2023-02-01' existing = {
resource postgreSqlFlexibleServer_UTKFzAL0U 'Microsoft.DBforPostgreSQL/flexibleServers@2022-12-01' = {
name: toLower(take(concat('pgsql', uniqueString(resourceGroup().id)), 24))
location: location
tags: {
'aspire-resource-name': 'pgsql'
}
sku: {
name: 'Standard_B1ms'
tier: 'Burstable'
Expand Down
3 changes: 3 additions & 0 deletions playground/cdk/CdkSample.AppHost/pgsql2.module.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ resource keyVault_IeF8jZvXV 'Microsoft.KeyVault/vaults@2023-02-01' existing = {
resource postgreSqlFlexibleServer_L4yCjMLWz 'Microsoft.DBforPostgreSQL/flexibleServers@2022-12-01' = {
name: toLower(take(concat('pgsql2', uniqueString(resourceGroup().id)), 24))
location: location
tags: {
'aspire-resource-name': 'pgsql2'
}
sku: {
name: 'Standard_B1ms'
tier: 'Burstable'
Expand Down
3 changes: 3 additions & 0 deletions playground/cdk/CdkSample.AppHost/sql.module.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ param principalName string
resource sqlServer_l5O9GRsSn 'Microsoft.Sql/servers@2022-08-01-preview' = {
name: toLower(take(concat('sql', uniqueString(resourceGroup().id)), 24))
location: location
tags: {
'aspire-resource-name': 'sql'
}
properties: {
version: '12.0'
minimalTlsVersion: '1.2'
Expand Down
3 changes: 3 additions & 0 deletions playground/cdk/CdkSample.AppHost/storage.module.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ param locationOverride string
resource storageAccount_65zdmu5tK 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: toLower(take(concat('storage', uniqueString(resourceGroup().id)), 24))
location: locationOverride
tags: {
'aspire-resource-name': 'storage'
}
sku: {
name: storagesku
}
Expand Down
2 changes: 2 additions & 0 deletions src/Aspire.Hosting.Azure/AzureCosmosDBResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public static IResourceBuilder<AzureCosmosDBConstructResource> AddAzureCosmosDBC
cosmosAccount.AssignProperty(x => x.Locations[0].LocationName, "location");
cosmosAccount.AssignProperty(x => x.Locations[0].FailoverPriority, "0");

cosmosAccount.Properties.Tags["aspire-resource-name"] = construct.Resource.Name;

var keyVaultNameParameter = new Parameter("keyVaultName");
construct.AddParameter(keyVaultNameParameter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static IResourceBuilder<AzureKeyVaultConstructResource> AddAzureKeyVaultC
var keyVault = construct.AddKeyVault(name: construct.Resource.Name);
keyVault.AddOutput(x => x.Properties.VaultUri, "vaultUri");

keyVault.Properties.Tags["aspire-resource-name"] = construct.Resource.Name;

var keyVaultAdministratorRoleAssignment = keyVault.AssignRole(RoleDefinition.KeyVaultAdministrator);
keyVaultAdministratorRoleAssignment.AssignProperty(x => x.PrincipalId, construct.PrincipalIdParameter);
keyVaultAdministratorRoleAssignment.AssignProperty(x => x.PrincipalType, construct.PrincipalTypeParameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ internal static IResourceBuilder<PostgresServerResource> PublishAsAzurePostgresF
postgres.AssignProperty(x => x.Backup.GeoRedundantBackup, "'Disabled'");
postgres.AssignProperty(x => x.AvailabilityZone, "'1'");

postgres.Properties.Tags["aspire-resource-name"] = construct.Resource.Name;

// Opens access to all Azure services.
var azureServicesFirewallRule = new PostgreSqlFirewallRule(construct, "0.0.0.0", "0.0.0.0", postgres, "AllowAllAzureIps");

Expand Down
2 changes: 2 additions & 0 deletions src/Aspire.Hosting.Azure/Extensions/AzureRedisExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ internal static IResourceBuilder<RedisResource> PublishAsAzureRedisConstruct(thi
{
var redisCache = new RedisCache(construct, name: builder.Resource.Name);

redisCache.Properties.Tags["aspire-resource-name"] = construct.Resource.Name;

var vaultNameParameter = new Parameter("keyVaultName");
construct.AddParameter(vaultNameParameter);

Expand Down
2 changes: 2 additions & 0 deletions src/Aspire.Hosting.Azure/Extensions/AzureSqlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ internal static IResourceBuilder<SqlServerServerResource> PublishAsAzureSqlDatab
sqlServer.AssignProperty(x => x.Administrators.Login, construct.PrincipalNameParameter);
sqlServer.AssignProperty(x => x.Administrators.TenantId, "subscription().tenantId");

sqlServer.Properties.Tags["aspire-resource-name"] = construct.Resource.Name;

var azureServicesFirewallRule = new SqlFirewallRule(construct, sqlServer, "AllowAllAzureIps");
azureServicesFirewallRule.AssignProperty(x => x.StartIPAddress, "'0.0.0.0'");
azureServicesFirewallRule.AssignProperty(x => x.EndIPAddress, "'0.0.0.0'");
Expand Down
2 changes: 2 additions & 0 deletions src/Aspire.Hosting.Azure/Extensions/AzureStorageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public static IResourceBuilder<AzureStorageConstructResource> AddAzureConstructS
sku: StorageSkuName.StandardGrs
);

storageAccount.Properties.Tags["aspire-resource-name"] = construct.Resource.Name;

var blobService = new BlobService(construct);

var blobRole = storageAccount.AssignRole(RoleDefinition.StorageBlobDataContributor);
Expand Down

0 comments on commit c236e90

Please sign in to comment.