Skip to content

Commit

Permalink
add ipaddresses validation
Browse files Browse the repository at this point in the history
  • Loading branch information
msambol committed Nov 29, 2023
1 parent ae29180 commit cd0e33a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/aws-cdk-lib/aws-globalaccelerator/lib/accelerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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.`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});

0 comments on commit cd0e33a

Please sign in to comment.