Skip to content

Commit a57eee3

Browse files
vgkowskidzeno
andauthored
feat: add global removal to DataLakeStorage and make it availble cros… (#104)
* feat: add global removal to DataLakeStorage and make it availble cross construct * update AnalyticsBucket * Update framework/test/unit/data-lake/data-lake-storage.test.ts Co-authored-by: Dženan Softić <dzeno10@gmail.com> --------- Co-authored-by: Dženan Softić <dzeno10@gmail.com>
1 parent b8cc2ce commit a57eee3

File tree

8 files changed

+234
-78
lines changed

8 files changed

+234
-78
lines changed

framework/src/data-lake/analytics-bucket.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// SPDX-License-Identifier: MIT-0
33

44

5-
import { Annotations, Duration, Names, RemovalPolicy } from 'aws-cdk-lib';
5+
import { Duration, Names, RemovalPolicy } from 'aws-cdk-lib';
66
import { Bucket, BucketEncryption, BlockPublicAccess } from 'aws-cdk-lib/aws-s3';
77
import { Construct } from 'constructs';
88

99
import { AnalyticsBucketProps } from './analytics-bucket-props';
10+
import { Context } from '../utils';
1011

1112
/**
1213
* Amazon S3 Bucket configured with best-practices and defaults for analytics.
@@ -35,24 +36,13 @@ import { AnalyticsBucketProps } from './analytics-bucket-props';
3536
export class AnalyticsBucket extends Bucket {
3637

3738
private static LIFECYCLE_RULE = [{ abortIncompleteMultipartUploadAfter: Duration.days(1) }];
38-
private static FRAMEWORK_CONTEXT_VALUES = 'adsf';
3939

4040
constructor(scope: Construct, id: string, props: AnalyticsBucketProps) {
4141

4242
const bucketName = (props?.bucketName || 'analytics-bucket') + '-' + Names.uniqueResourceName(scope, {}).toLowerCase();
4343

44-
const globalRemovalPolicy = scope.node.tryGetContext(AnalyticsBucket.FRAMEWORK_CONTEXT_VALUES)?.remove_data_on_destroy.toLowerCase() == 'true' || false;
45-
const removalPolicy = props?.removalPolicy == RemovalPolicy.DESTROY && globalRemovalPolicy ? RemovalPolicy.DESTROY : RemovalPolicy.RETAIN;
46-
const autoDeleteObjects = (removalPolicy == RemovalPolicy.DESTROY) && globalRemovalPolicy;
47-
48-
if (props?.removalPolicy == RemovalPolicy.DESTROY && !globalRemovalPolicy) {
49-
Annotations.of(scope).addWarning(
50-
`WARNING: removalPolicy was reverted back to 'RemovalPolicy.RETAIN'.
51-
If you wish to set 'removalPolicy' to 'DESTROY' you must also
52-
set the global removal policy flag context variable in the 'cdk.json'
53-
or 'cdk.context.json': "adsf": { "remove_data_on_destroy": "true" }.`,
54-
);
55-
}
44+
const removalPolicy = Context.revertRemovalPolicy(scope, props?.removalPolicy);
45+
const autoDeleteObjects = removalPolicy == RemovalPolicy.DESTROY;
5646

5747
super(scope, id, {
5848
...props,

framework/src/data-lake/data-lake-storage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Construct } from 'constructs';
1010
import { AccessLogsBucket } from './access-logs-bucket';
1111
import { AnalyticsBucket } from './analytics-bucket';
1212
import { TrackedConstruct, TrackedConstructProps } from '../utils';
13+
import { Context } from '../utils/context';
1314

1415

1516
/**
@@ -132,7 +133,7 @@ export class DataLakeStorage extends TrackedConstruct {
132133
super(scope, id, trackedConstructProps);
133134

134135
this.accessLogsBucket = new AccessLogsBucket(this, 'AccessLogsBucket');
135-
const removalPolicy = props?.removalPolicy || RemovalPolicy.RETAIN;
136+
const removalPolicy = Context.revertRemovalPolicy(this, props?.removalPolicy);
136137

137138
// create the key if it's not provided in the parameters
138139
this.dataLakeKey = props?.dataLakeKey || new Key(this, 'DataKey', {

framework/src/utils/context-options.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

framework/src/utils/context.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
4+
import { Annotations, RemovalPolicy } from 'aws-cdk-lib';
5+
import { Construct } from 'constructs';
6+
7+
/**
8+
* @internal
9+
* Utils class to work with the CDK context and options
10+
*/
11+
export class Context {
12+
/**
13+
* Method to revert a DESTROY removal policy to RETAIN if the global removal policy parameter
14+
* in the CDK context is not set to true.
15+
* Also create a warning to warn the user if the retention policy has been reverted to RETAIN.
16+
* @param retentionPolicy The retention policy provided to the construct
17+
* @return the new retention policy based on the global retention parameter set in the CDK context
18+
*/
19+
public static revertRemovalPolicy(scope: Construct, removalPolicy?: RemovalPolicy): RemovalPolicy {
20+
const globalRemovalPolicy = scope.node.tryGetContext(ContextOptions.REMOVE_DATA_ON_DESTROY) || false;
21+
22+
if (removalPolicy == RemovalPolicy.DESTROY && !globalRemovalPolicy) {
23+
Annotations.of(scope).addWarning(
24+
`WARNING: removalPolicy was reverted back to 'RemovalPolicy.RETAIN'.
25+
If you wish to set 'removalPolicy' to 'DESTROY' you must also
26+
set the global removal policy flag context variable in the 'cdk.json'
27+
or 'cdk.context.json': '@aws-data-solutions-framework/removeDataOnDestroy: true'`,
28+
);
29+
}
30+
return removalPolicy == RemovalPolicy.DESTROY && globalRemovalPolicy ? RemovalPolicy.DESTROY : RemovalPolicy.RETAIN;
31+
}
32+
}
33+
34+
/**
35+
* @internal
36+
* The options used in the CDK context
37+
*/
38+
export enum ContextOptions {
39+
DISABLE_CONSTRUCTS_DEPLOYMENT_TRACKING = '@aws-data-solutions-framework/disableConstructsDeploymentTracking',
40+
REMOVE_DATA_ON_DESTROY = '@aws-data-solutions-framework/removeDataOnDestroy',
41+
}

framework/src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44

55
export { TrackedConstruct, TrackedConstructProps } from './tracked-construct';
6-
export { ContextOptions } from './context-options';
6+
export { ContextOptions, Context } from './context';
77
export { EmrVersion } from './emr-releases';

framework/src/utils/tracked-construct.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { Stack, Tags } from 'aws-cdk-lib';
55
import { Construct } from 'constructs';
6-
import { ContextOptions } from './context-options';
6+
import { ContextOptions } from './context';
77
import { ADSF_AWS_TAG } from '../constants';
88

99
/**

framework/test/unit/data-lake/analytics-bucket.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('AnalyticsBucket Construct with DESTROY flag set to true', () => {
1919
const app = new App();
2020
const stack = new Stack(app, 'Stack');
2121
// Set context value for global data removal policy
22-
stack.node.setContext('adsf', { remove_data_on_destroy: 'true' });
22+
stack.node.setContext('@aws-data-solutions-framework/removeDataOnDestroy', true);
2323

2424
const encryptionKey = new Key(stack, 'DataKey', {
2525
removalPolicy: RemovalPolicy.DESTROY,
@@ -153,7 +153,7 @@ describe('AnalyticsBucket Construct with DESTROY flag set to false', () => {
153153
const app = new App();
154154
const stack = new Stack(app, 'Stack');
155155
// Set context value for global data removal policy
156-
stack.node.setContext('adsf', { remove_data_on_destroy: 'false' });
156+
stack.node.setContext('@aws-data-solutions-framework/removeDataOnDestroy', false);
157157

158158
const encryptionKey = new Key(stack, 'DataKey', {
159159
removalPolicy: RemovalPolicy.DESTROY,
@@ -174,7 +174,7 @@ describe('AnalyticsBucket Construct with DESTROY flag set to false', () => {
174174

175175
const template = Template.fromStack(stack);
176176

177-
test('AnalyticsBucket should not destroy objects if DESTROY flag is false', () => {
177+
test('DefaultAnalyticsBucket should not destroy objects if DESTROY flag is false', () => {
178178
// Set autoDeleteObjects only if DESTROY flag is true && Removal policy is DESTROY
179179
template.resourceCountIs('Custom::S3AutoDeleteObjects', 0);
180180
});

0 commit comments

Comments
 (0)