mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-07-16 10:03:14 -07:00
Basic controller JSON API seems to be working.
This commit is contained in:
parent
cf51961d52
commit
69ceb7e730
5 changed files with 397 additions and 292 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue