Skip to content

Commit

Permalink
Lambda support
Browse files Browse the repository at this point in the history
  • Loading branch information
cneijenhuis committed Aug 24, 2018
1 parent 6981f03 commit 2949bf7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ We'll provide examples for iron.io functions, google cloud functions and AWS Lam

## Packages

| Package | Version | Dependencies |
|--------|-------|------------|
| Package | Version | Dependencies |
| -------------------------------------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| [`example-inventory`](/packages/example-inventory) | [![example-inventory Version][example-inventory-icon]][example-inventory-version] | [![example-inventory Dependencies Status][example-inventory-dependencies-icon]][example-inventory-dependencies] |
| [`extension-base`](/packages/extension-base) | [![extension-base Version][extension-base-icon]][extension-base-version] | [![extension-base Dependencies Status][extension-base-dependencies-icon]][extension-base-dependencies] |
| [`extension-base`](/packages/extension-base) | [![extension-base Version][extension-base-icon]][extension-base-version] | [![extension-base Dependencies Status][extension-base-dependencies-icon]][extension-base-dependencies] |

[example-inventory-version]: https://www.npmjs.com/package/@commercetools/example-inventory
[example-inventory-icon]: https://img.shields.io/npm/v/@commercetools/example-inventory.svg?style=flat-square
Expand Down
14 changes: 7 additions & 7 deletions packages/extension-base/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Request For Comments: Wrapper around FaaS as common base for API Extensions
# Wrapper around FaaS as common base for API Extensions

Problem: We want to write examples once, but our customers may want to run them on AWS Lambda, Azure Functions, or Google Cloud Functions.
Problem: We want to write examples once, but our customers want to run them on AWS Lambda, Azure Functions, or Google Cloud Functions.
Solution: Put a small wrapper between the specific APIs offered by the FaaS providers and our examples.

To implement an extension, you create a function that takes three parameters:
Expand All @@ -9,10 +9,10 @@ To implement an extension, you create a function that takes three parameters:
* `ctpResponse` - An object that defines helpers for all possible responses defined here: https://docs.commercetools.com/http-api-projects-api-extensions.html#response It contains the helper functions `pass`, `update`, `updates`, `error`, `errors` that you need to use to make your Extension respond back to ctp.
* `log` - Because it would have been too easy if all FaaS would just use `console.log` ;)

## State
# Installation per cloud provider

I have implemented a POC for Azure and GCF. It actually works on both :)
First, check that `index.js` requires the addapter for your cloud provider (it may be commented out).

For GCF, upload index.js, ctpGcfWrapper.js and package.json

For Azure, upload index.js (and switch the comments on top of the file), ctpAzureWrapper.js and function.json
* For AWS Lambda, `upload index.js` and `ctp-extension-lambda-adapters.js`
* For Azure, upload `index.js`, `ctp-extension-azure-adapters.js` and `function.json`
* For GCF, upload `index.js`, `ctp-extension-gcf-adapters.js` and `package.json`
4 changes: 2 additions & 2 deletions packages/extension-base/ctp-extension-gcf-adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ctpResponse = ctx => ({
});

module.exports = {
createExtensionAdapter: fn => (ctx, req) => {
fn(req.body, ctpResponse(ctx), ctx.log);
createExtensionAdapter: fn => (req, res) => {
fn(req.body, ctpResponse(res), console.log);
},
};
26 changes: 26 additions & 0 deletions packages/extension-base/ctp-extension-lambda-adapters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// AWS Lambda

const createReject = callback => error => {
callback(null, {
responseType: 'FailedValidation',
errors: Array.isArray(error) ? error : [error],
});
};

const createAccept = callback => (update = []) => {
callback(null, {
responseType: 'UpdateRequest',
actions: Array.isArray(update) ? update : [update],
});
};

const ctpResponse = callback => ({
reject: createReject(callback),
accept: createAccept(callback),
});

module.exports = {
createExtensionAdapter: fn => (event, context, callback) => {
fn(event, ctpResponse(callback), console.log);
},
};
12 changes: 7 additions & 5 deletions packages/extension-base/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { createExtensionAdapter } = require('./ctp-extension-azure-adapters.js');

// var adapter = require('./ctp-extension-gcf-adapter.js');
// exports.handler = adapter.ctpExtensionAdapter(addInsurance);
const {
createExtensionAdapter,
} = require('./ctp-extension-lambda-adapters.js');
// const { createExtensionAdapter } = require('./ctp-extension-azure-adapters.js');
// const { createExtensionAdapter } = require('./ctp-extension-gcf-adapters.js');

const addInsurance = (request, ctpResponse, log) => {
// Use an ID from your project!
Expand Down Expand Up @@ -47,4 +48,5 @@ const addInsurance = (request, ctpResponse, log) => {
}
};

module.exports = createExtensionAdapter(addInsurance);
// module.exports = createExtensionAdapter(addInsurance);
exports.handler = createExtensionAdapter(addInsurance);

0 comments on commit 2949bf7

Please sign in to comment.