Skip to content

Commit

Permalink
Refactored repository service to use lambda proxy mode from API gatew…
Browse files Browse the repository at this point in the history
…ay and added to SAM template.

Fixed ‘queryable’ flag on type definitions.
  • Loading branch information
Gavin Cornwell committed Jan 18, 2017
1 parent de5d2d8 commit 11ab341
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This project implements a CMIS 1.1 compliant server on AWS using a serverless ar
npm install node-uuid

## Deploy
aws cloudformation package --template-file sam-template.yaml --s3-bucket cmiserverless-deployment --output-template-file sam-deploy.yaml
aws cloudformation package --template-file sam-template.yaml --s3-bucket <bucket-name> --output-template-file sam-deploy.yaml
aws cloudformation deploy --template-file sam-deploy.yaml --stack-name cmiserverless --capabilities CAPABILITY_IAM

## Testing
Expand Down
16 changes: 16 additions & 0 deletions sam-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ Resources:
Properties:
Path: /
Method: get
repository:
Type: AWS::Serverless::Function
Properties:
Handler: cmis-repository-service.handler
Runtime: nodejs4.3
CodeUri: ./src
Description: Function to retrieve information about a repository
MemorySize: 128
Timeout: 3
Role: !GetAtt LambdaExecutionIamRole.Arn
Events:
GetResource:
Type: Api
Properties:
Path: /{repoId}
Method: get
LambdaExecutionIamRole:
Type: AWS::IAM::Role
Properties:
Expand Down
4 changes: 2 additions & 2 deletions src/cmis-repositories-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ let event = {
// run the tests
console.log("Running repositories tests...");

// bootstrap
// retrieve repositories
cmisRepositoriesService.handler(event, context, function(error, result) {
if (error) {
console.error("Failed to retrive repositories: " + JSON.stringify(error, null, 2));
console.error("Failed to retrieve repositories: " + JSON.stringify(error, null, 2));
} else {

// ensure the status code is present and set to 200
Expand Down
183 changes: 183 additions & 0 deletions src/cmis-repository-service-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
'use strict';

var AWS = require("aws-sdk");
AWS.config.update({
region: "eu-central-1"
});

let cmisRepositoryService = require('./cmis-repository-service.js');

let context = {};
let typeDefinitionEvent = {
"resource": "/{repoId}",
"path": "/default",
"httpMethod": "GET",
"headers": null,
"queryStringParameters": {
"cmisselector": "typeDefinition",
"typeId": "cmis:folder"
},
"pathParameters": {
"repoId": "default"
},
"stageVariables": null,
"requestContext": {
"accountId": "0123456789",
"resourceId": "abcd9876",
"stage": "test-invoke-stage",
"requestId": "test-invoke-request",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": "0123456789",
"cognitoIdentityId": null,
"caller": "ABCDEFGHIJ",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"accessKey": "ABCDEFGHIJ",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::0123456789:user/adminuser",
"userAgent": "Apache-HttpClient/4.5.x (Java/1.8.0_102)",
"user": "ABCDEFGHIJ"
},
"resourcePath": "/{repoId}",
"httpMethod": "GET",
"apiId": "xyz1234"
},
"body": null,
"isBase64Encoded": false
};

let typeDescendantsEvent = {
"resource": "/{repoId}",
"path": "/default",
"httpMethod": "GET",
"headers": null,
"queryStringParameters": {
"cmisselector": "typeDescendants",
"typeId": "cmis:folder"
},
"pathParameters": {
"repoId": "default"
},
"stageVariables": null,
"requestContext": {
"accountId": "0123456789",
"resourceId": "abcd9876",
"stage": "test-invoke-stage",
"requestId": "test-invoke-request",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": "0123456789",
"cognitoIdentityId": null,
"caller": "ABCDEFGHIJ",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"accessKey": "ABCDEFGHIJ",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::0123456789:user/adminuser",
"userAgent": "Apache-HttpClient/4.5.x (Java/1.8.0_102)",
"user": "ABCDEFGHIJ"
},
"resourcePath": "/{repoId}",
"httpMethod": "GET",
"apiId": "xyz1234"
},
"body": null,
"isBase64Encoded": false
};

let invalidSelectorEvent = {
"resource": "/{repoId}",
"path": "/default",
"httpMethod": "GET",
"headers": null,
"queryStringParameters": {
"cmisselector": "invalid",
"typeId": "cmis:folder"
},
"pathParameters": {
"repoId": "default"
},
"stageVariables": null,
"requestContext": {
"accountId": "0123456789",
"resourceId": "abcd9876",
"stage": "test-invoke-stage",
"requestId": "test-invoke-request",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": "0123456789",
"cognitoIdentityId": null,
"caller": "ABCDEFGHIJ",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"accessKey": "ABCDEFGHIJ",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::0123456789:user/adminuser",
"userAgent": "Apache-HttpClient/4.5.x (Java/1.8.0_102)",
"user": "ABCDEFGHIJ"
},
"resourcePath": "/{repoId}",
"httpMethod": "GET",
"apiId": "xyz1234"
},
"body": null,
"isBase64Encoded": false
};

// run the tests
console.log("Running repository tests...");

// retrieve type definition
cmisRepositoryService.handler(typeDefinitionEvent, context, function(error, result) {
if (error) {
console.error("Failed to retrieve type definition: " + JSON.stringify(error, null, 2));
} else {

// ensure the status code is present and set to 200
if (result.statusCode != "200") throw "TEST FAILED: expecting to receive 200 status code";

var resultBody = JSON.parse(result.body);

// ensure there's a typeId property set to "cmis:folder"
if (!resultBody.typeId == "cmis:folder") throw "TEST FAILED: expecting to receive typeId of cmis:folder";

// ensure there's a property definition named cmis:objectId
if (!resultBody.propertyDefinitions["cmis:objectId"]) throw "TEST FAILED: expecting to receive cmis:objectId property definition";

// if we get this far the tests passed
console.log("Type definition tests passed!");
}
});

// retrieve type descendants
cmisRepositoryService.handler(typeDescendantsEvent, context, function(error, result) {
if (error) {
console.error("Failed to retrieve type descendants: " + JSON.stringify(error, null, 2));
} else {

// ensure the status code is present and set to 200
if (result.statusCode != "200") throw "TEST FAILED: expecting to receive 200 status code";

var resultBody = JSON.parse(result.body);

// ensure an empty array is returned
if (!resultBody.length == 0) throw "TEST FAILED: expecting to receive an empty array";

// if we get this far the tests passed
console.log("Type descendants tests passed!");
}
});

// test invalid selector
cmisRepositoryService.handler(invalidSelectorEvent, context, function(error, result) {
if (error) {
console.error(error);
console.log("Invalid selector tests passed!");
} else {
throw "TEST FAILED: expecting to receive an error";
}
});
23 changes: 18 additions & 5 deletions src/cmis-repository-service.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
'use strict';

let cmis = require('./cmis.js');
let utils = require('./lambda-utils.js');

// main entry point
module.exports.handler = (event, context, callback) => {

console.log("Repository service received event: " + JSON.stringify(event, null, 2));

// extract selector
var selector = event.cmisselector;
var selector = event.queryStringParameters.cmisselector;

// extract repoId
var repoId = event.repoId;
var repoId = event.pathParameters.repoId;

// extract typeId
var typeId = event.typeId;
var typeId = event.queryStringParameters.typeId;

// dispatch to appropriate function
switch(selector) {
case "typeDefinition":
cmis.getTypeDefinition(repoId, typeId, callback);
cmis.getTypeDefinition(repoId, typeId, function(error, data) {
if (error) {
callback(error);
} else {
callback(null, utils.buildProxyResponseObject(data));
}
});
break;
case "typeDescendants":
cmis.getTypeDescendants(repoId, typeId, callback);
cmis.getTypeDescendants(repoId, typeId, function(error, data) {
if (error) {
callback(error);
} else {
callback(null, utils.buildProxyResponseObject(data));
}
});
break;
default:
callback(new Error("Unrecognised selector: " + selector));
Expand Down
Loading

0 comments on commit 11ab341

Please sign in to comment.