Skip to content

Commit

Permalink
feat (Add a check for props for all constructs)
Browse files Browse the repository at this point in the history
  • Loading branch information
biffgaut committed May 11, 2021
1 parent 81e35ce commit aa28f33
Show file tree
Hide file tree
Showing 5 changed files with 455 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export class LambdaToS3 extends Construct {
*/
constructor(scope: Construct, id: string, props: LambdaToS3Props) {
super(scope, id);
defaults.CheckProps(props);

let bucket: s3.IBucket;

if (props.existingBucketObj && props.bucketProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,32 @@ test("Test bad call with existingBucket and bucketProps", () => {
};
// Assertion
expect(app).toThrowError();
});
});

test('Test that CheckProps() is flagging errors correctly', () => {
// Stack
const stack = new Stack();

const testLambdaFunction = new lambda.Function(stack, 'test-lamba', {
runtime: lambda.Runtime.NODEJS_10_X,
handler: "index.handler",
code: lambda.Code.fromAsset(`${__dirname}/lambda`),
});

const app = () => {
new LambdaToS3(stack, "lambda-to-s3-stack", {
existingLambdaObj: testLambdaFunction,
lambdaFunctionProps: {
runtime: lambda.Runtime.NODEJS_10_X,
handler: "index.handler",
code: lambda.Code.fromAsset(`${__dirname}/lambda`),
},
});
};

// Assertion
expect(app).toThrowError(
"Cannot specify an existing Lambda function AND Lambda function props\n"
);

});
1 change: 1 addition & 0 deletions source/patterns/@aws-solutions-constructs/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ export * from './lib/glue-table-defaults';
export * from './lib/glue-table-helper';
export * from './lib/glue-database-defaults';
export * from './lib/glue-database-helper';
export * from './lib/input-validation';
export * from './test/test-helper';
126 changes: 126 additions & 0 deletions source/patterns/@aws-solutions-constructs/core/lib/input-validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as kinesis from '@aws-cdk/aws-kinesis';
import * as lambda from '@aws-cdk/aws-lambda';
import * as sqs from '@aws-cdk/aws-sqs';
import * as mediastore from '@aws-cdk/aws-mediastore';
import * as s3 from '@aws-cdk/aws-s3';
import * as sns from '@aws-cdk/aws-sns';
import * as glue from '@aws-cdk/aws-glue';
import * as sagemaker from '@aws-cdk/aws-sagemaker';
import * as secretsmanager from "@aws-cdk/aws-secretsmanager";

export interface VerifiedProps {
dynamoTableProps?: dynamodb.TableProps,
existingTableObj?: dynamodb.Table,

existingStreamObj?: kinesis.Stream;
kinesisStreamProps?: kinesis.StreamProps,

existingLambdaObj?: lambda.Function,
lambdaFunctionProps?: lambda.FunctionProps,

existingQueueObj?: sqs.Queue,
queueProps?: sqs.QueueProps,
deployDeadLetterQueue?: boolean,
deadLetterQueueProps?: sqs.QueueProps,

existingMediaStoreContainerObj?: mediastore.CfnContainer;
mediaStoreContainerProps?: mediastore.CfnContainerProps;

existingBucketObj?: s3.Bucket,
bucketProps?: s3.BucketProps,

// topicsProps is an incorrect attribute used in event-rule-sns that
// we need to support
topicProps?: sns.TopicProps,
topicsProps?: sns.TopicProps,
existingTopicObj?: sns.Topic,

glueJobProps?: glue.CfnJobProps,
existingGlueJob?: glue.CfnJob,

existingSagemakerEndpointObj?: sagemaker.CfnEndpoint,
endpointProps?: sagemaker.CfnEndpointProps,

readonly existingSecretObj?: secretsmanager.Secret;
readonly secretProps?: secretsmanager.SecretProps;

}

export function CheckProps(propsObject: VerifiedProps | any) {
let errorMessages = '';
let errorFound = false;

if (propsObject.dynamoTableProps && propsObject.existingTableObj) {
errorMessages += 'Cannot specify an existing DDB table AND DDB table props\n';
errorFound = true;
}

if (propsObject.existingStreamObj && propsObject.kinesisStreamProps) {
errorMessages += 'Cannot specify an existing Stream table AND Stream props\n';
errorFound = true;
}

if (propsObject.existingLambdaObj && propsObject.lambdaFunctionProps) {
errorMessages += 'Cannot specify an existing Lambda function AND Lambda function props\n';
errorFound = true;
}

if (propsObject.existingQueueObj && propsObject.queueProps) {
errorMessages += 'Cannot specify an existing SQS queue AND SQS queue props\n';
errorFound = true;
}

if ((propsObject?.deployDeadLetterQueue == false) && propsObject.deadLetterQueueProps) {
errorMessages += 'Cannot specify no Dead Letter Queue AND Dead Letter Queue props\n';
errorFound = true;
}

if (propsObject.existingMediaStoreContainerObj && propsObject.mediaStoreContainerProps) {
errorMessages += 'Cannot specify an existing MediaStore container AND MediaStore container props\n';
errorFound = true;
}

if (propsObject.existingBucketObj && propsObject.bucketProps) {
errorMessages += 'Cannot specify an existing S3 bucket AND S3 bucket props\n';
errorFound = true;
}

if ((propsObject.topicProps || propsObject.topicsProps) && propsObject.existingTopicObj) {
errorMessages += 'Cannot specify an existing SNS topic AND SNS topic props\n';
errorFound = true;
}

if (propsObject.glueJobProps && propsObject.existingGlueJob) {
errorMessages += 'Cannot specify an existing Glue job AND Glue job props\n';
errorFound = true;
}

if (propsObject.existingSagemakerEndpointObj && propsObject.endpointProps) {
errorMessages += 'Cannot specify an existing SageMaker endpoint AND SageMaker endpoint props\n';
errorFound = true;
}

if (propsObject.existingSecretObj && propsObject.secretProps) {
errorMessages += 'Cannot specify an existing Secret AND Secret props\n';
errorFound = true;
}


if (errorFound) {
throw new Error(errorMessages);
}
}
Loading

0 comments on commit aa28f33

Please sign in to comment.