Skip to content

Commit

Permalink
switch from inline string to inline object. upgraded testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Niranjan Jayakar committed May 7, 2020
1 parent 926f070 commit e936e99
Show file tree
Hide file tree
Showing 9 changed files with 403 additions and 41 deletions.
61 changes: 54 additions & 7 deletions packages/@aws-cdk/aws-apigateway/lib/api-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,52 @@ export abstract class ApiDefinition {
}

/**
* Creates an API definition from a string
* Create an API definition from an inline object. The inline object must follow the
* schema of OpenAPI 2.0 or OpenAPI 3.0
*
* @example
* ApiDefinition.fromInline({
* openapi: '3.0.2',
* paths: {
* '/pets': {
* get: {
* 'responses': {
* 200: {
* content: {
* 'application/json': {
* schema: {
* $ref: '#/components/schemas/Empty',
* },
* },
* },
* },
* },
* 'x-amazon-apigateway-integration': {
* responses: {
* default: {
* statusCode: '200',
* },
* },
* requestTemplates: {
* 'application/json': '{"statusCode": 200}',
* },
* passthroughBehavior: 'when_no_match',
* type: 'mock',
* },
* },
* },
* },
* components: {
* schemas: {
* Empty: {
* title: 'Empty Schema',
* type: 'object',
* },
* },
* },
* });
*/
public static fromInline(definition: string): InlineApiDefinition {
public static fromInline(definition: any): InlineApiDefinition {
return new InlineApiDefinition(definition);
}

Expand Down Expand Up @@ -68,7 +111,7 @@ export interface ApiDefinitionConfig {
*
* @default - API definition is not defined inline
*/
readonly inlineDefinition?: string;
readonly inlineDefinition?: any;
}

/**
Expand Down Expand Up @@ -99,14 +142,18 @@ export class S3ApiDefinition extends ApiDefinition {
}

/**
* OpenAPI specification from an inline string.
* OpenAPI specification from an inline JSON object.
*/
export class InlineApiDefinition extends ApiDefinition {
constructor(private definition: string) {
constructor(private definition: any) {
super();

if (definition.length === 0) {
throw new Error('Inline API definition cannot be empty');
if (typeof(definition) !== 'object') {
throw new Error('definition should be of type object');
}

if (Object.keys(definition).length === 0) {
throw new Error('JSON definition cannot be empty');
}
}

Expand Down
9 changes: 1 addition & 8 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IVpcEndpoint } from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import { CfnOutput, Construct, IResource as IResourceBase, Resource, Stack } from '@aws-cdk/core';
import { ApiDefinition, ApiDefinitionConfig } from './api-definition';
import { ApiDefinition } from './api-definition';
import { ApiKey, IApiKey } from './api-key';
import { CfnAccount, CfnRestApi } from './apigateway.generated';
import { CorsOptions } from './cors';
Expand Down Expand Up @@ -620,10 +620,3 @@ class RootResource extends ResourceBase {
}
}
}

export function verifyAPIDefinitionConfig(definition: ApiDefinitionConfig) {
// mutually exclusive
if ((!definition.inlineDefinition && !definition.s3Location) || (definition.inlineDefinition && definition.s3Location)) {
throw new Error('APIDefinition must specify one of "inlineDefinition" or "s3Location" but not both');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"Properties": {
"BodyS3Location": {
"Bucket": {
"Ref": "AssetParametersb4e546901387aeeb588eabec043d35a7fdbe4d304185ae7ab763bb3ae4e61d58S3BucketC3882222"
"Ref": "AssetParameters68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fbS3Bucket42039E29"
},
"Key": {
"Fn::Join": [
Expand All @@ -18,7 +18,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParametersb4e546901387aeeb588eabec043d35a7fdbe4d304185ae7ab763bb3ae4e61d58S3VersionKey1FFB68C8"
"Ref": "AssetParameters68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fbS3VersionKeyB590532F"
}
]
}
Expand All @@ -31,7 +31,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParametersb4e546901387aeeb588eabec043d35a7fdbe4d304185ae7ab763bb3ae4e61d58S3VersionKey1FFB68C8"
"Ref": "AssetParameters68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fbS3VersionKeyB590532F"
}
]
}
Expand Down Expand Up @@ -137,20 +137,46 @@
]
]
}
},
"PetsURL": {
"Value": {
"Fn::Join": [
"",
[
"https://",
{
"Ref": "myapi4C7BF186"
},
".execute-api.",
{
"Ref": "AWS::Region"
},
".",
{
"Ref": "AWS::URLSuffix"
},
"/",
{
"Ref": "myapiDeploymentStageprod298F01AF"
},
"/pets"
]
]
}
}
},
"Parameters": {
"AssetParametersb4e546901387aeeb588eabec043d35a7fdbe4d304185ae7ab763bb3ae4e61d58S3BucketC3882222": {
"AssetParameters68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fbS3Bucket42039E29": {
"Type": "String",
"Description": "S3 bucket for asset \"b4e546901387aeeb588eabec043d35a7fdbe4d304185ae7ab763bb3ae4e61d58\""
"Description": "S3 bucket for asset \"68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fb\""
},
"AssetParametersb4e546901387aeeb588eabec043d35a7fdbe4d304185ae7ab763bb3ae4e61d58S3VersionKey1FFB68C8": {
"AssetParameters68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fbS3VersionKeyB590532F": {
"Type": "String",
"Description": "S3 key for asset version \"b4e546901387aeeb588eabec043d35a7fdbe4d304185ae7ab763bb3ae4e61d58\""
"Description": "S3 key for asset version \"68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fb\""
},
"AssetParametersb4e546901387aeeb588eabec043d35a7fdbe4d304185ae7ab763bb3ae4e61d58ArtifactHash1A35D17D": {
"AssetParameters68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fbArtifactHashA9C91B6D": {
"Type": "String",
"Description": "Artifact hash for asset \"b4e546901387aeeb588eabec043d35a7fdbe4d304185ae7ab763bb3ae4e61d58\""
"Description": "Artifact hash for asset \"68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fb\""
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as cdk from '@aws-cdk/core';
import * as path from 'path';
import * as apigateway from '../lib';

/*
* Stack verification steps:
* * `curl -i <CFN output PetsURL>` should return HTTP code 200
*/

const app = new cdk.App();
const stack = new cdk.Stack(app, 'integtest-restapi-fromdefinition-asset');

const api = new apigateway.SpecRestApi(stack, 'my-api', {
apiDefinition: apigateway.ApiDefinition.fromAsset(path.join(__dirname, 'sample-definition.yaml')),
});

new cdk.CfnOutput(stack, 'PetsURL', {
value: api.urlForPath('/pets'),
});

app.synth();
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
{
"Resources": {
"myapi4C7BF186": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"openapi": "3.0.2",
"info": {
"version": "1.0.0",
"title": "Test API for CDK"
},
"paths": {
"/pets": {
"get": {
"summary": "Test Method",
"operationId": "testMethod",
"responses": {
"200": {
"description": "A paged array of pets",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"responses": {
"default": {
"statusCode": "200"
}
},
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match",
"type": "mock"
}
}
}
},
"components": {
"schemas": {
"Empty": {
"title": "Empty Schema",
"type": "object"
}
}
}
},
"Name": "my-api"
}
},
"myapiDeployment92F2CB49": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "myapi4C7BF186"
},
"Description": "Automatically created by the RestApi construct"
}
},
"myapiDeploymentStageprod298F01AF": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"RestApiId": {
"Ref": "myapi4C7BF186"
},
"DeploymentId": {
"Ref": "myapiDeployment92F2CB49"
},
"StageName": "prod"
}
},
"myapiCloudWatchRole095452E5": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
]
]
}
]
}
},
"myapiAccountEC421A0A": {
"Type": "AWS::ApiGateway::Account",
"Properties": {
"CloudWatchRoleArn": {
"Fn::GetAtt": [
"myapiCloudWatchRole095452E5",
"Arn"
]
}
},
"DependsOn": [
"myapi4C7BF186"
]
}
},
"Outputs": {
"myapiEndpoint3628AFE3": {
"Value": {
"Fn::Join": [
"",
[
"https://",
{
"Ref": "myapi4C7BF186"
},
".execute-api.",
{
"Ref": "AWS::Region"
},
".",
{
"Ref": "AWS::URLSuffix"
},
"/",
{
"Ref": "myapiDeploymentStageprod298F01AF"
},
"/"
]
]
}
},
"PetsURL": {
"Value": {
"Fn::Join": [
"",
[
"https://",
{
"Ref": "myapi4C7BF186"
},
".execute-api.",
{
"Ref": "AWS::Region"
},
".",
{
"Ref": "AWS::URLSuffix"
},
"/",
{
"Ref": "myapiDeploymentStageprod298F01AF"
},
"/pets"
]
]
}
}
}
}

0 comments on commit e936e99

Please sign in to comment.