Skip to content

Commit

Permalink
Update construct to add user provided model
Browse files Browse the repository at this point in the history
  • Loading branch information
dscpinheiro committed Sep 9, 2020
1 parent e3b3f8f commit 8963808
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ _Parameters_
|:-------------|:----------------|-----------------|
|apiGatewayProps?|[`api.RestApiProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.RestApiProps.html)|Optional user-provided props to override the default props for the API Gateway.|
|putRecordRequestTemplate?|`string`|API Gateway request template for the PutRecord action. If not provided, a default one will be used.|
|putRecordRequestModel?|[`api.Model`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.Model.html)|API Gateway request model for the PutRecord action. If not provided, a default one will be created.|
|putRecordRequestModel?|[`api.ModelOptions`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.ModelOptions.html)|API Gateway request model for the PutRecord action. If not provided, a default one will be created.|
|putRecordsRequestTemplate?|`string`|API Gateway request template for the PutRecords action. If not provided, a default one will be used.|
|putRecordsRequestModel?|[`api.Model`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.Model.html)|API Gateway request model for the PutRecords action. If not provided, a default one will be created.|
|putRecordsRequestModel?|[`api.ModelOptions`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.ModelOptions.html)|API Gateway request model for the PutRecords action. If not provided, a default one will be created.|
|existingStreamObj?|[`kinesis.Stream`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-kinesis.Stream.html)|Existing instance of Kinesis Stream, if this is set then kinesisStreamProps is ignored.|
|kinesisStreamProps?|[`kinesis.StreamProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-kinesis.StreamProps.html)|Optional user-provided props to override the default props for the Kinesis stream.|

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface ApiGatewayToKinesisStreamsProps {
*
* @default - None
*/
readonly putRecordRequestModel?: api.IModel;
readonly putRecordRequestModel?: api.ModelOptions;

/**
* API Gateway request template for the PutRecords action.
Expand All @@ -60,7 +60,7 @@ export interface ApiGatewayToKinesisStreamsProps {
*
* @default - None
*/
readonly putRecordsRequestModel?: api.IModel;
readonly putRecordsRequestModel?: api.ModelOptions;

/**
* Existing instance of Kinesis Stream, if this is set then kinesisStreamProps is ignored.
Expand Down Expand Up @@ -156,26 +156,30 @@ export class ApiGatewayToKinesisStreams extends Construct {
return `{ "StreamName": "${this.kinesisStream.streamName}", "Data": "$util.base64Encode($input.json('$.data'))", "PartitionKey": "$input.path('$.partitionKey')" }`;
}

private getPutRecordModel(putRecordModel?: api.IModel): api.IModel {
private getPutRecordModel(putRecordModel?: api.ModelOptions): api.IModel {
let modelProps: api.ModelOptions;

if (putRecordModel !== undefined) {
return putRecordModel;
modelProps = putRecordModel;
} else {
modelProps = {
contentType: 'application/json',
modelName: 'PutRecordModel',
description: 'PutRecord proxy single-record payload',
schema: {
schema: api.JsonSchemaVersion.DRAFT4,
title: 'PutRecord proxy single-record payload',
type: api.JsonSchemaType.OBJECT,
required: ['data', 'partitionKey'],
properties: {
data: { type: api.JsonSchemaType.STRING },
partitionKey: { type: api.JsonSchemaType.STRING }
}
}
};
}

return this.apiGateway.addModel('PutRecordModel', {
contentType: 'application/json',
modelName: 'PutRecordModel',
description: 'PutRecord proxy single-record payload',
schema: {
schema: api.JsonSchemaVersion.DRAFT4,
title: 'PutRecord proxy single-record payload',
type: api.JsonSchemaType.OBJECT,
required: ['data', 'partitionKey'],
properties: {
data: { type: api.JsonSchemaType.STRING },
partitionKey: { type: api.JsonSchemaType.STRING }
}
}
});
return this.apiGateway.addModel('PutRecordModel', modelProps);
}

private getPutRecordsTemplate(putRecordsTemplate?: string): string {
Expand All @@ -186,34 +190,38 @@ export class ApiGatewayToKinesisStreams extends Construct {
return `{ "StreamName": "${this.kinesisStream.streamName}", "Records": [ #foreach($elem in $input.path('$.records')) { "Data": "$util.base64Encode($elem.data)", "PartitionKey": "$elem.partitionKey"}#if($foreach.hasNext),#end #end ] }`;
}

private getPutRecordsModel(putRecordsModel?: api.IModel): api.IModel {
if (putRecordsModel !== undefined) {
return putRecordsModel;
}
private getPutRecordsModel(putRecordsModel?: api.ModelOptions): api.IModel {
let modelProps: api.ModelOptions;

return this.apiGateway.addModel('PutRecordsModel', {
contentType: 'application/json',
modelName: 'PutRecordsModel',
description: 'PutRecords proxy payload data',
schema: {
schema: api.JsonSchemaVersion.DRAFT4,
title: 'PutRecords proxy payload data',
type: api.JsonSchemaType.OBJECT,
required: ['records'],
properties: {
records: {
type: api.JsonSchemaType.ARRAY,
items: {
type: api.JsonSchemaType.OBJECT,
required: ['data', 'partitionKey'],
properties: {
data: { type: api.JsonSchemaType.STRING },
partitionKey: { type: api.JsonSchemaType.STRING }
if (putRecordsModel !== undefined) {
modelProps = putRecordsModel;
} else {
modelProps = {
contentType: 'application/json',
modelName: 'PutRecordsModel',
description: 'PutRecords proxy payload data',
schema: {
schema: api.JsonSchemaVersion.DRAFT4,
title: 'PutRecords proxy payload data',
type: api.JsonSchemaType.OBJECT,
required: ['records'],
properties: {
records: {
type: api.JsonSchemaType.ARRAY,
items: {
type: api.JsonSchemaType.OBJECT,
required: ['data', 'partitionKey'],
properties: {
data: { type: api.JsonSchemaType.STRING },
partitionKey: { type: api.JsonSchemaType.STRING }
}
}
}
}
}
}
});
};
}

return this.apiGateway.addModel('PutRecordsModel', modelProps);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -699,12 +699,14 @@ Object {
},
"Type": "AWS::ApiGateway::RestApi",
},
"apigatewaykinesisRestApiDeployment10E9BBA51fc5c8b6c1eab8317f1df6f599bf503b": Object {
"apigatewaykinesisRestApiDeployment10E9BBA592f6dffe73c609db3a34add9add8b26e": Object {
"DependsOn": Array [
"apigatewaykinesisRestApirecordPOST833DA459",
"apigatewaykinesisRestApirecord3F4EC064",
"apigatewaykinesisRestApirecordsPOST08A9BEAD",
"apigatewaykinesisRestApirecordsE5337BB6",
"apigatewaykinesisRestApiPutRecordModel55C7723E",
"apigatewaykinesisRestApiPutRecordsModel217E2E73",
"apigatewaykinesisRestApirequestvalidator4482E7FC",
],
"Metadata": Object {
Expand Down Expand Up @@ -737,7 +739,7 @@ Object {
"Format": "{\\"requestId\\":\\"$context.requestId\\",\\"ip\\":\\"$context.identity.sourceIp\\",\\"user\\":\\"$context.identity.user\\",\\"caller\\":\\"$context.identity.caller\\",\\"requestTime\\":\\"$context.requestTime\\",\\"httpMethod\\":\\"$context.httpMethod\\",\\"resourcePath\\":\\"$context.resourcePath\\",\\"status\\":\\"$context.status\\",\\"protocol\\":\\"$context.protocol\\",\\"responseLength\\":\\"$context.responseLength\\"}",
},
"DeploymentId": Object {
"Ref": "apigatewaykinesisRestApiDeployment10E9BBA51fc5c8b6c1eab8317f1df6f599bf503b",
"Ref": "apigatewaykinesisRestApiDeployment10E9BBA592f6dffe73c609db3a34add9add8b26e",
},
"MethodSettings": Array [
Object {
Expand All @@ -761,6 +763,30 @@ Object {
},
"Type": "AWS::ApiGateway::Stage",
},
"apigatewaykinesisRestApiPutRecordModel55C7723E": Object {
"Properties": Object {
"ContentType": "application/json",
"RestApiId": Object {
"Ref": "apigatewaykinesisRestApi0DCC9877",
},
"Schema": Object {
"$schema": "http://json-schema.org/draft-04/schema#",
},
},
"Type": "AWS::ApiGateway::Model",
},
"apigatewaykinesisRestApiPutRecordsModel217E2E73": Object {
"Properties": Object {
"ContentType": "application/json",
"RestApiId": Object {
"Ref": "apigatewaykinesisRestApi0DCC9877",
},
"Schema": Object {
"$schema": "http://json-schema.org/draft-04/schema#",
},
},
"Type": "AWS::ApiGateway::Model",
},
"apigatewaykinesisRestApiUsagePlan40457E86": Object {
"Properties": Object {
"ApiStages": Array [
Expand Down Expand Up @@ -856,7 +882,9 @@ Object {
},
],
"RequestModels": Object {
"application/json": "Empty",
"application/json": Object {
"Ref": "apigatewaykinesisRestApiPutRecordModel55C7723E",
},
},
"RequestValidatorId": Object {
"Ref": "apigatewaykinesisRestApirequestvalidator4482E7FC",
Expand Down Expand Up @@ -949,7 +977,9 @@ Object {
},
],
"RequestModels": Object {
"application/json": "Empty",
"application/json": Object {
"Ref": "apigatewaykinesisRestApiPutRecordsModel217E2E73",
},
},
"RequestValidatorId": Object {
"Ref": "apigatewaykinesisRestApirequestvalidator4482E7FC",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { Stack, Duration } from '@aws-cdk/core';
import { ApiGatewayToKinesisStreams } from '../lib';
import { SynthUtils } from '@aws-cdk/assert';
import '@aws-cdk/assert/jest';
import * as api from '@aws-cdk/aws-apigateway';
import * as kinesis from '@aws-cdk/aws-kinesis';

// --------------------------------------------------------------
Expand Down Expand Up @@ -65,9 +64,9 @@ test('Test deployment w/ overwritten properties', () => {
streamName: 'my-stream'
},
putRecordRequestTemplate: `{ "Data": "$util.base64Encode($input.json('$.foo'))", "PartitionKey": "$input.path('$.bar')" }`,
putRecordRequestModel: api.Model.EMPTY_MODEL,
putRecordRequestModel: { schema: {} },
putRecordsRequestTemplate: `{ "Records": [ #foreach($elem in $input.path('$.records')) { "Data": "$util.base64Encode($elem.foo)", "PartitionKey": "$elem.bar"}#if($foreach.hasNext),#end #end ] }`,
putRecordsRequestModel: api.Model.EMPTY_MODEL
putRecordsRequestModel: { schema: {} }
});

expect(SynthUtils.toCloudFormation(stack)).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"Name": "my-api"
}
},
"testapigatewaykinesisoverwriteRestApiDeployment1EE3B096f7453da735d825aa4af9360039752401": {
"testapigatewaykinesisoverwriteRestApiDeployment1EE3B0963b59c487316048a0c96d810cbd1cf3bb": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
Expand All @@ -30,6 +30,8 @@
"testapigatewaykinesisoverwriteRestApirecordAECF6BBC",
"testapigatewaykinesisoverwriteRestApirecordsPOST1501AC33",
"testapigatewaykinesisoverwriteRestApirecordsB70B7E7F",
"testapigatewaykinesisoverwriteRestApiPutRecordModel82F936E0",
"testapigatewaykinesisoverwriteRestApiPutRecordsModel120A48A8",
"testapigatewaykinesisoverwriteRestApirequestvalidatorBAD987F2"
],
"Metadata": {
Expand Down Expand Up @@ -59,7 +61,7 @@
"Format": "{\"requestId\":\"$context.requestId\",\"ip\":\"$context.identity.sourceIp\",\"user\":\"$context.identity.user\",\"caller\":\"$context.identity.caller\",\"requestTime\":\"$context.requestTime\",\"httpMethod\":\"$context.httpMethod\",\"resourcePath\":\"$context.resourcePath\",\"status\":\"$context.status\",\"protocol\":\"$context.protocol\",\"responseLength\":\"$context.responseLength\"}"
},
"DeploymentId": {
"Ref": "testapigatewaykinesisoverwriteRestApiDeployment1EE3B096f7453da735d825aa4af9360039752401"
"Ref": "testapigatewaykinesisoverwriteRestApiDeployment1EE3B0963b59c487316048a0c96d810cbd1cf3bb"
},
"MethodSettings": [
{
Expand Down Expand Up @@ -165,7 +167,9 @@
}
],
"RequestModels": {
"application/json": "Empty"
"application/json": {
"Ref": "testapigatewaykinesisoverwriteRestApiPutRecordModel82F936E0"
}
},
"RequestValidatorId": {
"Ref": "testapigatewaykinesisoverwriteRestApirequestvalidatorBAD987F2"
Expand Down Expand Up @@ -258,7 +262,9 @@
}
],
"RequestModels": {
"application/json": "Empty"
"application/json": {
"Ref": "testapigatewaykinesisoverwriteRestApiPutRecordsModel120A48A8"
}
},
"RequestValidatorId": {
"Ref": "testapigatewaykinesisoverwriteRestApirequestvalidatorBAD987F2"
Expand Down Expand Up @@ -291,6 +297,30 @@
"ValidateRequestBody": true
}
},
"testapigatewaykinesisoverwriteRestApiPutRecordModel82F936E0": {
"Type": "AWS::ApiGateway::Model",
"Properties": {
"RestApiId": {
"Ref": "testapigatewaykinesisoverwriteRestApi77ABE5DA"
},
"ContentType": "application/json",
"Schema": {
"$schema": "http://json-schema.org/draft-04/schema#"
}
}
},
"testapigatewaykinesisoverwriteRestApiPutRecordsModel120A48A8": {
"Type": "AWS::ApiGateway::Model",
"Properties": {
"RestApiId": {
"Ref": "testapigatewaykinesisoverwriteRestApi77ABE5DA"
},
"ContentType": "application/json",
"Schema": {
"$schema": "http://json-schema.org/draft-04/schema#"
}
}
},
"testapigatewaykinesisoverwriteLambdaRestApiCloudWatchRole5ABBFDA0": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// Imports
import { App, Stack, Duration } from '@aws-cdk/core';
import { ApiGatewayToKinesisStreams, ApiGatewayToKinesisStreamsProps } from '../lib';
import * as api from '@aws-cdk/aws-apigateway';

// Setup
const app = new App();
Expand All @@ -39,10 +38,10 @@ const props: ApiGatewayToKinesisStreamsProps = {
retentionPeriod: Duration.days(4)
},
putRecordRequestTemplate: `{ "Data": "$util.base64Encode($input.json('$.foo'))", "PartitionKey": "$input.path('$.bar')" }`,
putRecordRequestModel: api.Model.EMPTY_MODEL,
putRecordRequestModel: { schema: {} },

putRecordsRequestTemplate: `{ "Records": [ #foreach($elem in $input.path('$.records')) { "Data": "$util.base64Encode($elem.foo)", "PartitionKey": "$elem.bar"}#if($foreach.hasNext),#end #end ] }`,
putRecordsRequestModel: api.Model.EMPTY_MODEL
putRecordsRequestModel: { schema: {} }
};

new ApiGatewayToKinesisStreams(stack, 'test-apigateway-kinesis-overwrite', props);
Expand Down

0 comments on commit 8963808

Please sign in to comment.