@gradientedge/cdk-utils 8.145.0 → 8.146.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ export declare enum RemoteBackend {
2
+ local = "local",
3
+ s3 = "s3"
4
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RemoteBackend = void 0;
4
+ var RemoteBackend;
5
+ (function (RemoteBackend) {
6
+ RemoteBackend["local"] = "local";
7
+ RemoteBackend["s3"] = "s3";
8
+ })(RemoteBackend || (exports.RemoteBackend = RemoteBackend = {}));
@@ -24,7 +24,11 @@ export declare class CommonCloudflareConstruct extends TerraformStack {
24
24
  * @summary Determine the account id based on the cdktf.json context
25
25
  */
26
26
  protected determineAccountId(): void;
27
+ /**
28
+ * @summary Determine the api token based on the cdktf.json context
29
+ */
27
30
  protected determineApiToken(): void;
31
+ protected determineRemoteBackend(): void;
28
32
  /**
29
33
  * @summary Utility method to determine if the initialisation is in development (dev) stage
30
34
  * This is determined by the stage property injected via cdk context
@@ -1,10 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CommonCloudflareConstruct = void 0;
4
- const provider_1 = require("@cdktf/provider-cloudflare/lib/provider");
4
+ const provider_1 = require("@cdktf/provider-aws/lib/provider");
5
+ const provider_2 = require("@cdktf/provider-cloudflare/lib/provider");
5
6
  const cdktf_1 = require("cdktf");
6
7
  const common_1 = require("../../common");
7
8
  const services_1 = require("../services");
9
+ const constants_1 = require("./constants");
10
+ const data_aws_s3_bucket_object_1 = require("@cdktf/provider-aws/lib/data-aws-s3-bucket-object");
8
11
  class CommonCloudflareConstruct extends cdktf_1.TerraformStack {
9
12
  id;
10
13
  fullyQualifiedDomainName;
@@ -33,7 +36,8 @@ class CommonCloudflareConstruct extends cdktf_1.TerraformStack {
33
36
  this.determineFullyQualifiedDomain();
34
37
  this.determineAccountId();
35
38
  this.determineApiToken();
36
- new provider_1.CloudflareProvider(this, `${this.id}-provider`, this.props);
39
+ this.determineRemoteBackend();
40
+ new provider_2.CloudflareProvider(this, `${this.id}-provider`, this.props);
37
41
  }
38
42
  /**
39
43
  * @summary Determine the fully qualified domain name based on domainName & subDomain
@@ -49,9 +53,43 @@ class CommonCloudflareConstruct extends cdktf_1.TerraformStack {
49
53
  determineAccountId() {
50
54
  this.props.accountId = new cdktf_1.TerraformVariable(this, `accountId`, {}).stringValue;
51
55
  }
56
+ /**
57
+ * @summary Determine the api token based on the cdktf.json context
58
+ */
52
59
  determineApiToken() {
53
60
  this.props.apiToken = new cdktf_1.TerraformVariable(this, `apiToken`, {}).stringValue;
54
61
  }
62
+ determineRemoteBackend() {
63
+ const debug = this.node.tryGetContext('debug');
64
+ switch (this.props.remoteBackend?.type) {
65
+ case constants_1.RemoteBackend.s3:
66
+ new provider_1.AwsProvider(this, `${this.id}-aws-provider`, {
67
+ profile: process.env.AWS_PROFILE ?? 'default',
68
+ region: this.props.remoteBackend.region,
69
+ });
70
+ new cdktf_1.S3Backend(this, {
71
+ bucket: this.props.remoteBackend.bucketName,
72
+ dynamodbTable: this.props.remoteBackend.tableName,
73
+ key: `${this.id}`,
74
+ profile: process.env.AWS_PROFILE ?? 'default',
75
+ region: this.props.remoteBackend.region,
76
+ });
77
+ new data_aws_s3_bucket_object_1.DataAwsS3BucketObject(this, `${this.id}-remote-state-ref`, {
78
+ bucket: this.props.remoteBackend.bucketName,
79
+ key: new cdktf_1.DataTerraformRemoteStateS3(this, `${this.id}-remote-state`, {
80
+ bucket: this.props.remoteBackend.bucketName,
81
+ key: `${this.id}`,
82
+ }).getString('bucket_key'),
83
+ });
84
+ break;
85
+ case constants_1.RemoteBackend.local:
86
+ if (debug)
87
+ console.debug(`Using local backend for ${this.id}`);
88
+ break;
89
+ default:
90
+ break;
91
+ }
92
+ }
55
93
  /**
56
94
  * @summary Utility method to determine if the initialisation is in development (dev) stage
57
95
  * This is determined by the stage property injected via cdk context
@@ -1,3 +1,4 @@
1
+ export * from './constants';
1
2
  export * from './construct';
2
3
  export * from './stack';
3
4
  export * from './types';
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./constants"), exports);
17
18
  __exportStar(require("./construct"), exports);
18
19
  __exportStar(require("./stack"), exports);
19
20
  __exportStar(require("./types"), exports);
@@ -38,6 +38,7 @@ class CommonCloudflareStack extends cdktf_1.TerraformStack {
38
38
  * @returns The stack properties
39
39
  */
40
40
  determineConstructProps(props) {
41
+ const stage = this.node.tryGetContext('stage');
41
42
  return {
42
43
  accountId: this.node.tryGetContext('accountId'),
43
44
  apiToken: this.node.tryGetContext('apiToken'),
@@ -46,7 +47,7 @@ class CommonCloudflareStack extends cdktf_1.TerraformStack {
46
47
  features: this.node.tryGetContext('features'),
47
48
  name: this.node.tryGetContext('resourceGroupName'),
48
49
  skipStageForARecords: this.node.tryGetContext('skipStageForARecords'),
49
- stage: this.node.tryGetContext('stage'),
50
+ stage: stage,
50
51
  subDomain: this.node.tryGetContext('subDomain'),
51
52
  };
52
53
  }
@@ -1,9 +1,17 @@
1
1
  import { CloudflareProviderConfig } from '@cdktf/provider-cloudflare/lib/provider';
2
2
  import { BaseProps } from '../../common';
3
+ import { RemoteBackend } from './constants';
4
+ export interface RemoteBackendProps {
5
+ bucketName: string;
6
+ region: string;
7
+ tableName: string;
8
+ type: RemoteBackend;
9
+ }
3
10
  /**
4
11
  */
5
12
  export interface CommonCloudflareStackProps extends BaseProps, CloudflareProviderConfig {
6
13
  accountId: string;
7
14
  apiToken: string;
15
+ remoteBackend?: RemoteBackendProps;
8
16
  useExistingZone?: boolean;
9
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradientedge/cdk-utils",
3
- "version": "8.145.0",
3
+ "version": "8.146.0",
4
4
  "description": "Utilities for AWS CDK provisioning",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -50,6 +50,7 @@
50
50
  "@aws-sdk/client-secrets-manager": "^3.458.0",
51
51
  "@aws-sdk/credential-providers": "^3.458.0",
52
52
  "@aws-sdk/types": "^3.451.0",
53
+ "@cdktf/provider-aws": "^18.0.6",
53
54
  "@cdktf/provider-azurerm": "^11.0.6",
54
55
  "@cdktf/provider-cloudflare": "^10.0.3",
55
56
  "@types/lodash": "^4.14.202",
@@ -0,0 +1,4 @@
1
+ export enum RemoteBackend {
2
+ local = 'local',
3
+ s3 = 's3',
4
+ }
@@ -1,5 +1,6 @@
1
+ import { AwsProvider } from '@cdktf/provider-aws/lib/provider'
1
2
  import { CloudflareProvider } from '@cdktf/provider-cloudflare/lib/provider'
2
- import { TerraformStack, TerraformVariable } from 'cdktf'
3
+ import { DataTerraformRemoteStateS3, S3Backend, TerraformStack, TerraformVariable } from 'cdktf'
3
4
  import { Construct } from 'constructs'
4
5
  import { isDevStage, isPrdStage, isTestStage, isUatStage } from '../../common'
5
6
  import {
@@ -13,7 +14,9 @@ import {
13
14
  CloudflareWorkerManager,
14
15
  CloudflareZoneManager,
15
16
  } from '../services'
17
+ import { RemoteBackend } from './constants'
16
18
  import { CommonCloudflareStackProps } from './types'
19
+ import { DataAwsS3BucketObject } from '@cdktf/provider-aws/lib/data-aws-s3-bucket-object'
17
20
 
18
21
  export class CommonCloudflareConstruct extends TerraformStack {
19
22
  declare props: CommonCloudflareStackProps
@@ -47,13 +50,14 @@ export class CommonCloudflareConstruct extends TerraformStack {
47
50
  this.determineFullyQualifiedDomain()
48
51
  this.determineAccountId()
49
52
  this.determineApiToken()
53
+ this.determineRemoteBackend()
50
54
  new CloudflareProvider(this, `${this.id}-provider`, this.props)
51
55
  }
52
56
 
53
57
  /**
54
58
  * @summary Determine the fully qualified domain name based on domainName & subDomain
55
59
  */
56
- protected determineFullyQualifiedDomain(): void {
60
+ protected determineFullyQualifiedDomain() {
57
61
  this.fullyQualifiedDomainName = this.props.subDomain
58
62
  ? `${this.props.subDomain}.${this.props.domainName}`
59
63
  : this.props.domainName
@@ -62,14 +66,49 @@ export class CommonCloudflareConstruct extends TerraformStack {
62
66
  /**
63
67
  * @summary Determine the account id based on the cdktf.json context
64
68
  */
65
- protected determineAccountId(): void {
69
+ protected determineAccountId() {
66
70
  this.props.accountId = new TerraformVariable(this, `accountId`, {}).stringValue
67
71
  }
68
72
 
69
- protected determineApiToken(): void {
73
+ /**
74
+ * @summary Determine the api token based on the cdktf.json context
75
+ */
76
+ protected determineApiToken() {
70
77
  this.props.apiToken = new TerraformVariable(this, `apiToken`, {}).stringValue
71
78
  }
72
79
 
80
+ protected determineRemoteBackend() {
81
+ const debug = this.node.tryGetContext('debug')
82
+ switch (this.props.remoteBackend?.type) {
83
+ case RemoteBackend.s3:
84
+ new AwsProvider(this, `${this.id}-aws-provider`, {
85
+ profile: process.env.AWS_PROFILE ?? 'default',
86
+ region: this.props.remoteBackend.region,
87
+ })
88
+ new S3Backend(this, {
89
+ bucket: this.props.remoteBackend.bucketName,
90
+ dynamodbTable: this.props.remoteBackend.tableName,
91
+ key: `${this.id}`,
92
+ profile: process.env.AWS_PROFILE ?? 'default',
93
+ region: this.props.remoteBackend.region,
94
+ })
95
+ new DataAwsS3BucketObject(this, `${this.id}-remote-state-ref`, {
96
+ bucket: this.props.remoteBackend.bucketName,
97
+ key: new DataTerraformRemoteStateS3(this, `${this.id}-remote-state`, {
98
+ bucket: this.props.remoteBackend.bucketName,
99
+ key: `${this.id}`,
100
+ }).getString('bucket_key'),
101
+ })
102
+
103
+ break
104
+ case RemoteBackend.local:
105
+ if (debug) console.debug(`Using local backend for ${this.id}`)
106
+ break
107
+ default:
108
+ break
109
+ }
110
+ }
111
+
73
112
  /**
74
113
  * @summary Utility method to determine if the initialisation is in development (dev) stage
75
114
  * This is determined by the stage property injected via cdk context
@@ -1,3 +1,4 @@
1
+ export * from './constants'
1
2
  export * from './construct'
2
3
  export * from './stack'
3
4
  export * from './types'
@@ -7,6 +7,7 @@ import { TerraformStack } from 'cdktf'
7
7
  import { Construct } from 'constructs'
8
8
  import _ from 'lodash'
9
9
  import { isDevStage } from '../../common'
10
+ import { RemoteBackend } from './constants'
10
11
 
11
12
  /**
12
13
  * @classdesc Common stack to use as a base for all higher level constructs.
@@ -42,6 +43,7 @@ export class CommonCloudflareStack extends TerraformStack {
42
43
  * @returns The stack properties
43
44
  */
44
45
  protected determineConstructProps(props: CommonCloudflareStackProps) {
46
+ const stage = this.node.tryGetContext('stage')
45
47
  return {
46
48
  accountId: this.node.tryGetContext('accountId'),
47
49
  apiToken: this.node.tryGetContext('apiToken'),
@@ -50,7 +52,7 @@ export class CommonCloudflareStack extends TerraformStack {
50
52
  features: this.node.tryGetContext('features'),
51
53
  name: this.node.tryGetContext('resourceGroupName'),
52
54
  skipStageForARecords: this.node.tryGetContext('skipStageForARecords'),
53
- stage: this.node.tryGetContext('stage'),
55
+ stage: stage,
54
56
  subDomain: this.node.tryGetContext('subDomain'),
55
57
  }
56
58
  }
@@ -1,10 +1,19 @@
1
1
  import { CloudflareProviderConfig } from '@cdktf/provider-cloudflare/lib/provider'
2
2
  import { BaseProps } from '../../common'
3
+ import { RemoteBackend } from './constants'
4
+
5
+ export interface RemoteBackendProps {
6
+ bucketName: string
7
+ region: string
8
+ tableName: string
9
+ type: RemoteBackend
10
+ }
3
11
 
4
12
  /**
5
13
  */
6
14
  export interface CommonCloudflareStackProps extends BaseProps, CloudflareProviderConfig {
7
15
  accountId: string
8
16
  apiToken: string
17
+ remoteBackend?: RemoteBackendProps
9
18
  useExistingZone?: boolean
10
19
  }