From 6e8753fcc3e3a89422b8485d3e2d9d2c81ecf001 Mon Sep 17 00:00:00 2001 From: Enzo Hernan Nicolorich Date: Thu, 2 Jun 2016 17:01:44 -0300 Subject: [PATCH] Add Ip and Device traffic methods --- README.md | 28 +- lib/http-methods/index.js | 89 +++++ lib/packet.js | 411 +----------------------- lib/resources/device/index.js | 63 ++++ lib/resources/email/index.js | 37 +++ lib/resources/event/index.js | 16 + lib/resources/index.js | 20 ++ lib/resources/invitation/index.js | 30 ++ lib/resources/ip/index.js | 23 ++ lib/resources/location/index.js | 16 + lib/resources/membership/index.js | 30 ++ lib/resources/notification/index.js | 16 + lib/resources/operating-system/index.js | 16 + lib/resources/plan/index.js | 16 + lib/resources/project/index.js | 67 ++++ lib/resources/ssh-key/index.js | 37 +++ lib/resources/transfer/index.js | 30 ++ lib/resources/user/index.js | 23 ++ package.json | 3 + test/devicesSpec.js | 22 ++ test/ipSpec.js | 27 ++ test/projectsSpec.js | 11 + 22 files changed, 625 insertions(+), 406 deletions(-) create mode 100644 lib/http-methods/index.js create mode 100644 lib/resources/device/index.js create mode 100644 lib/resources/email/index.js create mode 100644 lib/resources/event/index.js create mode 100644 lib/resources/index.js create mode 100644 lib/resources/invitation/index.js create mode 100644 lib/resources/ip/index.js create mode 100644 lib/resources/location/index.js create mode 100644 lib/resources/membership/index.js create mode 100644 lib/resources/notification/index.js create mode 100644 lib/resources/operating-system/index.js create mode 100644 lib/resources/plan/index.js create mode 100644 lib/resources/project/index.js create mode 100644 lib/resources/ssh-key/index.js create mode 100644 lib/resources/transfer/index.js create mode 100644 lib/resources/user/index.js create mode 100644 test/ipSpec.js diff --git a/README.md b/README.md index 5d00965..e127596 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ api.setApiToken('YOUR NEW TOKEN'); * [Plans](#plans) * [Memberships](#memberships) * [Invitations](#invitations) +* [Ips](#Ips) * [Transfers](#transfer) * [Users](#user) * [Events](#events) @@ -60,6 +61,12 @@ To make the documentation simpler and easier to read I'll put here comments on g ### Invite To Project `api.inviteToProject(id, user, callback);` +### Request more IPs for a single project +`api.transferProject(id, request, callback);` + +### Get Project's IP Reservations +`api.getProjectIpReservations(id, parameters, callback);` + ### Devices #### Parameters `projectId` Project's ID. @@ -84,6 +91,12 @@ To make the documentation simpler and easier to read I'll put here comments on g ### Device Actions `api.deviceAction(id, action, callback);` +### Assign IP to Device +`api.assignIp(id, ip, callback);` + +### Device Traffic +`api.getDeviceTraffic(id, parameters, callback);` + ### Facilities #### Parameters `callback` Callback Function used to return the api's response. @@ -125,6 +138,15 @@ To make the documentation simpler and easier to read I'll put here comments on g #### Remove invitation `api.removeInvitation(id, callback);` +### Ips +#### Parameters +`id` Ip's ID. +`callback` Callback Function used to return the api's response. +#### Get a single Ip +`api.getIp(id, callback);` +#### Remove invitation +`api.removeIp(id, callback);` + ### Transfer #### Parameters `id` Membership's ID @@ -138,12 +160,10 @@ To make the documentation simpler and easier to read I'll put here comments on g ### User #### Parameters -`id` User's ID `user` User object `callback` Callback Function used to return the api's response. -#### Get Users -**Note:** Leaving *id* as false will return all the users available to the logged user, -`àpi.getUsers(id, callback);` +#### Get User +`api.getUser(callback);` #### Update current user `api.updateCurrentUser(user, callback);` diff --git a/lib/http-methods/index.js b/lib/http-methods/index.js new file mode 100644 index 0000000..61563db --- /dev/null +++ b/lib/http-methods/index.js @@ -0,0 +1,89 @@ +(function() { + 'use strict'; + module.exports = function(Packet) { + var request = require('request'); + Packet.prototype._get = function(url, parameters, callback) { + var self = this; + request( + { + method: 'GET', + url: self.buildUrl(url), + qs: parameters, + headers: self.getAuthHeaders(), + json:true + }, function(err, res, body) { + callback(handleRequestError(err, body), body || {}); + } + ); + }; + + Packet.prototype._post = function(url, postData, callback) { + var self = this; + request( + { + method: 'POST', + url: self.buildUrl(url), + body: postData, + headers: self.getAuthHeaders(), + json:true + }, function(err, res, body) { + callback(handleRequestError(err, body), body || {}); + } + ); + }; + + Packet.prototype._patch = function(url, postData, callback) { + var self = this; + request( + { + method: 'PATCH', + url: self.buildUrl(url), + body: postData, + headers: self.getAuthHeaders(), + json:true + }, function(err, res, body) { + callback(handleRequestError(err, body), body || {}); + } + ); + }; + + Packet.prototype._delete = function(url, callback) { + var self = this; + request( + { + method: 'DELETE', + url: self.buildUrl(url), + headers: self.getAuthHeaders(), + json:true + }, function(err, res, body) { + callback(handleRequestError(err, body), body || {}); + } + ); + }; + + Packet.prototype.getAuthHeaders = function() { + return { + 'X-Auth-Token': this.apiKey, + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }; + }; + + Packet.prototype.buildUrl = function(url) { + return this.currentEnvironment + url; + }; + + Packet.prototype.setApiToken = function(token) { + this.apiKey = token; + }; + + function handleRequestError(err, body) { + + if (body && (body.error || body.errors)) { + return new Error(body.errors || body.error); + } else { + return err; + } + } + }; +})(); diff --git a/lib/packet.js b/lib/packet.js index 3a81122..136f3e3 100644 --- a/lib/packet.js +++ b/lib/packet.js @@ -1,404 +1,11 @@ -'use strict'; +(function() { + 'use strict'; -var request = require('request'); -var config = require('./config/config.json'); -var Packet = function(apiKey) { - this.apiKey = apiKey; - this.currentEnvironment = config.apiUrl; -}; - -module.exports = Packet; - -Packet.prototype.getProjects = function(id, parameters, callback) { - var path = getProjectsUrl(id); - this._get(path, parameters, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.addProject = function(project, callback) { - var path = getProjectsUrl(); - this._post(path, project, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.updateProject = function(id, project, callback) { - var path = getProjectsUrl(id); - this._patch(path, project, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.removeProject = function(id, callback) { - var path = getProjectsUrl(id); - this._delete(path, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.inviteToProject = function(id, invitation, callback) { - var path = getProjectsUrl(id, 'invitations'); - this._post(path, invitation, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.transferProject = function(id, user, callback) { - var path = getProjectsUrl(id, 'transfers'); - this._post(path, user, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getDevices = function(projectId, id, parameters, callback) { - var path = getDevicesUrl(projectId, id); - this._get(path, parameters, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.addDevice = function(id, device, callback) { - var path = getDevicesUrl(id); - this._post(path, device, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.updateDevice = function(id, device, callback) { - var path = getDevicesUrl(false, id); - this._patch(path, device, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.removeDevice = function(id, callback) { - var path = getDevicesUrl(false, id); - this._delete(path, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.deviceAction = function(id, action, callback) { - var path = getDevicesUrl(false, id, 'actions'); - this._post(path, action, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getLocations = function(callback) { - var path = getFacilitiesUrl(); - this._get(path, {}, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getOperatingSystems = function(callback) { - var path = getOperatingSystemsUrl(); - this._get(path, {}, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getPlans = function(callback) { - var path = getPlansUrl(); - this._get(path, {}, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getMembership = function(id, callback) { - var path = getMembershipsUrl(id); - this._get(path, {}, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.updateMembership = function(id, membership, callback) { - var path = getMembershipsUrl(id); - this._patch(path, membership, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.removeMembership = function(id, callback) { - var path = getMembershipsUrl(id); - this._delete(path, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getInvitation = function(id, callback) { - var path = getInvitationsUrl(id); - this._get(path, {}, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.acceptInvitation = function(id, callback) { - var path = getInvitationsUrl(id); - this._patch(path, {}, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.removeInvitation = function(id, callback) { - var path = getInvitationsUrl(id); - this._delete(path, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getTransfer = function(id, callback) { - var path = getTransfersUrl(id); - this._get(path, {}, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.acceptTransfer = function(id, callback) { - var path = getTransfersUrl(id); - this._patch(path, {}, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.removeTransfer = function(id, callback) { - var path = getTransfersUrl(id); - this._delete(path, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getUsers = function(id, callback) { - var path = getUsersUrl(id); - this._get(path, {}, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.updateCurrentUser = function(user, callback) { - var path = getProfileUrl(); - this._patch(path, user, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getEvents = function(id, parameters, callback) { - var path = getEventsUrl(id); - this._get(path, parameters, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getNotifications = function(id, parameters, callback) { - var path = getNotificationsUrl(id); - this._get(path, parameters, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getSshkeys = function(id, callback) { - var path = getSshKeysUrl(id); - this._get(path, {}, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.addSshkey = function(sshkey, callback) { - var path = getSshKeysUrl(); - this._post(path, sshkey, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.updateSshkey = function(id, sshkey, callback) { - var path = getSshKeysUrl(id); - this._patch(path, sshkey, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.removeSshkey = function(id, callback) { - var path = getSshKeysUrl(id); - this._delete(path, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.getEmail = function(id, callback) { - var path = getEmailsUrl(id); - this._get(path, {}, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.addEmail = function(email, callback) { - var path = getEmailsUrl(); - this._post(path, email, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.updateEmail = function(id, email, callback) { - var path = getEmailsUrl(id); - this._patch(path, email, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype.removeEmail = function(id, callback) { - var path = getEmailsUrl(id); - this._delete(path, function(err, body) { - callback(err, body); - }); -}; - -Packet.prototype._get = function(url, parameters, callback) { - var self = this; - request( - { - method: 'GET', - url: self.buildUrl(url), - qs: parameters, - headers: self.getAuthHeaders(), - json:true - }, function(err, res, body) { - callback(handleRequestError(err, body), body || {}); - } - ); -}; - -Packet.prototype._post = function(url, postData, callback) { - var self = this; - request( - { - method: 'POST', - url: self.buildUrl(url), - body: postData, - headers: self.getAuthHeaders(), - json:true - }, function(err, res, body) { - callback(handleRequestError(err, body), body || {}); - } - ); -}; - -Packet.prototype._patch = function(url, postData, callback) { - var self = this; - request( - { - method: 'PATCH', - url: self.buildUrl(url), - body: postData, - headers: self.getAuthHeaders(), - json:true - }, function(err, res, body) { - callback(handleRequestError(err, body), body || {}); - } - ); -}; - -Packet.prototype._delete = function(url, callback) { - var self = this; - request( - { - method: 'DELETE', - url: self.buildUrl(url), - headers: self.getAuthHeaders(), - json:true - }, function(err, res, body) { - callback(handleRequestError(err, body), body || {}); - } - ); -}; - -Packet.prototype.getAuthHeaders = function() { - return { - 'X-Auth-Token': this.apiKey, - 'Content-Type': 'application/json', - 'Accept': 'application/json' + var config = require('./config/config.json'); + var Packet = function(apiKey) { + this.apiKey = apiKey; + this.currentEnvironment = config.apiUrl; }; -}; - -Packet.prototype.buildUrl = function(url) { - return this.currentEnvironment + url; -}; - -Packet.prototype.setApiToken = function(token) { - this.apiKey = token; -}; - -function handleRequestError(err, body) { - - if (body && (body.error || body.errors)) { - return new Error(body.errors || body.error); - } else { - return err; - } -} - -function getProjectsUrl(id, action) { - if (id) { - return '/projects/' + id + '/' + (action || ''); - } - return '/projects/'; -} - -function getDevicesUrl(projectId, id, action) { - if (id) { - return '/devices/' + (id + '/' || '') + (action || ''); - } - if (projectId) { - return '/projects/' + projectId + '/devices/'; - } - return false; -} - -function getFacilitiesUrl(id) { - return '/facilities/' + (id || ''); -} - -function getOperatingSystemsUrl(id) { - return '/operating-systems/' + (id || ''); -} - -function getPlansUrl(id) { - return '/plans/' + (id || ''); -} - -function getMembershipsUrl(id) { - return '/memberships/' + (id || ''); -} - -function getInvitationsUrl(id) { - return '/invitations/' + (id || ''); -} - -function getTransfersUrl(id) { - return '/transfers/' + (id || ''); -} - -function getUsersUrl(id) { - return '/users/' + (id || ''); -} - -function getProfileUrl(id) { - return '/user/' + (id || ''); -} - -function getSshKeysUrl(id) { - return '/ssh-keys/' + (id || ''); -} - -function getEventsUrl(id) { - return '/events/' + (id || ''); -} - -function getNotificationsUrl(id) { - return '/notifications/' + (id || ''); -} - -function getEmailsUrl(id) { - return '/emails/' + (id || ''); -} + require('./resources')(Packet); + module.exports = Packet; +})(); diff --git a/lib/resources/device/index.js b/lib/resources/device/index.js new file mode 100644 index 0000000..f826212 --- /dev/null +++ b/lib/resources/device/index.js @@ -0,0 +1,63 @@ +(function() { + 'use strict'; + module.exports = function(Packet) { + Packet.prototype.getDevices = function(projectId, id, parameters, callback) { + var path = getDevicesUrl(projectId, id); + this._get(path, parameters, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.addDevice = function(id, device, callback) { + var path = getDevicesUrl(id); + this._post(path, device, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.updateDevice = function(id, device, callback) { + var path = getDevicesUrl(false, id); + this._patch(path, device, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.removeDevice = function(id, callback) { + var path = getDevicesUrl(false, id); + this._delete(path, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.deviceAction = function(id, action, callback) { + var path = getDevicesUrl(false, id, 'actions'); + this._post(path, action, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.assignIp = function(id, ip, callback) { + var path = getDevicesUrl(false, id, 'ips'); + this._post(path, ip, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.getDeviceTraffic = function(id, parameters, callback) { + var path = getDevicesUrl(false, id, 'traffic'); + this._get(path, parameters, function(err, body) { + callback(err, body); + }); + }; + + function getDevicesUrl(projectId, id, resource) { + if (id) { + return '/devices/' + (id + '/' || '') + (resource || ''); + } + if (projectId) { + return '/projects/' + projectId + '/devices/'; + } + return false; + } + }; +})(); diff --git a/lib/resources/email/index.js b/lib/resources/email/index.js new file mode 100644 index 0000000..9e67db1 --- /dev/null +++ b/lib/resources/email/index.js @@ -0,0 +1,37 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getEmail = function(id, callback) { + var path = getEmailsUrl(id); + this._get(path, {}, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.addEmail = function(email, callback) { + var path = getEmailsUrl(); + this._post(path, email, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.updateEmail = function(id, email, callback) { + var path = getEmailsUrl(id); + this._patch(path, email, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.removeEmail = function(id, callback) { + var path = getEmailsUrl(id); + this._delete(path, function(err, body) { + callback(err, body); + }); + }; + + function getEmailsUrl(id) { + return '/emails/' + (id || ''); + } + }; +})(); diff --git a/lib/resources/event/index.js b/lib/resources/event/index.js new file mode 100644 index 0000000..bb0cf00 --- /dev/null +++ b/lib/resources/event/index.js @@ -0,0 +1,16 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getEvents = function(id, parameters, callback) { + var path = getEventsUrl(id); + this._get(path, parameters, function(err, body) { + callback(err, body); + }); + }; + + function getEventsUrl(id) { + return '/events/' + (id || ''); + } + }; +})(); diff --git a/lib/resources/index.js b/lib/resources/index.js new file mode 100644 index 0000000..1aad997 --- /dev/null +++ b/lib/resources/index.js @@ -0,0 +1,20 @@ +(function() { + 'use strict'; + module.exports = function(Packet) { + require('../http-methods')(Packet); + require('./device')(Packet); + require('./email')(Packet); + require('./event')(Packet); + require('./invitation')(Packet); + require('./ip')(Packet); + require('./location')(Packet); + require('./membership')(Packet); + require('./notification')(Packet); + require('./operating-system')(Packet); + require('./plan')(Packet); + require('./project')(Packet); + require('./ssh-key')(Packet); + require('./transfer')(Packet); + require('./user')(Packet); + }; +})(); diff --git a/lib/resources/invitation/index.js b/lib/resources/invitation/index.js new file mode 100644 index 0000000..cb53db8 --- /dev/null +++ b/lib/resources/invitation/index.js @@ -0,0 +1,30 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getInvitation = function(id, callback) { + var path = getInvitationsUrl(id); + this._get(path, {}, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.acceptInvitation = function(id, callback) { + var path = getInvitationsUrl(id); + this._patch(path, {}, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.removeInvitation = function(id, callback) { + var path = getInvitationsUrl(id); + this._delete(path, function(err, body) { + callback(err, body); + }); + }; + + function getInvitationsUrl(id) { + return '/invitations/' + (id || ''); + } + }; +})(); diff --git a/lib/resources/ip/index.js b/lib/resources/ip/index.js new file mode 100644 index 0000000..2a74b5f --- /dev/null +++ b/lib/resources/ip/index.js @@ -0,0 +1,23 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getIp = function(id, callback) { + var path = getInvitationsUrl(id); + this._get(path, {}, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.removeIp = function(id, callback) { + var path = getInvitationsUrl(id); + this._delete(path, function(err, body) { + callback(err, body); + }); + }; + + function getInvitationsUrl(id) { + return '/ips/' + (id || ''); + } + }; +})(); diff --git a/lib/resources/location/index.js b/lib/resources/location/index.js new file mode 100644 index 0000000..07b6a6f --- /dev/null +++ b/lib/resources/location/index.js @@ -0,0 +1,16 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getLocations = function(callback) { + var path = getFacilitiesUrl(); + this._get(path, {}, function(err, body) { + callback(err, body); + }); + }; + + function getFacilitiesUrl(id) { + return '/facilities/' + (id || ''); + } + }; +})(); diff --git a/lib/resources/membership/index.js b/lib/resources/membership/index.js new file mode 100644 index 0000000..1e0ddc0 --- /dev/null +++ b/lib/resources/membership/index.js @@ -0,0 +1,30 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getMembership = function(id, callback) { + var path = getMembershipsUrl(id); + this._get(path, {}, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.updateMembership = function(id, membership, callback) { + var path = getMembershipsUrl(id); + this._patch(path, membership, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.removeMembership = function(id, callback) { + var path = getMembershipsUrl(id); + this._delete(path, function(err, body) { + callback(err, body); + }); + }; + + function getMembershipsUrl(id) { + return '/memberships/' + (id || ''); + } + }; +})(); diff --git a/lib/resources/notification/index.js b/lib/resources/notification/index.js new file mode 100644 index 0000000..b0671e8 --- /dev/null +++ b/lib/resources/notification/index.js @@ -0,0 +1,16 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getNotifications = function(id, parameters, callback) { + var path = getNotificationsUrl(id); + this._get(path, parameters, function(err, body) { + callback(err, body); + }); + }; + + function getNotificationsUrl(id) { + return '/notifications/' + (id || ''); + } + }; +})(); diff --git a/lib/resources/operating-system/index.js b/lib/resources/operating-system/index.js new file mode 100644 index 0000000..ada6790 --- /dev/null +++ b/lib/resources/operating-system/index.js @@ -0,0 +1,16 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getOperatingSystems = function(callback) { + var path = getOperatingSystemsUrl(); + this._get(path, {}, function(err, body) { + callback(err, body); + }); + }; + + function getOperatingSystemsUrl(id) { + return '/operating-systems/' + (id || ''); + } + }; +})(); diff --git a/lib/resources/plan/index.js b/lib/resources/plan/index.js new file mode 100644 index 0000000..aadd794 --- /dev/null +++ b/lib/resources/plan/index.js @@ -0,0 +1,16 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getPlans = function(callback) { + var path = getPlansUrl(); + this._get(path, {}, function(err, body) { + callback(err, body); + }); + }; + + function getPlansUrl(id) { + return '/plans/' + (id || ''); + } + }; +})(); diff --git a/lib/resources/project/index.js b/lib/resources/project/index.js new file mode 100644 index 0000000..d7ffac5 --- /dev/null +++ b/lib/resources/project/index.js @@ -0,0 +1,67 @@ +(function() { + 'use strict'; + module.exports = function(Packet) { + Packet.prototype.getProjects = function(id, parameters, callback) { + var path = getProjectsUrl(id); + this._get(path, parameters, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.addProject = function(project, callback) { + var path = getProjectsUrl(); + this._post(path, project, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.updateProject = function(id, project, callback) { + var path = getProjectsUrl(id); + this._patch(path, project, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.removeProject = function(id, callback) { + var path = getProjectsUrl(id); + this._delete(path, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.inviteToProject = function(id, invitation, callback) { + var path = getProjectsUrl(id, 'invitations'); + this._post(path, invitation, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.transferProject = function(id, user, callback) { + var path = getProjectsUrl(id, 'transfers'); + this._post(path, user, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.getProjectIpReservations = function(id, parameters, callback) { + var path = getProjectsUrl(id, 'ips'); + this._get(path, parameters, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.requestIp = function(id, parameters, callback) { + var path = getProjectsUrl(id, 'ips'); + this._post(path, parameters, function(err, body) { + callback(err, body); + }); + }; + + function getProjectsUrl(id, resource) { + if (id) { + return '/projects/' + id + '/' + (resource || ''); + } + return '/projects/'; + } + }; +})(); diff --git a/lib/resources/ssh-key/index.js b/lib/resources/ssh-key/index.js new file mode 100644 index 0000000..186b9f5 --- /dev/null +++ b/lib/resources/ssh-key/index.js @@ -0,0 +1,37 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getSshkeys = function(id, callback) { + var path = getSshKeysUrl(id); + this._get(path, {}, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.addSshkey = function(sshkey, callback) { + var path = getSshKeysUrl(); + this._post(path, sshkey, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.updateSshkey = function(id, sshkey, callback) { + var path = getSshKeysUrl(id); + this._patch(path, sshkey, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.removeSshkey = function(id, callback) { + var path = getSshKeysUrl(id); + this._delete(path, function(err, body) { + callback(err, body); + }); + }; + + function getSshKeysUrl(id) { + return '/ssh-keys/' + (id || ''); + } + }; +})(); diff --git a/lib/resources/transfer/index.js b/lib/resources/transfer/index.js new file mode 100644 index 0000000..c2c3d29 --- /dev/null +++ b/lib/resources/transfer/index.js @@ -0,0 +1,30 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getTransfer = function(id, callback) { + var path = getTransfersUrl(id); + this._get(path, {}, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.acceptTransfer = function(id, callback) { + var path = getTransfersUrl(id); + this._patch(path, {}, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.removeTransfer = function(id, callback) { + var path = getTransfersUrl(id); + this._delete(path, function(err, body) { + callback(err, body); + }); + }; + + function getTransfersUrl(id) { + return '/transfers/' + (id || ''); + } + }; +})(); diff --git a/lib/resources/user/index.js b/lib/resources/user/index.js new file mode 100644 index 0000000..97214e5 --- /dev/null +++ b/lib/resources/user/index.js @@ -0,0 +1,23 @@ +(function() { + 'use strict'; + + module.exports = function(Packet) { + Packet.prototype.getUser = function(callback) { + var path = getProfileUrl(); + this._get(path, {}, function(err, body) { + callback(err, body); + }); + }; + + Packet.prototype.updateCurrentUser = function(user, callback) { + var path = getProfileUrl(); + this._patch(path, user, function(err, body) { + callback(err, body); + }); + }; + + function getProfileUrl(id) { + return '/user/' + (id || ''); + } + }; +})(); diff --git a/package.json b/package.json index a9caa9e..b981231 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,9 @@ ], "devDependencies": { "chai": "^3.0.0", + "grunt": "^1.0.1", + "grunt-contrib-jshint": "^1.0.0", + "grunt-mocha-test": "^0.12.7", "jscs": "^1.11.0", "jshint": "^2.8.0", "mocha": "^2.2.5", diff --git a/test/devicesSpec.js b/test/devicesSpec.js index 6739660..cab4f48 100644 --- a/test/devicesSpec.js +++ b/test/devicesSpec.js @@ -36,5 +36,27 @@ describe('Client Devices Methods', function() { mock.done(); }); }); + it('should get a single device', function(done) { + var mock = nock(apiConfig.apiUrl) + .intercept('/devices/' + deviceId + '/', 'GET') + .reply(200, {name:'device 1'}); + api.getDevices(false, deviceId, false, function(err, data) { + expect(err).to.equal(null); + expect(data).to.deep.equal({name:'device 1'}); + done(); + mock.done(); + }); + }); + it('should get a device traffic', function(done) { + var mock = nock(apiConfig.apiUrl) + .intercept('/devices/' + deviceId + '/traffic', 'GET') + .reply(200, {}); + api.getDeviceTraffic(deviceId, false, function(err, data) { + expect(err).to.equal(null); + expect(data).to.deep.equal({}); + done(); + mock.done(); + }); + }); }); }); diff --git a/test/ipSpec.js b/test/ipSpec.js new file mode 100644 index 0000000..99f70f4 --- /dev/null +++ b/test/ipSpec.js @@ -0,0 +1,27 @@ +'use strict'; + +var nock = require('nock'); +var Packet = new require('../lib/packet'); +var uuid = require('node-uuid'); +var apiConfig = require('../lib/config/config.json'); +var expect = require('chai').expect; +var id; +var api = new Packet(''); +describe('Client IP Methods', function() { + beforeEach(function() { + id = uuid.v4(); + }); + describe('getIPs', function() { + it('should get a single IP/Reservation', function(done) { + var mock = nock(apiConfig.apiUrl) + .intercept('/ips/' + id, 'GET') + .reply(200, {}); + api.getIp(id, function(err, data) { + expect(err).to.equal(null); + expect(data).to.deep.equal({}); + done(); + mock.done(); + }); + }); + }); +}); diff --git a/test/projectsSpec.js b/test/projectsSpec.js index bbc4407..949bff6 100644 --- a/test/projectsSpec.js +++ b/test/projectsSpec.js @@ -34,5 +34,16 @@ describe('Client Projects Methods', function() { mock.done(); }); }); + it('should get ips of single project', function(done) { + var mock = nock(apiConfig.apiUrl) + .intercept('/projects/' + projectId + '/ips', 'GET') + .reply(200, {ips:[]}); + api.getProjectIpReservations(projectId, false, function(err, data) { + expect(err).to.equal(null); + expect(data).to.deep.equal({ips:[]}); + done(); + mock.done(); + }); + }); }); });