You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by cs...@apache.org on 2017/09/14 20:11:44 UTC

[incubator-openwhisk] branch master updated: Added functions to create a trigger and create a rule for Swift actions.

This is an automated email from the ASF dual-hosted git repository.

csantanapr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new 4309e3f  Added functions to create a trigger and create a rule for Swift actions.
4309e3f is described below

commit 4309e3fc1db0d2168b9a7809e0963967b5c069fd
Author: Scott Wilson-Billing <sc...@mac.com>
AuthorDate: Thu Sep 14 10:47:56 2017 -0400

    Added functions to create a trigger and create a rule for Swift actions.
    
    Updated name of method for sending Whisk API request.
    Cleaned up the parameter naming a little for consistency.
    Changed to using guard to check for the trigger name argument
    Added additional assertion that the rule created did actually get fired and run ok
    Added tests for creating a trigger and a rule
    Documentation
---
 core/swift3Action/spm-build/_Whisk.swift           | 27 ++++++---
 tests/dat/actions/createRule.swift                 | 13 ++++
 tests/dat/actions/createTrigger.swift              |  7 +++
 .../scala/whisk/core/cli/test/Swift3Tests.scala    | 70 ++++++++++++++++++++++
 4 files changed, 110 insertions(+), 7 deletions(-)

diff --git a/core/swift3Action/spm-build/_Whisk.swift b/core/swift3Action/spm-build/_Whisk.swift
index 2bdda05..59efc78 100644
--- a/core/swift3Action/spm-build/_Whisk.swift
+++ b/core/swift3Action/spm-build/_Whisk.swift
@@ -25,19 +25,32 @@ class Whisk {
         let strBlocking = blocking ? "true" : "false"
         let path = "/api/v1/namespaces/\(parsedAction.namespace)/actions/\(parsedAction.name)?blocking=\(strBlocking)"
 
-        return postSyncronish(uriPath: path, params: params)
+        return sendWhiskRequestSyncronish(uriPath: path, params: params, method: "post")
     }
 
     class func trigger(eventNamed event : String, withParameters params : [String:Any]) -> [String:Any] {
         let parsedEvent = parseQualifiedName(name: event)
         let path = "/api/v1/namespaces/\(parsedEvent.namespace)/triggers/\(parsedEvent.name)?blocking=true"
 
-        return postSyncronish(uriPath: path, params: params)
+        return sendWhiskRequestSyncronish(uriPath: path, params: params, method: "post")
+    }
+
+    class func createTrigger(triggerNamed trigger: String, withParameters params : [String:Any]) -> [String:Any] {
+        let parsedTrigger = parseQualifiedName(name: trigger)
+        let path = "/api/v1/namespaces/\(parsedTrigger.namespace)/triggers/\(parsedTrigger.name)"
+        return sendWhiskRequestSyncronish(uriPath: path, params: params, method: "put")
+    }
+
+    class func createRule(ruleNamed ruleName: String, withTrigger triggerName: String, andAction actionName: String) -> [String:Any] {
+        let parsedRule = parseQualifiedName(name: ruleName)
+        let path = "/api/v1/namespaces/\(parsedRule.namespace)/rules/\(parsedRule.name)"
+        let params = ["trigger":triggerName, "action":actionName]
+        return sendWhiskRequestSyncronish(uriPath: path, params: params, method: "put")
     }
 
     // handle the GCD dance to make the post async, but then obtain/return
     // the result from this function sync
-    private class func postSyncronish(uriPath path: String, params : [String:Any]) -> [String:Any] {
+    private class func sendWhiskRequestSyncronish(uriPath path: String, params : [String:Any], method: String) -> [String:Any] {
         var response : [String:Any]!
 
         let queue = DispatchQueue.global()
@@ -45,7 +58,7 @@ class Whisk {
 
         invokeGroup.enter()
         queue.async {
-            post(uriPath: path, params: params, group: invokeGroup) { result in
+            sendWhiskRequest(uriPath: path, params: params, method: method, group: invokeGroup) { result in
                 response = result
             }
         }
@@ -109,8 +122,8 @@ class Whisk {
         return (httpType, host, port, authKey)
     }
 
-    // actually do the POST call to the specified OpenWhisk URI path
-    private class func post(uriPath: String, params : [String:Any], group: DispatchGroup, callback : @escaping([String:Any]) -> Void) {
+    // actually do the call to the specified OpenWhisk URI path
+    private class func sendWhiskRequest(uriPath: String, params : [String:Any], method: String, group: DispatchGroup, callback : @escaping([String:Any]) -> Void) {
         let communicationDetails = initializeCommunication()
 
         let loginData: Data = communicationDetails.authKey.data(using: String.Encoding.utf8, allowLossyConversion: false)!
@@ -126,7 +139,7 @@ class Whisk {
 
         // TODO vary the schema based on the port?
         let requestOptions = [ClientRequest.Options.schema(communicationDetails.httpType),
-                              ClientRequest.Options.method("post"),
+                              ClientRequest.Options.method(method),
                               ClientRequest.Options.hostname(communicationDetails.host),
                               ClientRequest.Options.port(communicationDetails.port),
                               ClientRequest.Options.path(encodedPath),
diff --git a/tests/dat/actions/createRule.swift b/tests/dat/actions/createRule.swift
new file mode 100644
index 0000000..67fa062
--- /dev/null
+++ b/tests/dat/actions/createRule.swift
@@ -0,0 +1,13 @@
+func main(args: [String:Any]) -> [String:Any] {
+  guard let triggerName = args["triggerName"] as? String else {
+      return ["error": "You must specify a triggerName parameter!"]
+  }
+  guard let actionName = args["actionName"] as? String else {
+      return ["error": "You must specify a actionName parameter!"]
+  }
+  guard let ruleName = args["ruleName"] as? String else {
+      return ["error": "You must specify a ruleName parameter!"]
+  }
+  print("Rule Name: \(ruleName), Trigger Name: \(triggerName), actionName: \(actionName)")
+  return Whisk.createRule(ruleNamed: ruleName, withTrigger: triggerName, andAction: actionName)
+}
diff --git a/tests/dat/actions/createTrigger.swift b/tests/dat/actions/createTrigger.swift
new file mode 100644
index 0000000..7039ac4
--- /dev/null
+++ b/tests/dat/actions/createTrigger.swift
@@ -0,0 +1,7 @@
+func main(args: [String:Any]) -> [String:Any] {
+ guard let triggerName = args["triggerName"] as? String else {
+    return ["error": "You must specify a triggerName parameter!"]
+  }
+  print("Trigger Name: \(triggerName)")
+  return Whisk.createTrigger(triggerNamed: triggerName, withParameters: [:])
+}
diff --git a/tests/src/test/scala/whisk/core/cli/test/Swift3Tests.scala b/tests/src/test/scala/whisk/core/cli/test/Swift3Tests.scala
index 547a2e2..513cf94 100644
--- a/tests/src/test/scala/whisk/core/cli/test/Swift3Tests.scala
+++ b/tests/src/test/scala/whisk/core/cli/test/Swift3Tests.scala
@@ -143,4 +143,74 @@ class Swift3Tests extends TestHelpers with WskTestHelpers with Matchers {
       }
     }
   }
+
+  it should "allow Swift actions to create a trigger" in withAssetCleaner(wskprops) { (wp, assetHelper) =>
+    // create an action that creates the trigger
+    val file = TestUtils.getTestActionFilename("createTrigger.swift")
+    val actionName = "ActionThatTriggers"
+
+    // the name of the trigger to create
+    val triggerName = s"TestTrigger ${System.currentTimeMillis()}"
+
+    assetHelper.withCleaner(wsk.action, actionName) { (action, _) =>
+      assetHelper.withCleaner(wsk.trigger, triggerName) { (_, _) =>
+        // using an asset cleaner on the created trigger name will clean it up at the conclusion of the test
+        action.create(name = actionName, artifact = Some(file), kind = Some("swift:3"))
+      }
+    }
+
+    // invoke the action
+    val run = wsk.action.invoke(actionName, Map("triggerName" -> triggerName.toJson))
+    withActivation(wsk.activation, run, initialWait = 5 seconds, totalWait = 60 seconds) { activation =>
+      // should be successful
+      activation.response.success shouldBe true
+
+      // should have a field named "name" which is the name of the trigger created
+      activation.response.result.get.fields("name") shouldBe triggerName.toJson
+    }
+  }
+
+  it should "allow Swift actions to create a rule" in withAssetCleaner(wskprops) { (wp, assetHelper) =>
+    val ruleTriggerName = s"TestTrigger ${System.currentTimeMillis()}"
+    val ruleActionName = s"TestAction ${System.currentTimeMillis()}"
+    val ruleName = s"TestRule ${System.currentTimeMillis()}"
+
+    // create a dummy action and trigger for the rule
+    assetHelper.withCleaner(wsk.action, ruleActionName) { (action, name) =>
+      val dummyFile = TestUtils.getTestActionFilename("hello.swift")
+      action.create(name, artifact = Some(dummyFile), kind = Some("swift:3"))
+    }
+
+    assetHelper.withCleaner(wsk.trigger, ruleTriggerName) { (trigger, name) =>
+      assetHelper.withCleaner(wsk.rule, ruleName) { (_, _) =>
+        // using an asset cleaner on the created trigger name will clean it up at the conclusion of the test
+        trigger.create(name)
+      }
+    }
+
+    // create an action that creates the rule
+    val createRuleFile = TestUtils.getTestActionFilename("createRule.swift")
+    assetHelper.withCleaner(wsk.action, "ActionThatCreatesRule") { (action, name) =>
+      action.create(name, artifact = Some(createRuleFile), kind = Some("swift:3"))
+    }
+
+    // invoke the create rule action
+    val runCreateRule = wsk.action.invoke(
+      "ActionThatCreatesRule",
+      Map(
+        "triggerName" -> s"/_/$ruleTriggerName".toJson,
+        "actionName" -> s"/_/$ruleActionName".toJson,
+        "ruleName" -> ruleName.toJson))
+
+    withActivation(wsk.activation, runCreateRule, initialWait = 5 seconds, totalWait = 60 seconds) { activation =>
+      // should be successful
+      activation.response.success shouldBe true
+
+      // should have a field named "trigger" which is the name of the trigger associated with the rule
+      activation.response.result.get.fields("trigger").asJsObject.fields("name") shouldBe ruleTriggerName.toJson
+
+      // should have a field named "action" which is the name of the action associated with the rule
+      activation.response.result.get.fields("action").asJsObject.fields("name") shouldBe ruleActionName.toJson
+    }
+  }
 }

-- 
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].