Skip to content

Commit 7115e52

Browse files
committed
pushing new working version
1 parent c43aecd commit 7115e52

File tree

2 files changed

+133
-39
lines changed

2 files changed

+133
-39
lines changed

78-cloudant-cf.html renamed to 77-cloudant-cf.html

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,17 @@
8888
</script>
8989

9090
<script type="text/x-red" data-template-name="cloudant out">
91+
9192
<div class="form-row">
92-
<label for="node-input-cloudant"><i class="fa fa-tag"></i> Account</label>
93+
<label for="node-input-service"><i class="fa fa-folder-close"></i> Service</label>
94+
<select id="node-input-service">
95+
<option value="" disabled></option>
96+
<option value="_ext_"> External service</option>
97+
</select>
98+
</div>
99+
100+
<div class="form-row hide" id="node-input-external-details">
101+
<label for="node-input-cloudant"><i class=" fa fa-bookmark"></i> Server</label>
93102
<input type="text" id="node-input-cloudant">
94103
</div>
95104

@@ -127,6 +136,7 @@
127136
else $(".node-input-payonly").show();
128137
});
129138
</script>
139+
130140
</script>
131141

132142
<script type="text/x-red" data-help-name="cloudant out">
@@ -150,26 +160,72 @@
150160
</p>
151161
</script>
152162

153-
<script type="text/javascript">
154-
RED.nodes.registerType("cloudant out", {
155-
category: "storage-output",
156-
color: "rgb(114, 199, 231)",
157-
defaults: {
158-
cloudant: { type: "cloudant", required: true },
159-
name: { value: "" },
160-
db: { value: "", required: true },
161-
payonly: { value: false },
162-
operation: { value: "insert" }
163-
},
164-
inputs: 1,
165-
outputs: 0,
166-
icon: "cloudant.png",
167-
align: "right",
168-
label: function() {
169-
return this.name || "cloudant";
170-
},
171-
labelStyle: function() {
172-
return this.name ? "node_label_italic" : "";
163+
<script type="text/javascript"> //js functions
164+
(function() {
165+
166+
function oneditprepare() {
167+
var select = $('#node-input-service');
168+
var node = this;
169+
$.getJSON('cloudantnode/vcap/',function(data) {
170+
var last = select.children().last();
171+
var opts = [];
172+
for (var i =0;i<data.length;i++) {
173+
opts.push('<option value="'+data[i].name+'"'+(node.service==data[i].name?" selected":"")+'>'+data[i].name+'</option>');
174+
}
175+
if (opts.length == 0) {
176+
node.service = "_ext_";
177+
select.find("option").filter(function() {return $(this).val() == node.service;}).attr('selected',true);
178+
} else {
179+
last.before(opts.join(""));
180+
}
181+
select.change();
182+
});
183+
select.change(function() {
184+
var service = select.val();
185+
if (service == "_ext_") {
186+
$("#node-input-external-details").show();
187+
} else {
188+
$("#node-input-external-details").hide();
189+
}
190+
});
173191
}
174-
});
192+
193+
function label() {
194+
var cloudantNode = RED.nodes.node(this.cloudant);
195+
if (this.name) {
196+
return this.name;
197+
}
198+
if (this.service == "_ext_") {
199+
return (cloudantNode?cloudantNode.label()+" "+this.collection:"cloudant");
200+
}
201+
return this.service||"cloudant";
202+
}
203+
204+
function validateServer(v) {
205+
return this.service != "_ext_" || v!="_ADD_";
206+
}
207+
208+
RED.nodes.registerType("cloudant out", {
209+
category: "storage-output",
210+
color: "rgb(114, 199, 231)",
211+
defaults: {
212+
service: { value: "", required: true },
213+
cloudant: { type:"cloudant",validate:validateServer}, //validate server
214+
name: { value: "" },
215+
db: { value: "", required: true },
216+
payonly: { value: false },
217+
operation: { value: "insert" }
218+
},
219+
inputs: 1,
220+
outputs: 0,
221+
icon: "cloudant.png",
222+
align: "right",
223+
label: label, //called
224+
labelStyle: function() {
225+
return this.name?"node_label_italic":"";
226+
},
227+
oneditprepare: oneditprepare //called
228+
});
229+
})();
230+
175231
</script>

78-cloudant-cf.js renamed to 77-cloudant-cf.js

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,27 @@
1414
* limitations under the License.
1515
**/
1616

17-
module.exports = function(RED) {
18-
"use strict";
17+
18+
var when = require("when");
19+
20+
var cfEnv = require("cf-env");
21+
var cfCore = cfEnv.getCore();
22+
23+
var services = [];
24+
25+
for (var i in cfCore.services) {
26+
27+
console.log(cfCore.services[i]);
28+
29+
services = services.concat(cfCore.services[i].map(function(v) {
30+
return {name:v.name,label:v.label};
31+
}));
32+
}
33+
34+
console.log(services);
35+
36+
var RED = require(process.env.NODE_RED_HOME+"/red/red");
37+
1938
var url = require('url');
2039
var querystring = require('querystring');
2140

@@ -44,6 +63,7 @@ module.exports = function(RED) {
4463

4564
RED.nodes.registerType("cloudant", CloudantNode);
4665

66+
4767
RED.httpAdmin.get('/cloudant/:id', function(req,res) {
4868
var credentials = RED.nodes.getCredentials(req.params.id);
4969

@@ -64,6 +84,10 @@ module.exports = function(RED) {
6484
res.send(200);
6585
});
6686

87+
RED.httpAdmin.get('/cloudantnode/vcap', function(req,res) {
88+
res.send(JSON.stringify(services));
89+
});
90+
6791
RED.httpAdmin.post('/cloudant/:id', function(req,res) {
6892
var body = "";
6993

@@ -92,32 +116,47 @@ module.exports = function(RED) {
92116
});
93117
});
94118

119+
95120
function CloudantOutNode(n) {
96121
RED.nodes.createNode(this,n);
97122

98123
this.operation = n.operation;
99124
this.payonly = n.payonly || false;
100125
this.database = n.db;
101126
this.cloudant = n.cloudant;
102-
this.cloudantConfig = RED.nodes.getNode(this.cloudant);
127+
//this.cloudantConfig = RED.nodes.getNode(this.cloudant);
128+
129+
if (n.service == "_ext_") {
130+
var cloudantConfig = RED.nodes.getNode(this.cloudant);
131+
if (cloudantConfig) {
132+
this.url = cloudantConfig.url;
133+
}
134+
}
135+
else if (n.service != "") {
136+
var cloudantConfig = cfEnv.getService(n.service);
137+
if (cloudantConfig) {
138+
this.url = cloudantConfig.credentials.url||cloudantConfig.credentials.uri||Config.credentials.json_url;
139+
//this.url = cloudantConfig.credentials.url;
140+
}
141+
}
103142

104-
if (this.cloudantConfig) {
143+
if (this.url) { //this.url //this.cloudantConfig
105144
var node = this;
106145

107-
var nano = require('nano')(node.cloudantConfig.url);
146+
var nano = require('nano')(this.url);
108147
var db = nano.use(node.database);
109148

110149
// check if the database exists and create it if it doesn't
111-
nano.db.list(function(err, body) {
112-
if (err) { node.error(err); }
113-
else {
114-
if (body && body.indexOf(node.database) < 0) {
115-
nano.db.create(node.database, function(err, body) {
116-
if (err) { node.error(err); }
117-
});
118-
}
119-
}
120-
});
150+
nano.db.list(function(err, body) {
151+
if (err) { node.error(err); }
152+
else {
153+
if (body && body.indexOf(node.database) < 0) {
154+
nano.db.create(node.database, function(err, body) {
155+
if (err) { node.error(err); }
156+
});
157+
}
158+
}
159+
});
121160

122161
node.on("input", function(msg) {
123162
if (node.operation === "insert") {
@@ -130,7 +169,7 @@ module.exports = function(RED) {
130169
});
131170
}
132171
else if (node.operation === "delete") {
133-
var doc = msg.payload;
172+
var doc = parseMessage(msg.payload, "");
134173

135174
if ("_rev" in doc && "_id" in doc) {
136175
db.destroy(doc._id, doc._rev, function(err, body) {
@@ -166,4 +205,3 @@ module.exports = function(RED) {
166205
};
167206

168207
RED.nodes.registerType("cloudant out", CloudantOutNode);
169-
}

0 commit comments

Comments
 (0)