Skip to content

Commit

Permalink
Add error messages and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sayali Gaikawad <[email protected]>
  • Loading branch information
gaiksaya committed Feb 17, 2024
1 parent 04428da commit 3a5009f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/networking/vpc-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export class NetworkStack extends Stack {
const serverAccessType = `${props?.serverAccessType ?? scope.node.tryGetContext('serverAccessType')}`;
const restrictServerAccessTo = `${props?.restrictServerAccessTo ?? scope.node.tryGetContext('restrictServerAccessTo')}`;

if (typeof restrictServerAccessTo === 'undefined' || typeof serverAccessType === 'undefined') {
if (serverAccessType === 'securityGroupId' && vpcId === 'undefined') {
throw new Error('securityGroupID needs to belong to the same VPC as other resources. Please specify existing vpcId');
}

if (restrictServerAccessTo === 'undefined' || serverAccessType === 'undefined') {
throw new Error('serverAccessType and restrictServerAccessTo parameters are required - eg: serverAccessType=ipv4 restrictServerAccessTo=10.10.10.10/32');
} else {
serverAccess = NetworkStack.getServerAccess(restrictServerAccessTo, serverAccessType);
Expand Down
37 changes: 37 additions & 0 deletions test/opensearch-cluster-cdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ test('Test Resources with securityGroupId param', () => {
distVersion: '1.0.0',
serverAccessType: 'securityGroupId',
restrictServerAccessTo: 'sg-012a34s123d234f90',
vpcId: 'vpc-12345',
},
});

Expand All @@ -895,3 +896,39 @@ test('Test Resources with securityGroupId param', () => {
],
});
});

test('Test Resources with securityGroupId and vpcID param missing', () => {
const app = new App({
context: {
securityDisabled: false,
minDistribution: false,
distributionUrl: 'www.example.com',
cpuArch: 'x64',
singleNodeCluster: false,
dashboardsUrl: 'www.example.com',
distVersion: '1.0.0',
serverAccessType: 'securityGroupId',
restrictServerAccessTo: 'sg-012a34s123d234f90',
},
});

try {
const networkStack = new NetworkStack(app, 'opensearch-network-stack', {
env: { account: 'test-account', region: 'us-east-1' },
});

// @ts-ignore
const infraStack = new InfraStack(app, 'opensearch-infra-stack', {
vpc: networkStack.vpc,
securityGroup: networkStack.osSecurityGroup,
env: { account: 'test-account', region: 'us-east-1' },
});

// eslint-disable-next-line no-undef
fail('Expected an error to be thrown');
} catch (error) {
expect(error).toBeInstanceOf(Error);
// @ts-ignore
expect(error.message).toEqual('securityGroupID needs to belong to the same VPC as other resources. Please specify existing vpcId');
}
});

0 comments on commit 3a5009f

Please sign in to comment.