Skip to content

Commit

Permalink
create fromServiceAtrributes, remove ImportedBaseService. Some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atsushi-ishibashi committed Feb 26, 2020
1 parent fba4573 commit 6e1d696
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { expect, haveResourceLike } from '@aws-cdk/assert';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import { FakeSourceAction } from '@aws-cdk/aws-codepipeline/test/fake-source-action';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as ecs from '@aws-cdk/aws-ecs';
import * as cdk from '@aws-cdk/core';
Expand Down Expand Up @@ -81,18 +83,73 @@ export = {
test.done();
},

'can be created by ImportedBaseService'(test: Test) {
const service = anyIBaseService();
'can be created by existing service'(test: Test) {
const stack = new cdk.Stack();
const service = ecs.FargateService.fromFargateServiceAttributes(stack, 'FargateService', {
serviceName: 'my-http-service',
cluster: new ecs.Cluster(stack, 'Cluster', {
clusterName: 'cluster',
}),
});
const artifact = new codepipeline.Artifact('Artifact');

test.doesNotThrow(() => {
new cpactions.EcsDeployAction({
const action = new cpactions.EcsDeployAction({
actionName: 'ECS',
service,
imageFile: artifact.atPath('imageFile.json'),
});
new codepipeline.Pipeline(stack, 'Pipeline', {
stages: [
{
stageName: 'Source',
actions: [new FakeSourceAction({
actionName: 'Source',
output: artifact,
})],
},
{
stageName: 'Deploy',
actions: [action],
}
],
});
});

expect(stack).to(haveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: [
{
Actions: [
{
Name: 'Source',
ActionTypeId: {
Category: "Source",
Provider: "Fake"
},
}
]
},
{
Actions: [
{
Name: 'ECS',
ActionTypeId: {
Category: "Deploy",
Provider: "ECS"
},
Configuration: {
ClusterName: {
Ref: "ClusterEB0386A7",
},
ServiceName: "my-http-service",
FileName: "imageFile.json"
}
}
]
}
]
}));

test.done();
},
},
Expand All @@ -113,13 +170,3 @@ function anyEcsService(): ecs.FargateService {
taskDefinition,
});
}

function anyIBaseService(): ecs.IBaseService {
const stack = new cdk.Stack();
return new ecs.ImportedBaseService(stack, 'FargateService', {
serviceName: 'my-http-service',
cluster: new ecs.Cluster(stack, 'Cluster', {
clusterName: 'cluster',
}),
});
}
56 changes: 0 additions & 56 deletions packages/@aws-cdk/aws-ecs/lib/base/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,62 +262,6 @@ export interface IBaseService extends IService {
readonly cluster: ICluster;
}

/**
* The properties to import from the service.
*/
export interface ImportedBaseServiceProps {
/**
* The cluster that hosts the service.
*/
readonly cluster: ICluster;

/**
* The name of the service.
*/
readonly serviceName: string;
}

/**
* A Service that has been imported
*
* @resource AWS::ECS::Service
*/
export class ImportedBaseService extends Resource implements IBaseService {
/**
* The cluster that hosts the service.
*/
public readonly cluster: ICluster;

/**
* The service ARN.
*/
public readonly serviceArn: string;

/**
* The name of the service.
*/
public readonly serviceName: string;

/**
* Constructs a new instance of the ImportedBaseService class.
*/
constructor(scope: Construct, id: string, props: ImportedBaseServiceProps) {
super(scope, id);
const stack = Stack.of(scope);
const arn: string = stack.formatArn({
partition: stack.partition,
service: 'ecs',
region: stack.region,
account: stack.account,
resource: 'service',
resourceName: props.serviceName,
});
this.serviceArn = arn;
this.serviceName = props.serviceName;
this.cluster = props.cluster;
}
}

/**
* The base class for Ec2Service and FargateService services.
*/
Expand Down
57 changes: 57 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/base/from-service-attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Construct, Resource, Stack } from '@aws-cdk/core';
import { IBaseService } from '../base/base-service';
import { ICluster } from '../cluster';

/**
* The properties to import from the service.
*/
export interface ServiceAttributes {
/**
* The cluster that hosts the service.
*/
readonly cluster: ICluster;

/**
* The service ARN.
*
* @default - generated from serviceName
*/
readonly serviceArn?: string;

/**
* The name of the service.
*
* @default - generated from serviceArn
*/
readonly serviceName?: string;
}

export function fromServiceAtrributes(scope: Construct, id: string, attrs: ServiceAttributes): IBaseService {
if ((attrs.serviceArn && attrs.serviceName) || (!attrs.serviceArn && !attrs.serviceName)) {
throw new Error('You can only specify either serviceArn or serviceName.');
}

const stack = Stack.of(scope);
let name: string;
let arn: string;
if (attrs.serviceName) {
name = attrs.serviceName as string;
arn = stack.formatArn({
partition: stack.partition,
service: 'ecs',
region: stack.region,
account: stack.account,
resource: 'service',
resourceName: name,
});
} else {
arn = attrs.serviceArn as string;
name = stack.parseArn(arn).resourceName as string;
}
class Import extends Resource implements IBaseService {
public readonly serviceArn = arn;
public readonly serviceName = name;
public readonly cluster = attrs.cluster;
}
return new Import(scope, id);
}
35 changes: 34 additions & 1 deletion packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as ec2 from '@aws-cdk/aws-ec2';
import { Construct, Lazy, Resource, Stack } from '@aws-cdk/core';
import { BaseService, BaseServiceOptions, IService, LaunchType, PropagatedTagSource } from '../base/base-service';
import { BaseService, BaseServiceOptions, IBaseService, IService, LaunchType, PropagatedTagSource } from '../base/base-service';
import { fromServiceAtrributes } from '../base/from-service-attributes';
import { NetworkMode, TaskDefinition } from '../base/task-definition';
import { ICluster } from '../cluster';
import { CfnService } from '../ecs.generated';
import { PlacementConstraint, PlacementStrategy } from '../placement';

Expand Down Expand Up @@ -87,6 +89,30 @@ export interface IEc2Service extends IService {

}

/**
* The properties to import from the service using the EC2 launch type.
*/
export interface Ec2ServiceAttributes {
/**
* The cluster that hosts the service.
*/
readonly cluster: ICluster;

/**
* The service ARN.
*
* @default - generated from serviceName
*/
readonly serviceArn?: string;

/**
* The name of the service.
*
* @default - generated from serviceArn
*/
readonly serviceName?: string;
}

/**
* This creates a service using the EC2 launch type on an ECS cluster.
*
Expand All @@ -105,6 +131,13 @@ export class Ec2Service extends BaseService implements IEc2Service {
return new Import(scope, id);
}

/**
* Imports from the specified service attrributes.
*/
public static fromEc2ServiceAttributes(scope: Construct, id: string, attrs: Ec2ServiceAttributes): IBaseService {
return fromServiceAtrributes(scope, id, attrs);
}

private readonly constraints: CfnService.PlacementConstraintProperty[];
private readonly strategies: CfnService.PlacementStrategyProperty[];
private readonly daemon: boolean;
Expand Down
35 changes: 34 additions & 1 deletion packages/@aws-cdk/aws-ecs/lib/fargate/fargate-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import { BaseService, BaseServiceOptions, IService, LaunchType, PropagatedTagSource } from '../base/base-service';
import { BaseService, BaseServiceOptions, IBaseService, IService, LaunchType, PropagatedTagSource } from '../base/base-service';
import { fromServiceAtrributes } from '../base/from-service-attributes';
import { TaskDefinition } from '../base/task-definition';
import { ICluster } from '../cluster';

/**
* The properties for defining a service using the Fargate launch type.
Expand Down Expand Up @@ -65,6 +67,30 @@ export interface IFargateService extends IService {

}

/**
* The properties to import from the service using the Fargate launch type.
*/
export interface FargateServiceAttributes {
/**
* The cluster that hosts the service.
*/
readonly cluster: ICluster;

/**
* The service ARN.
*
* @default - generated from serviceName
*/
readonly serviceArn?: string;

/**
* The name of the service.
*
* @default - generated from serviceArn
*/
readonly serviceName?: string;
}

/**
* This creates a service using the Fargate launch type on an ECS cluster.
*
Expand All @@ -83,6 +109,13 @@ export class FargateService extends BaseService implements IFargateService {
return new Import(scope, id);
}

/**
* Imports from the specified service attrributes.
*/
public static fromFargateServiceAttributes(scope: cdk.Construct, id: string, attrs: FargateServiceAttributes): IBaseService {
return fromServiceAtrributes(scope, id, attrs);
}

/**
* Constructs a new instance of the FargateService class.
*/
Expand Down
24 changes: 0 additions & 24 deletions packages/@aws-cdk/aws-ecs/test/base/test.base-service.ts

This file was deleted.

Loading

0 comments on commit 6e1d696

Please sign in to comment.