Skip to content

Commit 3f7a0ad

Browse files
author
Elad Ben-Israel
authored
refactor(core): improvements to Construct API (#2767)
Closes #1934 BREAKING CHANGE: * **core:** `node.stack` is now `Stack.of(construct)` (fixes #2766) * **core:** `node.resolve` has been moved to `stack.resolve`. * **core:** `node.stringifyJson` has been moved to `stack.stringifyJson`. * **core:** `node.validateTree` is now `ConstructNode.validate(node)` * **core:** `node.prepareTree` is now `ConstructNode.prepare(node)` * **core:** `node.getContext` is now `node.tryGetContext` * **core:** `node.recordReference` is now `node.addReference` * **core:** `node.apply` is now `node.applyAspect` * **core:** `node.ancestors()` is now `node.scopes` * **core:** `node.required` has been removed. * **core:** `node.typename` has been removed. * **core:** `node.addChild` is now private * **core:** `node.findReferences()` is now `node.references` * **core:** `node.findDependencies()` is now `node.dependencies` * **core:** `stack.dependencies()` is now `stack.dependencies` * **core:** `CfnElement.stackPath` has been removed. * **core:** `CloudFormationLang` is now internal (use `stack.toJsonString()`)
1 parent 9a34d47 commit 3f7a0ad

File tree

148 files changed

+939
-988
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+939
-988
lines changed

packages/@aws-cdk/app-delivery/lib/pipeline-deploy-stack-action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class PipelineDeployStackAction extends cdk.Construct {
105105
constructor(scope: cdk.Construct, id: string, props: PipelineDeployStackActionProps) {
106106
super(scope, id);
107107

108-
if (!cdk.environmentEquals(props.stack.env, this.node.stack.env)) {
108+
if (!cdk.environmentEquals(props.stack.env, cdk.Stack.of(this).env)) {
109109
// FIXME: Add the necessary to extend to stacks in a different account
110110
throw new Error(`Cross-environment deployment is not supported`);
111111
}

packages/@aws-cdk/app-delivery/test/test.pipeline-deploy-stack-action.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import cpactions = require('@aws-cdk/aws-codepipeline-actions');
66
import iam = require('@aws-cdk/aws-iam');
77
import s3 = require('@aws-cdk/aws-s3');
88
import cdk = require('@aws-cdk/cdk');
9+
import { ConstructNode } from '@aws-cdk/cdk';
910
import cxapi = require('@aws-cdk/cx-api');
1011
import fc = require('fast-check');
1112
import nodeunit = require('nodeunit');
@@ -289,7 +290,7 @@ export = nodeunit.testCase({
289290
for (let i = 0 ; i < assetCount ; i++) {
290291
deployedStack.node.addMetadata(cxapi.ASSET_METADATA, {});
291292
}
292-
test.deepEqual(action.node.validateTree().map(x => x.message),
293+
test.deepEqual(ConstructNode.validate(action.node).map(x => x.message),
293294
[`Cannot deploy the stack DeployedStack because it references ${assetCount} asset(s)`]);
294295
}
295296
)

packages/@aws-cdk/assets/lib/asset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export class Asset extends cdk.Construct implements IAsset {
198198
* (e.g. "Code" for AWS::Lambda::Function)
199199
*/
200200
public addResourceMetadata(resource: cdk.CfnResource, resourceProperty: string) {
201-
if (!this.node.getContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT)) {
201+
if (!this.node.tryGetContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT)) {
202202
return; // not enabled
203203
}
204204

packages/@aws-cdk/assets/lib/staging.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Staging extends Construct {
5757
this.copyOptions = props;
5858
this.sourceHash = fingerprint(this.sourcePath, props);
5959

60-
const stagingDisabled = this.node.getContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT);
60+
const stagingDisabled = this.node.tryGetContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT);
6161
if (stagingDisabled) {
6262
this.stagedPath = this.sourcePath;
6363
} else {

packages/@aws-cdk/assets/test/test.asset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export = {
3131
// verify that now the template contains parameters for this asset
3232
const session = app.synth();
3333

34-
test.deepEqual(stack.node.resolve(entry!.data), {
34+
test.deepEqual(stack.resolve(entry!.data), {
3535
path: SAMPLE_ASSET_DIR,
3636
id: 'MyStackMyAssetBDDF29E3',
3737
packaging: 'zip',
@@ -84,7 +84,7 @@ export = {
8484
// synthesize first so "prepare" is called
8585
const template = SynthUtils.synthesize(stack).template;
8686

87-
test.deepEqual(stack.node.resolve(entry!.data), {
87+
test.deepEqual(stack.resolve(entry!.data), {
8888
path: 'asset.78add9eaf468dfa2191da44a7da92a21baba4c686cf6053d772556768ef21197.txt',
8989
packaging: 'file',
9090
id: 'MyAsset',

packages/@aws-cdk/aws-apigateway/lib/deployment.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Construct, DeletionPolicy, Resource, Token } from '@aws-cdk/cdk';
1+
import { Construct, DeletionPolicy, Resource, Stack, Token } from '@aws-cdk/cdk';
22
import crypto = require('crypto');
33
import { CfnDeployment, CfnDeploymentProps } from './apigateway.generated';
44
import { IRestApi } from './restapi';
@@ -99,7 +99,7 @@ class LatestDeploymentResource extends CfnDeployment {
9999
constructor(scope: Construct, id: string, props: CfnDeploymentProps) {
100100
super(scope, id, props);
101101

102-
this.originalLogicalId = this.node.stack.logicalIds.getLogicalId(this);
102+
this.originalLogicalId = Stack.of(this).logicalIds.getLogicalId(this);
103103
}
104104

105105
/**
@@ -121,12 +121,14 @@ class LatestDeploymentResource extends CfnDeployment {
121121
* add via `addToLogicalId`.
122122
*/
123123
protected prepare() {
124+
const stack = Stack.of(this);
125+
124126
// if hash components were added to the deployment, we use them to calculate
125127
// a logical ID for the deployment resource.
126128
if (this.hashComponents.length > 0) {
127129
const md5 = crypto.createHash('md5');
128130
this.hashComponents
129-
.map(c => this.node.resolve(c))
131+
.map(c => stack.resolve(c))
130132
.forEach(c => md5.update(JSON.stringify(c)));
131133

132134
this.overrideLogicalId(this.originalLogicalId + md5.digest("hex"));

packages/@aws-cdk/aws-apigateway/lib/integrations/aws.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cdk = require('@aws-cdk/cdk');
2+
import { Stack } from '@aws-cdk/cdk';
23
import { Integration, IntegrationOptions, IntegrationType } from '../integration';
34
import { Method } from '../method';
45
import { parseAwsApiCall } from '../util';
@@ -80,7 +81,7 @@ export class AwsIntegration extends Integration {
8081
integrationHttpMethod: props.integrationHttpMethod || 'POST',
8182
uri: new cdk.Token(() => {
8283
if (!this.scope) { throw new Error('AwsIntegration must be used in API'); }
83-
return this.scope.node.stack.formatArn({
84+
return Stack.of(this.scope).formatArn({
8485
service: 'apigateway',
8586
account: backend,
8687
resource: apiType,

packages/@aws-cdk/aws-apigateway/lib/method.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Construct, Resource } from '@aws-cdk/cdk';
1+
import { Construct, Resource, Stack } from '@aws-cdk/cdk';
22
import { CfnMethod, CfnMethodProps } from './apigateway.generated';
33
import { ConnectionType, Integration } from './integration';
44
import { MockIntegration } from './integrations/mock';
@@ -197,7 +197,7 @@ export class Method extends Resource {
197197
} else if (options.credentialsPassthrough) {
198198
// arn:aws:iam::*:user/*
199199
// tslint:disable-next-line:max-line-length
200-
credentials = this.node.stack.formatArn({ service: 'iam', region: '', account: '*', resource: 'user', sep: '/', resourceName: '*' });
200+
credentials = Stack.of(this).formatArn({ service: 'iam', region: '', account: '*', resource: 'user', sep: '/', resourceName: '*' });
201201
}
202202

203203
return {

packages/@aws-cdk/aws-apigateway/lib/restapi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import iam = require('@aws-cdk/aws-iam');
2-
import { CfnOutput, Construct, IResource as IResourceBase, Resource } from '@aws-cdk/cdk';
2+
import { CfnOutput, Construct, IResource as IResourceBase, Resource, Stack } from '@aws-cdk/cdk';
33
import { ApiKey, IApiKey } from './api-key';
44
import { CfnAccount, CfnRestApi } from './apigateway.generated';
55
import { Deployment } from './deployment';
@@ -284,7 +284,7 @@ export class RestApi extends Resource implements IRestApi {
284284
method = '*';
285285
}
286286

287-
return this.node.stack.formatArn({
287+
return Stack.of(this).formatArn({
288288
service: 'execute-api',
289289
resource: this.restApiId,
290290
sep: '/',
@@ -343,7 +343,7 @@ export class RestApi extends Resource implements IRestApi {
343343
private configureCloudWatchRole(apiResource: CfnRestApi) {
344344
const role = new iam.Role(this, 'CloudWatchRole', {
345345
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'),
346-
managedPolicyArns: [ this.node.stack.formatArn({
346+
managedPolicyArns: [ Stack.of(this).formatArn({
347347
service: 'iam',
348348
region: '',
349349
account: 'aws',

packages/@aws-cdk/aws-apigateway/lib/stage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Construct, Resource } from '@aws-cdk/cdk';
1+
import { Construct, Resource, Stack } from '@aws-cdk/cdk';
22
import { CfnStage } from './apigateway.generated';
33
import { Deployment } from './deployment';
44
import { IRestApi } from './restapi';
@@ -209,7 +209,7 @@ export class Stage extends Resource {
209209
if (!path.startsWith('/')) {
210210
throw new Error(`Path must begin with "/": ${path}`);
211211
}
212-
return `https://${this.restApi.restApiId}.execute-api.${this.node.stack.region}.${this.node.stack.urlSuffix}/${this.stageName}${path}`;
212+
return `https://${this.restApi.restApiId}.execute-api.${Stack.of(this).region}.${Stack.of(this).urlSuffix}/${this.stageName}${path}`;
213213
}
214214

215215
private renderMethodSettings(props: StageProps): CfnStage.MethodSettingProperty[] | undefined {

0 commit comments

Comments
 (0)