diff --git a/README.md b/README.md index 366fc3a..85d2451 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ### Overview You can use this template to deploy some IBM Cloud Functions assets for you. The assets created by this template are described in the manifest.yaml file, which can be found at `template-hello-world/runtimes/your_language_choice/manifest.yaml` -The only assets described by this hello world template are a single action, named helloworld, which takes as input a message parameter. +The only assets described by this hello world template are a single action, named helloworld, which takes as input a name parameter. You can use the wskdeploy tool to deploy this asset yourself using the manifest and available code. diff --git a/runtimes/nodejs-6/actions/helloworld.js b/runtimes/nodejs-6/actions/helloworld.js new file mode 100644 index 0000000..8e30651 --- /dev/null +++ b/runtimes/nodejs-6/actions/helloworld.js @@ -0,0 +1,22 @@ +/** + * + * main() will be invoked when you Run This Action. + * + * @param Cloud Functions actions accept a single parameter, + * which must be a JSON object. + * + * In this case, the params variable will look like: + * { "name": "xxxx" } + * + * @return which must be a JSON object. + * It will be the output of this action. + * + */ +function main(params) { + if (params.name) { + return { greeting: `Hello ${params.name}` }; + } + return { greeting: 'Hello stranger!' }; +} + +exports.main = main; diff --git a/runtimes/nodejs-6/manifest.yaml b/runtimes/nodejs-6/manifest.yaml new file mode 100644 index 0000000..066685e --- /dev/null +++ b/runtimes/nodejs-6/manifest.yaml @@ -0,0 +1,15 @@ +# Wskdeploy manifest for hello-world + +# Deployment using this manifest file creates the following OpenWhisk components: +# Package: openwhisk-helloworld +# Action: openwhisk-helloworld/helloworld.js + +packages: + $PACKAGE_NAME: + version: 1.0 + license: Apache-2.0 + namespace: _ + actions: + helloworld: + function: actions/helloworld.js + runtime: nodejs:6 diff --git a/runtimes/nodejs-8/actions/helloworld.js b/runtimes/nodejs-8/actions/helloworld.js new file mode 100644 index 0000000..8e30651 --- /dev/null +++ b/runtimes/nodejs-8/actions/helloworld.js @@ -0,0 +1,22 @@ +/** + * + * main() will be invoked when you Run This Action. + * + * @param Cloud Functions actions accept a single parameter, + * which must be a JSON object. + * + * In this case, the params variable will look like: + * { "name": "xxxx" } + * + * @return which must be a JSON object. + * It will be the output of this action. + * + */ +function main(params) { + if (params.name) { + return { greeting: `Hello ${params.name}` }; + } + return { greeting: 'Hello stranger!' }; +} + +exports.main = main; diff --git a/runtimes/nodejs-8/manifest.yaml b/runtimes/nodejs-8/manifest.yaml new file mode 100644 index 0000000..65bc18f --- /dev/null +++ b/runtimes/nodejs-8/manifest.yaml @@ -0,0 +1,15 @@ +# Wskdeploy manifest for hello-world + +# Deployment using this manifest file creates the following OpenWhisk components: +# Package: openwhisk-helloworld +# Action: openwhisk-helloworld/helloworld.js + +packages: + $PACKAGE_NAME: + version: 1.0 + license: Apache-2.0 + namespace: _ + actions: + helloworld: + function: actions/helloworld.js + runtime: nodejs:8 diff --git a/runtimes/nodejs/actions/helloworld.js b/runtimes/nodejs/actions/helloworld.js index 21ec1ea..91a4aa8 100644 --- a/runtimes/nodejs/actions/helloworld.js +++ b/runtimes/nodejs/actions/helloworld.js @@ -6,15 +6,15 @@ * which must be a JSON object. * * In this case, the params variable will look like: - * { "message": "xxxx" } + * { "name": "xxxx" } * * @return which must be a JSON object. * It will be the output of this action. * */ function main(params) { - if (params.message) { - return { "greeting": "Hello " + params.message }; + if (params.name) { + return { "greeting": "Hello " + params.name }; } else { return {"greeting": "Hello stranger!"} } diff --git a/runtimes/php-7.1/actions/helloworld.php b/runtimes/php-7.1/actions/helloworld.php new file mode 100644 index 0000000..4efae9a --- /dev/null +++ b/runtimes/php-7.1/actions/helloworld.php @@ -0,0 +1,22 @@ + $greeting]; +} diff --git a/runtimes/php-7.1/manifest.yaml b/runtimes/php-7.1/manifest.yaml new file mode 100644 index 0000000..44ac35f --- /dev/null +++ b/runtimes/php-7.1/manifest.yaml @@ -0,0 +1,15 @@ +# Wskdeploy manifest for hello-world + +# Deployment using this manifest file creates the following OpenWhisk components: +# Package: openwhisk-helloworld +# Action: openwhisk-helloworld/helloworld.js + +packages: + $PACKAGE_NAME: + version: 1.0 + license: Apache-2.0 + namespace: _ + actions: + helloworld: + function: actions/helloworld.php + runtime: php:7.1 diff --git a/runtimes/php/actions/helloworld.php b/runtimes/php/actions/helloworld.php index 4004c09..4efae9a 100644 --- a/runtimes/php/actions/helloworld.php +++ b/runtimes/php/actions/helloworld.php @@ -7,7 +7,7 @@ * which must be a JSON object. * * In this case, the params variable will look like: - * { "message": "xxxx" } + * { "name": "xxxx" } * * @return which must be a JSON object. * It will be the output of this action. @@ -15,7 +15,7 @@ */ function main(array $args) : array { - $name = $args["message"] ?? "stranger"; + $name = $args["name"] ?? "stranger"; $greeting = "Hello $name!"; echo $greeting; return ["greeting" => $greeting]; diff --git a/runtimes/python-3.6.4/actions/helloworld.py b/runtimes/python-3.6.4/actions/helloworld.py new file mode 100644 index 0000000..072729e --- /dev/null +++ b/runtimes/python-3.6.4/actions/helloworld.py @@ -0,0 +1,21 @@ +# +# +# main() will be invoked when you Run This Action. +# +# @param Cloud Functions actions accept a single parameter, +# which must be a JSON object. +# +# @return which must be a JSON object. +# It will be the output of this action. +# +# +import sys + +def main(dict): + if 'name' in dict: + name = dict['name'] + else: + name = 'stranger' + greeting = 'Hello ' + name + '!' + print(greeting) + return {'greeting':greeting} diff --git a/runtimes/python-3.6.4/manifest.yaml b/runtimes/python-3.6.4/manifest.yaml new file mode 100644 index 0000000..11bca29 --- /dev/null +++ b/runtimes/python-3.6.4/manifest.yaml @@ -0,0 +1,15 @@ +# Wskdeploy manifest for hello-world + +# Deployment using this manifest file creates the following OpenWhisk components: +# Package: openwhisk-helloworld +# Action: openwhisk-helloworld/helloworld.js + +packages: + $PACKAGE_NAME: + version: 1.0 + license: Apache-2.0 + namespace: _ + actions: + helloworld: + function: actions/helloworld.py + runtime: python-jessie:3 diff --git a/runtimes/python/actions/helloworld.py b/runtimes/python/actions/helloworld.py index 9d0684c..072729e 100644 --- a/runtimes/python/actions/helloworld.py +++ b/runtimes/python/actions/helloworld.py @@ -12,8 +12,8 @@ import sys def main(dict): - if 'message' in dict: - name = dict['message'] + if 'name' in dict: + name = dict['name'] else: name = 'stranger' greeting = 'Hello ' + name + '!' diff --git a/runtimes/swift-3.1.1/actions/helloworld.swift b/runtimes/swift-3.1.1/actions/helloworld.swift new file mode 100644 index 0000000..96465c4 --- /dev/null +++ b/runtimes/swift-3.1.1/actions/helloworld.swift @@ -0,0 +1,21 @@ +/** + * + * main() will be invoked when you Run This Action. + * + * @param Cloud Functions actions accept a single parameter, + * which must be a JSON object. + * + * In this case, the params variable will look like: + * { "name": "xxxx" } + * + * @return which must be a JSON object. + * It will be the output of this action. + * + */ +func main(args: [String:Any]) -> [String:Any] { + if let name = args["name"] as? String { + return [ "greeting" : "Hello \(name)!" ] + } else { + return [ "greeting" : "Hello stranger!" ] + } +} diff --git a/runtimes/swift-3.1.1/manifest.yaml b/runtimes/swift-3.1.1/manifest.yaml new file mode 100644 index 0000000..d099e2d --- /dev/null +++ b/runtimes/swift-3.1.1/manifest.yaml @@ -0,0 +1,15 @@ +# Wskdeploy manifest for hello-world + +# Deployment using this manifest file creates the following OpenWhisk components: +# Package: openwhisk-helloworld +# Action: openwhisk-helloworld/helloworld.js + +packages: + $PACKAGE_NAME: + version: 1.0 + license: Apache-2.0 + namespace: _ + actions: + helloworld: + function: actions/helloworld.swift + runtime: swift:3.1.1 diff --git a/runtimes/swift/actions/helloworld.swift b/runtimes/swift/actions/helloworld.swift index a986798..96465c4 100644 --- a/runtimes/swift/actions/helloworld.swift +++ b/runtimes/swift/actions/helloworld.swift @@ -6,15 +6,15 @@ * which must be a JSON object. * * In this case, the params variable will look like: - * { "message": "xxxx" } + * { "name": "xxxx" } * * @return which must be a JSON object. * It will be the output of this action. * */ func main(args: [String:Any]) -> [String:Any] { - if let message = args["message"] as? String { - return [ "greeting" : "Hello \(message)!" ] + if let name = args["name"] as? String { + return [ "greeting" : "Hello \(name)!" ] } else { return [ "greeting" : "Hello stranger!" ] } diff --git a/tests/src/test/scala/templates/HelloTests.scala b/tests/src/test/scala/templates/HelloTests.scala index 388920b..2ad9db8 100644 --- a/tests/src/test/scala/templates/HelloTests.scala +++ b/tests/src/test/scala/templates/HelloTests.scala @@ -35,30 +35,31 @@ class HelloTests extends TestHelpers val wsk = new Wsk() //set parameters for deploy tests - val nodejsfolder = "../runtimes/nodejs/actions"; - val phpfolder = "../runtimes/php/actions"; - val pythonfolder = "../runtimes/python/actions"; - val swiftfolder = "../runtimes/swift/actions"; + val nodejs8folder = "../runtimes/nodejs-8/actions"; + val nodejs6folder = "../runtimes/nodejs-6/actions"; + val phpfolder = "../runtimes/php-7.1/actions"; + val pythonfolder = "../runtimes/python-3/actions"; + val swiftfolder = "../runtimes/swift-3.1.1/actions"; behavior of "Hello World Template" /** - * Test the nodejs "hello world" template + * Test the nodejs 8 "hello world" template */ - it should "invoke helloworld.js and get the result" in withAssetCleaner(wskprops) { (wp, assetHelper) => + it should "invoke nodejs 8 helloworld.js and get the result" in withAssetCleaner(wskprops) { (wp, assetHelper) => val name = "helloNode" - val file = Some(new File(nodejsfolder, "helloworld.js").toString()); + val file = Some(new File(nodejs8folder, "helloworld.js").toString()); assetHelper.withCleaner(wsk.action, name) { (action, _) => action.create(name, file) } - withActivation(wsk.activation, wsk.action.invoke(name, Map("message" -> "Mindy".toJson))) { + withActivation(wsk.activation, wsk.action.invoke(name, Map("name" -> "Mindy".toJson))) { _.response.result.get.toString should include("Mindy") } } - it should "invoke helloworld.js without input and get stranger" in withAssetCleaner(wskprops) { (wp, assetHelper) => + it should "invoke nodejs 8 helloworld.js without input and get stranger" in withAssetCleaner(wskprops) { (wp, assetHelper) => val name = "helloNode" - val file = Some(new File(nodejsfolder, "helloworld.js").toString()); + val file = Some(new File(nodejs8folder, "helloworld.js").toString()); assetHelper.withCleaner(wsk.action, name) { (action, _) => action.create(name, file) } @@ -67,8 +68,34 @@ class HelloTests extends TestHelpers _.response.result.get.toString should include("stranger") } } + + /** + * Test the nodejs 6 "hello world" template + */ + it should "invoke nodejs 6 helloworld.js and get the result" in withAssetCleaner(wskprops) { (wp, assetHelper) => + val name = "helloNode" + val file = Some(new File(nodejs6folder, "helloworld.js").toString()); + assetHelper.withCleaner(wsk.action, name) { (action, _) => + action.create(name, file) + } + + withActivation(wsk.activation, wsk.action.invoke(name, Map("name" -> "Mindy".toJson))) { + _.response.result.get.toString should include("Mindy") + } + } + it should "invoke nodejs 6 helloworld.js without input and get stranger" in withAssetCleaner(wskprops) { (wp, assetHelper) => + val name = "helloNode" + val file = Some(new File(nodejs6folder, "helloworld.js").toString()); + assetHelper.withCleaner(wsk.action, name) { (action, _) => + action.create(name, file) + } + + withActivation(wsk.activation, wsk.action.invoke(name)) { + _.response.result.get.toString should include("stranger") + } + } /** - * Test the php "hello world" template + * Test the php 7.1 "hello world" template */ it should "invoke helloworld.php and get the result" in withAssetCleaner(wskprops) { (wp, assetHelper) => val name = "helloPhp" @@ -77,7 +104,7 @@ class HelloTests extends TestHelpers action.create(name, file) } - withActivation(wsk.activation, wsk.action.invoke(name, Map("message" -> "Mindy".toJson))) { + withActivation(wsk.activation, wsk.action.invoke(name, Map("name" -> "Mindy".toJson))) { _.response.result.get.toString should include("Mindy") } } @@ -93,7 +120,7 @@ class HelloTests extends TestHelpers } } /** - * Test the python "hello world" template + * Test the python 3 "hello world" template */ it should "invoke helloworld.py and get the result" in withAssetCleaner(wskprops) { (wp, assetHelper) => val name = "helloPython" @@ -102,7 +129,7 @@ class HelloTests extends TestHelpers action.create(name, file) } - withActivation(wsk.activation, wsk.action.invoke(name, Map("message" -> "Mindy".toJson))) { + withActivation(wsk.activation, wsk.action.invoke(name, Map("name" -> "Mindy".toJson))) { _.response.result.get.toString should include("Mindy") } } @@ -118,7 +145,7 @@ class HelloTests extends TestHelpers } } /** - * Test the swift "hello world" template + * Test the swift 3.1.1 "hello world" template */ it should "invoke helloworld.swift and get the result" in withAssetCleaner(wskprops) { (wp, assetHelper) => val name = "helloSwift" @@ -127,7 +154,7 @@ class HelloTests extends TestHelpers action.create(name, file) } - withActivation(wsk.activation, wsk.action.invoke(name, Map("message" -> "Mindy".toJson))) { + withActivation(wsk.activation, wsk.action.invoke(name, Map("name" -> "Mindy".toJson))) { _.response.result.get.toString should include("Mindy") } }