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

fix: Iservice for ecsdeployaction #6203

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface EcsDeployActionProps extends codepipeline.CommonAwsActionProps
/**
* The ECS Service to deploy.
*/
readonly service: ecs.BaseService;
readonly service: ecs.IBaseService;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/base/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ export interface IService extends IResource {
* @attribute
*/
readonly serviceArn: string;

/**
ahammond marked this conversation as resolved.
Show resolved Hide resolved
* The name of the service.
*
* @attribute
*/
readonly serviceName: string;

ahammond marked this conversation as resolved.
Show resolved Hide resolved
}

export interface IBaseService extends IService {
readonly cluster: ICluster;
}

/**
Expand Down
31 changes: 29 additions & 2 deletions packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
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 { NetworkMode, TaskDefinition } from '../base/task-definition';
import { CfnService } from '../ecs.generated';
import { PlacementConstraint, PlacementStrategy } from '../placement';
Expand Down Expand Up @@ -98,12 +106,31 @@ export class Ec2Service extends BaseService implements IEc2Service {
* Imports from the specified service ARN.
*/
public static fromEc2ServiceArn(scope: Construct, id: string, ec2ServiceArn: string): IEc2Service {
class Import extends Resource implements IEc2Service {
const serviceName = cdk.Stack.of(scope).parseArn(ec2ServiceArn).serviceName;
if (!serviceName) {
throw new Error(`ECS ARN must be in the format 'arn:aws:ecs:<region>:<account>:service/<serviceName>', got: '${ec2ServiceArn}'`);
}
class Import extends cdk.Resource implements IEc2Service {
public readonly serviceArn = ec2ServiceArn;
public readonly serviceName = serviceName;
}

return new Import(scope, id);
}

/**
* Import a service definition from the specified service attributes.
*/
public static fromEc2ServiceAttributes(scope: Construct, id: string, attributes: string): IBaseService {
Copy link
Contributor

Choose a reason for hiding this comment

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

fromEc2ServiceAttributes needs a struct as the last argument, not a string. Something like this:

export interface Ec2ServiceAttributes {
  /** One of this, or {@link serviceName}, is required. */
  public readonly serviceArn?: string;

  /** One of this, or {@link serviceArn}, is required. */
  public readonly serviceName?: string;

  public readonly cluster: ICluster;
}

public static fromEc2ServiceAttributes(scope: Construct, id: string, attributes: Ec2ServiceAttributes): IBaseService {
  // ...
}

Take a look at the fromBucketAttributes method in S3 for inspiration.

class Import extends Resource implements IBaseService {
public readonly cluster = ;
public readonly serviceArn = fargateServiceArn;
public readonly serviceName = matcher.exec(fargateServiceArn)[0];
}
return new Import(scope, id)
}


Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary empty line.

private readonly constraints: CfnService.PlacementConstraintProperty[];
private readonly strategies: CfnService.PlacementStrategyProperty[];
private readonly daemon: boolean;
Expand Down
30 changes: 28 additions & 2 deletions packages/@aws-cdk/aws-ecs/lib/fargate/fargate-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
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 { TaskDefinition } from '../base/task-definition';

/**
Expand Down Expand Up @@ -73,15 +80,34 @@ export interface IFargateService extends IService {
export class FargateService extends BaseService implements IFargateService {

/**
* Import a task definition from the specified task definition ARN.
* Import a service definition from the specified task definition ARN.
*/
public static fromFargateServiceArn(scope: cdk.Construct, id: string, fargateServiceArn: string): IFargateService {
const serviceName = cdk.Stack.of(scope).parseArn(fargateServiceArn).serviceName;
if (!serviceName) {
throw new Error(`ECS ARN must be in the format 'arn:aws:ecs:<region>:<account>:service/<serviceName>', got: '${fargateServiceArn}'`);
}
class Import extends cdk.Resource implements IFargateService {
public readonly serviceArn = fargateServiceArn;
public readonly serviceName = serviceName;
}
return new Import(scope, id);
}

/**
* Import a service definition from the specified service attributes.
*/
public static fromFargateServiceAttributes(scope: cdk.Construct, id: string, attributes: string): IBaseService {
const matcher = new RegExp(/arn:[^:]+:ecs:[^:]+[^:]+:[^:]+:service\/(.*)/);
class Import extends cdk.Resource implements IBaseService {
public readonly cluster = ;
public readonly serviceArn = fargateServiceArn;
public readonly serviceName = matcher.exec(fargateServiceArn)[0];
}
return new Import(scope, id);
}


Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary empty line.

/**
* Constructs a new instance of the FargateService class.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Same exact comments in this file as in ec2-service.ts (use Stack.parseArn, new interface FargateServiceAttributes).

Expand Down