Skip to content

Commit

Permalink
refactor: resource bases (aws#2312)
Browse files Browse the repository at this point in the history
Added abstract `cdk.Resource` and `cdk.IResource` (currently placeholders) as bases for all constructs that represent AWS resources.

Added and applies the following awslint rules:
- `resource-class-extends-resource`
- `resource-interface-extends-resource`

BREAKING CHANGE: `cloudformation.CustomResource` no longer extends `CfnCustomResource`.
* `ssm.ParameterProps` renamed to `ssm.ParameterOptions`.
  • Loading branch information
Elad Ben-Israel authored Apr 17, 2019
1 parent cf89c7a commit e5e4a3d
Show file tree
Hide file tree
Showing 79 changed files with 532 additions and 470 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Here are a few useful commands:

* `npm run awslint` in every module will run __awslint__ for that module.
* `npm run awslint list` prints all rules (details and rationale in the guidelines doc)
* `lerna run awslint` will run __awslint__ in all modules.
* `lerna run awslint --no-bail --stream 2> awslint.txt` will run __awslint__ in all modules and collect all results into awslint.txt
* `lerna run awslint -- -i <RULE>` will run awslint throughout the repo and
evaluate only the rule specified [awslint README](./tools/awslint/README.md)
for details on include/exclude rule patterns.
Expand Down
18 changes: 9 additions & 9 deletions packages/@aws-cdk/aws-apigateway/lib/deployment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cdk = require('@aws-cdk/cdk');
import { Construct, DeletionPolicy, Resource, Token } from '@aws-cdk/cdk';
import crypto = require('crypto');
import { CfnDeployment, CfnDeploymentProps } from './apigateway.generated';
import { IRestApi } from './restapi';
Expand Down Expand Up @@ -54,13 +54,13 @@ export interface DeploymentProps {
* model. Use the `node.addDependency(dep)` method to circumvent that. This is done
* automatically for the `restApi.latestDeployment` deployment.
*/
export class Deployment extends cdk.Construct {
export class Deployment extends Resource {
public readonly deploymentId: string;
public readonly api: IRestApi;

private readonly resource: LatestDeploymentResource;

constructor(scope: cdk.Construct, id: string, props: DeploymentProps) {
constructor(scope: Construct, id: string, props: DeploymentProps) {
super(scope, id);

this.resource = new LatestDeploymentResource(this, 'Resource', {
Expand All @@ -69,11 +69,11 @@ export class Deployment extends cdk.Construct {
});

if (props.retainDeployments) {
this.resource.options.deletionPolicy = cdk.DeletionPolicy.Retain;
this.resource.options.deletionPolicy = DeletionPolicy.Retain;
}

this.api = props.api;
this.deploymentId = new cdk.Token(() => this.resource.deploymentId).toString();
this.deploymentId = new Token(() => this.resource.deploymentId).toString();
}

/**
Expand All @@ -93,16 +93,16 @@ class LatestDeploymentResource extends CfnDeployment {
private originalLogicalId?: string;
private lazyLogicalIdRequired: boolean;
private lazyLogicalId?: string;
private logicalIdToken: cdk.Token;
private logicalIdToken: Token;
private hashComponents = new Array<any>();

constructor(scope: cdk.Construct, id: string, props: CfnDeploymentProps) {
constructor(scope: Construct, id: string, props: CfnDeploymentProps) {
super(scope, id, props);

// from this point, don't allow accessing logical ID before synthesis
this.lazyLogicalIdRequired = true;

this.logicalIdToken = new cdk.Token(() => this.lazyLogicalId);
this.logicalIdToken = new Token(() => this.lazyLogicalId);
}

/**
Expand All @@ -127,7 +127,7 @@ class LatestDeploymentResource extends CfnDeployment {
* Returns a lazy reference to this resource (evaluated only upon synthesis).
*/
public get ref() {
return new cdk.Token(() => ({ Ref: this.lazyLogicalId })).toString();
return new Token(() => ({ Ref: this.lazyLogicalId })).toString();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-apigateway/lib/method.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cdk = require('@aws-cdk/cdk');
import { Construct, Resource } from '@aws-cdk/cdk';
import { CfnMethod, CfnMethodProps } from './apigateway.generated';
import { ConnectionType, Integration } from './integration';
import { MockIntegration } from './integrations/mock';
Expand Down Expand Up @@ -84,13 +84,13 @@ export interface MethodProps {
readonly options?: MethodOptions;
}

export class Method extends cdk.Construct {
export class Method extends Resource {
public readonly methodId: string;
public readonly httpMethod: string;
public readonly resource: IRestApiResource;
public readonly restApi: RestApi;

constructor(scope: cdk.Construct, id: string, props: MethodProps) {
constructor(scope: Construct, id: string, props: MethodProps) {
super(scope, id);

this.resource = props.resource;
Expand Down
12 changes: 6 additions & 6 deletions packages/@aws-cdk/aws-apigateway/lib/resource.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import cdk = require('@aws-cdk/cdk');
import { Construct, IResource, Resource as ResourceConstruct } from '@aws-cdk/cdk';
import { CfnResource, CfnResourceProps } from './apigateway.generated';
import { Integration } from './integration';
import { Method, MethodOptions } from './method';
import { RestApi } from './restapi';

export interface IRestApiResource extends cdk.IConstruct {
export interface IRestApiResource extends IResource {
/**
* The parent of this resource or undefined for the root resource.
*/
Expand Down Expand Up @@ -113,7 +113,7 @@ export interface ResourceProps extends ResourceOptions {
readonly pathPart: string;
}

export abstract class ResourceBase extends cdk.Construct implements IRestApiResource {
export abstract class ResourceBase extends ResourceConstruct implements IRestApiResource {
public abstract readonly parentResource?: IRestApiResource;
public abstract readonly resourceApi: RestApi;
public abstract readonly resourceId: string;
Expand All @@ -123,7 +123,7 @@ export abstract class ResourceBase extends cdk.Construct implements IRestApiReso

private readonly children: { [pathPart: string]: Resource } = { };

constructor(scope: cdk.Construct, id: string) {
constructor(scope: Construct, id: string) {
super(scope, id);
}

Expand Down Expand Up @@ -184,7 +184,7 @@ export class Resource extends ResourceBase {
public readonly defaultIntegration?: Integration;
public readonly defaultMethodOptions?: MethodOptions;

constructor(scope: cdk.Construct, id: string, props: ResourceProps) {
constructor(scope: Construct, id: string, props: ResourceProps) {
super(scope, id);

validateResourcePathPart(props.pathPart);
Expand Down Expand Up @@ -253,7 +253,7 @@ export class ProxyResource extends Resource {
*/
public readonly anyMethod?: Method;

constructor(scope: cdk.Construct, id: string, props: ProxyResourceProps) {
constructor(scope: Construct, id: string, props: ProxyResourceProps) {
super(scope, id, {
parent: props.parent,
pathPart: '{proxy+}',
Expand Down
18 changes: 9 additions & 9 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import iam = require('@aws-cdk/aws-iam');
import cdk = require('@aws-cdk/cdk');
import { CfnOutput, Construct, IResource, Resource } from '@aws-cdk/cdk';
import { CfnAccount, CfnRestApi } from './apigateway.generated';
import { Deployment } from './deployment';
import { Integration } from './integration';
Expand All @@ -14,7 +14,7 @@ export interface RestApiImportProps {
readonly restApiId: string;
}

export interface IRestApi extends cdk.IConstruct {
export interface IRestApi extends IResource {
/**
* The ID of this API Gateway RestApi.
*/
Expand Down Expand Up @@ -154,14 +154,14 @@ export interface RestApiProps extends ResourceOptions {
* By default, the API will automatically be deployed and accessible from a
* public endpoint.
*/
export class RestApi extends cdk.Construct implements IRestApi {
export class RestApi extends Resource implements IRestApi {
/**
* Imports an existing REST API resource.
* @param scope Parent construct
* @param id Construct ID
* @param props Imported rest API properties
*/
public static import(scope: cdk.Construct, id: string, props: RestApiImportProps): IRestApi {
public static import(scope: Construct, id: string, props: RestApiImportProps): IRestApi {
return new ImportedRestApi(scope, id, props);
}

Expand Down Expand Up @@ -196,7 +196,7 @@ export class RestApi extends cdk.Construct implements IRestApi {

private readonly methods = new Array<Method>();

constructor(scope: cdk.Construct, id: string, props: RestApiProps = { }) {
constructor(scope: Construct, id: string, props: RestApiProps = { }) {
super(scope, id);

const resource = new CfnRestApi(this, 'Resource', {
Expand Down Expand Up @@ -230,7 +230,7 @@ export class RestApi extends cdk.Construct implements IRestApi {
*/
public export(): RestApiImportProps {
return {
restApiId: new cdk.CfnOutput(this, 'RestApiId', { value: this.restApiId }).makeImportValue().toString()
restApiId: new CfnOutput(this, 'RestApiId', { value: this.restApiId }).makeImportValue().toString()
};
}

Expand Down Expand Up @@ -319,7 +319,7 @@ export class RestApi extends cdk.Construct implements IRestApi {
...props.deployOptions
});

new cdk.CfnOutput(this, 'Endpoint', { value: this.urlForPath() });
new CfnOutput(this, 'Endpoint', { value: this.urlForPath() });
} else {
if (props.deployOptions) {
throw new Error(`Cannot set 'deployOptions' if 'deploy' is disabled`);
Expand Down Expand Up @@ -377,10 +377,10 @@ export enum EndpointType {
Private = 'PRIVATE'
}

class ImportedRestApi extends cdk.Construct implements IRestApi {
class ImportedRestApi extends Construct implements IRestApi {
public restApiId: string;

constructor(scope: cdk.Construct, id: string, private readonly props: RestApiImportProps) {
constructor(scope: Construct, id: string, private readonly props: RestApiImportProps) {
super(scope, id);

this.restApiId = props.restApiId;
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-apigateway/lib/stage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cdk = require('@aws-cdk/cdk');
import { Construct, Resource } from '@aws-cdk/cdk';
import { CfnStage } from './apigateway.generated';
import { Deployment } from './deployment';
import { IRestApi } from './restapi';
Expand Down Expand Up @@ -132,13 +132,13 @@ export interface MethodDeploymentOptions {
readonly cacheDataEncrypted?: boolean;
}

export class Stage extends cdk.Construct {
export class Stage extends Resource {
public readonly stageName: string;

private readonly restApi: IRestApi;
private enableCacheCluster?: boolean;

constructor(scope: cdk.Construct, id: string, props: StageProps) {
constructor(scope: Construct, id: string, props: StageProps) {
super(scope, id);

this.enableCacheCluster = props.cacheClusterEnabled;
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-apigateway/lib/vpc-link.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
import cdk = require('@aws-cdk/cdk');
import { Construct, Resource } from '@aws-cdk/cdk';
import { CfnVpcLink } from './apigateway.generated';

/**
Expand Down Expand Up @@ -29,13 +29,13 @@ export interface VpcLinkProps {
* Define a new VPC Link
* Specifies an API Gateway VPC link for a RestApi to access resources in an Amazon Virtual Private Cloud (VPC).
*/
export class VpcLink extends cdk.Construct {
export class VpcLink extends Resource {
/**
* Physical ID of the VpcLink resource
*/
public readonly vpcLinkId: string;

constructor(scope: cdk.Construct, id: string, props: VpcLinkProps) {
constructor(scope: Construct, id: string, props: VpcLinkProps) {
super(scope, id);

const cfnResource = new CfnVpcLink(this, 'Resource', {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import iam = require('@aws-cdk/aws-iam');
import cdk = require('@aws-cdk/cdk');
import { Construct, Resource } from '@aws-cdk/cdk';
import { CfnScalableTarget } from './applicationautoscaling.generated';
import { BasicStepScalingPolicyProps, StepScalingPolicy } from './step-scaling-policy';
import { BasicTargetTrackingScalingPolicyProps, TargetTrackingScalingPolicy } from './target-tracking-scaling-policy';
Expand Down Expand Up @@ -61,7 +61,7 @@ export interface ScalableTargetProps {
/**
* Define a scalable target
*/
export class ScalableTarget extends cdk.Construct {
export class ScalableTarget extends Resource {
/**
* ID of the Scalable Target
*
Expand All @@ -76,7 +76,7 @@ export class ScalableTarget extends cdk.Construct {

private readonly actions = new Array<CfnScalableTarget.ScheduledActionProperty>();

constructor(scope: cdk.Construct, id: string, props: ScalableTargetProps) {
constructor(scope: Construct, id: string, props: ScalableTargetProps) {
super(scope, id);

if (props.maxCapacity < 0) {
Expand Down
20 changes: 10 additions & 10 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import elb = require('@aws-cdk/aws-elasticloadbalancing');
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
import iam = require('@aws-cdk/aws-iam');
import sns = require('@aws-cdk/aws-sns');
import cdk = require('@aws-cdk/cdk');

import { AutoScalingRollingUpdate, Construct, Fn, IResource, Resource, Tag, Token } from '@aws-cdk/cdk';
import { CfnAutoScalingGroup, CfnAutoScalingGroupProps, CfnLaunchConfiguration } from './autoscaling.generated';
import { BasicLifecycleHookProps, LifecycleHook } from './lifecycle-hook';
import { BasicScheduledActionProps, ScheduledAction } from './scheduled-action';
Expand Down Expand Up @@ -187,7 +187,7 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
*
* The ASG spans all availability zones.
*/
export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup, elb.ILoadBalancerTarget, ec2.IConnectable,
export class AutoScalingGroup extends Resource implements IAutoScalingGroup, elb.ILoadBalancerTarget, ec2.IConnectable,
elbv2.IApplicationLoadBalancerTarget, elbv2.INetworkLoadBalancerTarget {
/**
* The type of OS instances of this fleet are running.
Expand Down Expand Up @@ -217,7 +217,7 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup
private readonly targetGroupArns: string[] = [];
private albTargetGroup?: elbv2.ApplicationTargetGroup;

constructor(scope: cdk.Construct, id: string, props: AutoScalingGroupProps) {
constructor(scope: Construct, id: string, props: AutoScalingGroupProps) {
super(scope, id);

if (props.cooldownSeconds !== undefined && props.cooldownSeconds < 0) {
Expand All @@ -230,7 +230,7 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup
});
this.connections = new ec2.Connections({ securityGroups: [this.securityGroup] });
this.securityGroups.push(this.securityGroup);
this.node.apply(new cdk.Tag(NAME_TAG, this.node.path));
this.node.apply(new Tag(NAME_TAG, this.node.path));

this.role = props.role || new iam.Role(this, 'InstanceRole', {
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com')
Expand All @@ -242,8 +242,8 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup

// use delayed evaluation
const machineImage = props.machineImage.getImage(this);
const userDataToken = new cdk.Token(() => cdk.Fn.base64((machineImage.os.createUserData(this.userDataLines)))).toString();
const securityGroupsToken = new cdk.Token(() => this.securityGroups.map(sg => sg.securityGroupId));
const userDataToken = new Token(() => Fn.base64((machineImage.os.createUserData(this.userDataLines)))).toString();
const securityGroupsToken = new Token(() => this.securityGroups.map(sg => sg.securityGroupId));

const launchConfig = new CfnLaunchConfiguration(this, 'LaunchConfig', {
imageId: machineImage.imageId,
Expand Down Expand Up @@ -275,8 +275,8 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup
maxSize: maxCapacity.toString(),
desiredCapacity: desiredCapacity.toString(),
launchConfigurationName: launchConfig.ref,
loadBalancerNames: new cdk.Token(() => this.loadBalancerNames.length > 0 ? this.loadBalancerNames : undefined).toList(),
targetGroupArns: new cdk.Token(() => this.targetGroupArns.length > 0 ? this.targetGroupArns : undefined).toList(),
loadBalancerNames: new Token(() => this.loadBalancerNames.length > 0 ? this.loadBalancerNames : undefined).toList(),
targetGroupArns: new Token(() => this.targetGroupArns.length > 0 ? this.targetGroupArns : undefined).toList(),
notificationConfigurations: !props.notificationsTopic ? undefined : [
{
topicArn: props.notificationsTopic.topicArn,
Expand Down Expand Up @@ -614,7 +614,7 @@ export enum ScalingProcess {
/**
* Render the rolling update configuration into the appropriate object
*/
function renderRollingUpdateConfig(config: RollingUpdateConfiguration = {}): cdk.AutoScalingRollingUpdate {
function renderRollingUpdateConfig(config: RollingUpdateConfiguration = {}): AutoScalingRollingUpdate {
const waitOnResourceSignals = config.minSuccessfulInstancesPercent !== undefined ? true : false;
const pauseTimeSec = config.pauseTimeSec !== undefined ? config.pauseTimeSec : (waitOnResourceSignals ? 300 : 0);

Expand Down Expand Up @@ -665,7 +665,7 @@ function validatePercentage(x?: number): number | undefined {
/**
* An AutoScalingGroup
*/
export interface IAutoScalingGroup {
export interface IAutoScalingGroup extends IResource {
/**
* The name of the AutoScalingGroup
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-autoscaling/lib/lifecycle-hook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import api = require('@aws-cdk/aws-autoscaling-api');
import iam = require('@aws-cdk/aws-iam');
import cdk = require('@aws-cdk/cdk');
import { Construct, Resource } from '@aws-cdk/cdk';
import { IAutoScalingGroup } from './auto-scaling-group';
import { CfnLifecycleHook } from './autoscaling.generated';

Expand Down Expand Up @@ -64,7 +64,7 @@ export interface LifecycleHookProps extends BasicLifecycleHookProps {
readonly autoScalingGroup: IAutoScalingGroup;
}

export class LifecycleHook extends cdk.Construct implements api.ILifecycleHook {
export class LifecycleHook extends Resource implements api.ILifecycleHook {
/**
* The role that allows the ASG to publish to the notification target
*/
Expand All @@ -75,7 +75,7 @@ export class LifecycleHook extends cdk.Construct implements api.ILifecycleHook {
*/
public readonly lifecycleHookName: string;

constructor(scope: cdk.Construct, id: string, props: LifecycleHookProps) {
constructor(scope: Construct, id: string, props: LifecycleHookProps) {
super(scope, id);

this.role = props.role || new iam.Role(this, 'Role', {
Expand Down
Loading

0 comments on commit e5e4a3d

Please sign in to comment.