Skip to content

Commit

Permalink
feat(cloud9): support AWS CodeCommit repository clone on launch (aws#…
Browse files Browse the repository at this point in the history
…8205)

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

Add a new `repositories` property to allow users to clone AWS CodeCommit repositories on environment launch.

Closes aws#8204

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
pahud authored Jun 8, 2020
1 parent bdb4ca5 commit 4781f94
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 8 deletions.
22 changes: 22 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,25 @@ const c9env = new cloud9.Ec2Environment(this, 'Cloud9Env3', {
new cdk.CfnOutput(this, 'URL', { value: c9env.ideUrl });
```

### Cloning Repositories

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

```ts
// create a codecommit repository to clone into the cloud9 environment
const repoNew = new codecommit.Repository(this, 'RepoNew', {
repositoryName: 'new-repo',
});

// import an existing codecommit repository to clone into the cloud9 environment
const repoExisting = codecommit.Repository.fromRepositoryName(stack, 'RepoExisting', 'existing-repo');

// create a new Cloud9 environment and clone the two repositories
new cloud9.Ec2Environment(stack, 'C9Env', {
vpc,
clonedRepositories: [
cloud9.CloneRepository.fromCodeCommit(repoNew, '/src/new-repo'),
cloud9.CloneRepository.fromCodeCommit(repoExisting, '/src/existing-repo'),
],
});
```
38 changes: 35 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[];
readonly clonedRepositories?: CloneRepository[];
}

/**
Expand Down Expand Up @@ -125,11 +133,35 @@ 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.pathComponent,
})) : 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
*
* @param repository the codecommit repository to clone from
* @param path the target path in cloud9 environment
*/
public static fromCodeCommit(repository: codecommit.IRepository, path: string): CloneRepository {
return {
repositoryUrl: repository.repositoryCloneUrlHttp,
pathComponent: path,
};
}

private constructor(public readonly repositoryUrl: string, public readonly pathComponent: 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.pathComponent",
"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, '/src'),
],
});
// THEN
expectCDK(stack).to(haveResourceLike('AWS::Cloud9::EnvironmentEC2', {
InstanceType: 't2.micro',
Repositories: [
{
PathComponent: '/src',
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');

0 comments on commit 4781f94

Please sign in to comment.