Skip to content

Commit a46cfd8

Browse files
authored
feat(core): Add construct.node.stack attribute (#1753)
Makes it easier to access some construct's `Stack`, avoiding having to sprinkle `Stack.find(this)` everywhere. BREAKING CHANGE: `Stack.find(c)` and `Stack.tryFind(c)` were replaced by `c.node.stack`. Fixes #798
1 parent 77b516f commit a46cfd8

File tree

53 files changed

+171
-193
lines changed

Some content is hidden

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

53 files changed

+171
-193
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
@@ -104,7 +104,7 @@ export class PipelineDeployStackAction extends cdk.Construct {
104104
constructor(scope: cdk.Construct, id: string, props: PipelineDeployStackActionProps) {
105105
super(scope, id);
106106

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class AwsIntegration extends Integration {
7373
integrationHttpMethod: 'POST',
7474
uri: new cdk.Token(() => {
7575
if (!this.scope) { throw new Error('AwsIntegration must be used in API'); }
76-
return cdk.Stack.find(this.scope).formatArn({
76+
return this.scope.node.stack.formatArn({
7777
service: 'apigateway',
7878
account: backend,
7979
resource: apiType,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export class Method extends cdk.Construct {
189189
} else if (options.credentialsPassthrough) {
190190
// arn:aws:iam::*:user/*
191191
// tslint:disable-next-line:max-line-length
192-
credentials = cdk.Stack.find(this).formatArn({ service: 'iam', region: '', account: '*', resource: 'user', sep: '/', resourceName: '*' });
192+
credentials = this.node.stack.formatArn({ service: 'iam', region: '', account: '*', resource: 'user', sep: '/', resourceName: '*' });
193193
}
194194

195195
return {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export class RestApi extends cdk.Construct implements IRestApi {
271271
method = '*';
272272
}
273273

274-
return cdk.Stack.find(this).formatArn({
274+
return this.node.stack.formatArn({
275275
service: 'execute-api',
276276
resource: this.restApiId,
277277
sep: '/',
@@ -328,7 +328,7 @@ export class RestApi extends cdk.Construct implements IRestApi {
328328
private configureCloudWatchRole(apiResource: CfnRestApi) {
329329
const role = new iam.Role(this, 'CloudWatchRole', {
330330
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'),
331-
managedPolicyArns: [ cdk.Stack.find(this).formatArn({
331+
managedPolicyArns: [ this.node.stack.formatArn({
332332
service: 'iam',
333333
region: '',
334334
account: 'aws',

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import cdk = require('@aws-cdk/cdk');
2-
import { Stack } from '@aws-cdk/cdk';
32
import { CfnStage } from './apigateway.generated';
43
import { Deployment } from './deployment';
54
import { IRestApi } from './restapi';
@@ -179,8 +178,7 @@ export class Stage extends cdk.Construct {
179178
if (!path.startsWith('/')) {
180179
throw new Error(`Path must begin with "/": ${path}`);
181180
}
182-
const stack = Stack.find(this);
183-
return `https://${this.restApi.restApiId}.execute-api.${stack.region}.${stack.urlSuffix}/${this.stageName}${path}`;
181+
return `https://${this.restApi.restApiId}.execute-api.${this.node.stack.region}.${this.node.stack.urlSuffix}/${this.stageName}${path}`;
184182
}
185183

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

packages/@aws-cdk/aws-cloudformation/lib/pipeline-actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ class SingletonPolicy extends cdk.Construct {
544544
}
545545

546546
private stackArnFromProps(props: { stackName: string, region?: string }): string {
547-
return cdk.Stack.find(this).formatArn({
547+
return this.node.stack.formatArn({
548548
region: props.region,
549549
service: 'cloudformation',
550550
resource: 'stack',

packages/@aws-cdk/aws-cloudformation/test/test.pipeline-actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ function _isOrContains(entity: string | string[], value: string): boolean {
293293
}
294294

295295
function _stackArn(stackName: string, scope: cdk.IConstruct): string {
296-
return cdk.Stack.find(scope).formatArn({
296+
return scope.node.stack.formatArn({
297297
service: 'cloudformation',
298298
resource: 'stack',
299299
resourceName: `${stackName}/*`,
@@ -308,7 +308,7 @@ class PipelineDouble extends cdk.Construct implements cpapi.IPipeline {
308308
constructor(scope: cdk.Construct, id: string, { pipelineName, role }: { pipelineName?: string, role: iam.Role }) {
309309
super(scope, id);
310310
this.pipelineName = pipelineName || 'TestPipeline';
311-
this.pipelineArn = cdk.Stack.find(this).formatArn({ service: 'codepipeline', resource: 'pipeline', resourceName: this.pipelineName });
311+
this.pipelineArn = this.node.stack.formatArn({ service: 'codepipeline', resource: 'pipeline', resourceName: this.pipelineName });
312312
this.role = role;
313313
}
314314

packages/@aws-cdk/aws-cloudtrail/lib/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,13 @@ export class CloudTrail extends cdk.Construct {
132132
const s3bucket = new s3.Bucket(this, 'S3', {encryption: s3.BucketEncryption.Unencrypted});
133133
const cloudTrailPrincipal = "cloudtrail.amazonaws.com";
134134

135-
const stack = cdk.Stack.find(this);
136-
137135
s3bucket.addToResourcePolicy(new iam.PolicyStatement()
138136
.addResource(s3bucket.bucketArn)
139137
.addActions('s3:GetBucketAcl')
140138
.addServicePrincipal(cloudTrailPrincipal));
141139

142140
s3bucket.addToResourcePolicy(new iam.PolicyStatement()
143-
.addResource(s3bucket.arnForObjects(`AWSLogs/${stack.accountId}/*`))
141+
.addResource(s3bucket.arnForObjects(`AWSLogs/${this.node.stack.accountId}/*`))
144142
.addActions("s3:PutObject")
145143
.addServicePrincipal(cloudTrailPrincipal)
146144
.setCondition("StringEquals", {'s3:x-amz-acl': "bucket-owner-full-control"}));

packages/@aws-cdk/aws-cloudwatch/lib/dashboard.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Construct, Stack, Token } from "@aws-cdk/cdk";
1+
import { Construct, Token } from "@aws-cdk/cdk";
22
import { CfnDashboard } from './cloudwatch.generated';
33
import { Column, Row } from "./layout";
44
import { IWidget } from "./widget";
@@ -61,7 +61,6 @@ export class Dashboard extends Construct {
6161
*/
6262
private generateDashboardName(): string {
6363
// Combination of stack name and LogicalID, which are guaranteed to be unique.
64-
const stack = Stack.find(this);
65-
return stack.name + '-' + this.dashboard.logicalId;
64+
return this.node.stack.name + '-' + this.dashboard.logicalId;
6665
}
6766
}

packages/@aws-cdk/aws-codebuild/lib/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ class ImportedProject extends ProjectBase {
409409
constructor(scope: cdk.Construct, id: string, private readonly props: ProjectImportProps) {
410410
super(scope, id);
411411

412-
this.projectArn = cdk.Stack.find(this).formatArn({
412+
this.projectArn = this.node.stack.formatArn({
413413
service: 'codebuild',
414414
resource: 'project',
415415
resourceName: props.projectName,
@@ -744,7 +744,7 @@ export class Project extends ProjectBase {
744744
}
745745

746746
private createLoggingPermission() {
747-
const logGroupArn = cdk.Stack.find(this).formatArn({
747+
const logGroupArn = this.node.stack.formatArn({
748748
service: 'logs',
749749
resource: 'log-group',
750750
sep: ':',

0 commit comments

Comments
 (0)