|
| 1 | +/** |
| 2 | + * Copyright 2014 IBM Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | +var url = require('url'); |
| 17 | +var querystring = require('querystring'); |
| 18 | +var cfEnv = require("cf-env"); |
| 19 | +var RED = require(process.env.NODE_RED_HOME + "/red/red"); |
| 20 | + |
| 21 | +var cfCore = cfEnv.getCore(); |
| 22 | +var services = []; |
| 23 | + |
| 24 | +// load the services bindded to this application |
| 25 | +for (var i in cfCore.services) { |
| 26 | + // filter the services to include only the Cloudant ones |
| 27 | + if (i.match(/^(cloudant)/i)) { |
| 28 | + services = services.concat(cfCore.services[i].map(function(v) { |
| 29 | + return { name: v.name, label: v.label }; |
| 30 | + })); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// |
| 35 | +// HTTP endpoints that will be accessed from the HTML file |
| 36 | +// |
| 37 | +RED.httpAdmin.get('/cloudant/vcap', function(req,res) { |
| 38 | + res.send(JSON.stringify(services)); |
| 39 | +}); |
| 40 | + |
| 41 | +// REMINDER: routes are order dependent |
| 42 | +RED.httpAdmin.get('/cloudant/:id', function(req,res) { |
| 43 | + var credentials = RED.nodes.getCredentials(req.params.id); |
| 44 | + |
| 45 | + if (credentials) { |
| 46 | + res.send(JSON.stringify( |
| 47 | + { |
| 48 | + user: credentials.user, |
| 49 | + hasPassword: (credentials.password && credentials.password !== "") |
| 50 | + } |
| 51 | + )); |
| 52 | + } else { |
| 53 | + res.send(JSON.stringify({})); |
| 54 | + } |
| 55 | +}); |
| 56 | + |
| 57 | +RED.httpAdmin.delete('/cloudant/:id', function(req,res) { |
| 58 | + RED.nodes.deleteCredentials(req.params.id); |
| 59 | + res.send(200); |
| 60 | +}); |
| 61 | + |
| 62 | +RED.httpAdmin.post('/cloudant/:id', function(req,res) { |
| 63 | + var body = ""; |
| 64 | + |
| 65 | + req.on('data', function(chunk) { |
| 66 | + body += chunk; |
| 67 | + }); |
| 68 | + |
| 69 | + req.on('end', function() { |
| 70 | + var newCreds = querystring.parse(body); |
| 71 | + var credentials = RED.nodes.getCredentials(req.params.id) || {}; |
| 72 | + |
| 73 | + if (newCreds.user == null || newCreds.user == "") { |
| 74 | + delete credentials.user; |
| 75 | + } else { |
| 76 | + credentials.user = newCreds.user; |
| 77 | + } |
| 78 | + |
| 79 | + if (newCreds.password == "") { |
| 80 | + delete credentials.password; |
| 81 | + } else { |
| 82 | + credentials.password = newCreds.password || credentials.password; |
| 83 | + } |
| 84 | + |
| 85 | + RED.nodes.addCredentials(req.params.id, credentials); |
| 86 | + res.send(200); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +// |
| 91 | +// Create and register nodes |
| 92 | +// |
| 93 | +function CloudantNode(n) { |
| 94 | + RED.nodes.createNode(this, n); |
| 95 | + |
| 96 | + this.name = n.name; |
| 97 | + this.hostname = n.hostname; |
| 98 | + |
| 99 | + var credentials = RED.nodes.getCredentials(n.id); |
| 100 | + if (credentials) { |
| 101 | + this.username = credentials.user; |
| 102 | + this.password = credentials.password; |
| 103 | + } |
| 104 | + |
| 105 | + var parsedUrl = url.parse(this.hostname); |
| 106 | + var authUrl = parsedUrl.protocol+'//'; |
| 107 | + |
| 108 | + if (this.username && this.password) { |
| 109 | + authUrl += this.username + ":" + encodeURIComponent(this.password) + "@"; |
| 110 | + } |
| 111 | + authUrl += parsedUrl.hostname; |
| 112 | + |
| 113 | + this.url = authUrl; |
| 114 | +} |
| 115 | +RED.nodes.registerType("cloudant", CloudantNode); |
| 116 | + |
| 117 | +function CloudantOutNode(n) { |
| 118 | + RED.nodes.createNode(this,n); |
| 119 | + |
| 120 | + this.operation = n.operation; |
| 121 | + this.payonly = n.payonly || false; |
| 122 | + this.database = n.database; |
| 123 | + this.cloudant = n.cloudant; |
| 124 | + |
| 125 | + if (n.service == "_ext_") { |
| 126 | + var cloudantConfig = RED.nodes.getNode(this.cloudant); |
| 127 | + if (cloudantConfig) { |
| 128 | + this.url = cloudantConfig.url; |
| 129 | + } |
| 130 | + } |
| 131 | + else if (n.service != "") { |
| 132 | + var cloudantConfig = cfEnv.getService(n.service); |
| 133 | + if (cloudantConfig) { |
| 134 | + this.url = cloudantConfig.credentials.url; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if (this.url) { |
| 139 | + var node = this; |
| 140 | + |
| 141 | + var nano = require('nano')(this.url); |
| 142 | + var db = nano.use(node.database); |
| 143 | + |
| 144 | + // check if the database exists and create it if it doesn't |
| 145 | + nano.db.list(function(err, body) { |
| 146 | + if (err) { node.error(err); } |
| 147 | + else { |
| 148 | + if (body && body.indexOf(node.database) < 0) { |
| 149 | + nano.db.create(node.database, function(err, body) { |
| 150 | + if (err) { node.error(err); } |
| 151 | + }); |
| 152 | + } |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + node.on("input", function(msg) { |
| 157 | + if (node.operation === "insert") { |
| 158 | + var msg = node.payonly ? msg.payload : msg; |
| 159 | + var root = node.payonly ? "payload" : "msg"; |
| 160 | + var doc = parseMessage(msg, root); |
| 161 | + |
| 162 | + db.insert(doc, function(err, body) { |
| 163 | + if (err) { node.error(err); } |
| 164 | + }); |
| 165 | + } |
| 166 | + else if (node.operation === "delete") { |
| 167 | + var doc = parseMessage(msg.payload || msg, ""); |
| 168 | + |
| 169 | + if ("_rev" in doc && "_id" in doc) { |
| 170 | + db.destroy(doc._id, doc._rev, function(err, body) { |
| 171 | + if (err) { node.error(err); } |
| 172 | + }); |
| 173 | + } else { |
| 174 | + node.error("_rev and _id are required to delete a document"); |
| 175 | + } |
| 176 | + } |
| 177 | + }); |
| 178 | + |
| 179 | + } else { |
| 180 | + this.error("missing cloudant configuration"); |
| 181 | + } |
| 182 | + |
| 183 | + function parseMessage(msg, root) { |
| 184 | + if (typeof msg !== "object") { |
| 185 | + try { |
| 186 | + msg = JSON.parse(msg); |
| 187 | + |
| 188 | + // JSON.parse accepts numbers, so make sure that an |
| 189 | + // object is return, otherwise create a new one |
| 190 | + if (typeof msg !== "object") { |
| 191 | + msg = JSON.parse('{"' + root + '":"' + msg + '"}'); |
| 192 | + } |
| 193 | + } catch (e) { |
| 194 | + // payload is not in JSON format |
| 195 | + msg = JSON.parse('{"' + root + '":"' + msg + '"}'); |
| 196 | + } |
| 197 | + } |
| 198 | + return msg; |
| 199 | + } |
| 200 | +}; |
| 201 | +RED.nodes.registerType("cloudant out", CloudantOutNode); |
0 commit comments