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(core): add Tokenization.isResolvable for aspects #3122

Merged
merged 1 commit into from
Jun 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 52 additions & 0 deletions packages/@aws-cdk/aws-s3/test/test.aspect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// import { expect, haveResource, haveResourceLike, SynthUtils } from '@aws-cdk/assert';
import { SynthUtils } from '@aws-cdk/assert';
import cdk = require('@aws-cdk/core');
import { Stack, Tokenization } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import s3 = require('../lib');

export = {
'bucket must have versioning: failure'(test: Test) {
// GIVEN
const stack = new Stack();
new s3.Bucket(stack, 'MyBucket');

// WHEN
stack.node.applyAspect(new BucketVersioningChecker());

// THEN
const assembly = SynthUtils.synthesize(stack);
const errorMessage = assembly.messages.find(m => m.entry.data === 'Bucket versioning is not enabled');
test.ok(errorMessage, 'Error message not reported');

test.done();
},

'bucket must have versioning: success'(test: Test) {
// GIVEN
const stack = new Stack();
new s3.Bucket(stack, 'MyBucket', {
versioned: true
});

// WHEN
stack.node.applyAspect(new BucketVersioningChecker());

// THEN
const assembly = SynthUtils.synthesize(stack);
test.deepEqual(assembly.messages, []);

test.done();
},
};

class BucketVersioningChecker implements cdk.IAspect {
public visit(node: cdk.IConstruct): void {
if (node instanceof s3.CfnBucket) {
if (!node.versioningConfiguration ||
(!Tokenization.isResolvable(node.versioningConfiguration) && node.versioningConfiguration.status !== 'Enabled')) {
node.node.addError('Bucket versioning is not enabled');
}
}
}
}
11 changes: 11 additions & 0 deletions packages/@aws-cdk/core/lib/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ export class Tokenization {
return resolve(obj, options);
}

/**
* Return whether the given object is an IResolvable object
*
* This is different from Token.isUnresolved() which will also check for
* encoded Tokens, whereas this method will only do a type check on the given
* object.
*/
public static isResolvable(obj: any): obj is IResolvable {
return isResolvableObject(obj);
}

private constructor() {
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/aws-cdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.