Skip to content

Commit

Permalink
feat(core): add applyRemovalPolicy to IResource (aws#17746)
Browse files Browse the repository at this point in the history
The motivation behind this change is in both the linked issue and the added test case: change removal policy of a child resource with an interface type.

Thanks @skinny85 for pointing me in the right direction.

Closes aws#17728

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mskrip authored and TikiTDO committed Feb 21, 2022
1 parent c68c8d2 commit b158bee
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('ClientVpnAuthorizationRule constructor', () => {
env: { account: 'myAccount', region: 'us-east-1' },
connections: new Connections(),
node: stack.node,
applyRemovalPolicy: () => { },
};
new ClientVpnAuthorizationRule(stack, 'NormalRule', {
cidr: '10.0.10.0/32',
Expand Down Expand Up @@ -50,6 +51,7 @@ describe('ClientVpnAuthorizationRule constructor', () => {
env: { account: 'myAccount', region: 'us-east-1' },
connections: new Connections(),
node: stack.node,
applyRemovalPolicy: () => { },
};
const clientVpnEndpoint: IClientVpnEndpoint = {
endpointId: 'myClientVpnEndpoint',
Expand All @@ -58,6 +60,7 @@ describe('ClientVpnAuthorizationRule constructor', () => {
env: { account: 'myAccount', region: 'us-east-1' },
connections: new Connections(),
node: stack.node,
applyRemovalPolicy: () => { },
};
expect(() => {
new ClientVpnAuthorizationRule(stack, 'RuleBothEndointAndEndpoint', {
Expand Down Expand Up @@ -88,6 +91,7 @@ describe('ClientVpnAuthorizationRule constructor', () => {
env: { account: 'myAccount', region: 'us-east-1' },
connections: new Connections(),
node: stack.node,
applyRemovalPolicy: () => { },
};
new ClientVpnAuthorizationRule(stack, 'RuleWithEndointTypo', {
cidr: '10.0.10.0/32',
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda/lib/function-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export abstract class FunctionBase extends Resource implements IFunction, ec2.IC
node: this.node,
stack: this.stack,
env: this.env,
applyRemovalPolicy: this.applyRemovalPolicy,
},
});
this._invocationGrants[identifier] = grant;
Expand Down
13 changes: 13 additions & 0 deletions packages/@aws-cdk/core/lib/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ export interface IResource extends IConstruct {
* that might be different than the stack they were imported into.
*/
readonly env: ResourceEnvironment;

/**
* Apply the given removal policy to this resource
*
* The Removal Policy controls what happens to this resource when it stops
* being managed by CloudFormation, either because you've removed it from the
* CDK application or because you've made a change that requires the resource
* to be replaced.
*
* The resource can be deleted (`RemovalPolicy.DESTROY`), or left in your AWS
* account for data recovery and cleanup later (`RemovalPolicy.RETAIN`).
*/
applyRemovalPolicy(policy: RemovalPolicy): void;
}

/**
Expand Down
29 changes: 28 additions & 1 deletion packages/@aws-cdk/core/test/resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cxapi from '@aws-cdk/cx-api';
import {
App, App as Root, CfnCondition,
CfnDeletionPolicy, CfnResource, Construct,
Fn, RemovalPolicy, Resource, Stack,
Fn, IResource, RemovalPolicy, Resource, Stack,
} from '../lib';
import { synthesize } from '../lib/private/synthesis';
import { toCloudFormation } from './util';
Expand Down Expand Up @@ -318,6 +318,33 @@ describe('resource', () => {

});

test('applyRemovalPolicy available for interface resources', () => {
class Child extends Resource {
constructor(scope: Construct, id: string) {
super(scope, id);

new CfnResource(this, 'Resource', {
type: 'ChildResourceType',
});
}
}

const stack = new Stack();
const child: IResource = new Child(stack, 'Child');

child.applyRemovalPolicy(RemovalPolicy.RETAIN);

expect(toCloudFormation(stack)).toEqual({
Resources: {
ChildDAB30558: {
DeletionPolicy: 'Retain',
Type: 'ChildResourceType',
UpdateReplacePolicy: 'Retain',
},
},
});
});

test('addDependency adds all dependencyElements of dependent constructs', () => {

class C1 extends Construct {
Expand Down

0 comments on commit b158bee

Please sign in to comment.