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

feat(cloud9): support AWS CodeCommit repository clone on launch #8205

Merged
merged 20 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
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
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-cloud9/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ const c9env = new cloud9.Ec2Environment(this, 'Cloud9Env3', {
new cdk.CfnOutput(this, 'URL', { value: c9env.ideUrl });
```

### Cloning Repositories

Use `clonedRepositories` to clone AWS Codecommit repositories into the environment:

```ts
const repo = codecommit.Repository.fromRepositoryName(stack, 'Repo', 'foo');

new cloud9.Ec2Environment(stack, 'C9Env', {
vpc,
clonedRepositories: [
cloud9.CloneRepository.fromCodeCommit(repo, '/foo'),
],
});
```
35 changes: 32 additions & 3 deletions packages/@aws-cdk/aws-cloud9/lib/environment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import { CfnEnvironmentEC2 } from '../lib/cloud9.generated';
Expand All @@ -20,7 +21,6 @@ export interface IEc2Environment extends cdk.IResource {
* @attribute environmentE2Arn
*/
readonly ec2EnvironmentArn: string;

}

/**
Expand Down Expand Up @@ -61,6 +61,14 @@ export interface Ec2EnvironmentProps {
* @default - no description
*/
readonly description?: string;

/**
* The AWS CodeCommit repository to be cloned
*
* @default - do not clone any repository
*/
// readonly clonedRepositories?: Cloud9Repository[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this commented out line anymore.

readonly clonedRepositories?: CloneRepository[];
}

/**
Expand Down Expand Up @@ -125,11 +133,32 @@ export class Ec2Environment extends cdk.Resource implements IEc2Environment {
name: props.ec2EnvironmentName,
description: props.description,
instanceType: props.instanceType?.toString() ?? ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO).toString(),
subnetId: this.vpc.selectSubnets(vpcSubnets).subnetIds[0] ,
subnetId: this.vpc.selectSubnets(vpcSubnets).subnetIds[0],
repositories: props.clonedRepositories ? props.clonedRepositories.map(r => ({
repositoryUrl: r.repositoryUrl,
pathComponent: r.clonePath,
})) : undefined,
});
this.environmentId = c9env.ref;
this.ec2EnvironmentArn = c9env.getAtt('Arn').toString();
this.ec2EnvironmentName = c9env.getAtt('Name').toString();
this.ideUrl = `https://${this.stack.region}.console.aws.amazon.com/cloud9/ide/${this.environmentId}`;
}
}
}

/**
* The class for different repository providers
*/
export class CloneRepository {
/**
* import repository to cloud9 environment from AWS CodeCommit
*/
public static fromCodeCommit(repository: codecommit.IRepository, path: string): CloneRepository {
return {
repositoryUrl: repository.repositoryCloneUrlHttp,
clonePath: path,
};
}

private constructor(public readonly repositoryUrl: string, public readonly clonePath: string) {}
}
7 changes: 6 additions & 1 deletion packages/@aws-cdk/aws-cloud9/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,22 @@
"license": "Apache-2.0",
"devDependencies": {
"@aws-cdk/assert": "0.0.0",
"@aws-cdk/aws-codecommit": "0.0.0",
"cdk-build-tools": "0.0.0",
"cdk-integ-tools": "0.0.0",
"cfn2ts": "0.0.0",
"pkglint": "0.0.0"
},
"dependencies": {
"@aws-cdk/core": "0.0.0",
"@aws-cdk/aws-codecommit": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"constructs": "^3.0.2"
},
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/core": "0.0.0",
"@aws-cdk/aws-codecommit": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"constructs": "^3.0.2"
},
Expand All @@ -87,7 +90,9 @@
"exclude": [
"resource-attribute:@aws-cdk/aws-cloud9.Ec2Environment.environmentEc2Arn",
"resource-attribute:@aws-cdk/aws-cloud9.Ec2Environment.environmentEc2Name",
"props-physical-name:@aws-cdk/aws-cloud9.Ec2EnvironmentProps"
"props-physical-name:@aws-cdk/aws-cloud9.Ec2EnvironmentProps",
"docs-public-apis:@aws-cdk/aws-cloud9.CloneRepository.clonePath",
"docs-public-apis:@aws-cdk/aws-cloud9.CloneRepository.repositoryUrl"
]
},
"stability": "experimental",
Expand Down
42 changes: 40 additions & 2 deletions packages/@aws-cdk/aws-cloud9/test/cloud9.environment.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect as expectCDK, haveResource } from '@aws-cdk/assert';
import { expect as expectCDK, haveResource, haveResourceLike } from '@aws-cdk/assert';
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import * as cloud9 from '../lib';
Expand Down Expand Up @@ -66,4 +67,41 @@ test('throw error when subnetSelection not specified and the provided VPC has no
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C5, ec2.InstanceSize.LARGE),
});
}).toThrow(/no subnetSelection specified and no public subnet found in the vpc, please specify subnetSelection/);
});
});

test('can use CodeCommit repositories', () => {
// WHEN
const repo = codecommit.Repository.fromRepositoryName(stack, 'Repo', 'foo');

new cloud9.Ec2Environment(stack, 'C9Env', {
vpc,
clonedRepositories: [
cloud9.CloneRepository.fromCodeCommit(repo, '/foo'),
],
});
// THEN
expectCDK(stack).to(haveResourceLike('AWS::Cloud9::EnvironmentEC2', {
InstanceType: 't2.micro',
Repositories: [
{
PathComponent: '/foo',
RepositoryUrl: {
'Fn::Join': [
'',
[
'https://git-codecommit.',
{
Ref: 'AWS::Region',
},
'.',
{
Ref: 'AWS::URLSuffix',
},
'/v1/repos/foo',
],
],
},
},
],
}));
});
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-cloud9/test/integ.cloud9.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,27 @@
}
}
},
"Repo02AC86CF": {
"Type": "AWS::CodeCommit::Repository",
"Properties": {
"RepositoryName": "foo"
}
},
"C9EnvF05FC3BE": {
"Type": "AWS::Cloud9::EnvironmentEC2",
"Properties": {
"InstanceType": "t2.micro",
"Repositories": [
{
"PathComponent": "/foo",
"RepositoryUrl": {
"Fn::GetAtt": [
"Repo02AC86CF",
"CloneUrlHttp"
]
}
}
],
"SubnetId": {
"Ref": "VPCPublicSubnet1SubnetB4246D30"
}
Expand Down
16 changes: 14 additions & 2 deletions packages/@aws-cdk/aws-cloud9/test/integ.cloud9.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import * as cloud9 from '../lib';
Expand All @@ -11,13 +12,24 @@ export class Cloud9Env extends cdk.Stack {
natGateways: 1,
});

// create a codecommit repository to clone into the cloud9 environment
const repo = new codecommit.Repository(this, 'Repo', {
repositoryName: 'foo',
});

// create a cloud9 ec2 environment in a new VPC
const c9env = new cloud9.Ec2Environment(this, 'C9Env', { vpc });
const c9env = new cloud9.Ec2Environment(this, 'C9Env', {
vpc,
// clone repositories into the environment
clonedRepositories: [
cloud9.CloneRepository.fromCodeCommit(repo, '/foo'),
],
});
new cdk.CfnOutput(this, 'URL', { value: c9env.ideUrl });
new cdk.CfnOutput(this, 'ARN', { value: c9env.ec2EnvironmentArn });
}
}

const app = new cdk.App();

new Cloud9Env(app, 'C9Stack');
new Cloud9Env(app, 'C9Stack');