Skip to content

Commit

Permalink
[BREAKING] Move @aws-cdk/resources classes into L2 packages (aws#264)
Browse files Browse the repository at this point in the history
The `create_missing_libraries.sh` script was used to bootstrap L2
packages for all the namespaces that didn't have one already.

Existing L2 packages were manually edited to:
- Invoke `cfn2ts` to generate the L1 class library for the service/scope
- Depend on `@aws-cdk/runtime` (which used to be blended in
  `@aws-cdk/resources`, and was extracted out)
- Export the L1 library as expected
- Generated resource and property classes are namespaced under 'cloudformation'

The `@aws-cdk/assert` behavior was changed to rely on
`@aws-cdk/cdk-cfnspec` (formerly `@aws-cdk/cloudformation-resource-spec`)
to determine the `UpdateType` of attributes, rendering the `registry`
mechanism that was introduced to `@aws-cdk/resources` a while
ago redundant (hence it got dropped).

This required temporarily disabling use of '--reject-cycles' on 'lerna bootstrap'
as it would blow up on the cycle created (see aws#293).
  • Loading branch information
RomainMuller authored Jul 10, 2018
1 parent 9e711a3 commit aa404a0
Show file tree
Hide file tree
Showing 446 changed files with 4,938 additions and 32,432 deletions.
6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ rm -rf $BUILD_INDICATOR
export PATH=node_modules/.bin:$PATH

echo "============================================================================================="
echo "boostrapping..."
lerna bootstrap --reject-cycles
echo "building..."
lerna exec --stream "npm run build"

echo "============================================================================================="
echo "testing..."
lerna run test --stream
lerna run --stream test

touch $BUILD_INDICATOR

Expand Down
120 changes: 120 additions & 0 deletions create-missing-libraries.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/bin/bash
set -euo pipefail

export PATH=node_modules/.bin:$PATH

# Making sure the bare minimum packages allowing be able to test-build the generated packages is available:
lerna exec --scope=cfn2ts \
--scope=pkglint \
--scope=@aws-cdk/cdk-cfnspec \
--scope=@aws-cdk/assert \
--scope=@aws-cdk/cloudformation-diff \
--scope=@aws-cdk/core \
--scope=@aws-cdk/cx-api \
--stream \
npm run build

VERSION=$(node -e 'console.log(require("./lerna.json").version);')

for S in $(node -e 'console.log(require("./packages/@aws-cdk/cdk-cfnspec").namespaces.join("\n"));'); do
P=$(tr 'A-Z' 'a-z' <<< "${S/AWS::/@aws-cdk/}")
PB=$(basename ${P})
if [ ! -d packages/${P} ]; then
echo "⏳ Creating package ${P} for scope ${S}..."
mkdir -p packages/${P}/test
mkdir -p packages/${P}/lib
cat <<EOM > packages/${P}/.gitignore
*.js
*.js.map
*.d.ts
*.generated.ts
tsconfig.json
tslint.json
node_modules
dist
EOM

cat <<EOM > packages/${P}/.npmignore
# Don't include original .ts files when doing \`npm pack\`
*.ts
!*.d.ts
coverage
.nyc_output
*.tgz
EOM

cat <<EOM > packages/${P}/package.json
{
"name": "${P}",
"version": "${VERSION}",
"description": "The CDK Construct Library for ${S}",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"jsii": {
"outdir": "dist",
"bundledDependencies": [],
"names": {
"java": "com.amazonaws.cdk.${PB}",
"dotnet": "${S/AWS::/Aws.Cdk.}"
}
},
"repository": {
"type": "git",
"url": "git://github.com/awslabs/aws-cdk"
},
"scripts": {
"build": "cfn2ts --scope=${S} && jsii && tslint -p . && pkglint",
"watch": "jsii -w",
"lint": "jsii && tslint -p . --force",
"test": "nodeunit test/test.*.js && cdk-integ-assert",
"integ": "cdk-integ",
"pkglint": "pkglint -f"
},
"keywords": [
"aws",
"cdk",
"constructs",
"${PB}"
],
"author": {
"name": "Amazon Web Services",
"url": "https://aws.amazon.com"
},
"license": "LicenseRef-LICENSE",
"devDependencies": {
"@aws-cdk/assert": "^${VERSION}",
"cfn2ts": "^${VERSION}",
"pkglint": "^${VERSION}"
},
"dependencies": {
"@aws-cdk/core": "^${VERSION}"
}
}
EOM

cat <<EOM > packages/${P}/lib/index.ts
// ${S} CloudFormation Resources:
export * from './${PB}.generated';
EOM

cat <<EOM > packages/${P}/test/test.${PB}.ts
import { Test, testCase } from 'nodeunit';
import {} from '../lib';
exports = testCase({
notTested(test: Test) {
test.ok(true, 'No tests are specified for this package.');
test.done();
}
});
EOM

echo "⌛️ Bootstrapping & building ${P}"
lerna bootstrap --scope=${P}
lerna run build --scope=${P}

git add packages/${P}

echo "✅ Have fun with your new package ${P} (⚠️ don't forget to add it to 'aws-cdk-all')"
fi
done
4 changes: 2 additions & 2 deletions examples/cdk-examples-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This an example of a CDK program written in Java.

## Building

To build this app, run `npm run prepare`. This will:
To build this app, run `npm run build`. This will:

1. Generate `project/pom.xml` with correct references to jsii and CDK
dependencies.
Expand Down Expand Up @@ -48,4 +48,4 @@ $ cdk diff
```

If you make modifications, make sure to rebuild the app, either by callign `mvn
package` from `./project` or `npm run prepare` from the root.
package` from `./project` or `npm run build` from the root.
2 changes: 1 addition & 1 deletion examples/cdk-examples-java/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"ignore": true
},
"scripts": {
"prepare": "/bin/bash generate.sh && cd project && mvn package",
"build": "/bin/bash generate.sh && cd project && mvn package",
"cdk": "cdk",
"test": "echo ok"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.amazonaws.cdk.ec2.VpcNetwork;
import com.amazonaws.cdk.ec2.WindowsImage;
import com.amazonaws.cdk.ec2.WindowsVersion;
import com.amazonaws.cdk.resources.s3.BucketResource;
import com.amazonaws.cdk.resources.s3.BucketResourceProps;
import com.amazonaws.cdk.s3.cloudformation.BucketResource;
import com.amazonaws.cdk.s3.cloudformation.BucketResourceProps;
import com.amazonaws.cdk.sns.Topic;
import com.amazonaws.cdk.sqs.Queue;
import com.amazonaws.cdk.sqs.QueueProps;
Expand Down Expand Up @@ -63,4 +63,4 @@ public List<String> validate() {
return Collections.emptyList();
}
}
}
}
2 changes: 1 addition & 1 deletion examples/cdk-examples-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ file at the root.
To synthesize or deploy and example app, you will first need to build it:

```shell
npm run prepare
npm run build
```

Or you can watch:
Expand Down
31 changes: 16 additions & 15 deletions examples/cdk-examples-typescript/advanced-usage/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { cloudformation } from '@aws-cdk/cloudformation';
import * as cdk from '@aws-cdk/core';
import { PolicyStatement, ServicePrincipal } from '@aws-cdk/core';
import { WindowsImage, WindowsVersion } from '@aws-cdk/ec2';
import { Role } from '@aws-cdk/iam';
import { cloudformation, ec2, sns, sqs } from '@aws-cdk/resources';
import { Bucket } from '@aws-cdk/s3';
import * as ec2 from '@aws-cdk/ec2';
import * as iam from '@aws-cdk/iam';
import * as s3 from '@aws-cdk/s3';
import * as sns from '@aws-cdk/sns';
import * as sqs from '@aws-cdk/sqs';

/**
* This stack demonstrates the use of the IAM policy library shipped with the CDK.
Expand All @@ -14,16 +15,16 @@ class PolicyExample extends cdk.Stack {

// here's how to create an IAM Role with an assume policy for the Lambda
// service principal.
const role = new Role(this, 'Role', {
assumedBy: new ServicePrincipal('lambda.amazon.aws.com')
const role = new iam.Role(this, 'Role', {
assumedBy: new cdk.ServicePrincipal('lambda.amazon.aws.com')
});

// when you call `addToPolicy`, a default policy is defined and attached
// to the bucket.
const bucket = new Bucket(this, 'MyBucket');
const bucket = new s3.Bucket(this, 'MyBucket');

// the role also has a policy attached to it.
role.addToPolicy(new PolicyStatement()
role.addToPolicy(new cdk.PolicyStatement()
.addResource(bucket.arnForObjects('*'))
.addResource(bucket.bucketArn)
.addActions('s3:*'));
Expand All @@ -44,7 +45,7 @@ class EnvContextExample extends cdk.Stack {
const azs = new cdk.AvailabilityZoneProvider(this).availabilityZones;

// get the AMI ID for a specific Windows version in this region
const ami = new WindowsImage(WindowsVersion.WindowsServer2016EnglishNanoBase).getImage(this);
const ami = new ec2.WindowsImage(ec2.WindowsVersion.WindowsServer2016EnglishNanoBase).getImage(this);

for (const az of azs) {
if (typeof(az) !== 'string') {
Expand All @@ -54,7 +55,7 @@ class EnvContextExample extends cdk.Stack {
// render construct name based on it's availablity zone
const constructName = `InstanceFor${az.replace(/-/g, '').toUpperCase()}`;

new ec2.InstanceResource(this, constructName, {
new ec2.cloudformation.InstanceResource(this, constructName, {
imageId: ami.imageId,
availabilityZone: az,
});
Expand Down Expand Up @@ -89,7 +90,7 @@ class IncludeExample extends cdk.Stack {

// add constructs (and resources) programmatically
new EnvContextExample(parent, 'Example');
new sqs.QueueResource(this, 'CDKQueue', {});
new sqs.cloudformation.QueueResource(this, 'CDKQueue', {});
}
}

Expand Down Expand Up @@ -124,10 +125,10 @@ class ResourceReferencesExample extends cdk.Stack {
constructor(parent: cdk.App, name: string, props?: cdk.StackProps) {
super(parent, name, props);

const topic = new sns.TopicResource(this, 'Topic', {});
const queue = new sqs.QueueResource(this, 'Queue', {});
const topic = new sns.cloudformation.TopicResource(this, 'Topic', {});
const queue = new sqs.cloudformation.QueueResource(this, 'Queue', {});

new sns.SubscriptionResource(this, 'Subscription', {
new sns.cloudformation.SubscriptionResource(this, 'Subscription', {
topicArn: topic.ref, // resolves to { Ref: <topic-id> }
protocol: 'sqs',
endpoint: queue.queueArn // resolves to { "Fn::GetAtt": [ <queue-id>, "Arn" ] }
Expand Down
15 changes: 0 additions & 15 deletions examples/cdk-examples-typescript/chat-app/DynamodbPostsTable.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Construct } from '@aws-cdk/core';
import { cognito } from '@aws-cdk/resources';
import * as cognito from '@aws-cdk/cognito';
import * as cdk from '@aws-cdk/core';

export class CognitoChatRoomPool extends Construct {
constructor(parent: Construct, name: string) {
export class CognitoChatRoomPool extends cdk.Construct {
constructor(parent: cdk.Construct, name: string) {
super(parent, name);

// Create chat room user pool
const chatPool = new cognito.UserPoolResource(this, 'UserPool', {
const chatPool = new cognito.cloudformation.UserPoolResource(this, 'UserPool', {
adminCreateUserConfig: {
allowAdminCreateUserOnly: false
},
Expand All @@ -26,7 +26,7 @@ export class CognitoChatRoomPool extends Construct {
});

// Now for the client
new cognito.UserPoolClientResource(this, 'UserPoolClient', {
new cognito.cloudformation.UserPoolClientResource(this, 'UserPoolClient', {
clientName: 'Chat-Room',
explicitAuthFlows: [ 'ADMIN_NO_SRP_AUTH' ],
refreshTokenValidity: 30,
Expand Down
15 changes: 15 additions & 0 deletions examples/cdk-examples-typescript/chat-app/dynamodb-posts-table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Construct } from '@aws-cdk/core';
import { KeyAttributeType, Table } from '@aws-cdk/dynamodb';

export class DynamoPostsTable extends Construct {
constructor(parent: Construct, name: string) {
super(parent, name);

const table = new Table(this, 'Table', {
readCapacity: 5, writeCapacity: 5
});

table.addPartitionKey('Alias', KeyAttributeType.String);
table.addSortKey('Timestamp', KeyAttributeType.String);
}
}
4 changes: 2 additions & 2 deletions examples/cdk-examples-typescript/chat-app/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { App, Construct, Stack, StackProps } from '@aws-cdk/core';
import { Lambda, LambdaRuntime, LambdaS3Code } from '@aws-cdk/lambda';
import { BucketName, BucketRef } from '@aws-cdk/s3';
import { CognitoChatRoomPool } from './CognitoChatRoomPool';
import { DynamoPostsTable } from './DynamodbPostsTable';
import { CognitoChatRoomPool } from './cognito-chat-room-pool';
import { DynamoPostsTable } from './dynamodb-posts-table';

class MyStack extends Stack {
constructor(parent: App, name: string, props?: StackProps) {
Expand Down
12 changes: 6 additions & 6 deletions examples/cdk-examples-typescript/cloudformation/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { App, Stack } from '@aws-cdk/core';
import { sqs } from '@aws-cdk/resources';
import * as cdk from '@aws-cdk/core';
import * as sqs from '@aws-cdk/sqs';

class CloudFormationExample extends Stack {
constructor(parent: App) {
class CloudFormationExample extends cdk.Stack {
constructor(parent: cdk.App) {
super(parent);

new sqs.QueueResource(this, 'MyQueue', {
new sqs.cloudformation.QueueResource(this, 'MyQueue', {
visibilityTimeout: 300
});
}
}

const app = new App(process.argv);
const app = new cdk.App(process.argv);

new CloudFormationExample(app);

Expand Down
5 changes: 3 additions & 2 deletions examples/cdk-examples-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A bunch of CDK examples",
"private": true,
"scripts": {
"prepare": "tsc && tslint -p . && pkglint",
"build": "tsc && tslint -p . && pkglint",
"watch": "tsc -w",
"lint": "tsc && tslint -p . --force",
"test": "echo ok",
Expand All @@ -21,14 +21,15 @@
"pkglint": "^0.7.3-beta"
},
"dependencies": {
"@aws-cdk/cloudformation": "^0.7.3-beta",
"@aws-cdk/cognito": "^0.7.3-beta",
"@aws-cdk/core": "^0.7.3-beta",
"@aws-cdk/dynamodb": "^0.7.3-beta",
"@aws-cdk/ec2": "^0.7.3-beta",
"@aws-cdk/iam": "^0.7.3-beta",
"@aws-cdk/lambda": "^0.7.3-beta",
"@aws-cdk/neptune": "^0.7.3-beta",
"@aws-cdk/rds": "^0.7.3-beta",
"@aws-cdk/resources": "^0.7.3-beta",
"@aws-cdk/rtv": "^0.7.3-beta",
"@aws-cdk/s3": "^0.7.3-beta",
"@aws-cdk/sns": "^0.7.3-beta",
Expand Down
Loading

0 comments on commit aa404a0

Please sign in to comment.