Skip to content

Commit 2afad8d

Browse files
authored
refactor(ecs): replace instanceOf into Cluster.isCluster() method (#24455)
Closes #24454 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6bda4e5 commit 2afad8d

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

packages/@aws-cdk/aws-ecs/lib/cluster.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { InstanceDrainHook } from './drain-hook/instance-drain-hook';
1313
import { ECSMetrics } from './ecs-canned-metrics.generated';
1414
import { CfnCluster, CfnCapacityProvider, CfnClusterCapacityProviderAssociations } from './ecs.generated';
1515

16+
const CLUSTER_SYMBOL = Symbol.for('@aws-cdk/aws-ecs/lib/cluster.Cluster');
17+
1618
/**
1719
* The properties used to define an ECS cluster.
1820
*/
@@ -94,6 +96,14 @@ export enum MachineImageType {
9496
* A regional grouping of one or more container instances on which you can run tasks and services.
9597
*/
9698
export class Cluster extends Resource implements ICluster {
99+
100+
/**
101+
* Return whether the given object is a Cluster
102+
*/
103+
public static isCluster(x: any) : x is Cluster {
104+
return x !== null && typeof(x) === 'object' && CLUSTER_SYMBOL in x;
105+
}
106+
97107
/**
98108
* Import an existing cluster to the stack from its attributes.
99109
*/
@@ -679,6 +689,12 @@ export class Cluster extends Resource implements ICluster {
679689
}
680690
}
681691

692+
Object.defineProperty(Cluster.prototype, CLUSTER_SYMBOL, {
693+
value: true,
694+
enumerable: false,
695+
writable: false,
696+
});
697+
682698
/**
683699
* A regional grouping of one or more container instances on which you can run tasks and services.
684700
*/
@@ -1259,7 +1275,7 @@ class MaybeCreateCapacityProviderAssociations implements IAspect {
12591275
}
12601276

12611277
public visit(node: IConstruct): void {
1262-
if (node instanceof Cluster) {
1278+
if (Cluster.isCluster(node)) {
12631279
if ((this.scope.defaultCapacityProviderStrategy.length > 0 || this.scope.capacityProviderNames.length > 0 && !this.resource)) {
12641280
this.resource = new CfnClusterCapacityProviderAssociations(this.scope, this.id, {
12651281
cluster: node.clusterName,

packages/@aws-cdk/aws-ecs/test/cluster.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,38 @@ import * as cxapi from '@aws-cdk/cx-api';
1111
import * as ecs from '../lib';
1212

1313
describe('cluster', () => {
14+
describe('isCluster() returns', () => {
15+
test('true if given cluster instance', () => {
16+
// GIVEN
17+
const stack = new cdk.Stack();
18+
// WHEN
19+
const createdCluster = new ecs.Cluster(stack, 'EcsCluster');
20+
// THEN
21+
expect(ecs.Cluster.isCluster(createdCluster)).toBe(true);
22+
});
23+
24+
test('false if given imported cluster instance', () => {
25+
// GIVEN
26+
const stack = new cdk.Stack();
27+
const vpc = new ec2.Vpc(stack, 'Vpc');
28+
29+
const importedSg = ec2.SecurityGroup.fromSecurityGroupId(stack, 'SG1', 'sg-1', { allowAllOutbound: false });
30+
// WHEN
31+
const importedCluster = ecs.Cluster.fromClusterAttributes(stack, 'Cluster', {
32+
clusterName: 'cluster-name',
33+
securityGroups: [importedSg],
34+
vpc,
35+
});
36+
// THEN
37+
expect(ecs.Cluster.isCluster(importedCluster)).toBe(false);
38+
});
39+
40+
test('false if given undefined', () => {
41+
// THEN
42+
expect(ecs.Cluster.isCluster(undefined)).toBe(false);
43+
});
44+
});
45+
1446
describe('When creating an ECS Cluster', () => {
1547
testDeprecated('with no properties set, it correctly sets default properties', () => {
1648
// GIVEN

0 commit comments

Comments
 (0)