Skip to content

Commit f9c6ad6

Browse files
author
Elad Ben-Israel
authored
refactor: use construct interfaces in public api (awslint:ref-via-interface) (#2499)
Adds a new awslint:ref-via-interface rule which validates that all input APIs (e.g. props, method arguments) use construct interface (IBucket) and not concrete classes (Bucket). This is in order to enable passing in unowned resources and in accordance with the aws construct library guidelines. There are situations where an owned resource is required. In those, the rule can be disabled by adding [disable-awslint:ref-via-interface] to the element's inline documentation. To enable this, the following new construct interfaces were added, along with `fromXxx` import methods: * `applicationautoscaling.IScalableTarget` * `cloudwatch.IAlarm` * `ecs.IService` * `ecs.IEc2Service` * `ec2.IFargateService` * `ecs.ITaskDefinition` * `iam.IGroup` * `iam.IUser` * `iam.IPolicy` * `lambda.IVersion` Fixes #2412 BREAKING CHANGE: `apigateway.ResourceBase.trackChild` is now internal. * `cloudfront.S3OriginConfig.originAccessIdentity` is now `originAccessIdentityId` * `codedeploy.LambdaDeploymentGroup.alarms` is now `cloudwatch.IAlarm[]` (previously `cloudwatch.Alarm[]`) * `codepipeline.crossRegionScaffoldingStacks` renamed to `crossRegionScaffolding` * `codepipeline.CrossRegionScaffoldingStack` renamed to `codepipeline.CrossRegionScaffolding` and cannot be instantiated (abstract) * `ec2.VpcSubnet.addDefaultRouteToNAT` renamed to `addDefaultNatRoute` and made public * `ec2.VpcSubnet.addDefaultRouteToIGW` renamed to `addDefaultInternetRoute`, made public and first argument is the gateway ID (string) and not the CFN L1 class * `ecs.Ec2EventRuleTarget.taskDefinition` is now `ITaskDefinition` (previously `TaskDefinition`) * `lambda.IEventSource.bind` now accepts `IFunction` instead of `FunctionBase`. Use `IFunction.addEventSourceMapping` to add an event source mapping under the function. * `lambda.Layer.grantUsage` renamed to `lambda.layer.addPermission` and returns void * `stepfunctions.StateMachine.role` is now `iam.IRole` (previously `iam.Role`)
1 parent e4ac767 commit f9c6ad6

File tree

70 files changed

+1068
-605
lines changed

Some content is hidden

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

70 files changed

+1068
-605
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.tsbuildinfo
2+
.cdk.staging
23

34
.vscode
45
# VSCode extension

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export class Asset extends cdk.Construct {
158158
*
159159
* @see https://github.com/awslabs/aws-cdk/issues/1432
160160
*
161-
* @param resource The CloudFormation resource which is using this asset.
161+
* @param resource The CloudFormation resource which is using this asset [disable-awslint:ref-via-interface]
162162
* @param resourceProperty The property name where this asset is referenced
163163
* (e.g. "Code" for AWS::Lambda::Function)
164164
*/

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ export abstract class ResourceBase extends ResourceConstruct implements IResourc
144144
return this.children[pathPart];
145145
}
146146

147-
public trackChild(pathPart: string, resource: Resource) {
147+
/**
148+
* @internal
149+
*/
150+
public _trackChild(pathPart: string, resource: Resource) {
148151
this.children[pathPart] = resource;
149152
}
150153

@@ -194,7 +197,7 @@ export class Resource extends ResourceBase {
194197
this.parentResource = props.parent;
195198

196199
if (props.parent instanceof ResourceBase) {
197-
props.parent.trackChild(props.pathPart, this);
200+
props.parent._trackChild(props.pathPart, this);
198201
}
199202

200203
const resourceProps: CfnResourceProps = {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export interface StageOptions extends MethodDeploymentOptions {
6969

7070
export interface StageProps extends StageOptions {
7171
/**
72-
* The deployment that this stage points to.
72+
* The deployment that this stage points to [disable-awslint:ref-via-interface].
7373
*/
7474
readonly deployment: Deployment;
7575
}

packages/@aws-cdk/aws-apigateway/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@
8686
},
8787
"awslint": {
8888
"exclude": [
89-
"resource-attribute:@aws-cdk/aws-apigateway.IRestApi.restApiRootResourceId",
90-
"from-method:@aws-cdk/aws-apigateway.Resource",
91-
"construct-base-is-private:@aws-cdk/aws-apigateway.ResourceBase"
89+
"from-method:@aws-cdk/aws-apigateway.Resource"
9290
]
9391
}
9492
}

packages/@aws-cdk/aws-applicationautoscaling/lib/scalable-target.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import iam = require('@aws-cdk/aws-iam');
2-
import { Construct, Resource } from '@aws-cdk/cdk';
2+
import { Construct, IResource, Resource } from '@aws-cdk/cdk';
33
import { CfnScalableTarget } from './applicationautoscaling.generated';
44
import { BasicStepScalingPolicyProps, StepScalingPolicy } from './step-scaling-policy';
55
import { BasicTargetTrackingScalingPolicyProps, TargetTrackingScalingPolicy } from './target-tracking-scaling-policy';
66

7+
export interface IScalableTarget extends IResource {
8+
/**
9+
* @attribute
10+
*/
11+
readonly scalableTargetId: string;
12+
}
13+
714
/**
815
* Properties for a scalable target
916
*/
@@ -61,7 +68,15 @@ export interface ScalableTargetProps {
6168
/**
6269
* Define a scalable target
6370
*/
64-
export class ScalableTarget extends Resource {
71+
export class ScalableTarget extends Resource implements IScalableTarget {
72+
73+
public static fromScalableTargetId(scope: Construct, id: string, scalableTargetId: string): IScalableTarget {
74+
class Import extends Resource implements IScalableTarget {
75+
public readonly scalableTargetId = scalableTargetId;
76+
}
77+
return new Import(scope, id);
78+
}
79+
6580
/**
6681
* ID of the Scalable Target
6782
*

packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-action.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
22
import cdk = require('@aws-cdk/cdk');
33
import { CfnScalingPolicy } from './applicationautoscaling.generated';
4-
import { ScalableTarget } from './scalable-target';
4+
import { IScalableTarget } from './scalable-target';
55

66
/**
77
* Properties for a scaling policy
@@ -10,7 +10,7 @@ export interface StepScalingActionProps {
1010
/**
1111
* The scalable target
1212
*/
13-
readonly scalingTarget: ScalableTarget;
13+
readonly scalingTarget: IScalableTarget;
1414

1515
/**
1616
* A name for the scaling policy

packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-policy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { findAlarmThresholds, normalizeIntervals } from '@aws-cdk/aws-autoscaling-common';
22
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
33
import cdk = require('@aws-cdk/cdk');
4-
import { ScalableTarget } from './scalable-target';
4+
import { IScalableTarget } from './scalable-target';
55
import { AdjustmentType, MetricAggregationType, StepScalingAction } from './step-scaling-action';
66

77
export interface BasicStepScalingPolicyProps {
@@ -52,7 +52,7 @@ export interface StepScalingPolicyProps extends BasicStepScalingPolicyProps {
5252
/**
5353
* The scaling target
5454
*/
55-
readonly scalingTarget: ScalableTarget;
55+
readonly scalingTarget: IScalableTarget;
5656
}
5757

5858
/**

packages/@aws-cdk/aws-applicationautoscaling/lib/target-tracking-scaling-policy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
22
import cdk = require('@aws-cdk/cdk');
33
import { CfnScalingPolicy } from './applicationautoscaling.generated';
4-
import { ScalableTarget } from './scalable-target';
4+
import { IScalableTarget } from './scalable-target';
55

66
/**
77
* Base interface for target tracking props
@@ -95,7 +95,7 @@ export interface TargetTrackingScalingPolicyProps extends BasicTargetTrackingSca
9595
/*
9696
* The scalable target
9797
*/
98-
readonly scalingTarget: ScalableTarget;
98+
readonly scalingTarget: IScalableTarget;
9999
}
100100

101101
export class TargetTrackingScalingPolicy extends cdk.Construct {

packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import route53 = require('@aws-cdk/aws-route53');
22
import s3 = require('@aws-cdk/aws-s3');
33
import cdk = require('@aws-cdk/cdk');
4-
import { CfnCloudFrontOriginAccessIdentity, CfnDistribution } from './cloudfront.generated';
4+
import { CfnDistribution } from './cloudfront.generated';
55

66
export enum HttpVersion {
77
HTTP1_1 = "http1.1",
@@ -237,12 +237,12 @@ export interface S3OriginConfig {
237237
/**
238238
* The source bucket to serve content from
239239
*/
240-
readonly s3BucketSource: s3.IBucket,
240+
readonly s3BucketSource: s3.IBucket;
241241

242242
/**
243-
* The optional origin identity cloudfront will use when calling your s3 bucket.
243+
* The optional ID of the origin identity cloudfront will use when calling your s3 bucket.
244244
*/
245-
readonly originAccessIdentity?: CfnCloudFrontOriginAccessIdentity
245+
readonly originAccessIdentityId?: string;
246246
}
247247

248248
/**
@@ -570,8 +570,8 @@ export class CloudFrontWebDistribution extends cdk.Construct implements route53.
570570
: originConfig.customOriginSource!.domainName,
571571
originPath: originConfig.originPath,
572572
originCustomHeaders: originHeaders.length > 0 ? originHeaders : undefined,
573-
s3OriginConfig: originConfig.s3OriginSource && originConfig.s3OriginSource.originAccessIdentity
574-
? { originAccessIdentity: `origin-access-identity/cloudfront/${originConfig.s3OriginSource.originAccessIdentity.ref}` }
573+
s3OriginConfig: originConfig.s3OriginSource && originConfig.s3OriginSource.originAccessIdentityId
574+
? { originAccessIdentity: `origin-access-identity/cloudfront/${originConfig.s3OriginSource.originAccessIdentityId}` }
575575
: originConfig.s3OriginSource
576576
? { }
577577
: undefined,

0 commit comments

Comments
 (0)