Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudformation): Add removalPolicy on CustomResource #2770

Merged
merged 13 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions packages/@aws-cdk/aws-cloudformation/lib/custom-resource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import lambda = require('@aws-cdk/aws-lambda');
import sns = require('@aws-cdk/aws-sns');
import { CfnResource, Construct, Resource } from '@aws-cdk/cdk';
import { applyRemovalPolicy, CfnResource, Construct, RemovalPolicy, Resource } from '@aws-cdk/cdk';
import { CfnCustomResource } from './cloudformation.generated';

/**
Expand All @@ -21,9 +21,7 @@ export class CustomResourceProvider {
*/
public static topic(topic: sns.ITopic) { return new CustomResourceProvider(topic.topicArn); }

private constructor(public readonly serviceToken: string) {

}
private constructor(public readonly serviceToken: string) {}
}

/**
Expand Down Expand Up @@ -67,6 +65,13 @@ export interface CustomResourceProps {
* @default - AWS::CloudFormation::CustomResource
*/
readonly resourceType?: string;

/**
* The policy to apply when this resource is removed from the application.
*
* @default cdk.RemovalPolicy.Destroy
*/
readonly removalPolicy?: RemovalPolicy;
}

/**
Expand All @@ -91,6 +96,8 @@ export class CustomResource extends Resource {
...uppercaseProperties(props.properties || {})
}
});

applyRemovalPolicy(this.resource, props.removalPolicy);
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
}

public getAtt(attributeName: string) {
Expand Down
94 changes: 83 additions & 11 deletions packages/@aws-cdk/aws-cloudformation/test/test.resource.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,83 @@
import { expect, haveResource } from '@aws-cdk/assert';
import { expect, haveResource, ResourcePart } from '@aws-cdk/assert';
import lambda = require('@aws-cdk/aws-lambda');
import sns = require('@aws-cdk/aws-sns');
import cdk = require('@aws-cdk/cdk');
import { Test } from 'nodeunit';
import { Test, testCase } from 'nodeunit';
import { CustomResource, CustomResourceProvider } from '../lib';

// tslint:disable:object-literal-key-quotes

export = {
export = testCase({
'custom resources honor removalPolicy': {
'unspecified (aka .Destroy)'(test: Test) {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Test');

// WHEN
new TestCustomResource(stack, 'Custom');

// THEN
expect(stack).to(haveResource('AWS::CloudFormation::CustomResource', {}, ResourcePart.CompleteDefinition));
test.equal(app.synth().tryGetArtifact(stack.name)!.findMetadataByType('aws:cdk:protected').length, 0);

test.done();
},

'.Destroy'(test: Test) {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Test');

// WHEN
new TestCustomResource(stack, 'Custom', { removalPolicy: cdk.RemovalPolicy.Destroy });

// THEN
expect(stack).to(haveResource('AWS::CloudFormation::CustomResource', {}, ResourcePart.CompleteDefinition));
test.equal(app.synth().tryGetArtifact(stack.name)!.findMetadataByType('aws:cdk:protected').length, 0);

test.done();
},

'.Orphan'(test: Test) {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Test');

// WHEN
new TestCustomResource(stack, 'Custom', { removalPolicy: cdk.RemovalPolicy.Orphan });

// THEN
expect(stack).to(haveResource('AWS::CloudFormation::CustomResource', {
DeletionPolicy: 'Retain',
}, ResourcePart.CompleteDefinition));
test.equal(app.synth().tryGetArtifact(stack.name)!.findMetadataByType('aws:cdk:protected').length, 0);

test.done();
},

'.Forbid'(test: Test) {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Test');

// WHEN
new TestCustomResource(stack, 'Custom', { removalPolicy: cdk.RemovalPolicy.Forbid });

// THEN
expect(stack).to(haveResource('AWS::CloudFormation::CustomResource', {
DeletionPolicy: 'Retain',
}, ResourcePart.CompleteDefinition));
test.ok(app.synth().tryGetArtifact(stack.name)!.findMetadataByType('aws:cdk:protected')[0].data);

test.done();
},
},

'custom resource is added twice, lambda is added once'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Test');

// WHEN
new TestCustomResource(stack, 'Custom1');
Expand Down Expand Up @@ -88,7 +155,8 @@ export = {
},

'custom resources can specify a resource type that starts with Custom::'(test: Test) {
const stack = new cdk.Stack();
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Test');
new CustomResource(stack, 'MyCustomResource', {
resourceType: 'Custom::MyCustomResourceType',
provider: CustomResourceProvider.topic(new sns.Topic(stack, 'Provider'))
Expand All @@ -99,7 +167,8 @@ export = {

'fails if custom resource type is invalid': {
'does not start with "Custom::"'(test: Test) {
const stack = new cdk.Stack();
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Test');

test.throws(() => {
new CustomResource(stack, 'MyCustomResource', {
Expand All @@ -112,7 +181,8 @@ export = {
},

'has invalid characters'(test: Test) {
const stack = new cdk.Stack();
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Test');

test.throws(() => {
new CustomResource(stack, 'MyCustomResource', {
Expand All @@ -125,7 +195,8 @@ export = {
},

'is longer than 60 characters'(test: Test) {
const stack = new cdk.Stack();
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Test');

test.throws(() => {
new CustomResource(stack, 'MyCustomResource', {
Expand All @@ -138,10 +209,10 @@ export = {
},

},
};
});

class TestCustomResource extends cdk.Construct {
constructor(scope: cdk.Construct, id: string) {
constructor(scope: cdk.Construct, id: string, opts: { removalPolicy?: cdk.RemovalPolicy } = {}) {
super(scope, id);

const singletonLambda = new lambda.SingletonFunction(this, 'Lambda', {
Expand All @@ -153,7 +224,8 @@ class TestCustomResource extends cdk.Construct {
});

new CustomResource(this, 'Resource', {
provider: CustomResourceProvider.lambda(singletonLambda)
...opts,
provider: CustomResourceProvider.lambda(singletonLambda),
});
}
}