Skip to content

Commit 8c17ca7

Browse files
author
Elad Ben-Israel
authored
feat: aws resource api linting (breaking changes) (#1434)
Fixes #742 Built on top of #1428 BREAKING CHANGE: AWS resource-related classes have been changed to conform to API guidelines: - `XxxRef` abstract classes are now `IXxx` interfaces - `XxxRefProps` are now `XxxImportProps` - `XxxRef.import(...)` are now `Xxx.import(...)` accept `XxxImportProps` and return `IXxx` - `export(): XxxImportProps` is now defined in `IXxx` and implemented by imported resources - Lambda's static "metric" methods moved from `lambda.FunctionRef` to `lambda.Function`. - Route53 record classes now require a `zone` when created (not assuming zone is the parent construct).
1 parent 0d2b633 commit 8c17ca7

File tree

142 files changed

+2540
-1360
lines changed

Some content is hidden

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

142 files changed

+2540
-1360
lines changed

docs/src/aws-construct-lib.rst

+9-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function.
3939

4040
Furthermore, most AWS Constructs expose ``grant*`` methods which allow
4141
intent-based permission definitions. For example, the AWS S3 :py:class:`Bucket <@aws-cdk/aws-s3.Bucket>`
42-
construct has a :py:meth:`grantRead(principal) <@aws-cdk/aws-s3.BucketRef.grantRead>`
42+
construct has a :py:meth:`grantRead(principal) <@aws-cdk/aws-s3.IBucket.grantRead>`
4343
method which accepts an AWS IAM :py:class:`Principal <@aws-cdk/aws-iam.IPrincipal>`
4444
such as a :py:class:`User <@aws-cdk/aws-iam.User>` or a :py:class:`Role <@aws-cdk/aws-iam.Role>`,
4545
and will modify their policy to allow the principal to read objects from the bucket.
@@ -52,7 +52,7 @@ Event-Driven APIs
5252
Many of the AWS constructs include ``on*`` methods which can be used to react
5353
to events emitted by the construct. For example, the AWS CodeCommit
5454
:py:mod:`Repository <@aws-cdk/aws-codecommit.Repository>` construct has an
55-
:py:meth:`onCommit <@aws-cdk/aws-codecommit.RepositoryRef.onCommit>` method.
55+
:py:meth:`onCommit <@aws-cdk/aws-codecommit.IRepository.onCommit>` method.
5656

5757
AWS Constructs that can be used as targets for various event providers implement
5858
interfaces such as :py:mod:`IEventRuleTarget <@aws-cdk/aws-events.IEventRuleTarget>`
@@ -84,7 +84,7 @@ Many AWS resources emit AWS CloudWatch metrics as part of their normal operation
8484
be used to setup :py:mod:`Alarms <@aws-cdk/aws-cloudwatch.Alarm>` or included in :py:mod:`Dashboards <@aws-cdk/aws-cloudwatch.Dashboard>`.
8585

8686
:py:mod:`Metric <@aws-cdk/aws-cloudwatch.Metric>` objects for AWS Constructs can be obtained
87-
via ``metricXxx()`` methods. For example, the :py:meth:`metricDuration() <@aws-cdk/aws-lambda.FunctionRef.metricDuration>`
87+
via ``metricXxx()`` methods. For example, the :py:meth:`metricDuration() <@aws-cdk/aws-lambda.IFunction.metricDuration>`
8888
method reports the execution time of an AWS Lambda function.
8989

9090
For more information see the :doc:`refs/_aws-cdk_aws-cloudwatch` documentation.
@@ -95,12 +95,15 @@ Imports
9595
=======
9696

9797
If you need to reference a resource which is defined outside of your CDK app (e.g. a bucket, a VPC, etc),
98-
you can use the ``Xxxx.import(...)`` static methods which are available on AWS Constructs. For example,
99-
the :py:meth:`Bucket.import() <@aws-cdk/aws-s3.BucketRef.import>` method can be used to obtain
100-
a :py:mod:`BucketRef <@aws-cdk/aws-s3.BucketRef>` object which can be used in most places where
98+
you can use the ``Xxxx.import(...)`` static methods which are available on AWS Constructs.
99+
100+
For example, the :py:meth:`Bucket.import() <@aws-cdk/aws-s3.Bucket.import>` method can be used to obtain
101+
an :py:mod:`IBucket <@aws-cdk/aws-s3.IBucket>` object which can be used in most places where
101102
a bucket is required. This patterns allows treating resources defined outside your app as if they
102103
were part of your app.
103104

105+
106+
104107
.. _cloudformation_layer:
105108

106109
Access the AWS CloudFormation Layer

docs/src/passing-in-data.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,15 @@ in the stack property.
174174
175175
class HelloCdkStack extends cdk.Stack {
176176
// Property that defines the stack you are exporting from
177-
public readonly myBucketRefProps: s3.BucketRefProps;
177+
public readonly myBucketAttributes: s3.BucketAttributes;
178178
179179
constructor(parent: cdk.App, name: string, props?: cdk.StackProps) {
180180
super(parent, name, props);
181181
182182
const mybucket = new s3.Bucket(this, "MyFirstBucket");
183183
184-
// Save bucket's *BucketRefProps*
185-
this.myBucketRefProps = mybucket.export();
184+
// Save bucket's *BucketAttributes*
185+
this.myBucketAttributes = mybucket.export();
186186
}
187187
}
188188
@@ -193,7 +193,7 @@ We use this interface to pass the bucket properties between the two stacks.
193193
194194
// Interface we'll use to pass the bucket's properties to another stack
195195
interface MyCdkStackProps {
196-
theBucketRefProps: s3.BucketRefProps;
196+
theBucketAttributes: s3.BucketAttributes;
197197
}
198198
199199
Create the second stack that gets a reference to the other bucket
@@ -206,7 +206,7 @@ from the properties passed in through the constructor.
206206
constructor(parent: cdk.App, name: string, props: MyCdkStackProps) {
207207
super(parent, name);
208208
209-
const myOtherBucket = s3.Bucket.import(this, "MyOtherBucket", props.theBucketRefProps);
209+
const myOtherBucket = s3.Bucket.import(this, "MyOtherBucket", props.theBucketAttributes);
210210
211211
// Do something with myOtherBucket
212212
}
@@ -221,7 +221,7 @@ Finally, connect the dots in your app.
221221
const myStack = new HelloCdkStack(app, "HelloCdkStack");
222222
223223
new MyCdkStack(app, "MyCdkStack", {
224-
theBucketRefProps: myStack.myBucketRefProps
224+
theBucketAttributes: myStack.myBucketAttributes
225225
});
226226
227227
app.run();

examples/cdk-examples-typescript/bucket-import-export/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import s3 = require('@aws-cdk/aws-s3');
33
import cdk = require('@aws-cdk/cdk');
44

55
// Define a stack with an S3 bucket and export it using `bucket.export()`.
6-
// bucket.export returns a `BucketRef` object which can later be used in
6+
// bucket.export returns an `IBucket` object which can later be used in
77
// `Bucket.import`.
88

99
class Producer extends cdk.Stack {
10-
public readonly myBucketRef: s3.BucketRefProps;
10+
public readonly myBucketRef: s3.BucketImportProps;
1111

1212
constructor(parent: cdk.App, name: string) {
1313
super(parent, name);
@@ -18,7 +18,7 @@ class Producer extends cdk.Stack {
1818
}
1919

2020
interface ConsumerConstructProps {
21-
bucket: s3.BucketRef;
21+
bucket: s3.IBucket;
2222
}
2323

2424
class ConsumerConstruct extends cdk.Construct {
@@ -29,13 +29,13 @@ class ConsumerConstruct extends cdk.Construct {
2929
}
3030
}
3131

32-
// Define a stack that requires a BucketRef as an input and uses `Bucket.import`
32+
// Define a stack that requires an IBucket as an input and uses `Bucket.import`
3333
// to create a `Bucket` object that represents this external bucket. Grant a
3434
// user principal created within this consuming stack read/write permissions to
3535
// this bucket and contents.
3636

3737
interface ConsumerProps {
38-
userBucketRef: s3.BucketRefProps;
38+
userBucketRef: s3.BucketImportProps;
3939
}
4040

4141
class Consumer extends cdk.Stack {

examples/cdk-examples-typescript/chat-app/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MyStack extends cdk.Stack {
1212

1313
new CognitoChatRoomPool(this, 'UserPool');
1414

15-
const bucket = s3.BucketRef.import(this, 'DougsBucket', {
15+
const bucket = s3.Bucket.import(this, 'DougsBucket', {
1616
bucketName: 'dougs-chat-app'
1717
});
1818

@@ -69,7 +69,7 @@ class MyStack extends cdk.Stack {
6969
}
7070

7171
interface ChatAppFuncProps {
72-
bucket: s3.BucketRef;
72+
bucket: s3.IBucket;
7373
zipFile: string;
7474
}
7575

examples/cdk-examples-typescript/ec2/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class MyApp extends cdk.Stack {
5252
}
5353

5454
class CommonInfrastructure extends cdk.Stack {
55-
public vpc: ec2.VpcNetworkRefProps;
55+
public vpc: ec2.VpcNetworkImportProps;
5656

5757
constructor(parent: cdk.App, name: string, props?: cdk.StackProps) {
5858
super(parent, name, props);

examples/cdk-examples-typescript/use-vpc-from-another-stack/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const exportedVpc = new ec2.VpcNetwork(vpcStack, 'VPC', {
1717

1818
const appStack = new cdk.Stack(app, 'AppStack');
1919

20-
const importedVpc = ec2.VpcNetworkRef.import(appStack, 'VPC', exportedVpc.export());
20+
const importedVpc = ec2.VpcNetwork.import(appStack, 'VPC', exportedVpc.export());
2121

2222
const asg = new autoscaling.AutoScalingGroup(appStack, 'ASG', {
2323
vpc: importedVpc,

packages/@aws-cdk/assets-docker/lib/adopted-repository.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class AdoptedRepository extends ecr.RepositoryBase {
2929

3030
private readonly policyDocument = new iam.PolicyDocument();
3131

32-
constructor(parent: cdk.Construct, id: string, props: AdoptedRepositoryProps) {
32+
constructor(parent: cdk.Construct, id: string, private readonly props: AdoptedRepositoryProps) {
3333
super(parent, id);
3434

3535
const fn = new lambda.SingletonFunction(this, 'Function', {
@@ -70,6 +70,13 @@ export class AdoptedRepository extends ecr.RepositoryBase {
7070
this.repositoryArn = ecr.Repository.arnForLocalRepository(this.repositoryName);
7171
}
7272

73+
/**
74+
* Export this repository from the stack
75+
*/
76+
public export() {
77+
return this.props;
78+
}
79+
7380
/**
7481
* Adds a statement to the repository resource policy.
7582
*

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class Asset extends cdk.Construct {
6868
/**
6969
* The S3 bucket in which this asset resides.
7070
*/
71-
public readonly bucket: s3.BucketRef;
71+
public readonly bucket: s3.IBucket;
7272

7373
/**
7474
* Indicates if this asset is a zip archive. Allows constructs to ensure that the
@@ -114,7 +114,7 @@ export class Asset extends cdk.Construct {
114114
const s3Filename = cdk.Fn.select(1, cdk.Fn.split(cxapi.ASSET_PREFIX_SEPARATOR, keyParam.valueAsString)).toString();
115115
this.s3ObjectKey = `${this.s3Prefix}${s3Filename}`;
116116

117-
this.bucket = s3.BucketRef.import(this, 'AssetBucket', {
117+
this.bucket = s3.Bucket.import(this, 'AssetBucket', {
118118
bucketName: this.s3BucketName
119119
});
120120

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import cdk = require('@aws-cdk/cdk');
22
import crypto = require('crypto');
33
import { CfnDeployment, CfnDeploymentProps } from './apigateway.generated';
4-
import { RestApiRef } from './restapi-ref';
4+
import { IRestApi } from './restapi';
55

66
export interface DeploymentProps {
77
/**
88
* The Rest API to deploy.
99
*/
10-
api: RestApiRef;
10+
api: IRestApi;
1111

1212
/**
1313
* A description of the purpose of the API Gateway deployment.
@@ -56,7 +56,7 @@ export interface DeploymentProps {
5656
*/
5757
export class Deployment extends cdk.Construct implements cdk.IDependable {
5858
public readonly deploymentId: string;
59-
public readonly api: RestApiRef;
59+
public readonly api: IRestApi;
6060

6161
/**
6262
* Allows taking a dependency on this construct.

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from './restapi';
2-
export * from './restapi-ref';
32
export * from './resource';
43
export * from './method';
54
export * from './integration';

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ export interface LambdaIntegrationOptions extends IntegrationOptions {
3434
*
3535
*/
3636
export class LambdaIntegration extends AwsIntegration {
37-
private readonly handler: lambda.FunctionRef;
37+
private readonly handler: lambda.IFunction;
3838
private readonly enableTest: boolean;
3939

40-
constructor(handler: lambda.FunctionRef, options: LambdaIntegrationOptions = { }) {
40+
constructor(handler: lambda.IFunction, options: LambdaIntegrationOptions = { }) {
4141
const proxy = options.proxy === undefined ? true : options.proxy;
4242

4343
super({

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface MethodOptions {
2323
* If `authorizationType` is `Custom`, this specifies the ID of the method
2424
* authorizer resource.
2525
*
26-
* NOTE: in the future this will be replaced with an `AuthorizerRef`
26+
* NOTE: in the future this will be replaced with an `IAuthorizer`
2727
* construct.
2828
*/
2929
authorizerId?: string;
Original file line numberDiff line numberDiff line change
@@ -1,46 +0,0 @@
1-
import cdk = require('@aws-cdk/cdk');
2-
3-
export interface RestApiRefProps {
4-
/**
5-
* The REST API ID of an existing REST API resource.
6-
*/
7-
restApiId: string;
8-
}
9-
10-
export abstract class RestApiRef extends cdk.Construct {
11-
12-
/**
13-
* Imports an existing REST API resource.
14-
* @param parent Parent construct
15-
* @param id Construct ID
16-
* @param props Imported rest API properties
17-
*/
18-
public static import(parent: cdk.Construct, id: string, props: RestApiRefProps): RestApiRef {
19-
return new ImportedRestApi(parent, id, props);
20-
}
21-
22-
/**
23-
* The ID of this API Gateway RestApi.
24-
*/
25-
public readonly abstract restApiId: string;
26-
27-
/**
28-
* Exports a REST API resource from this stack.
29-
* @returns REST API props that can be imported to another stack.
30-
*/
31-
public export(): RestApiRefProps {
32-
return {
33-
restApiId: new cdk.Output(this, 'RestApiId', { value: this.restApiId }).makeImportValue().toString()
34-
};
35-
}
36-
}
37-
38-
class ImportedRestApi extends RestApiRef {
39-
public restApiId: string;
40-
41-
constructor(parent: cdk.Construct, id: string, props: RestApiRefProps) {
42-
super(parent, id);
43-
44-
this.restApiId = props.restApiId;
45-
}
46-
}

0 commit comments

Comments
 (0)