summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--controller/zt1-controller-client/index.js64
-rw-r--r--controller/zt1-controller-client/test-controller.js14
-rw-r--r--nodejs-zt1-client/index.js71
-rw-r--r--nodejs-zt1-client/package.json (renamed from controller/zt1-controller-client/package.json)4
-rw-r--r--nodejs-zt1-client/test-controller.js14
5 files changed, 87 insertions, 80 deletions
diff --git a/controller/zt1-controller-client/index.js b/controller/zt1-controller-client/index.js
deleted file mode 100644
index b262ae8b..00000000
--- a/controller/zt1-controller-client/index.js
+++ /dev/null
@@ -1,64 +0,0 @@
-'use strict'
-
-var request = require('request');
-
-function ZT1ControllerClient(url,authToken)
-{
- this.url = url;
- this.authToken = authToken;
-}
-
-ZT1ControllerClient.prototype.status = function(callback)
-{
- request({
- url: this.url + 'controller',
- method: 'GET',
- headers: {
- 'X-ZT1-Auth': this.authToken
- }
- },function(error,response,body) {
- if (error)
- return callback(error,{});
- if (response.statusCode !== 200)
- return callback(new Error('server responded with '+response.statusCode),{});
- var controllerStatus = JSON.parse(body);
- if (controllerStatus.controller === true) {
- request({
- url: this.url + 'status',
- method: 'GET',
- headers: {
- 'X-ZT1-Auth': this.authToken
- }
- },function(error,response,body) {
- if (error)
- return callback(error,{});
- if (response.statusCode !== 200)
- return callback(new Error('server responded with '+response.statusCode),{});
- var nodeStatus = JSON.parse(body);
- for(var k in controllerStatus)
- nodeStatus[k] = controllerStatus[k];
- return callback(null,nodeStatus);
- }.bind(this));
- } else return callback(new Error('No "controller==true" test value present.'),{});
- }.bind(this));
-};
-
-ZT1ControllerClient.prototype.listNetworks = function(callback)
-{
- request({
- url: this.url + 'controller/network',
- method: 'GET',
- headers: {
- 'X-ZT1-Auth': this.authToken
- }
- },function(error,response,body) {
- if (error)
- return callback(error,{});
- if (response.statusCode !== 200)
- return callback(new Error('server responded with '+response.statusCode),{});
- var r = JSON.parse(body);
- return callback(null,Array.isArray(r) ? r : []);
- });
-};
-
-exports.ZT1ControllerClient = ZT1ControllerClient;
diff --git a/controller/zt1-controller-client/test-controller.js b/controller/zt1-controller-client/test-controller.js
deleted file mode 100644
index cddff031..00000000
--- a/controller/zt1-controller-client/test-controller.js
+++ /dev/null
@@ -1,14 +0,0 @@
-var ZT1ControllerClient = require('./index.js').ZT1ControllerClient;
-
-var zt1c = new ZT1ControllerClient('http://127.0.0.1:9993/','5d6181b71fae2684f9cc64ed');
-
-zt1c.status(function(err,status) {
- if (err)
- console.log(err);
- console.log(status);
- zt1c.listNetworks(function(err,networks) {
- if (err)
- console.log(err);
- console.log(networks);
- });
-});
diff --git a/nodejs-zt1-client/index.js b/nodejs-zt1-client/index.js
new file mode 100644
index 00000000..e44fe795
--- /dev/null
+++ b/nodejs-zt1-client/index.js
@@ -0,0 +1,71 @@
+'use strict'
+
+var request = require('request');
+
+function ZT1Client(url,authToken)
+{
+ this.url = url;
+ this.authToken = authToken;
+}
+
+ZT1Client.prototype._jsonGet = function(getPath,callback)
+{
+ request({
+ url: this.url + getPath,
+ method: 'GET',
+ headers: {
+ 'X-ZT1-Auth': this.authToken
+ }
+ },function(error,response,body) {
+ if (error)
+ return callback(error,{});
+ if (response.statusCode !== 200)
+ return callback(new Error('server responded with '+response.statusCode),{});
+ return callback(null,(typeof body === 'string') ? JSON.parse(body) : null);
+ });
+};
+
+ZT1Client.prototype.status = function(callback)
+{
+ request({
+ url: this.url + 'controller',
+ method: 'GET',
+ headers: {
+ 'X-ZT1-Auth': this.authToken
+ }
+ },function(error,response,body) {
+ if (error)
+ return callback(error,{});
+ var controllerStatus = {};
+ if (typeof body === 'string')
+ controllerStatus = JSON.parse(body);
+ request({
+ url: this.url + 'status',
+ method: 'GET',
+ headers: {
+ 'X-ZT1-Auth': this.authToken
+ }
+ },function(error,response,body) {
+ if (error)
+ return callback(error,{});
+ if (response.statusCode !== 200)
+ return callback(new Error('server responded with '+response.statusCode),{});
+ var nodeStatus = JSON.parse(body);
+ for(var k in controllerStatus)
+ nodeStatus[k] = controllerStatus[k];
+ return callback(null,nodeStatus);
+ }.bind(this));
+ }.bind(this));
+};
+
+ZT1Client.prototype.networks = function(callback)
+{
+ this._jsonGet('network',callback);
+};
+
+ZT1Client.prototype.controllerNetworks = function(callback)
+{
+ this._jsonGet('controller/network',callback);
+};
+
+exports.ZT1Client = ZT1Client;
diff --git a/controller/zt1-controller-client/package.json b/nodejs-zt1-client/package.json
index 8eda13ad..c840d8ae 100644
--- a/controller/zt1-controller-client/package.json
+++ b/nodejs-zt1-client/package.json
@@ -1,7 +1,7 @@
{
- "name": "zt1-controller-client",
+ "name": "nodejs-zt1-client",
"version": "1.0.0",
- "description": "ZeroTier One network controller client for NodeJS",
+ "description": "ZeroTier One Network Virtualization Service JSON API Client",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
diff --git a/nodejs-zt1-client/test-controller.js b/nodejs-zt1-client/test-controller.js
new file mode 100644
index 00000000..e7c4846f
--- /dev/null
+++ b/nodejs-zt1-client/test-controller.js
@@ -0,0 +1,14 @@
+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);
+ });
+});