Skip to content

Commit

Permalink
feat(core): add Tokenization.isResolvable for aspects
Browse files Browse the repository at this point in the history
Add an example of a validating aspect, add `Tokenization.isResolvable()`
to make it easier/possible to rule out `IResolvable`s from L1
properties.

Included unit tests  validates that the bug reported in #3026 is not systemic.
  • Loading branch information
rix0rrr committed Jun 28, 2019
1 parent f2293e0 commit b7fbd1a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
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.

0 comments on commit b7fbd1a

Please sign in to comment.