Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(azure): add azure default tagging to all resources #760

Merged
merged 2 commits into from
Mar 10, 2025
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
5 changes: 5 additions & 0 deletions src/lib/azure/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ export enum AzureRemoteBackend {
local = 'local',
azurerm = 'azurerm',
}

/**
* List of Azure resources that excludes tags
*/
export const RESOURCES_TO_EXCLUDE_TAGS = new Set(['ApiManagementNamedValue'])
1 change: 1 addition & 0 deletions src/lib/azure/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './constants'
export * from './construct'
export * from './resource-name-formatter'
export * from './stack'
export * from './tagging'
export * from './types'
4 changes: 3 additions & 1 deletion src/lib/azure/common/resource-name-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export class AzureResourceNameFormatter extends Construct {

azureResourceNameElements.push(this.props.stage)

return azureResourceNameElements.filter(Boolean).join('-')
return azureResourceNameElements
.filter(azureResourceNameElements => azureResourceNameElements != undefined)
.join('-')
}
}
6 changes: 4 additions & 2 deletions src/lib/azure/common/stack.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs'
import { CommonAzureConstruct } from './construct'
import { CommonAzureStackProps } from './types'

import { TagsAddingAspect } from './tagging'
import appRoot from 'app-root-path'
import { TerraformStack } from 'cdktf'
import { Aspects, TerraformStack } from 'cdktf'
import { Construct } from 'constructs'
import _ from 'lodash'
import { isDevStage } from '../../common'
Expand Down Expand Up @@ -35,6 +35,8 @@ export class CommonAzureStack extends TerraformStack {
this.determineStageContexts()

this.props = this.determineConstructProps(props)

Aspects.of(this).add(new TagsAddingAspect(this.props.defaultTags || {}))
}

/**
Expand Down
33 changes: 33 additions & 0 deletions src/lib/azure/common/tagging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IAspect } from 'cdktf'
import { IConstruct } from 'constructs'
import { RESOURCES_TO_EXCLUDE_TAGS } from './constants'

type TaggableConstruct = IConstruct & {
tags?: { [key: string]: string } | string[]
tagsInput?: { [key: string]: string } | string[]
}

function isTaggableConstruct(node: IConstruct): node is TaggableConstruct {
return 'tags' in node && 'tagsInput' in node
}

export class TagsAddingAspect implements IAspect {
constructor(private tagsToAdd: Record<string, string>) {}

// This method is called on every Construct within the specified scope (resources, data sources, etc.).
visit(node: IConstruct) {
// We need to take the input value to not create a circular reference
if (!isTaggableConstruct(node)) {
return
}

// Determine if the resource excludes `tags`
if (RESOURCES_TO_EXCLUDE_TAGS.has(node.constructor.name)) {
node.tags = undefined // Completely remove tags for this resource
return
}

const currentTags = node.tagsInput || {}
node.tags = { ...this.tagsToAdd, ...currentTags }
}
}
1 change: 1 addition & 0 deletions src/lib/azure/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface CommonAzureStackProps extends BaseProps, AzurermProviderConfig
resourceSuffix?: string
resourceNameOptions?: { [key: string]: AzureResourceNameFormatterProps }
location?: string
defaultTags?: { [key: string]: string }
}

export interface AzureRemoteBackendProps extends AzurermBackendConfig {
Expand Down
2 changes: 1 addition & 1 deletion src/test/azure/common/cdkConfig/api-management.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
"displayName": "test",
"method": "post",
"urlTemplate": "/test/{path}",
"urlTemplate": "/test/{*path}",
"templateParameter": [
{
"name": "path",
Expand Down
2 changes: 1 addition & 1 deletion src/test/azure/services/api-management-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe('TestAzureApiManagementConstruct', () => {
type: '',
},
],
url_template: '/test/{path}',
url_template: '/test/{*path}',
})
})
})
Expand Down