Skip to content

Commit

Permalink
Check VPC and S3 props
Browse files Browse the repository at this point in the history
  • Loading branch information
biffgaut committed Sep 15, 2023
1 parent 2a1222f commit 7f6f349
Show file tree
Hide file tree
Showing 60 changed files with 669 additions and 326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export class AlbToFargate extends Construct {
defaults.CheckProps(props);
defaults.CheckAlbProps(props);
defaults.CheckFargateProps(props);
defaults.CheckVpcProps(props);

// Obtain VPC for construct (existing or created)
this.vpc = defaults.buildVpc(scope, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,24 @@ test('Test HTTPS API with new vpc, load balancer, service and private API', () =
]
});
});

test('Confirm that CheckVpcProps is called', () => {
const stack = new cdk.Stack(undefined, undefined, {
env: { account: "123456789012", region: 'us-east-1' },
});

const props: AlbToFargateProps = {
ecrRepositoryArn: defaults.fakeEcrRepoArn,
listenerProps: {
certificates: [defaults.getFakeCertificate(stack, "fake-cert")]
},
publicApi: false,
vpcProps: {},
existingVpc: defaults.getTestVpc(stack),
};
const app = () => {
new AlbToFargate(stack, 'new-construct', props);
};
// Assertion
expect(app).toThrowError('Error - Either provide an existingVpc or some combination of deployVpc and vpcProps, but not both.\n');
});
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class AlbToLambda extends Construct {
super(scope, id);
defaults.CheckProps(props);
defaults.CheckAlbProps(props);
defaults.CheckVpcProps(props);

// Obtain VPC for construct (existing or created)
this.vpc = defaults.buildVpc(scope, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ test("Test existing load balancer and existing lambda function", () => {
existingLambdaObj: lambdaFunction,
existingLoadBalancerObj: existingAlb,
listenerProps: {
certificates: [ defaults.getFakeCertificate(stack, "fake-cert") ],
certificates: [defaults.getFakeCertificate(stack, "fake-cert")],
},
publicApi: true,
existingVpc: testExistingVpc,
Expand Down Expand Up @@ -289,7 +289,7 @@ test('Test new load balancer and new lambda function', () => {
});

const props: AlbToLambdaProps = {
lambdaFunctionProps: {
lambdaFunctionProps: {
code: lambda.Code.fromAsset(`${__dirname}/lambda`),
runtime: lambda.Runtime.NODEJS_16_X,
handler: 'index.handler',
Expand Down Expand Up @@ -952,3 +952,28 @@ test('Test existingLoadBalancerObj and no existingVpc is an error', () => {
expect(app).toThrowError(
/An existing ALB is already in a VPC, that VPC must be provided in props.existingVpc for the rest of the construct to use./);
});

test('Confirm that CheckVpcProps is called', () => {
const stack = new cdk.Stack(undefined, undefined, {
env: { account: "123456789012", region: 'us-east-1' },
});

const props: AlbToLambdaProps = {
lambdaFunctionProps: {
code: lambda.Code.fromAsset(`${__dirname}/lambda`),
runtime: lambda.Runtime.NODEJS_16_X,
handler: 'index.handler'
},
listenerProps: {
certificates: [defaults.getFakeCertificate(stack, "fake-cert")]
},
publicApi: false,
vpcProps: {},
existingVpc: defaults.getTestVpc(stack),
};
const app = () => {
new AlbToLambda(stack, 'new-construct', props);
};
// Assertion
expect(app).toThrowError('Error - Either provide an existingVpc or some combination of deployVpc and vpcProps, but not both.\n');
});
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,22 @@ test('Test minimal deployment with an existing isolated VPC', () => {

template.resourceCountIs("AWS::EC2::VPC", 1);
expect(construct.vpc).toBeDefined();
});
});

test('Confirm CheckVpcProps is being called', () => {
const stack = new cdk.Stack();

const app = () => {
new DynamoDBStreamsToLambdaToElasticSearchAndKibana(stack, 'test-construct', {
lambdaFunctionProps: getDefaultTestLambdaProps(),
domainName: "test",
deployVpc: true,
vpcProps: {
vpcName: "existing-vpc-test"
},
existingVpc: defaults.getTestVpc(stack),
});
};

expect(app).toThrowError('Error - Either provide an existingVpc or some combination of deployVpc and vpcProps, but not both.\n');
});
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class FargateToDynamoDB extends Construct {
defaults.CheckProps(props);
defaults.CheckFargateProps(props);
defaults.CheckDynamoDBProps(props);
defaults.CheckVpcProps(props);

// Other permissions for constructs are accepted as arrays, turning tablePermissions into
// an array to use the same validation function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { Template } from 'aws-cdk-lib/assertions';
import * as defaults from '@aws-solutions-constructs/core';
import * as cdk from "aws-cdk-lib";
import { FargateToDynamoDB } from "../lib";
import { FargateToDynamoDB, FargateToDynamoDBProps } from "../lib";
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
Expand Down Expand Up @@ -713,4 +713,37 @@ test('test that DDB input args are getting checked', () => {
};

expect(app).toThrowError('Error - Either provide existingTableInterface or dynamoTableProps, but not both.\n');
});
});

test('Confirm that CheckVpcProps was called', () => {
const stack = new cdk.Stack();
const publicApi = true;
const clusterName = "custom-cluster-name";
const containerName = "custom-container-name";
const serviceName = "custom-service-name";
const familyName = "custom-family-name";

const props: FargateToDynamoDBProps = {
publicApi,
ecrRepositoryArn: defaults.fakeEcrRepoArn,
clusterProps: { clusterName },
containerDefinitionProps: { containerName },
fargateTaskDefinitionProps: { family: familyName },
fargateServiceProps: { serviceName },
dynamoTableProps: {
tableName: 'fake-name',
partitionKey: {
name: 'id',
type: dynamodb.AttributeType.STRING
},
},
existingVpc: defaults.getTestVpc(stack),
vpcProps: { },
};

const app = () => {
new FargateToDynamoDB(stack, 'test-construct', props);
};
// Assertion
expect(app).toThrowError('Error - Either provide an existingVpc or some combination of deployVpc and vpcProps, but not both.\n');
});
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export class FargateToEventbridge extends Construct {
super(scope, id);
defaults.CheckProps(props);
defaults.CheckFargateProps(props);
defaults.CheckVpcProps(props);

this.vpc = defaults.buildVpc(scope, {
existingVpc: props.existingVpc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { Template } from 'aws-cdk-lib/assertions';
import * as defaults from '@aws-solutions-constructs/core';
import * as cdk from "aws-cdk-lib";
import { FargateToEventbridge } from "../lib";
import { FargateToEventbridge, FargateToEventbridgeProps } from "../lib";
import * as events from 'aws-cdk-lib/aws-events';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
Expand Down Expand Up @@ -372,4 +372,29 @@ function createFargateConstructWithNewResources(stack: cdk.Stack, publicApi: boo
eventBusName: 'custom-name'
}
});
}
}

test('Confirm that CheckVpcProps was called', () => {
const stack = new cdk.Stack();
const publicApi = true;

const props: FargateToEventbridgeProps = {
publicApi,
ecrRepositoryArn: defaults.fakeEcrRepoArn,
clusterProps: { clusterName },
containerDefinitionProps: { containerName },
fargateTaskDefinitionProps: { family: familyName },
fargateServiceProps: { serviceName },
eventBusProps: {
eventBusName: 'custom-name'
},
existingVpc: defaults.getTestVpc(stack),
vpcProps: { },
};

const app = () => {
new FargateToEventbridge(stack, 'test-construct', props);
};
// Assertion
expect(app).toThrowError('Error - Either provide an existingVpc or some combination of deployVpc and vpcProps, but not both.\n');
});
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class FargateToKinesisFirehose extends Construct {
super(scope, id);
defaults.CheckProps(props);
defaults.CheckFargateProps(props);
defaults.CheckVpcProps(props);

if (!props.existingKinesisFirehose.deliveryStreamName) {
throw new Error('existingKinesisFirehose must have a defined deliveryStreamName');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import * as defaults from '@aws-solutions-constructs/core';
import * as cdk from "aws-cdk-lib";
import { FargateToKinesisFirehose } from "../lib";
import { FargateToKinesisFirehose, FargateToKinesisFirehoseProps } from "../lib";
import * as ecs from 'aws-cdk-lib/aws-ecs';
import { Match, Template } from "aws-cdk-lib/assertions";
import { GetTestFirehoseDestination } from './test-helper';
Expand Down Expand Up @@ -711,3 +711,31 @@ test('Test fail if existingFirehose does not have a stream name', () => {

expect(app).toThrowError(/existingKinesisFirehose must have a defined deliveryStreamName/);
});

test('Confirm that CheckVpcProps was called', () => {
const stack = new cdk.Stack();
const publicApi = true;
const clusterName = "custom-cluster-name";
const containerName = "custom-container-name";
const serviceName = "custom-service-name";
const familyName = "custom-family-name";
const destination = GetTestFirehoseDestination(stack, 'test-destination');

const props: FargateToKinesisFirehoseProps = {
publicApi,
ecrRepositoryArn: defaults.fakeEcrRepoArn,
clusterProps: { clusterName },
containerDefinitionProps: { containerName },
fargateTaskDefinitionProps: { family: familyName },
fargateServiceProps: { serviceName },
existingKinesisFirehose: destination.kinesisFirehose,
existingVpc: defaults.getTestVpc(stack),
vpcProps: { },
};

const app = () => {
new FargateToKinesisFirehose(stack, 'test-construct', props);
};
// Assertion
expect(app).toThrowError('Error - Either provide an existingVpc or some combination of deployVpc and vpcProps, but not both.\n');
});
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class FargateToKinesisStreams extends Construct {
super(scope, id);
defaults.CheckProps(props);
defaults.CheckFargateProps(props);
defaults.CheckVpcProps(props);

// Setup the VPC
this.vpc = defaults.buildVpc(scope, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* and limitations under the License.
*/

import { FargateToKinesisStreams } from "../lib";
import { FargateToKinesisStreams, FargateToKinesisStreamsProps } from "../lib";
import * as cdk from "aws-cdk-lib";
import * as kinesis from 'aws-cdk-lib/aws-kinesis';
import * as defaults from '@aws-solutions-constructs/core';
Expand Down Expand Up @@ -596,4 +596,30 @@ test('Construct uses existingFargateServiceObject when provided', () => {
template.hasResourceProperties("AWS::ECS::Service", {
ServiceName: 'my-service',
});
});
});

test('Confirm that CheckVpcProps was called', () => {
const stack = new cdk.Stack();
const publicApi = true;
const clusterName = "custom-cluster-name";
const containerName = "custom-container-name";
const serviceName = "custom-service-name";
const familyName = "custom-family-name";

const props: FargateToKinesisStreamsProps = {
publicApi,
ecrRepositoryArn: defaults.fakeEcrRepoArn,
clusterProps: { clusterName },
containerDefinitionProps: { containerName },
fargateTaskDefinitionProps: { family: familyName },
fargateServiceProps: { serviceName },
existingVpc: defaults.getTestVpc(stack),
vpcProps: { },
};

const app = () => {
new FargateToKinesisStreams(stack, 'test-construct', props);
};
// Assertion
expect(app).toThrowError('Error - Either provide an existingVpc or some combination of deployVpc and vpcProps, but not both.\n');
});
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export class FargateToOpenSearch extends Construct {
super(scope, id);
defaults.CheckProps(props);
defaults.CheckFargateProps(props);
defaults.CheckVpcProps(props);

this.vpc = defaults.buildVpc(scope, {
existingVpc: props.existingVpc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { Template } from 'aws-cdk-lib/assertions';
import * as defaults from '@aws-solutions-constructs/core';
import * as cdk from "aws-cdk-lib";
import { FargateToOpenSearch } from "../lib";
import { FargateToOpenSearch, FargateToOpenSearchProps } from "../lib";
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';

Expand Down Expand Up @@ -737,3 +737,31 @@ test('Check error for using OpenSearch VPC prop parameter', () => {

expect(app).toThrowError("Error - Define VPC using construct parameters not the OpenSearch Service props");
});

test('Confirm that CheckVpcProps was called', () => {
const stack = new cdk.Stack();
const publicApi = true;
const clusterName = "custom-cluster-name";
const containerName = "custom-container-name";
const serviceName = "custom-service-name";
const familyName = "custom-family-name";

const props: FargateToOpenSearchProps = {
publicApi,
ecrRepositoryArn: defaults.fakeEcrRepoArn,
clusterProps: { clusterName },
containerDefinitionProps: { containerName },
fargateTaskDefinitionProps: { family: familyName },
fargateServiceProps: { serviceName },
openSearchDomainName: DOMAIN_NAME,
cognitoDomainName: COGNITO_DOMAIN_NAME,
existingVpc: defaults.getTestVpc(stack),
vpcProps: { },
};

const app = () => {
new FargateToOpenSearch(stack, 'test-construct', props);
};
// Assertion
expect(app).toThrowError('Error - Either provide an existingVpc or some combination of deployVpc and vpcProps, but not both.\n');
});
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export class FargateToS3 extends Construct {
defaults.CheckProps(props);
defaults.CheckFargateProps(props);
defaults.CheckS3Props(props);
defaults.CheckVpcProps(props);

if (props.bucketPermissions) {
defaults.CheckListValues(['Delete', 'Read', 'Write'], props.bucketPermissions, 'bucket permission');
Expand Down
Loading

0 comments on commit 7f6f349

Please sign in to comment.