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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@cdktf/provider-azurerm": "^14.23.1",
"@cdktf/provider-cloudflare": "^13.9.1",
"@pulumi/aws": "^7.16.0",
"@pulumi/azure": "^6.31.0",
"@pulumi/azure-native": "^3.12.1",
"@pulumi/cloudflare": "^6.13.0",
"@pulumi/command": "^1.1.3",
Expand Down
430 changes: 430 additions & 0 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/lib/azure/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum AzureRemoteBackend {
local = 'local',
azurerm = 'azurerm',
pulumi = 'pulumi',
local = 'local',
}

/**
Expand Down
80 changes: 32 additions & 48 deletions src/lib/azure/common/construct.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { DataAzurermClientConfig } from '@cdktf/provider-azurerm/lib/data-azurerm-client-config/index.js'
import { AzurermProvider } from '@cdktf/provider-azurerm/lib/provider/index.js'
import { AzurermBackend, TerraformStack } from 'cdktf'
import { Provider } from 'cdktf-local-exec'
import { Construct } from 'constructs'
import { ComponentResource, ComponentResourceOptions } from '@pulumi/pulumi'
import { isDevStage, isPrdStage, isTestStage, isUatStage } from '../../common/index.js'
import {
AzureApiManagementManager,
Expand All @@ -14,43 +10,60 @@ import {
AzureEventgridManager,
AzureFunctionManager,
AzureKeyVaultManager,
AzureLogAnalyticsWorkspaceManager,
AzureOperationalInsightsManager,
AzureMonitorManager,
AzureRedisManager,
AzureResourceGroupManager,
AzureServicebusManager,
AzureStorageManager,
} from '../services/index.js'
import { AzureRemoteBackend } from './constants.js'
import { AzureResourceNameFormatter } from './resource-name-formatter.js'
import { CommonAzureStackProps } from './types.js'

export class CommonAzureConstruct extends TerraformStack {
/**
* @classdesc Common Azure construct to use as a base for all higher level constructs using Pulumi
* - Provides manager instances for all Azure services
* - Handles resource naming conventions
* - Manages common properties and utilities
* @example
* ```typescript
* import { CommonAzureConstruct } from '@gradientedge/cdk-utils'
*
* class CustomConstruct extends CommonAzureConstruct {
* constructor(name: string, props: CommonAzureStackProps) {
* super(name, props)
* // provision resources using this.resourceGroupManager, etc.
* }
* }
* ```
*/
export class CommonAzureConstruct extends ComponentResource {
declare props: CommonAzureStackProps
declare options?: ComponentResourceOptions
id: string
fullyQualifiedDomainName: string
apiManagementManager: AzureApiManagementManager
appConfigurationManager: AzureAppConfigurationManager
appServiceManager: AzureAppServiceManager
applicationInsightsManager: AzureApplicationInsightsManager
cosmosDbManager: AzureCosmosDbManager
dnsManager: AzureDnsManager
eventgridManager: AzureEventgridManager
fullyQualifiedDomainName: string
functiontManager: AzureFunctionManager
id: string
functionManager: AzureFunctionManager
keyVaultManager: AzureKeyVaultManager
logAnalyticsWorkspaceManager: AzureLogAnalyticsWorkspaceManager
operationalInsightsManager: AzureOperationalInsightsManager
monitorManager: AzureMonitorManager
redisManager: AzureRedisManager
resourceGroupManager: AzureResourceGroupManager
resourceNameFormatter: AzureResourceNameFormatter
servicebusManager: AzureServicebusManager
storageManager: AzureStorageManager
tenantId: string

constructor(scope: Construct, id: string, props: CommonAzureStackProps) {
super(scope, id)
constructor(name: string, props: CommonAzureStackProps, options?: ComponentResourceOptions) {
super(`custom:azure:Construct:${name}`, name, props, options)
this.props = props
this.id = id
this.options = options
this.id = name

this.apiManagementManager = new AzureApiManagementManager()
this.appConfigurationManager = new AzureAppConfigurationManager()
Expand All @@ -59,22 +72,17 @@ export class CommonAzureConstruct extends TerraformStack {
this.cosmosDbManager = new AzureCosmosDbManager()
this.dnsManager = new AzureDnsManager()
this.eventgridManager = new AzureEventgridManager()
this.functiontManager = new AzureFunctionManager()
this.functionManager = new AzureFunctionManager()
this.keyVaultManager = new AzureKeyVaultManager()
this.logAnalyticsWorkspaceManager = new AzureLogAnalyticsWorkspaceManager()
this.operationalInsightsManager = new AzureOperationalInsightsManager()
this.monitorManager = new AzureMonitorManager()
this.redisManager = new AzureRedisManager()
this.resourceGroupManager = new AzureResourceGroupManager()
this.resourceNameFormatter = new AzureResourceNameFormatter(this, `${id}-rnf`, props)
this.resourceNameFormatter = new AzureResourceNameFormatter(props)
this.servicebusManager = new AzureServicebusManager()
this.storageManager = new AzureStorageManager()

this.determineFullyQualifiedDomain()
this.determineRemoteBackend()
this.determineTenantId()

new AzurermProvider(this, `${this.id}-provider`, this.props)
new Provider(this, `${this.id}-local-exec-provider`)
}

/**
Expand All @@ -86,30 +94,6 @@ export class CommonAzureConstruct extends TerraformStack {
: this.props.domainName
}

protected determineRemoteBackend() {
const debug = this.node.tryGetContext('debug')
switch (this.props.remoteBackend?.type) {
case AzureRemoteBackend.azurerm:
new AzurermBackend(this, {
storageAccountName: this.props.remoteBackend.storageAccountName,
containerName: this.props.remoteBackend.containerName,
key: `${this.id}`,
subscriptionId: this.props.subscriptionId,
resourceGroupName: this.props.remoteBackend.resourceGroupName,
})
break
case AzureRemoteBackend.local:
if (debug) console.debug(`Using local backend for ${this.id}`)
break
default:
break
}
}

protected determineTenantId() {
this.tenantId = new DataAzurermClientConfig(this, 'current', {}).tenantId
}

/**
* @summary Utility method to determine if the initialisation is in development (dev) stage
* This is determined by the stage property injected via cdk context
Expand Down
16 changes: 10 additions & 6 deletions src/lib/azure/common/resource-name-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Construct } from 'constructs'
import { AzureResourceNameFormatterProps, CommonAzureStackProps } from '../index.js'

export class AzureResourceNameFormatter extends Construct {
/**
* @classdesc Formats Azure resource names according to naming conventions
* - Applies global/resource prefixes and suffixes
* - Automatically appends stage to resource names
* - Supports per-resource customization via options
*/
export class AzureResourceNameFormatter {
props: CommonAzureStackProps

constructor(parent: Construct, id: string, props: CommonAzureStackProps) {
super(parent, id)
constructor(props: CommonAzureStackProps) {
this.props = props
}

Expand All @@ -15,15 +19,15 @@ export class AzureResourceNameFormatter extends Construct {
* @param options Options to control the formatting of the resource name
* @returns The formatted Azure-compliant resource name
*/
public format(resourceName: string, options?: AzureResourceNameFormatterProps) {
public format(resourceName: string | undefined, options?: AzureResourceNameFormatterProps): string {
const azureResourceNameElements = []

if (!options?.exclude) {
azureResourceNameElements.push(options?.globalPrefix ? this.props.globalPrefix : undefined)
azureResourceNameElements.push(options?.prefix ?? this.props.resourcePrefix)
}

azureResourceNameElements.push(resourceName)
azureResourceNameElements.push(resourceName || '')

if (!options?.exclude) {
azureResourceNameElements.push(options?.suffix ?? this.props.resourceSuffix)
Expand Down
Loading