This is an automated email from the ASF dual-hosted git repository. dubeejw pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/openwhisk-package-kafka.git
View the commit online: https://github.com/apache/openwhisk-package-kafka/commit/645e0240862d042463fa8f9a2a83581b797916a3 The following commit(s) were added to refs/heads/master by this push: new 645e024 Update existing trigger feeds on create instead of failing (#360) 645e024 is described below commit 645e0240862d042463fa8f9a2a83581b797916a3 Author: James Dubee <jwdu...@us.ibm.com> AuthorDate: Wed Nov 20 13:13:50 2019 -0500 Update existing trigger feeds on create instead of failing (#360) * Update existing triggers on create instead of failing * Refactoring * Catch errors... * Pass correct argument to disable * Properly update trigger * Formatting * Return promise properly * Valid updated parameters first --- action/kafkaFeedWeb.js | 9 ++++++-- action/lib/Database.js | 51 ++++++++++++++++++++++++++------------------- action/messageHubFeedWeb.js | 9 ++++++-- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/action/kafkaFeedWeb.js b/action/kafkaFeedWeb.js index 1886107..ef18bed 100644 --- a/action/kafkaFeedWeb.js +++ b/action/kafkaFeedWeb.js @@ -119,10 +119,15 @@ function main(params) { }) .then(triggerDoc => { if (!triggerDoc.status.active) { - resolve(common.webResponse(400, `${params.triggerName} cannot be updated because it is disabled`)); + return resolve(common.webResponse(400, `${params.triggerName} cannot be updated because it is disabled`)); } + return common.performUpdateParameterValidation(params, triggerDoc) - .then(updatedParams => db.updateTrigger(triggerDoc, updatedParams)) + .then(updatedParams => { + return db.disableTrigger(triggerDoc) + .then(() => db.getTrigger(params.triggerName)) + .then(doc => db.updateTrigger(doc, updatedParams)); + }); }) .then(() => { console.log('successfully updated the trigger'); diff --git a/action/lib/Database.js b/action/lib/Database.js index 831ae1a..4cc5d3c 100644 --- a/action/lib/Database.js +++ b/action/lib/Database.js @@ -58,7 +58,16 @@ module.exports = function(dbURL, dbName) { return new Promise((resolve, reject) => { this.db.insert(params, (err, result) => { if(err) { - reject(err); + if(err.statusCode && err.statusCode === 409) { + this.getTrigger(params.triggerName) + .then(doc => this.disableTrigger(doc)) + .then(() => this.getTrigger(params.triggerName)) + .then(doc => this.updateTrigger(params, {_rev: doc._rev})) + .then(result => resolve(result)) + .catch(err => reject(err)); + } else { + reject(err); + } } else { resolve(result); } @@ -122,7 +131,7 @@ module.exports = function(dbURL, dbName) { }); }; - this.updateTrigger = function(existing, params) { + this.disableTrigger = function(existing) { return new Promise((resolve, reject) => { var message = 'Automatically disabled trigger while updating'; var status = { @@ -139,27 +148,27 @@ module.exports = function(dbURL, dbName) { } }); }) - .then(() => this.getTrigger(existing.triggerName)) - .then(doc => { - for (var key in params) { - if (params[key] !== undefined) { - doc[key] = params[key]; - } + }; + + this.updateTrigger = function(existing, params) { + for (var key in params) { + if (params[key] !== undefined) { + existing[key] = params[key]; } - var status = { - 'active': true, - 'dateChanged': Date.now() - }; - doc.status = status; + } + var status = { + 'active': true, + 'dateChanged': Date.now() + }; + existing.status = status; - return new Promise((resolve, reject) => { - this.db.insert(doc, (err, result) => { - if(err) { - reject(err); - } else { - resolve(result); - } - }); + return new Promise((resolve, reject) => { + this.db.insert(existing, (err, result) => { + if(err) { + reject(err); + } else { + resolve(result); + } }); }); }; diff --git a/action/messageHubFeedWeb.js b/action/messageHubFeedWeb.js index e1fb9d8..50e3819 100644 --- a/action/messageHubFeedWeb.js +++ b/action/messageHubFeedWeb.js @@ -126,10 +126,15 @@ function main(params) { }) .then(triggerDoc => { if (!triggerDoc.status.active) { - resolve(common.webResponse(400, `${params.triggerName} cannot be updated because it is disabled`)); + return resolve(common.webResponse(400, `${params.triggerName} cannot be updated because it is disabled`)); } + return common.performUpdateParameterValidation(params, triggerDoc) - .then(updatedParams => db.updateTrigger(triggerDoc, updatedParams)) + .then(updatedParams => { + return db.disableTrigger(triggerDoc) + .then(() => db.getTrigger(params.triggerName)) + .then(doc => db.updateTrigger(doc, updatedParams)); + }); }) .then(() => { console.log('successfully updated the trigger');