From cd0e33a4a6237159fcb65ed3d5ecb0dc24a54010 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Sat, 25 Nov 2023 04:20:25 -0800 Subject: [PATCH] add ipaddresses validation --- .../aws-globalaccelerator/lib/accelerator.ts | 8 ++++++ .../test/globalaccelerator.test.ts | 28 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/packages/aws-cdk-lib/aws-globalaccelerator/lib/accelerator.ts b/packages/aws-cdk-lib/aws-globalaccelerator/lib/accelerator.ts index 69197a0a9fce1..8323c8fb3236a 100644 --- a/packages/aws-cdk-lib/aws-globalaccelerator/lib/accelerator.ts +++ b/packages/aws-cdk-lib/aws-globalaccelerator/lib/accelerator.ts @@ -179,6 +179,8 @@ export class Accelerator extends cdk.Resource implements IAccelerator { super(scope, id); this.validateAcceleratorName(props.acceleratorName); + this.validateIpAddresses(props.ipAddresses); + const name = props.acceleratorName ?? cdk.Names.uniqueResourceName(this, { maxLength: 64, }); @@ -211,4 +213,10 @@ export class Accelerator extends cdk.Resource implements IAccelerator { throw new Error(`Invalid acceleratorName value ${name}, must have length between 1 and 64, got: ${name.length}`); } } + + private validateIpAddresses(ipAddresses?: string[]) { + if (ipAddresses !== undefined && (ipAddresses.length < 1 || ipAddresses.length > 2)) { + throw new Error(`Invalid ipAddresses value [${ipAddresses}], you can specify one or two addresses separated by a comma.`); + } + } } diff --git a/packages/aws-cdk-lib/aws-globalaccelerator/test/globalaccelerator.test.ts b/packages/aws-cdk-lib/aws-globalaccelerator/test/globalaccelerator.test.ts index 34fcb00c38a4e..22ce7b64e6cb3 100644 --- a/packages/aws-cdk-lib/aws-globalaccelerator/test/globalaccelerator.test.ts +++ b/packages/aws-cdk-lib/aws-globalaccelerator/test/globalaccelerator.test.ts @@ -280,3 +280,31 @@ test('should validate acceleratorName minimum and maximum length', () => { }); }).toThrowError(/must have length between 1 and 64/); }); + +test('should validate ipAddresses minimum and maximum length', () => { + const { stack } = testFixture(); + + expect(() => { + new ga.Accelerator(stack, 'Acc1', {}); + }).not.toThrow(); + expect(() => { + new ga.Accelerator(stack, 'Acc2', { + ipAddresses: ['1.1.1.1'], + }); + }).not.toThrow(); + expect(() => { + new ga.Accelerator(stack, 'Acc3', { + ipAddresses: ['1.1.1.1', '2.2.2.2'], + }); + }).not.toThrow(); + expect(() => { + new ga.Accelerator(stack, 'Acc4', { + ipAddresses: [], + }); + }).toThrow('Invalid ipAddresses value [], you can specify one or two addresses separated by a comma.'); + expect(() => { + new ga.Accelerator(stack, 'Acc5', { + ipAddresses: ['1.1.1.1', '2.2.2.2', '3.3.3.3'], + }); + }).toThrow('Invalid ipAddresses value [1.1.1.1,2.2.2.2,3.3.3.3], you can specify one or two addresses separated by a comma.'); +});