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

refactor(core): CfnResource.options => cfnOptions #3030

Merged
merged 3 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions packages/@aws-cdk/core/lib/cfn-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ export class CfnResource extends CfnRefElement {
return (construct as any).cfnResourceType !== undefined;
}

// MAINTAINERS NOTE: this class serves as the base class for the generated L1
// ("CFN") resources (such as `s3.CfnBucket`). These resources will have a
// property for each CloudFormation property of the resource. This means that
// if at some point in the future a property is introduced with a name similar
// to one of the properties here, it will be "masked" by the derived class. To
// that end, we prefix all properties in this class with `cfnXxx` with the
// hope to avoid those conflicts in the future.

/**
* Options for this resource, such as condition, update policy etc.
*/
public readonly options: IResourceOptions = {};
public readonly cfnOptions: ICfnResourceOptions = {};

/**
* AWS resource type.
Expand Down Expand Up @@ -84,7 +92,7 @@ export class CfnResource extends CfnRefElement {
// 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.options.metadata = {
this.cfnOptions.metadata = {
[cxapi.PATH_METADATA_KEY]: this.node.path
};
}
Expand All @@ -111,9 +119,9 @@ export class CfnResource extends CfnRefElement {
throw new Error(`Invalid removal policy: ${policy}`);
}

this.options.deletionPolicy = deletionPolicy;
this.cfnOptions.deletionPolicy = deletionPolicy;
if (options.applyToUpdateReplacePolicy) {
this.options.updateReplacePolicy = deletionPolicy;
this.cfnOptions.updateReplacePolicy = deletionPolicy;
}
}

Expand Down Expand Up @@ -215,12 +223,12 @@ export class CfnResource extends CfnRefElement {
Type: this.cfnResourceType,
Properties: ignoreEmpty(this.cfnProperties),
DependsOn: ignoreEmpty(renderDependsOn(this.dependsOn)),
CreationPolicy: capitalizePropertyNames(this, renderCreationPolicy(this.options.creationPolicy)),
UpdatePolicy: capitalizePropertyNames(this, this.options.updatePolicy),
UpdateReplacePolicy: capitalizePropertyNames(this, this.options.updateReplacePolicy),
DeletionPolicy: capitalizePropertyNames(this, this.options.deletionPolicy),
Metadata: ignoreEmpty(this.options.metadata),
Condition: this.options.condition && this.options.condition.logicalId
CreationPolicy: capitalizePropertyNames(this, renderCreationPolicy(this.cfnOptions.creationPolicy)),
UpdatePolicy: capitalizePropertyNames(this, this.cfnOptions.updatePolicy),
UpdateReplacePolicy: capitalizePropertyNames(this, this.cfnOptions.updateReplacePolicy),
DeletionPolicy: capitalizePropertyNames(this, this.cfnOptions.deletionPolicy),
Metadata: ignoreEmpty(this.cfnOptions.metadata),
Condition: this.cfnOptions.condition && this.cfnOptions.condition.logicalId
}, props => {
props.Properties = this.renderProperties(props.Properties);
return deepMerge(props, this.rawOverrides);
Expand Down Expand Up @@ -294,7 +302,7 @@ export enum TagType {
NOT_TAGGABLE = 'NotTaggable',
}

export interface IResourceOptions {
export interface ICfnResourceOptions {
/**
* A condition to associate with this resource. This means that only if the condition evaluates to 'true' when the stack
* is deployed, the resource will be included. This is provided to allow CDK projects to produce legacy templates, but noramlly
Expand Down
14 changes: 7 additions & 7 deletions packages/@aws-cdk/core/test/test.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export = {
const stack = new Stack();
const r1 = new CfnResource(stack, 'Resource', { type: 'Type' });
const cond = new CfnCondition(stack, 'MyCondition', { expression: Fn.conditionNot(Fn.conditionEquals('a', 'b')) });
r1.options.condition = cond;
r1.cfnOptions.condition = cond;

test.deepEqual(toCloudFormation(stack), {
Resources: { Resource: { Type: 'Type', Condition: 'MyCondition' } },
Expand All @@ -204,9 +204,9 @@ export = {
const stack = new Stack();
const r1 = new CfnResource(stack, 'Resource', { type: 'Type' });

r1.options.creationPolicy = { autoScalingCreationPolicy: { minSuccessfulInstancesPercent: 10 } };
r1.cfnOptions.creationPolicy = { autoScalingCreationPolicy: { minSuccessfulInstancesPercent: 10 } };
// tslint:disable-next-line:max-line-length
r1.options.updatePolicy = {
r1.cfnOptions.updatePolicy = {
autoScalingScheduledAction: { ignoreUnmodifiedGroupSizeProperties: false },
autoScalingReplacingUpdate: { willReplace: true },
codeDeployLambdaAliasUpdate: {
Expand All @@ -215,8 +215,8 @@ export = {
beforeAllowTrafficHook: 'lambda1',
},
};
r1.options.deletionPolicy = CfnDeletionPolicy.RETAIN;
r1.options.updateReplacePolicy = CfnDeletionPolicy.SNAPSHOT;
r1.cfnOptions.deletionPolicy = CfnDeletionPolicy.RETAIN;
r1.cfnOptions.updateReplacePolicy = CfnDeletionPolicy.SNAPSHOT;

test.deepEqual(toCloudFormation(stack), {
Resources: {
Expand Down Expand Up @@ -245,7 +245,7 @@ export = {
const stack = new Stack();
const r1 = new CfnResource(stack, 'Resource', { type: 'Type' });

r1.options.updatePolicy = { useOnlineResharding: true };
r1.cfnOptions.updatePolicy = { useOnlineResharding: true };

test.deepEqual(toCloudFormation(stack), {
Resources: {
Expand All @@ -265,7 +265,7 @@ export = {
const stack = new Stack();
const r1 = new CfnResource(stack, 'Resource', { type: 'Type' });

r1.options.metadata = {
r1.cfnOptions.metadata = {
MyKey: 10,
MyValue: 99
};
Expand Down