From 5c01570c9845030762aba343a9aed1b06a09e175 Mon Sep 17 00:00:00 2001 From: Vincent De Smet Date: Wed, 11 Sep 2024 21:47:26 +0700 Subject: [PATCH] feat: CDKTF Component improvements - Add `this.awsProvider(alias: string)` method to FoggStack helper - Add `this.remoteState(name: string)` method to FoggStack helper - Fix `default_tags` configuration from `fogg.yaml` parsing in FoggStack helper --- .../cdktf/src/helpers/fogg-stack.ts.tmpl | 64 ++++++++++++++++--- .../test/lambda/src/helpers/fogg-stack.ts | 64 ++++++++++++++++--- .../test/network/src/helpers/fogg-stack.ts | 64 ++++++++++++++++--- 3 files changed, 165 insertions(+), 27 deletions(-) diff --git a/templates/templates/component/cdktf/src/helpers/fogg-stack.ts.tmpl b/templates/templates/component/cdktf/src/helpers/fogg-stack.ts.tmpl index 4086afc76..896b3d056 100644 --- a/templates/templates/component/cdktf/src/helpers/fogg-stack.ts.tmpl +++ b/templates/templates/component/cdktf/src/helpers/fogg-stack.ts.tmpl @@ -7,6 +7,7 @@ import { TerraformStack, S3Backend, S3BackendConfig, + DataTerraformRemoteState, DataTerraformRemoteStateS3, DataTerraformRemoteStateS3Config, TerraformHclModule, @@ -44,6 +45,8 @@ export class FoggStack extends TerraformStack { public readonly foggComp: Component; public readonly modules: Record = {}; public readonly locals: Record = {}; + private readonly _providers: Record = {}; + private readonly _remoteStates: Record = {}; constructor(scope: Construct, id: string, props: FoggStackProps) { super(scope, id); @@ -78,6 +81,32 @@ export class FoggStack extends TerraformStack { this.setModuleVariables(id, variables); } + /** + * Return a remote state defined in the fogg component configuration. + * @param name - The name of the remote state to get + * @returns the DataTerraformRemoteState object + * @throws if the remote state is not found + */ + public remoteState(name: string): DataTerraformRemoteState { + if (!this._remoteStates[name]) { + throw new Error(`Remote state ${name} not found`); + } + return this._remoteStates[name]; + } + + /** + * Return a provider defined in the fogg component configuration. + * @param alias - The alias of the provider to get + * @returns the AwsProvider object + * @throws if the provider is not found + */ + public awsProvider(alias: string): awsProvider.AwsProvider { + if (!this._providers[alias]) { + throw new Error(`Provider ${alias} not found`); + } + return this._providers[alias]; + } + /** * Set variables for a module included in the fogg component modules[] configuration. * @@ -154,14 +183,11 @@ export class FoggStack extends TerraformStack { roleArn: s3Config.role_arn, }; } - // console.log( - // `Setting ${id} Remote backend Config ${JSON.stringify( - // remoteStateConfig, - // null, - // 2 - // )}` - // ); - new DataTerraformRemoteStateS3(this, id, remoteStateConfig); + this._remoteStates[id] = new DataTerraformRemoteStateS3( + this, + id, + remoteStateConfig, + ); } else { throw new Error(`Unsupported backend configuration ${remoteConfig.kind}`); } @@ -244,6 +270,22 @@ export class FoggStack extends TerraformStack { region: config.region, alias: config.alias, }; + if (config.default_tags && config.default_tags.enabled) { + c.defaultTags = [ + { + tags: { + env: this.foggComp.env, + owner: this.foggComp.owner, + project: this.foggComp.project, + managedBy: "terraform", + service: this.foggComp.name, + ...(this.foggComp.backend.s3?.key_path && { + tfstateKey: this.foggComp.backend.s3?.key_path, + }), + }, + }, + ]; + } if (config.profile) { c.profile = config.profile; } else if (config.role_arn) { @@ -253,7 +295,11 @@ export class FoggStack extends TerraformStack { }, ]; } - new awsProvider.AwsProvider(this, id, c); + this._providers[c.alias ?? "Default"] = new awsProvider.AwsProvider( + this, + id, + c, + ); } private parseDataDogProviderConfig(_config: DatadogProvider): void { diff --git a/testdata/v2_cdktf_components/terraform/envs/test/lambda/src/helpers/fogg-stack.ts b/testdata/v2_cdktf_components/terraform/envs/test/lambda/src/helpers/fogg-stack.ts index bb28b215b..48a5694ed 100644 --- a/testdata/v2_cdktf_components/terraform/envs/test/lambda/src/helpers/fogg-stack.ts +++ b/testdata/v2_cdktf_components/terraform/envs/test/lambda/src/helpers/fogg-stack.ts @@ -9,6 +9,7 @@ import { TerraformStack, S3Backend, S3BackendConfig, + DataTerraformRemoteState, DataTerraformRemoteStateS3, DataTerraformRemoteStateS3Config, TerraformHclModule, @@ -46,6 +47,8 @@ export class FoggStack extends TerraformStack { public readonly foggComp: Component; public readonly modules: Record = {}; public readonly locals: Record = {}; + private readonly _providers: Record = {}; + private readonly _remoteStates: Record = {}; constructor(scope: Construct, id: string, props: FoggStackProps) { super(scope, id); @@ -80,6 +83,32 @@ export class FoggStack extends TerraformStack { this.setModuleVariables(id, variables); } + /** + * Return a remote state defined in the fogg component configuration. + * @param name - The name of the remote state to get + * @returns the DataTerraformRemoteState object + * @throws if the remote state is not found + */ + public remoteState(name: string): DataTerraformRemoteState { + if (!this._remoteStates[name]) { + throw new Error(`Remote state ${name} not found`); + } + return this._remoteStates[name]; + } + + /** + * Return a provider defined in the fogg component configuration. + * @param alias - The alias of the provider to get + * @returns the AwsProvider object + * @throws if the provider is not found + */ + public awsProvider(alias: string): awsProvider.AwsProvider { + if (!this._providers[alias]) { + throw new Error(`Provider ${alias} not found`); + } + return this._providers[alias]; + } + /** * Set variables for a module included in the fogg component modules[] configuration. * @@ -156,14 +185,11 @@ export class FoggStack extends TerraformStack { roleArn: s3Config.role_arn, }; } - // console.log( - // `Setting ${id} Remote backend Config ${JSON.stringify( - // remoteStateConfig, - // null, - // 2 - // )}` - // ); - new DataTerraformRemoteStateS3(this, id, remoteStateConfig); + this._remoteStates[id] = new DataTerraformRemoteStateS3( + this, + id, + remoteStateConfig, + ); } else { throw new Error(`Unsupported backend configuration ${remoteConfig.kind}`); } @@ -246,6 +272,22 @@ export class FoggStack extends TerraformStack { region: config.region, alias: config.alias, }; + if (config.default_tags && config.default_tags.enabled) { + c.defaultTags = [ + { + tags: { + env: this.foggComp.env, + owner: this.foggComp.owner, + project: this.foggComp.project, + managedBy: "terraform", + service: this.foggComp.name, + ...(this.foggComp.backend.s3?.key_path && { + tfstateKey: this.foggComp.backend.s3?.key_path, + }), + }, + }, + ]; + } if (config.profile) { c.profile = config.profile; } else if (config.role_arn) { @@ -255,7 +297,11 @@ export class FoggStack extends TerraformStack { }, ]; } - new awsProvider.AwsProvider(this, id, c); + this._providers[c.alias ?? "Default"] = new awsProvider.AwsProvider( + this, + id, + c, + ); } private parseDataDogProviderConfig(_config: DatadogProvider): void { diff --git a/testdata/v2_cdktf_components/terraform/envs/test/network/src/helpers/fogg-stack.ts b/testdata/v2_cdktf_components/terraform/envs/test/network/src/helpers/fogg-stack.ts index bb28b215b..48a5694ed 100644 --- a/testdata/v2_cdktf_components/terraform/envs/test/network/src/helpers/fogg-stack.ts +++ b/testdata/v2_cdktf_components/terraform/envs/test/network/src/helpers/fogg-stack.ts @@ -9,6 +9,7 @@ import { TerraformStack, S3Backend, S3BackendConfig, + DataTerraformRemoteState, DataTerraformRemoteStateS3, DataTerraformRemoteStateS3Config, TerraformHclModule, @@ -46,6 +47,8 @@ export class FoggStack extends TerraformStack { public readonly foggComp: Component; public readonly modules: Record = {}; public readonly locals: Record = {}; + private readonly _providers: Record = {}; + private readonly _remoteStates: Record = {}; constructor(scope: Construct, id: string, props: FoggStackProps) { super(scope, id); @@ -80,6 +83,32 @@ export class FoggStack extends TerraformStack { this.setModuleVariables(id, variables); } + /** + * Return a remote state defined in the fogg component configuration. + * @param name - The name of the remote state to get + * @returns the DataTerraformRemoteState object + * @throws if the remote state is not found + */ + public remoteState(name: string): DataTerraformRemoteState { + if (!this._remoteStates[name]) { + throw new Error(`Remote state ${name} not found`); + } + return this._remoteStates[name]; + } + + /** + * Return a provider defined in the fogg component configuration. + * @param alias - The alias of the provider to get + * @returns the AwsProvider object + * @throws if the provider is not found + */ + public awsProvider(alias: string): awsProvider.AwsProvider { + if (!this._providers[alias]) { + throw new Error(`Provider ${alias} not found`); + } + return this._providers[alias]; + } + /** * Set variables for a module included in the fogg component modules[] configuration. * @@ -156,14 +185,11 @@ export class FoggStack extends TerraformStack { roleArn: s3Config.role_arn, }; } - // console.log( - // `Setting ${id} Remote backend Config ${JSON.stringify( - // remoteStateConfig, - // null, - // 2 - // )}` - // ); - new DataTerraformRemoteStateS3(this, id, remoteStateConfig); + this._remoteStates[id] = new DataTerraformRemoteStateS3( + this, + id, + remoteStateConfig, + ); } else { throw new Error(`Unsupported backend configuration ${remoteConfig.kind}`); } @@ -246,6 +272,22 @@ export class FoggStack extends TerraformStack { region: config.region, alias: config.alias, }; + if (config.default_tags && config.default_tags.enabled) { + c.defaultTags = [ + { + tags: { + env: this.foggComp.env, + owner: this.foggComp.owner, + project: this.foggComp.project, + managedBy: "terraform", + service: this.foggComp.name, + ...(this.foggComp.backend.s3?.key_path && { + tfstateKey: this.foggComp.backend.s3?.key_path, + }), + }, + }, + ]; + } if (config.profile) { c.profile = config.profile; } else if (config.role_arn) { @@ -255,7 +297,11 @@ export class FoggStack extends TerraformStack { }, ]; } - new awsProvider.AwsProvider(this, id, c); + this._providers[c.alias ?? "Default"] = new awsProvider.AwsProvider( + this, + id, + c, + ); } private parseDataDogProviderConfig(_config: DatadogProvider): void {