diff --git a/packages/@aws-cdk/aws-ecs/lib/cluster.ts b/packages/@aws-cdk/aws-ecs/lib/cluster.ts index b0154f3ec5d0b..95e0fd6eab340 100644 --- a/packages/@aws-cdk/aws-ecs/lib/cluster.ts +++ b/packages/@aws-cdk/aws-ecs/lib/cluster.ts @@ -87,9 +87,9 @@ export class Cluster extends cdk.Construct implements ICluster { const namespaceType = options.type !== undefined ? options.type - : cloudmap.NamespaceType.DnsPrivate; + : NamespaceType.PrivateDns; - const sdNamespace = namespaceType === cloudmap.NamespaceType.DnsPrivate ? + const sdNamespace = namespaceType === NamespaceType.PrivateDns ? new cloudmap.PrivateDnsNamespace(this, 'DefaultServiceDiscoveryNamespace', { name: options.name, vpc: this.vpc @@ -446,7 +446,7 @@ export interface NamespaceOptions { * * @default PrivateDns */ - readonly type?: cloudmap.NamespaceType.DnsPrivate | cloudmap.NamespaceType.DnsPublic; + readonly type?: NamespaceType /** * The Amazon VPC that you want to associate the namespace with. Required for Private DNS namespaces @@ -455,3 +455,8 @@ export interface NamespaceOptions { */ readonly vpc?: ec2.IVpcNetwork; } + +export enum NamespaceType { + PrivateDns = cloudmap.NamespaceType.DnsPrivate, + PublicDns = cloudmap.NamespaceType.DnsPublic +} diff --git a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts index 5103213256099..2195bdc80f017 100644 --- a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts +++ b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts @@ -5,7 +5,7 @@ import cloudmap = require('@aws-cdk/aws-servicediscovery'); import cdk = require('@aws-cdk/cdk'); import { Test } from 'nodeunit'; import ecs = require('../../lib'); -import { BinPackResource, BuiltInAttributes, ContainerImage, NetworkMode } from '../../lib'; +import { BinPackResource, BuiltInAttributes, ContainerImage, NamespaceType, NetworkMode } from '../../lib'; export = { "When creating an ECS Service": { @@ -606,7 +606,7 @@ export = { // WHEN cluster.addDefaultCloudMapNamespace({ name: 'foo.com', - type: cloudmap.NamespaceType.DnsPrivate + type: NamespaceType.PrivateDns }); new ecs.Ec2Service(stack, 'Service', { @@ -683,7 +683,7 @@ export = { // WHEN cluster.addDefaultCloudMapNamespace({ name: 'foo.com', - type: cloudmap.NamespaceType.DnsPrivate + type: NamespaceType.PrivateDns }); new ecs.Ec2Service(stack, 'Service', { @@ -794,7 +794,7 @@ export = { // WHEN cluster.addDefaultCloudMapNamespace({ name: 'foo.com', - type: cloudmap.NamespaceType.DnsPrivate + type: NamespaceType.PrivateDns }); new ecs.Ec2Service(stack, 'Service', { @@ -869,7 +869,7 @@ export = { // WHEN cluster.addDefaultCloudMapNamespace({ name: 'foo.com', - type: cloudmap.NamespaceType.DnsPrivate + type: NamespaceType.PrivateDns }); new ecs.Ec2Service(stack, 'Service', { diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts b/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts index 005d79ea0a613..ec4bd3d47bb6d 100644 --- a/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts @@ -5,7 +5,7 @@ import cloudmap = require('@aws-cdk/aws-servicediscovery'); import cdk = require('@aws-cdk/cdk'); import { Test } from 'nodeunit'; import ecs = require('../../lib'); -import { ContainerImage } from '../../lib'; +import { ContainerImage, NamespaceType } from '../../lib'; export = { "When creating a Fargate Service": { @@ -281,7 +281,7 @@ export = { // WHEN cluster.addDefaultCloudMapNamespace({ name: 'foo.com', - type: cloudmap.NamespaceType.DnsPrivate + type: NamespaceType.PrivateDns }); new ecs.FargateService(stack, 'Service', { @@ -341,7 +341,7 @@ export = { // WHEN cluster.addDefaultCloudMapNamespace({ name: 'foo.com', - type: cloudmap.NamespaceType.DnsPrivate + type: NamespaceType.PrivateDns }); new ecs.FargateService(stack, 'Service', { diff --git a/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts b/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts index 2ac92b9436a4f..ea4f4b0c218a5 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts +++ b/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts @@ -244,4 +244,29 @@ export = { test.done(); }, + + "throws if default service discovery namespace added more than once"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.VpcNetwork(stack, 'MyVpc', {}); + + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + cluster.addCapacity('DefaultAutoScalingGroup', { + instanceType: new ec2.InstanceType('t2.micro'), + }); + + // WHEN + cluster.addDefaultCloudMapNamespace({ + name: "foo.com" + }); + + // THEN + test.throws(() => { + cluster.addDefaultCloudMapNamespace({ + name: "foo.com" + }); + }, /Can only add default namespace once./); + + test.done(); + }, };