Skip to content

Commit 9f88696

Browse files
jogoldrix0rrr
authored andcommitted
fix(servicediscovery): allow to register multiple instances on a service (#2207)
1 parent d46b814 commit 9f88696

File tree

5 files changed

+53
-35
lines changed

5 files changed

+53
-35
lines changed

packages/@aws-cdk/aws-servicediscovery/lib/service.ts

+9-16
Original file line numberDiff line numberDiff line change
@@ -262,37 +262,30 @@ export class Service extends cdk.Construct implements IService {
262262
/**
263263
* Registers a resource that is accessible using values other than an IP address or a domain name (CNAME).
264264
*/
265-
public registerNonIpInstance(props: NonIpInstanceBaseProps): IInstance {
266-
return new NonIpInstance(this, "NonIpInstance", {
265+
public registerNonIpInstance(id: string, props: NonIpInstanceBaseProps): IInstance {
266+
return new NonIpInstance(this, id, {
267267
service: this,
268-
instanceId: props.instanceId,
269-
customAttributes: props.customAttributes
268+
...props
270269
});
271270
}
272271

273272
/**
274273
* Registers a resource that is accessible using an IP address.
275274
*/
276-
public registerIpInstance(props: IpInstanceBaseProps): IInstance {
277-
return new IpInstance(this, "IpInstance", {
275+
public registerIpInstance(id: string, props: IpInstanceBaseProps): IInstance {
276+
return new IpInstance(this, id, {
278277
service: this,
279-
instanceId: props.instanceId,
280-
ipv4: props.ipv4,
281-
ipv6: props.ipv6,
282-
port: props.port,
283-
customAttributes: props.customAttributes
278+
...props
284279
});
285280
}
286281

287282
/**
288283
* Registers a resource that is accessible using a CNAME.
289284
*/
290-
public registerCnameInstance(props: CnameInstanceBaseProps): IInstance {
291-
return new CnameInstance(this, "CnameInstance", {
285+
public registerCnameInstance(id: string, props: CnameInstanceBaseProps): IInstance {
286+
return new CnameInstance(this, id, {
292287
service: this,
293-
instanceId: props.instanceId,
294-
instanceCname: props.instanceCname,
295-
customAttributes: props.customAttributes
288+
...props
296289
});
297290
}
298291
}

packages/@aws-cdk/aws-servicediscovery/test/integ.service-with-cname-record.lit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const service = namespace.createService('Service', {
1414
dnsTtlSec: 30
1515
});
1616

17-
service.registerCnameInstance({
17+
service.registerCnameInstance('CnameInstance', {
1818
instanceCname: 'service.pizza',
1919
});
2020

packages/@aws-cdk/aws-servicediscovery/test/integ.service-with-http-namespace.lit.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const service1 = namespace.createService('NonIpService', {
1212
description: 'service registering non-ip instances',
1313
});
1414

15-
service1.registerNonIpInstance({
15+
service1.registerNonIpInstance('NonIpInstance', {
1616
customAttributes: { arn: 'arn:aws:s3:::mybucket' }
1717
});
1818

@@ -24,7 +24,7 @@ const service2 = namespace.createService('IpService', {
2424
}
2525
});
2626

27-
service2.registerIpInstance({
27+
service2.registerIpInstance('IpInstance', {
2828
ipv4: '54.239.25.192',
2929
});
3030

packages/@aws-cdk/aws-servicediscovery/test/integ.service-with-public-dns-namespace.lit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const service = namespace.createService('Service', {
1919
}
2020
});
2121

22-
service.registerIpInstance({
22+
service.registerIpInstance('IpInstance', {
2323
ipv4: '54.239.25.192',
2424
port: 443
2525
});

packages/@aws-cdk/aws-servicediscovery/test/test.instance.ts

+40-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, haveResource } from '@aws-cdk/assert';
1+
import { countResources, expect, haveResource } from '@aws-cdk/assert';
22
import ec2 = require('@aws-cdk/aws-ec2');
33
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
44
import cdk = require('@aws-cdk/cdk');
@@ -18,7 +18,7 @@ export = {
1818
name: 'service',
1919
});
2020

21-
service.registerIpInstance({
21+
service.registerIpInstance('IpInstance', {
2222
ipv4: '10.0.0.0',
2323
ipv6: '0:0:0:0:0:ffff:a00:0',
2424
port: 443
@@ -56,7 +56,7 @@ export = {
5656
dnsRecordType: servicediscovery.DnsRecordType.A_AAAA
5757
});
5858

59-
service.registerIpInstance({
59+
service.registerIpInstance('IpInstance', {
6060
ipv4: '54.239.25.192',
6161
ipv6: '0:0:0:0:0:ffff:a00:0',
6262
port: 443
@@ -96,7 +96,7 @@ export = {
9696
dnsRecordType: servicediscovery.DnsRecordType.A_AAAA
9797
});
9898

99-
service.registerIpInstance({
99+
service.registerIpInstance('IpInstance', {
100100
ipv4: '10.0.0.0',
101101
ipv6: '0:0:0:0:0:ffff:a00:0',
102102
port: 443
@@ -136,7 +136,7 @@ export = {
136136

137137
// THEN
138138
test.throws(() => {
139-
service.registerIpInstance({
139+
service.registerIpInstance('IpInstance', {
140140
instanceId: 'id',
141141
});
142142
}, /A `port` must be specified for a service using a `SRV` record./);
@@ -159,7 +159,7 @@ export = {
159159

160160
// THEN
161161
test.throws(() => {
162-
service.registerIpInstance({
162+
service.registerIpInstance('IpInstance', {
163163
port: 3306
164164
});
165165
}, /At least `ipv4` or `ipv6` must be specified for a service using a `SRV` record./);
@@ -182,7 +182,7 @@ export = {
182182

183183
// THEN
184184
test.throws(() => {
185-
service.registerIpInstance({
185+
service.registerIpInstance('IpInstance', {
186186
port: 3306
187187
});
188188
}, /An `ipv4` must be specified for a service using a `A` record./);
@@ -205,7 +205,7 @@ export = {
205205

206206
// THEN
207207
test.throws(() => {
208-
service.registerIpInstance({
208+
service.registerIpInstance('IpInstance', {
209209
port: 3306
210210
});
211211
}, /An `ipv6` must be specified for a service using a `AAAA` record./);
@@ -228,7 +228,7 @@ export = {
228228

229229
// THEN
230230
test.throws(() => {
231-
service.registerIpInstance({
231+
service.registerIpInstance('IpInstance', {
232232
port: 3306
233233
});
234234
}, /Service must support `A`, `AAAA` or `SRV` records to register this instance type./);
@@ -338,7 +338,7 @@ export = {
338338
dnsRecordType: servicediscovery.DnsRecordType.CNAME
339339
});
340340

341-
service.registerCnameInstance({
341+
service.registerCnameInstance('CnameInstance', {
342342
instanceCname: 'foo.com',
343343
customAttributes: { dogs: 'good' }
344344
});
@@ -375,7 +375,7 @@ export = {
375375

376376
// THEN
377377
test.throws(() => {
378-
service.registerCnameInstance({
378+
service.registerCnameInstance('CnameInstance', {
379379
instanceCname: 'foo.com',
380380
});
381381
}, /Namespace associated with Service must be a DNS Namespace/);
@@ -393,7 +393,7 @@ export = {
393393

394394
const service = namespace.createService('MyService');
395395

396-
service.registerNonIpInstance({
396+
service.registerNonIpInstance('NonIpInstance', {
397397
customAttributes: { dogs: 'good' }
398398
});
399399

@@ -426,7 +426,7 @@ export = {
426426

427427
// THEN
428428
test.throws(() => {
429-
service.registerNonIpInstance({
429+
service.registerNonIpInstance('NonIpInstance', {
430430
instanceId: 'nonIp',
431431
});
432432
}, /This type of instance can only be registered for HTTP namespaces./);
@@ -446,7 +446,7 @@ export = {
446446

447447
// THEN
448448
test.throws(() => {
449-
service.registerNonIpInstance({
449+
service.registerNonIpInstance('NonIpInstance', {
450450
instanceId: 'nonIp',
451451
});
452452
}, /You must specify at least one custom attribute for this instance type./);
@@ -466,12 +466,37 @@ export = {
466466

467467
// THEN
468468
test.throws(() => {
469-
service.registerNonIpInstance({
469+
service.registerNonIpInstance('NonIpInstance', {
470470
instanceId: 'nonIp',
471471
customAttributes: {}
472472
});
473473
}, /You must specify at least one custom attribute for this instance type./);
474474

475475
test.done();
476476
},
477+
478+
'Register multiple instances on the same service'(test: Test) {
479+
// GIVEN
480+
const stack = new cdk.Stack();
481+
482+
const namespace = new servicediscovery.PublicDnsNamespace(stack, 'MyNamespace', {
483+
name: 'public',
484+
});
485+
486+
const service = namespace.createService('MyService');
487+
488+
// WHEN
489+
service.registerIpInstance('First', {
490+
ipv4: '10.0.0.0'
491+
});
492+
493+
service.registerIpInstance('Second', {
494+
ipv4: '10.0.0.1'
495+
});
496+
497+
// THEN
498+
expect(stack).to(countResources('AWS::ServiceDiscovery::Instance', 2));
499+
500+
test.done();
501+
}
477502
};

0 commit comments

Comments
 (0)