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): deprecate "Construct.node" in favor of "Construct.construct" #9557

Merged
merged 10 commits into from
Aug 10, 2020
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Vpc } from '@aws-cdk/aws-ec2';
import { Cluster, ContainerImage } from '@aws-cdk/aws-ecs';
import { ApplicationProtocol } from '@aws-cdk/aws-elasticloadbalancingv2';
import { HostedZone } from '@aws-cdk/aws-route53';
import { App, Stack } from '@aws-cdk/core';

import { ApplicationLoadBalancedFargateService } from '../../lib';
Expand All @@ -20,13 +21,10 @@ new ApplicationLoadBalancedFargateService(stack, 'myService', {
protocol: ApplicationProtocol.HTTPS,
enableECSManagedTags: true,
domainName: 'test.example.com',
domainZone: {
domainZone: HostedZone.fromHostedZoneAttributes(stack, 'HostedZone', {
hostedZoneId: 'fakeId',
zoneName: 'example.com.',
hostedZoneArn: 'arn:aws:route53:::hostedzone/fakeId',
stack,
node: stack.node,
},
zoneName: 'example.com',
}),
});

app.synth();
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as ec2 from '@aws-cdk/aws-ec2';
import * as ecs from '@aws-cdk/aws-ecs';
import { ApplicationLoadBalancer, ApplicationProtocol, NetworkLoadBalancer } from '@aws-cdk/aws-elasticloadbalancingv2';
import * as iam from '@aws-cdk/aws-iam';
import * as route53 from '@aws-cdk/aws-route53';
import * as cdk from '@aws-cdk/core';
import { Test } from 'nodeunit';
import * as ecsPatterns from '../../lib';
Expand Down Expand Up @@ -370,13 +371,10 @@ export = {
cluster,
protocol: ApplicationProtocol.HTTPS,
domainName: 'domain.com',
domainZone: {
domainZone: route53.HostedZone.fromHostedZoneAttributes(stack, 'MyHostedZone', {
hostedZoneId: 'fakeId',
zoneName: 'domain.com',
hostedZoneArn: 'arn:aws:route53:::hostedzone/fakeId',
stack,
node: stack.node,
},
}),
taskImageOptions: {
containerPort: 2015,
image: ecs.ContainerImage.fromRegistry('abiosoft/caddy'),
Expand Down Expand Up @@ -408,13 +406,10 @@ export = {
cluster,
protocol: ApplicationProtocol.HTTPS,
domainName: 'test.domain.com',
domainZone: {
domainZone: route53.HostedZone.fromHostedZoneAttributes(stack, 'MyHostedZone', {
hostedZoneId: 'fakeId',
zoneName: 'domain.com.',
hostedZoneArn: 'arn:aws:route53:::hostedzone/fakeId',
stack,
node: stack.node,
},
}),
taskImageOptions: {
containerPort: 2015,
image: ecs.ContainerImage.fromRegistry('abiosoft/caddy'),
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda/lib/function-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export abstract class FunctionBase extends Resource implements IFunction {
return { statementAdded: true, policyDependable: this._functionNode().findChild(identifier) } as iam.AddToResourcePolicyResult;
},
node: this.node,
construct: this.construct,
},
});
}
Expand Down
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-route53/test/test.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export = {
hostedZoneArn: 'arn:aws:route53:::hostedzone/fakeId',
stack,
node: stack.node,
construct: stack.construct,
});

// THEN
Expand All @@ -45,6 +46,7 @@ export = {
hostedZoneArn: 'arn:aws:route53:::hostedzone/fakeId',
stack,
node: stack.node,
construct: stack.construct,
});

// THEN
Expand All @@ -64,6 +66,7 @@ export = {
hostedZoneArn: 'arn:aws:route53:::hostedzone/fakeId',
stack,
node: stack.node,
construct: stack.construct,
});

// THEN
Expand All @@ -83,6 +86,7 @@ export = {
hostedZoneArn: 'arn:aws:route53:::hostedzone/fakeId',
stack,
node: stack.node,
construct: stack.construct,
});

// THEN
Expand Down
10 changes: 5 additions & 5 deletions packages/@aws-cdk/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ you.

If you need to add an ordering dependency that is not automatically inferred,
you do so by adding a dependency relationship using
`constructA.node.addDependency(constructB)`. This will add a dependency
`constructA.construct.addDependency(constructB)`. This will add a dependency
relationship between all resources in the scope of `constructA` and all
resources in the scope of `constructB`.

Expand All @@ -230,7 +230,7 @@ bAndC.add(constructB);
bAndC.add(constructC);

// Take the dependency
constructA.node.addDependency(bAndC);
constructA.construct.addDependency(bAndC);
```

### Stack Dependencies
Expand Down Expand Up @@ -319,7 +319,7 @@ examples ensures that only a single SNS topic is defined:
function getOrCreate(scope: Construct): sns.Topic {
const stack = Stack.of(this);
const uniqueid = 'GloballyUniqueIdForSingleton';
return stack.node.tryFindChild(uniqueid) as sns.Topic ?? new sns.Topic(stack, uniqueid);
return stack.construct.tryFindChild(uniqueid) as sns.Topic ?? new sns.Topic(stack, uniqueid);
}
```

Expand Down Expand Up @@ -675,7 +675,7 @@ accessing those through the `cfnOptions` property:
```ts
const rawBucket = new s3.CfnBucket(this, 'Bucket', { /* ... */ });
// -or-
const rawBucket = bucket.node.defaultChild as s3.CfnBucket;
const rawBucket = bucket.construct.defaultChild as s3.CfnBucket;

// then
rawBucket.cfnOptions.condition = new CfnCondition(this, 'EnableBucket', { /* ... */ });
Expand Down Expand Up @@ -734,7 +734,7 @@ const stage = Fn.conditionIf(isProd.logicalId, 'Beta', 'Prod').toString();
// Make Bucket creation condition to IsProduction by accessing
// and overriding the CloudFormation resource
const bucket = new s3.Bucket(this, 'Bucket');
const cfnBucket = bucket.node.defaultChild as s3.CfnBucket;
const cfnBucket = bucket.construct.defaultChild as s3.CfnBucket;
cfnBucket.cfnOptions.condition = isProd;
```

Expand Down
8 changes: 4 additions & 4 deletions packages/@aws-cdk/core/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ export class App extends Stage {
this.loadContext(props.context);

if (props.stackTraces === false) {
this.node.setContext(cxapi.DISABLE_METADATA_STACK_TRACE, true);
this.construct.setContext(cxapi.DISABLE_METADATA_STACK_TRACE, true);
}

if (props.runtimeInfo === false) {
this.node.setContext(cxapi.DISABLE_VERSION_REPORTING, true);
this.construct.setContext(cxapi.DISABLE_VERSION_REPORTING, true);
}

const autoSynth = props.autoSynth !== undefined ? props.autoSynth : cxapi.OUTDIR_ENV in process.env;
Expand All @@ -120,7 +120,7 @@ export class App extends Stage {
private loadContext(defaults: { [key: string]: string } = { }) {
// prime with defaults passed through constructor
for (const [ k, v ] of Object.entries(defaults)) {
this.node.setContext(k, v);
this.construct.setContext(k, v);
}

// read from environment
Expand All @@ -130,7 +130,7 @@ export class App extends Stage {
: { };

for (const [ k, v ] of Object.entries(contextFromEnvironment)) {
this.node.setContext(k, v);
this.construct.setContext(k, v);
}
}
}
8 changes: 4 additions & 4 deletions packages/@aws-cdk/core/lib/asset-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import * as cxapi from '@aws-cdk/cx-api';
import * as fs from 'fs-extra';
import { AssetHashType, AssetOptions } from './assets';
import { BundlingOptions } from './bundling';
import { Construct } from './construct-compat';
import { FileSystem, FingerprintOptions } from './fs';
import { Stage } from './stage';
import { Construct } from './construct-compat';

const STAGING_TMP = '.cdk.staging';

Expand Down Expand Up @@ -95,7 +95,7 @@ export class AssetStaging extends Construct {

this.assetHash = this.calculateHash(props);

const stagingDisabled = this.node.tryGetContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT);
const stagingDisabled = this.construct.tryGetContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT);
if (stagingDisabled) {
this.stagedPath = this.bundleDir ?? this.sourcePath;
} else {
Expand Down Expand Up @@ -179,7 +179,7 @@ export class AssetStaging extends Construct {
];

try {
process.stderr.write(`Bundling asset ${this.node.path}...\n`);
process.stderr.write(`Bundling asset ${this.construct.path}...\n`);
options.image._run({
command: options.command,
user,
Expand All @@ -188,7 +188,7 @@ export class AssetStaging extends Construct {
workingDirectory: options.workingDirectory ?? AssetStaging.BUNDLING_INPUT_DIR,
});
} catch (err) {
throw new Error(`Failed to run bundling Docker image for asset ${this.node.path}: ${err}`);
throw new Error(`Failed to run bundling Docker image for asset ${this.construct.path}: ${err}`);
}

if (FileSystem.isEmpty(bundleDir)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/core/lib/cfn-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export abstract class CfnElement extends Construct {
this.stack = Stack.of(this);

this.logicalId = Lazy.stringValue({ produce: () => this.synthesizeLogicalId() }, {
displayHint: `${notTooLong(this.node.path)}.LogicalID`,
displayHint: `${notTooLong(this.construct.path)}.LogicalID`,
});

this.node.addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, this.constructor);
this.construct.addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, this.constructor);
}

/**
Expand All @@ -78,7 +78,7 @@ export abstract class CfnElement extends Construct {
* node +internal+ entries filtered.
*/
public get creationStack(): string[] {
const trace = this.node.metadata.find(md => md.type === cxschema.ArtifactMetadataEntryType.LOGICAL_ID)!.trace;
const trace = this.construct.metadata.find(md => md.type === cxschema.ArtifactMetadataEntryType.LOGICAL_ID)!.trace;
if (!trace) {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/cfn-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class CfnOutput extends CfnElement {
super(scope, id);

if (props.value === undefined) {
throw new Error(`Missing value for CloudFormation output at path "${this.node.path}"`);
throw new Error(`Missing value for CloudFormation output at path "${this.construct.path}"`);
}

this._description = props.description;
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/cfn-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class CfnParser {
if (!depResource) {
throw new Error(`Resource '${logicalId}' depends on '${dep}' that doesn't exist`);
}
resource.node.addDependency(depResource);
resource.construct.addDependency(depResource);
}
}

Expand Down
10 changes: 5 additions & 5 deletions packages/@aws-cdk/core/lib/cfn-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export class CfnResource extends CfnRefElement {
// if aws:cdk:enable-path-metadata is set, embed the current construct's
// path in the CloudFormation template, so it will be possible to trace
// back to the actual construct path.
if (this.node.tryGetContext(cxapi.PATH_METADATA_ENABLE_CONTEXT)) {
this.addMetadata(cxapi.PATH_METADATA_KEY, this.node.path);
if (this.construct.tryGetContext(cxapi.PATH_METADATA_ENABLE_CONTEXT)) {
this.addMetadata(cxapi.PATH_METADATA_KEY, this.construct.path);
}
}

Expand Down Expand Up @@ -238,7 +238,7 @@ export class CfnResource extends CfnRefElement {
return;
}

addDependency(this, target, `"${this.node.path}" depends on "${target.node.path}"`);
addDependency(this, target, `"${this.construct.path}" depends on "${target.construct.path}"`);
}

/**
Expand Down Expand Up @@ -312,7 +312,7 @@ export class CfnResource extends CfnRefElement {
return ret;
} catch (e) {
// Change message
e.message = `While synthesizing ${this.node.path}: ${e.message}`;
e.message = `While synthesizing ${this.construct.path}: ${e.message}`;
// Adjust stack trace (make it look like node built it, too...)
const trace = this.creationStack;
if (trace) {
Expand All @@ -330,7 +330,7 @@ export class CfnResource extends CfnRefElement {
function renderDependsOn(dependsOn: Set<CfnResource>) {
return Array
.from(dependsOn)
.sort((x, y) => x.node.path.localeCompare(y.node.path))
.sort((x, y) => x.construct.path.localeCompare(y.construct.path))
.map(r => r.logicalId);
}

Expand Down
23 changes: 19 additions & 4 deletions packages/@aws-cdk/core/lib/construct-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export interface IConstruct extends constructs.IConstruct, IDependable {
* The construct tree node for this construct.
*/
readonly node: ConstructNode;

/**
* The construct tree node for this construct.
*/
readonly construct: ConstructNode;
}

/**
Expand Down Expand Up @@ -61,9 +66,18 @@ export class Construct extends constructs.Construct implements IConstruct {

/**
* The construct tree node associated with this construct.
*
* @deprecate `Construct.node` is being deprecated in favor of
* `Construct.construct`. This API will be removed in the next major version
* of the AWS CDK, please migrate your code to use `construct` instead.
*/
public readonly node: ConstructNode;

/**
* Construct API.
*/
public readonly construct: ConstructNode;

constructor(scope: Construct, id: string) {
super(scope, id, {
nodeFactory: {
Expand All @@ -78,15 +92,16 @@ export class Construct extends constructs.Construct implements IConstruct {

Object.defineProperty(this, CONSTRUCT_SYMBOL, { value: true });
this.node = ConstructNode._unwrap(constructs.Node.of(this));
this.construct = this.node;

const disableTrace =
this.node.tryGetContext(cxapi.DISABLE_METADATA_STACK_TRACE) ||
this.node.tryGetContext(constructs.ConstructMetadata.DISABLE_STACK_TRACE_IN_METADATA) ||
this.construct.tryGetContext(cxapi.DISABLE_METADATA_STACK_TRACE) ||
this.construct.tryGetContext(constructs.ConstructMetadata.DISABLE_STACK_TRACE_IN_METADATA) ||
process.env.CDK_DISABLE_STACK_TRACE;

if (disableTrace) {
this.node.setContext(cxapi.DISABLE_METADATA_STACK_TRACE, true);
this.node.setContext(constructs.ConstructMetadata.DISABLE_STACK_TRACE_IN_METADATA, true);
this.construct.setContext(cxapi.DISABLE_METADATA_STACK_TRACE, true);
this.construct.setContext(constructs.ConstructMetadata.DISABLE_STACK_TRACE_IN_METADATA, true);
process.env.CDK_DISABLE_STACK_TRACE = '1';
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/core/lib/context-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class ContextProvider {
}

const { key, props } = this.getKey(scope, options);
const value = scope.node.tryGetContext(key);
const value = scope.construct.tryGetContext(key);
const providerError = extractProviderError(value);

// if context is missing or an error occurred during context retrieval,
Expand All @@ -107,7 +107,7 @@ export class ContextProvider {
});

if (providerError !== undefined) {
scope.node.addError(providerError);
scope.construct.addError(providerError);
}
return { value: options.dummyValue };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class CustomResourceProvider extends Construct {
public static getOrCreate(scope: Construct, uniqueid: string, props: CustomResourceProviderProps) {
const id = `${uniqueid}CustomResourceProvider`;
const stack = Stack.of(scope);
const provider = stack.node.tryFindChild(id) as CustomResourceProvider
const provider = stack.construct.tryFindChild(id) as CustomResourceProvider
?? new CustomResourceProvider(stack, id, props);

return provider.serviceToken;
Expand Down
Loading