You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2019/07/26 10:27:00 UTC
[sling-whiteboard] branch master updated (d6a6ccd -> 9f00339)
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git.
from d6a6ccd Factor our the default renderers
new 379d708 Better names
new e6e6478 Content-type provided by action annotation
new 9f00339 Fix 'action not found' case
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
serverless-microsling/install | 2 +-
serverless-microsling/lib/default-renderers.js | 9 ++---
serverless-microsling/lib/openwhisk-renderer.js | 49 +++++++++++++++++++------
serverless-microsling/lib/render.js | 17 ++++-----
4 files changed, 49 insertions(+), 28 deletions(-)
[sling-whiteboard] 01/03: Better names
Posted by bd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit 379d708cd7dcc7179a2e2b4af91e410a74779879
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Jul 26 11:37:02 2019 +0200
Better names
---
serverless-microsling/lib/openwhisk-renderer.js | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/serverless-microsling/lib/openwhisk-renderer.js b/serverless-microsling/lib/openwhisk-renderer.js
index f58a32e..199e26d 100644
--- a/serverless-microsling/lib/openwhisk-renderer.js
+++ b/serverless-microsling/lib/openwhisk-renderer.js
@@ -39,7 +39,7 @@ const getAction = async (resourceType, extension) => {
})
};
-const render = (resource, action) => {
+const renderWithAction = (resource, action) => {
const name = action.name;
const blocking = true, result = true
const params = {
@@ -55,7 +55,7 @@ const render = (resource, action) => {
return getAction(resourceType, extension)
},
render : (resource, action) => {
- return render(resource, action);
+ return renderWithAction(resource, action);
},
}
@@ -66,7 +66,7 @@ function main () {
title: 'cmdline title test',
body: 'cmdline body test',
}
- resolve(render(resource, getAction('microsling/somedoc', 'html')));
+ resolve(renderWithAction(resource, getAction('microsling/somedoc', 'html')));
});
}
[sling-whiteboard] 02/03: Content-type provided by action annotation
Posted by bd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit e6e6478a197e16d46f1977ccbf63c6e416c968e8
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Jul 26 12:21:46 2019 +0200
Content-type provided by action annotation
---
serverless-microsling/install | 2 +-
serverless-microsling/lib/default-renderers.js | 9 ++----
serverless-microsling/lib/openwhisk-renderer.js | 43 +++++++++++++++++++------
serverless-microsling/lib/render.js | 17 ++++------
4 files changed, 44 insertions(+), 27 deletions(-)
diff --git a/serverless-microsling/install b/serverless-microsling/install
index 8e5283e..eccbccc 100755
--- a/serverless-microsling/install
+++ b/serverless-microsling/install
@@ -15,7 +15,7 @@ echo Preparing $ZIP ... \
&& zip -r $ZIP $ACTION_FILES > /dev/null \
&& echo "$ZIP: $(du -h $ZIP | cut -f1)" \
&& echo "Updating action '$ACTION...'" \
-&& wsk action update $ACTION $ZIP --web true --kind nodejs:10 --param debug true
+&& wsk action update $ACTION $ZIP --web true --kind nodejs:10 --param debug true -a provide-api-key true
echo "Installing rendering actions..."
wsk action update somedoc-rendering rendering-actions/somedoc-html.js -a sling:resourceType microsling/somedoc -a sling:extensions html
diff --git a/serverless-microsling/lib/default-renderers.js b/serverless-microsling/lib/default-renderers.js
index dca7c27..0562249 100644
--- a/serverless-microsling/lib/default-renderers.js
+++ b/serverless-microsling/lib/default-renderers.js
@@ -19,27 +19,24 @@
module.exports.renderers = {
text: {
- contentType: 'text/plain',
getRendererInfo : (resourceType, extension) => {
- return extension == 'txt';
+ return extension == 'txt' ? { contentType : 'text/plain' } : null;
},
render : (resource) => {
return { output: `${resource.title}\n${resource.body}\n` };
},
},
json: {
- contentType: 'application/json',
getRendererInfo : (resourceType, extension) => {
- return extension == 'json';
+ return extension == 'json' ? { contentType : 'application/json' } : null;
},
render : (resource) => {
return { output: JSON.stringify(resource, 2, null) };
},
},
html: {
- contentType: 'text/html',
getRendererInfo : (resourceType, extension) => {
- return extension == 'html';
+ return extension == 'html' ? { contentType : 'text/html' } : null;
},
render : (resource) => {
return { output: `
diff --git a/serverless-microsling/lib/openwhisk-renderer.js b/serverless-microsling/lib/openwhisk-renderer.js
index 199e26d..a323f94 100644
--- a/serverless-microsling/lib/openwhisk-renderer.js
+++ b/serverless-microsling/lib/openwhisk-renderer.js
@@ -19,11 +19,13 @@
const openwhisk = require('openwhisk');
const getAnnotation = (act, key) => {
- const annotation = act.annotations.find(ann => key == ann.key);
- return annotation ? annotation.value : undefined;
+ if(act != null) {
+ const annotation = act.annotations.find(ann => key == ann.key);
+ return annotation ? annotation.value : undefined;
+ }
}
-const getAction = async (resourceType, extension) => {
+const getActionInfo = async (resourceType, extension) => {
return new Promise(resolve => {
var ow = openwhisk();
ow.actions.list()
@@ -31,7 +33,10 @@ const getAction = async (resourceType, extension) => {
const act = actions.find(act => {
return resourceType == getAnnotation(act, 'sling:resourceType') && extension == getAnnotation(act, 'sling:extensions')
})
- resolve(act);
+ resolve({
+ action: act,
+ contentType: getAnnotation(act, 'sling:contentType'),
+ });
})
.catch(e => {
throw e;
@@ -39,8 +44,11 @@ const getAction = async (resourceType, extension) => {
})
};
-const renderWithAction = (resource, action) => {
- const name = action.name;
+const renderWithAction = (resource, actionInfo) => {
+ if(!actionInfo.action) {
+ throw Error("No Action provided, cannot render");
+ }
+ const name = actionInfo.action.name;
const blocking = true, result = true
const params = {
resource: resource
@@ -50,9 +58,8 @@ const renderWithAction = (resource, action) => {
};
const renderer = {
- contentType: 'text/html',
getRendererInfo : async (resourceType, extension) => {
- return getAction(resourceType, extension)
+ return getActionInfo(resourceType, extension);
},
render : (resource, action) => {
return renderWithAction(resource, action);
@@ -60,15 +67,31 @@ const renderWithAction = (resource, action) => {
}
// For testing as a standalone OpenWhisk action
+// (requires installing the action with -a provide-api-key true)
+// or from the command line
function main () {
- return new Promise(async resolve => {
+ return new Promise(async (resolve, reject) => {
const resource = {
title: 'cmdline title test',
body: 'cmdline body test',
}
- resolve(renderWithAction(resource, getAction('microsling/somedoc', 'html')));
+ try {
+ const actionInfo = await getActionInfo('microsling/somedoc', 'html');
+ console.log(`actionInfo=${JSON.stringify(actionInfo, 2, null)}`);
+ const rendered = await renderWithAction(resource, actionInfo);
+ console.log(`rendered=${JSON.stringify(rendered, 2, null)}`);
+ resolve(rendered);
+ } catch(e) {
+ reject(e);
+ }
});
}
+// From the command line, __OW_API_HOST and __OW_API_KEY environment
+// variables must be set
+if (require.main === module) {
+ main();
+}
+
module.exports.openWhiskRenderer = renderer;
module.exports.main = main;
\ No newline at end of file
diff --git a/serverless-microsling/lib/render.js b/serverless-microsling/lib/render.js
index 5ceaa25..91fcaf9 100644
--- a/serverless-microsling/lib/render.js
+++ b/serverless-microsling/lib/render.js
@@ -31,21 +31,18 @@ const renderers = [
async function selectRendererInfo(resourceType, extension) {
return new Promise(async resolve => {
let i;
- let resolved;
+ let result;
for(i in renderers) {
const rendererInfo = await renderers[i].getRendererInfo(resourceType, extension);
if(rendererInfo) {
- resolve({
+ result = {
'renderer': renderers[i],
- 'rendererInfo': rendererInfo,
- });
- resolved = true;
+ 'info': rendererInfo,
+ };
break;
}
}
- if(!resolved) {
- resolve();
- }
+ resolve(result);
})
}
@@ -63,13 +60,13 @@ async function render(context) {
if(!rendererInfo) {
throw Error(`Renderer not found for ${resourceType} extension ${extension}`);
}
- const rendered = await rendererInfo.renderer.render(resource, rendererInfo.rendererInfo);
+ const rendered = await rendererInfo.renderer.render(resource, rendererInfo.info);
if(!rendered.output) {
throw Error('Renderer generated no output');
}
context.response.body = rendered.output;
context.response.headers = {
- 'Content-Type': rendererInfo.renderer.contentType
+ 'Content-Type': rendererInfo.contentType
};
return context;
[sling-whiteboard] 03/03: Fix 'action not found' case
Posted by bd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit 9f00339937811e792cfbba3a493974266b183dc0
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Jul 26 12:26:48 2019 +0200
Fix 'action not found' case
---
serverless-microsling/lib/openwhisk-renderer.js | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/serverless-microsling/lib/openwhisk-renderer.js b/serverless-microsling/lib/openwhisk-renderer.js
index a323f94..5ac5259 100644
--- a/serverless-microsling/lib/openwhisk-renderer.js
+++ b/serverless-microsling/lib/openwhisk-renderer.js
@@ -33,10 +33,14 @@ const getActionInfo = async (resourceType, extension) => {
const act = actions.find(act => {
return resourceType == getAnnotation(act, 'sling:resourceType') && extension == getAnnotation(act, 'sling:extensions')
})
- resolve({
- action: act,
- contentType: getAnnotation(act, 'sling:contentType'),
- });
+ let result;
+ if(act) {
+ result = {
+ action: act,
+ contentType: getAnnotation(act, 'sling:contentType'),
+ };
+ }
+ resolve(result);
})
.catch(e => {
throw e;