summaryrefslogtreecommitdiff
path: root/nodejs-zt1-client
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2015-05-16 17:12:29 -0700
committerAdam Ierymenko <adam.ierymenko@gmail.com>2015-05-16 17:12:29 -0700
commit69ceb7e730a1fe4e2d0f82c7d29875bd796468ea (patch)
treee754adc8d6f867d86d50d4277350a9b50c8a52d2 /nodejs-zt1-client
parentcf51961d523222815cde064d88955aee88352eb5 (diff)
downloadinfinitytier-69ceb7e730a1fe4e2d0f82c7d29875bd796468ea.tar.gz
infinitytier-69ceb7e730a1fe4e2d0f82c7d29875bd796468ea.zip
Basic controller JSON API seems to be working.
Diffstat (limited to 'nodejs-zt1-client')
-rw-r--r--nodejs-zt1-client/index.js74
-rw-r--r--nodejs-zt1-client/test-controller.js14
-rw-r--r--nodejs-zt1-client/test.js33
3 files changed, 103 insertions, 18 deletions
diff --git a/nodejs-zt1-client/index.js b/nodejs-zt1-client/index.js
index e44fe795..f61e3b54 100644
--- a/nodejs-zt1-client/index.js
+++ b/nodejs-zt1-client/index.js
@@ -18,9 +18,9 @@ ZT1Client.prototype._jsonGet = function(getPath,callback)
}
},function(error,response,body) {
if (error)
- return callback(error,{});
+ return callback(error,null);
if (response.statusCode !== 200)
- return callback(new Error('server responded with '+response.statusCode),{});
+ return callback(new Error('server responded with error: '+response.statusCode),null);
return callback(null,(typeof body === 'string') ? JSON.parse(body) : null);
});
};
@@ -58,14 +58,80 @@ ZT1Client.prototype.status = function(callback)
}.bind(this));
};
-ZT1Client.prototype.networks = function(callback)
+ZT1Client.prototype.getNetworks = function(callback)
{
this._jsonGet('network',callback);
};
-ZT1Client.prototype.controllerNetworks = function(callback)
+ZT1Client.prototype.getPeers = function(callback)
+{
+ this._jsonGet('peer',callback);
+};
+
+ZT1Client.prototype.listControllerNetworks = function(callback)
{
this._jsonGet('controller/network',callback);
};
+ZT1Client.prototype.getControllerNetwork = function(nwid,callback)
+{
+ this._jsonGet('controller/network/' + nwid,callback);
+};
+
+ZT1Client.prototype.saveControllerNetwork = function(network,callback)
+{
+ if ((typeof network.nwid !== 'string')||(network.nwid.length !== 16))
+ return callback(new Error('Missing required field: nwid'),null);
+
+ // The ZT1 service is type variation intolerant, so recreate our submission with the correct types
+ var n = {
+ nwid: network.nwid
+ };
+ if (network.name)
+ n.name = network.name.toString();
+ if ('private' in network)
+ n.private = (network.private) ? true : false;
+ if ('enableBroadcast' in network)
+ n.enableBroadcast = (network.enableBroadcast) ? true : false;
+ if ('allowPassiveBridging' in network)
+ n.allowPassiveBridging = (network.allowPassiveBridging) ? true : false;
+ if ('v4AssignMode' in network) {
+ if (network.v4AssignMode)
+ n.v4AssignMode = network.v4AssignMode.toString();
+ else n.v4AssignMode = 'none';
+ }
+ if ('v6AssignMode' in network) {
+ if (network.v6AssignMode)
+ n.v6AssignMode = network.v6AssignMode.toString();
+ else n.v4AssignMode = 'none';
+ }
+ if ('multicastLimit' in network) {
+ if (typeof network.multicastLimit === 'number')
+ n.multicastLimit = network.multicastLimit;
+ else n.multicastLimit = parseInt(network.multicastLimit.toString());
+ }
+ if (Array.isArray(network.relays))
+ n.relays = network.relays;
+ if (Array.isArray(network.ipAssignmentPools))
+ n.ipAssignmentPools = network.ipAssignmentPools;
+ if (Array.isArray(network.rules))
+ n.rules = network.rules;
+
+ request({
+ url: this.url + 'controller/network/' + n.nwid,
+ method: 'POST',
+ json: true,
+ body: n,
+ headers: {
+ 'X-ZT1-Auth': this.authToken
+ }
+ },function(err,response,body) {
+ if (err)
+ return callback(err,null);
+ if (response.statusCode !== 200)
+ return callback(new Error('server responded with error: '+response.statusCode),null);
+ return callback(null,(typeof body === 'string') ? JSON.parse(body) : body);
+ });
+};
+
exports.ZT1Client = ZT1Client;
diff --git a/nodejs-zt1-client/test-controller.js b/nodejs-zt1-client/test-controller.js
deleted file mode 100644
index e7c4846f..00000000
--- a/nodejs-zt1-client/test-controller.js
+++ /dev/null
@@ -1,14 +0,0 @@
-var ZT1Client = require('./index.js').ZT1Client;
-
-var zt1c = new ZT1Client('http://127.0.0.1:9993/','5d6181b71fae2684f9cc64ed');
-
-zt1c.status(function(err,status) {
- if (err)
- console.log(err);
- console.log(status);
- zt1c.networks(function(err,networks) {
- if (err)
- console.log(err);
- console.log(networks);
- });
-});
diff --git a/nodejs-zt1-client/test.js b/nodejs-zt1-client/test.js
new file mode 100644
index 00000000..347ca1a1
--- /dev/null
+++ b/nodejs-zt1-client/test.js
@@ -0,0 +1,33 @@
+var ZT1Client = require('./index.js').ZT1Client;
+
+var zt1c = new ZT1Client('http://127.0.0.1:9993/','5d6181b71fae2684f9cc64ed');
+
+zt1c.status(function(err,status) {
+ if (err)
+ console.log(err);
+ else console.log(status);
+
+ zt1c.getNetworks(function(err,networks) {
+ if (err)
+ console.log(err);
+ else console.log(networks);
+
+ zt1c.getPeers(function(err,peers) {
+ if (err)
+ console.log(err);
+ else console.log(peers);
+
+ if (status.controller) {
+ zt1c.saveControllerNetwork({
+ nwid: status.address + 'dead01',
+ name: 'test network',
+ private: true
+ },function(err,network) {
+ if (err)
+ console.log(err);
+ else console.log(network);
+ });
+ }
+ });
+ });
+});