|
| 1 | +import { CfnOutput, CustomResource, Lazy } from '@aws-cdk/core'; |
| 2 | +import { Construct, IConstruct, Node } from 'constructs'; |
| 3 | +import { md5hash } from './private/hash'; |
| 4 | +import { RESULTS_RESOURCE_TYPE, AssertionsProvider } from './providers'; |
| 5 | +import { SdkQuery, SdkQueryOptions } from './sdk'; |
| 6 | + |
| 7 | +const DEPLOY_ASSERT_SYMBOL = Symbol.for('@aws-cdk/integ-tests.DeployAssert'); |
| 8 | + |
| 9 | +// keep this import separate from other imports to reduce chance for merge conflicts with v2-main |
| 10 | +// eslint-disable-next-line no-duplicate-imports, import/order |
| 11 | +import { Construct as CoreConstruct } from '@aws-cdk/core'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Represents a deploy time assertion |
| 15 | + */ |
| 16 | +export interface IAssertion { |
| 17 | + /** |
| 18 | + * The result of the assertion |
| 19 | + */ |
| 20 | + readonly result: string; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Options for DeployAssert |
| 25 | + */ |
| 26 | +export interface DeployAssertProps { } |
| 27 | + |
| 28 | +/** |
| 29 | + * Construct that allows for registering a list of assertions |
| 30 | + * that should be performed on a construct |
| 31 | + */ |
| 32 | +export class DeployAssert extends CoreConstruct { |
| 33 | + |
| 34 | + /** |
| 35 | + * Returns whether the construct is a DeployAssert construct |
| 36 | + */ |
| 37 | + public static isDeployAssert(x: any): x is DeployAssert { |
| 38 | + return x !== null && typeof(x) === 'object' && DEPLOY_ASSERT_SYMBOL in x; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Finds a DeployAssert construct in the given scope |
| 43 | + */ |
| 44 | + public static of(construct: IConstruct): DeployAssert { |
| 45 | + const scopes = Node.of(construct).scopes.reverse(); |
| 46 | + const deployAssert = scopes.find(s => DeployAssert.isDeployAssert(s)); |
| 47 | + if (!deployAssert) { |
| 48 | + throw new Error('No DeployAssert construct found in scopes'); |
| 49 | + } |
| 50 | + return deployAssert as DeployAssert; |
| 51 | + } |
| 52 | + |
| 53 | + /** @internal */ |
| 54 | + public readonly _assertions: IAssertion[]; |
| 55 | + |
| 56 | + constructor(scope: Construct) { |
| 57 | + super(scope, 'DeployAssert'); |
| 58 | + |
| 59 | + Object.defineProperty(this, DEPLOY_ASSERT_SYMBOL, { value: true }); |
| 60 | + this._assertions = []; |
| 61 | + |
| 62 | + const provider = new AssertionsProvider(this, 'ResultsProvider'); |
| 63 | + |
| 64 | + const resource = new CustomResource(this, 'ResultsCollection', { |
| 65 | + serviceToken: provider.serviceToken, |
| 66 | + properties: { |
| 67 | + assertionResults: Lazy.list({ |
| 68 | + produce: () => this._assertions.map(a => a.result), |
| 69 | + }), |
| 70 | + }, |
| 71 | + resourceType: RESULTS_RESOURCE_TYPE, |
| 72 | + }); |
| 73 | + |
| 74 | + // TODO: need to show/store this information |
| 75 | + new CfnOutput(this, 'Results', { |
| 76 | + value: `\n${resource.getAttString('message')}`, |
| 77 | + }).overrideLogicalId('Results'); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Query AWS using JavaScript SDK V2 API calls |
| 82 | + */ |
| 83 | + public queryAws(options: SdkQueryOptions): SdkQuery { |
| 84 | + const id = md5hash(options); |
| 85 | + return new SdkQuery(this, `SdkQuery${id}`, options); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Register an assertion that should be run as part of the |
| 90 | + * deployment |
| 91 | + */ |
| 92 | + public registerAssertion(assertion: IAssertion) { |
| 93 | + this._assertions.push(assertion); |
| 94 | + } |
| 95 | +} |
0 commit comments