├── .idea
├── .name
├── dropletapi.iml
├── encodings.xml
├── misc.xml
├── modules.xml
├── scopes
│ └── scope_settings.xml
├── vcs.xml
└── workspace.xml
├── README.md
├── index.js
├── lib
├── digitalocean.account.js
├── digitalocean.actions.js
├── digitalocean.domains.js
└── digitalocean.droplets.js
└── package.json
/.idea/.name:
--------------------------------------------------------------------------------
1 | dropletapi
--------------------------------------------------------------------------------
/.idea/dropletapi.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/scopes/scope_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
55 |
56 |
57 | true
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 | true
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 | 1423739313851
170 |
171 | 1423739313851
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | DropletApi
2 | =======
3 |
4 | [](https://nodei.co/npm/dropletapi/)
5 |
6 | (C) Oliwer Helsén (oliwer.helsen@live.com) 2015
7 |
8 | A wrapper for DigitalOceans API v2
9 |
10 | See [Version History](https://github.com/oliwerhelsen/DigitalOcean-API-v2/wiki/Version-History) for changes
11 |
12 | Installing
13 | ----------
14 |
15 | ```
16 | npm install dropletapi
17 | npm install dropletapi --save
18 | ```
19 |
20 | Features
21 | --------
22 |
23 | -- DROPLETS
24 | * Create new Droplet
25 | * Retrieve an existing Droplet by id
26 | * List all Droplets in your account
27 | * Delete a Droplet by id
28 | * Retrieve a list of all kernels available to a Dropet
29 | * Retrieve snapshots for a Droplet
30 | * Retrieve backups for a Droplet
31 | * Retrieve actions for a Droplet
32 |
33 | -- Actions
34 | * List all Actions
35 | * Retrieve an existing Action
36 |
37 | -- Domains
38 | * List all Domains
39 | * Create a new Domain
40 |
41 | -- Account
42 | * Get User Information
43 |
44 | API
45 | ---
46 |
47 | ### createDroplet(dropletData, callback)
48 |
49 | Create a new Droplet
50 |
51 | Example usage
52 | -------------
53 |
54 | ```javascript
55 | var DIGITALOCEAN = require('dropletapi').Droplets;
56 |
57 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
58 |
59 | var myNewDropletData = {
60 | "name": "example.com",
61 | "region": "nyc3",
62 | "size": "512mb",
63 | "image": "ubuntu-14-04-x64",
64 | "ssh_keys": null,
65 | "backups": false,
66 | "ipv6": true,
67 | "user_data": null,
68 | "private_networking": null
69 | }
70 |
71 | digitalocean.createDroplet(myNewDropletData, function (error, result) {
72 | if (error) {
73 | console.log(error);
74 | }
75 | else {
76 | console.log(result);
77 | }
78 | });
79 |
80 | ```
81 |
82 | ### getDropletById(dropletID, callback)
83 |
84 | Retrieve an existing Droplet by id
85 |
86 | Example usage
87 | -------------
88 |
89 | ```javascript
90 | var DIGITALOCEAN = require('dropletapi').Droplets;
91 |
92 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
93 |
94 | digitalocean.getDropletById(PUT THE DROPLETID HERE, function (error, result) {
95 | if (error) {
96 | console.log(error);
97 | }
98 | else {
99 | console.log(result);
100 | }
101 | });
102 |
103 | ```
104 |
105 | ### listDroplets(callback)
106 |
107 | List all Droplets in your account
108 |
109 | Example usage
110 | -------------
111 |
112 | ```javascript
113 | var DIGITALOCEAN = require('dropletapi').Droplets;
114 |
115 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
116 |
117 | digitalocean.listDroplets(function (error, result) {
118 | if (error) {
119 | console.log(error);
120 | }
121 | else {
122 | console.log(result);
123 | }
124 | });
125 |
126 | ```
127 |
128 | ### deleteDroplet(dropletID, callback)
129 |
130 | Delete a Droplet by id
131 |
132 | Example usage
133 | -------------
134 |
135 | ```javascript
136 | var DIGITALOCEAN = require('dropletapi').Droplets;
137 |
138 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
139 |
140 | digitalocean.deleteDroplet(PUT THE DROPLETID HERE, function (error, result) {
141 | if (error) {
142 | console.log(error);
143 | }
144 | else {
145 | console.log(result);
146 | }
147 | });
148 |
149 | ```
150 |
151 | ### availableKernelsForDroplet(dropletID, callback)
152 |
153 | Retrieve a list of all kernels available to a Dropet
154 |
155 | Example usage
156 | -------------
157 |
158 | ```javascript
159 | var DIGITALOCEAN = require('dropletapi').Droplets;
160 |
161 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
162 |
163 | digitalocean.availableKernelsForDroplet(PUT THE DROPLETID HERE, function (error, result) {
164 | if (error) {
165 | console.log(error);
166 | }
167 | else {
168 | console.log(result);
169 | }
170 | });
171 |
172 | ```
173 |
174 | ### getSnapshotsForDroplet(dropletID, callback)
175 |
176 | Retrieve the snapshots that have been created from a Droplet
177 |
178 | Example usage
179 | -------------
180 |
181 | ```javascript
182 | var DIGITALOCEAN = require('dropletapi').Droplets;
183 |
184 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
185 |
186 | digitalocean.getSnapshotsForDroplet(PUT THE DROPLETID HERE, function (error, result) {
187 | if (error) {
188 | console.log(error);
189 | }
190 | else {
191 | console.log(result);
192 | }
193 | });
194 |
195 | ```
196 |
197 | ### getBackupsForDroplet(dropletID, callback)
198 |
199 | Retrieve any backups associated with a Droplet
200 |
201 | Example usage
202 | -------------
203 |
204 | ```javascript
205 | var DIGITALOCEAN = require('dropletapi').Droplets;
206 |
207 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
208 |
209 | digitalocean.getBackupsForDroplet(PUT THE DROPLETID HERE, function (error, result) {
210 | if (error) {
211 | console.log(error);
212 | }
213 | else {
214 | console.log(result);
215 | }
216 | });
217 |
218 | ```
219 |
220 | ### getActionsForDroplet(dropletID, callback)
221 |
222 | Retrieve all actions that have been executed on a Droplet
223 |
224 | Example usage
225 | -------------
226 |
227 | ```javascript
228 | var DIGITALOCEAN = require('dropletapi').Droplets;
229 |
230 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
231 |
232 | digitalocean.getActionsForDroplet(PUT THE DROPLETID HERE, function (error, result) {
233 | if (error) {
234 | console.log(error);
235 | }
236 | else {
237 | console.log(result);
238 | }
239 | });
240 |
241 | ```
242 |
243 | ### listDropletUpgrades(callback)
244 |
245 | Retrieve a list of droplets that are scheduled to be upgraded
246 |
247 | Example usage
248 | -------------
249 |
250 | ```javascript
251 | var DIGITALOCEAN = require('dropletapi').Droplets;
252 |
253 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
254 |
255 | digitalocean.listDropletUpgrades(function (error, result) {
256 | if (error) {
257 | console.log(error);
258 | }
259 | else {
260 | console.log(result);
261 | }
262 | });
263 |
264 | ```
265 | ### listAllActions(pageData, callback)
266 |
267 | List all of the actions that have been executed on the current account
268 |
269 | Example usage
270 | -------------
271 |
272 | ```javascript
273 | var DIGITALOCEAN = require('dropletapi').Actions;
274 |
275 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
276 |
277 | digitalocean.listAllActions({page: 1, per_page:1},function (error, result) {
278 | if (error) {
279 | console.log(error);
280 | }
281 | else {
282 | console.log(result);
283 | }
284 | });
285 |
286 | ```
287 |
288 | ### listAllActions(pageData, callback)
289 |
290 | List all of the actions that have been executed on the current account
291 |
292 | Example usage
293 | -------------
294 |
295 | ```javascript
296 | var DIGITALOCEAN = require('dropletapi').Actions;
297 |
298 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
299 |
300 | digitalocean.getActionById(YOUR ACTIONS ID, function (error, result) {
301 | if (error) {
302 | console.log(error);
303 | }
304 | else {
305 | console.log(result);
306 | }
307 | });
308 |
309 | ```
310 | ### listDomains(callback)
311 |
312 | List all of the domains in your account
313 |
314 | Example usage
315 | -------------
316 |
317 | ```javascript
318 | var DIGITALOCEAN = require('dropletapi').Domains;
319 |
320 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
321 |
322 | digitalocean.listDomains(function (error, result) {
323 | if (error) {
324 | console.log(error);
325 | }
326 | else {
327 | console.log(result);
328 | }
329 | });
330 |
331 | ```
332 | ### createDomain(data, callback)
333 |
334 | Create a new domain for your Droplet
335 |
336 | Example usage
337 | -------------
338 |
339 | ```javascript
340 | var DIGITALOCEAN = require('dropletapi').Domains;
341 |
342 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
343 |
344 | var data = {
345 | "name": "digitaloceanisthebombdiggity.com",
346 | "ip_address": "1.2.3.4"
347 | };
348 |
349 | digitalocean.createDomain(data, function (error, result) {
350 | if (error) {
351 | console.log(error);
352 | }
353 | else {
354 | console.log(result);
355 | }
356 | });
357 |
358 | ```
359 | ### getDomainByName(domainName, callback)
360 |
361 | Create a new domain for your Droplet
362 |
363 | Example usage
364 | -------------
365 |
366 | ```javascript
367 | var DIGITALOCEAN = require('dropletapi').Domains;
368 |
369 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
370 |
371 | var domainName = 'digitaloceanisthebombdiggity.com';
372 |
373 | digitalocean.getDomainByName(domainName, function (error, result) {
374 | if (error) {
375 | console.log(error);
376 | }
377 | else {
378 | console.log(result);
379 | }
380 | });
381 |
382 | ```
383 | ### deleteDomain(domainName, callback)
384 |
385 | Create a new domain for your Droplet
386 |
387 | Example usage
388 | -------------
389 |
390 | ```javascript
391 | var DIGITALOCEAN = require('dropletapi').Domains;
392 |
393 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
394 |
395 | var domainName = 'digitaloceanisthebombdiggity.com';
396 |
397 | digitalocean.deleteDomain(domainName, function (error, result) {
398 | if (error) {
399 | console.log(error);
400 | }
401 | else {
402 | console.log(result);
403 | }
404 | });
405 |
406 | ```
407 | ### getUserInfo(callback)
408 |
409 | Get user information
410 |
411 | Example usage
412 | -------------
413 |
414 | ```javascript
415 | var DIGITALOCEAN = require('dropletapi').Domains;
416 |
417 | var digitalocean = new DIGITALOCEAN('Your API-TOKEN');
418 |
419 | digitalocean.getUserInfo(function (error, result) {
420 | if (error) {
421 | console.log(error);
422 | }
423 | else {
424 | console.log(result);
425 | }
426 | });
427 |
428 | ```
429 |
430 | TODO
431 | ----
432 | * Keep on adding more actions from the DigitalOcean API v2
433 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | /** Module dependencies **/
4 |
5 | var _ = require('lodash');
6 |
7 | /** Extend the DropletAPi **/
8 | module.exports = _.extend(
9 | require('./lib/digitalocean.droplets'),
10 | require('./lib/digitalocean.actions'),
11 | require('./lib/digitalocean.domains')
12 | );
13 |
--------------------------------------------------------------------------------
/lib/digitalocean.account.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * DigitalOcean API V3
3 | */
4 |
5 | /**
6 | * Module dependencies.
7 | */
8 |
9 | var rest = require('restler');
10 |
11 | var Account = function(token) {
12 | this.baseUri = "https://api.digitalocean.com/v2/";
13 | this.token = token;
14 | };
15 |
16 | Account.prototype.createQuery = function() {
17 | return {};
18 | };
19 |
20 | function makeRequest(fn, uri, options, callback) {
21 | fn(uri, options)
22 | .on('complete', function(result) {
23 | if(result instanceof Error) {
24 | callback(result);
25 | } else {
26 | callback(null, result);
27 | }
28 | });
29 | }
30 |
31 | /**
32 | * Get User Information
33 | *
34 | * @response Object containing the standard attributes for your account
35 | *
36 | * Api documentation: https://developers.digitalocean.com/#get-user-information
37 | */
38 |
39 | Account.prototype.getUserInfo = function(callback) {
40 |
41 | makeRequest(rest.get, this.baseUri + 'account', {query: headers: {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + this.token}}, callback);
42 | };
43 |
44 | module.exports = Account;
45 |
--------------------------------------------------------------------------------
/lib/digitalocean.actions.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * DigitalOcean API V3
3 | */
4 |
5 |
6 | /**
7 | Actions
8 |
9 | Actions are records of events that have occurred on the resources in your account.
10 | These can be things like rebooting a Droplet, or transferring an image to a new region.
11 |
12 | An action object is created every time one of these actions is initiated.
13 | The action object contains information about the current status of the action, start and complete timestamps, and the associated resource type and ID.
14 |
15 | Every action that creates an action object is available through this endpoint.
16 | Completed actions are not removed from this list and are always available for querying.
17 |
18 | **/
19 |
20 | /**
21 | * Module dependencies.
22 | */
23 |
24 | var rest = require('restler');
25 |
26 | var Actions = function(token) {
27 | this.baseUri = "https://api.digitalocean.com/v2/actions/";
28 | this.token = token;
29 | };
30 |
31 | function makeRequest(fn, uri, options, callback) {
32 | fn(uri, options)
33 | .on('complete', function(result) {
34 | if(result instanceof Error) {
35 | callback(result);
36 | } else {
37 | callback(null, result);
38 | }
39 | });
40 | }
41 |
42 | /**
43 | * List all Actions
44 | *
45 | * To list all of the actions that have been executed on the current account
46 | *
47 | * This will be the entire list of actions taken on your account, so it will be quite large.
48 | * As with any large collection returned by the API, the results will be paginated with only 25 on each page by default.
49 | *
50 | * @response The results will be returned as a JSON object with an actions key.
51 | * This will be set to an array filled with action objects containing the standard action attributes:
52 | *
53 | * Api documentation: https://developers.digitalocean.com/#list-all-action
54 | */
55 |
56 | Actions.prototype.listAllActions = function(pageData, callback) {
57 | if(!pageData) {
58 | pageData = {
59 | page: 1,
60 | per_page:1
61 | };
62 | } else {
63 | pageData = pageData;
64 | }
65 | makeRequest(rest.get, this.baseUri, {query: pageData, headers: {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + this.token}}, callback);
66 | };
67 |
68 | /**
69 | * Retrieve an existing Action
70 | *
71 | * Retrieve a specific action object
72 | *
73 | * @response The result will be a JSON object with an action key.
74 | * This will be set to an action object containing the standard action attributes
75 | *
76 | * Api documentation: https://developers.digitalocean.com/#retrieve-an-existing-action
77 | */
78 |
79 | Actions.prototype.getActionById = function(actionId, callback) {
80 | makeRequest(rest.get, this.baseUri + actionId, {headers: {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + this.token}}, callback);
81 | };
82 |
83 |
84 | exports.Actions = Actions;
--------------------------------------------------------------------------------
/lib/digitalocean.domains.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * DigitalOcean API V3
3 | *
4 | * Domains
5 | * Domain resources are domain names that you have purchased from a domain name registrar
6 | * that you are managing through the DigitalOcean DNS interface.
7 | *
8 | * This resource establishes top-level control over each domain.
9 | * Actions that affect individual domain records should be taken on the [Domain Records] resource.
10 | *
11 | */
12 |
13 | /**
14 | * Module dependencies.
15 | */
16 |
17 | var rest = require('restler');
18 |
19 | var Domains = function(token) {
20 | this.baseUri = "https://api.digitalocean.com/v2/domains/";
21 | this.token = token;
22 | };
23 |
24 | function makeRequest(fn, uri, options, callback) {
25 | fn(uri, options)
26 | .on('complete', function(result) {
27 | if (result instanceof Error) {
28 | callback(result);
29 | } else {
30 | callback(null, result);
31 | }
32 | });
33 | }
34 |
35 | function makeRequestJson(fn, uri, data, options, callback) {
36 | fn(uri, data, options)
37 | .on('complete', function(result) {
38 | if (result instanceof Error) {
39 | callback(result);
40 | } else {
41 | callback(null, result);
42 | }
43 | });
44 | }
45 |
46 | /**
47 | * Create a new Domain
48 | *
49 | * @param name {String} [Required]
50 | * @param ip_address {String} [Required]
51 | *
52 | * @response
53 | * The response will be a JSON object with a key called domain.
54 | * The value of this will be an object that contains the standard attributes associated with a domain
55 | *
56 | * Api documentation: https://developers.digitalocean.com/#create-a-new-domain
57 | */
58 |
59 | Domains.prototype.createDomain = function(data, callback) {
60 | makeRequestJson(
61 | rest.postJson, this.baseUri, data, {
62 | headers: {
63 | 'Content-Type': 'application/json',
64 | 'Authorization': 'Bearer ' + this.token
65 | }
66 | },
67 | callback
68 | );
69 | };
70 |
71 | /**
72 | * Retrieve an existing Domain by name
73 | *
74 | * @param domainName {string} [Required]
75 | *
76 | * @response: Will be a JSON object with a key called domain.
77 | * The value of this will be an object that contains the standard attributes defined for a domain
78 | *
79 | * Api documentation: https://developers.digitalocean.com/#retrieve-an-existing-domain
80 | */
81 |
82 | Domains.prototype.getDomainByName = function(domainName, callback) {
83 | makeRequest(rest.get, this.baseUri + domainName, {
84 | headers: {
85 | 'Content-Type': 'application/json',
86 | 'Authorization': 'Bearer ' + this.token
87 | }
88 | }, callback);
89 | };
90 |
91 | /**
92 | * List all Domains
93 | *
94 | * @Reponse:
95 | * Will be a JSON object with a key called domains.
96 | * The value of this will be an array of Domain objects, each of which contain the standard domain attributes
97 | *
98 | * Api documentation: https://developers.digitalocean.com/#list-all-domains
99 | */
100 |
101 | Domains.prototype.listDomains = function(callback) {
102 | makeRequest(rest.get, this.baseUri, {
103 | headers: {
104 | 'Content-Type': 'application/json',
105 | 'Authorization': 'Bearer ' + this.token
106 | }
107 | }, callback);
108 | };
109 |
110 | /**
111 | * Delete a Domain
112 | *
113 | * @reponse:
114 | * The domain will be removed from your account and a response status of 204 will be returned.
115 | * This indicates a successful request with no response body.
116 | *
117 | * Api documentation: https://developers.digitalocean.com/#delete-a-domain
118 | */
119 |
120 | Domains.prototype.deleteDomain = function(domainName, callback) {
121 | makeRequest(rest.del, this.baseUri + domainName, {
122 | headers: {
123 | 'Content-Type': 'application/json',
124 | 'Authorization': 'Bearer ' + this.token
125 | }
126 | }, callback);
127 | };
128 |
129 |
130 | exports.Domains = Domains;
131 |
--------------------------------------------------------------------------------
/lib/digitalocean.droplets.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * DigitalOcean API V3
3 | */
4 |
5 | /**
6 | * Module dependencies.
7 | */
8 |
9 | var rest = require('restler');
10 |
11 | var Droplets = function(token) {
12 | this.baseUri = "https://api.digitalocean.com/v2/droplets/";
13 | this.token = token;
14 | };
15 |
16 | Droplets.prototype.createQuery = function() {
17 | return {};
18 | };
19 |
20 | function makeRequest(fn, uri, options, callback) {
21 | var handler = function(result) {
22 | if (result instanceof Error) {
23 | callback(result);
24 | } else {
25 | callback(null, result);
26 | }
27 |
28 | this.removeListener('complete', handler);
29 | };
30 |
31 | fn(uri, options)
32 | .on('complete', handler);
33 | }
34 |
35 | function makeRequestJson(fn, uri, data, options, callback) {
36 | fn(uri, data, options)
37 | .on('complete', function(result) {
38 | if (result instanceof Error) {
39 | callback(result);
40 | } else {
41 | callback(null, result);
42 | }
43 | });
44 | }
45 |
46 | /**
47 | * Create a new Droplet
48 | *
49 | * @param name {String} [Required]
50 | * @param region {String} [Required]
51 | * @param size {String} [Required]
52 | * @param image {Number} [Required]
53 | * @param ssh_keys {Array} [Optional]
54 | * @param backups {Boolean} [Optional]
55 | * @param ipv6 {Boolean} [Optional]
56 | * @param private_networking {Boolean} [Optional]
57 | * @param user_data {String} [Optional]
58 | *
59 | * @response Object containing the standard attributes for your new Droplet
60 | *
61 | * Api documentation: https://developers.digitalocean.com/v2/#create-a-new-droplet
62 | */
63 |
64 | Droplets.prototype.createDroplet = function(data, callback) {
65 | makeRequestJson(
66 | rest.postJson, this.baseUri, data, {
67 | headers: {
68 | 'Content-Type': 'application/json',
69 | 'Authorization': 'Bearer ' + this.token
70 | }
71 | },
72 | callback
73 | );
74 | };
75 |
76 | /**
77 | * Retrieve an existing Droplet by id
78 | *
79 | * @param id {number} [Required]
80 | *
81 | * @The response will be a JSON object with a key called droplet. This will be set to a JSON object that contains the Droplet's attributes:
82 | *
83 | * Api documentation: https://developers.digitalocean.com/v2/#retrieve-an-existing-droplet-by-id
84 | */
85 |
86 | Droplets.prototype.getDropletById = function(dropletID, callback) {
87 | makeRequest(rest.get, this.baseUri + dropletID, {
88 | headers: {
89 | 'Content-Type': 'application/json',
90 | 'Authorization': 'Bearer ' + this.token
91 | }
92 | }, callback);
93 | };
94 |
95 | /**
96 | * List all Droplets in your account
97 | *
98 | * @The response body will be a JSON object with a key of droplets. This will be set to an array containing objects representing each Droplet.
99 | *
100 | * Api documentation: https://developers.digitalocean.com/v2/#list-all-droplets
101 | */
102 |
103 | Droplets.prototype.listDroplets = function(callback) {
104 | makeRequest(rest.get, this.baseUri, {
105 | headers: {
106 | 'Content-Type': 'application/json',
107 | 'Authorization': 'Bearer ' + this.token
108 | }
109 | }, callback);
110 | };
111 |
112 | /**
113 | * Delete a Droplet
114 | *
115 | * @No response body will be sent back, but the response code will indicate success.
116 | * Specifically, the response code will be a 204, which means that the action was successful with no returned body data.
117 | *
118 | * Api documentation: https://developers.digitalocean.com/#delete-a-droplet
119 | */
120 |
121 | Droplets.prototype.deleteDroplet = function(dropletID, callback) {
122 | makeRequest(rest.del, this.baseUri + dropletID, {
123 | headers: {
124 | 'Content-Type': 'application/json',
125 | 'Authorization': 'Bearer ' + this.token
126 | }
127 | }, callback);
128 | };
129 |
130 | /*
131 | List all available Kernels for a Droplet
132 |
133 | Retrieve a list of all kernels available to a Dropet
134 |
135 | @param id {number} [Required]
136 |
137 | @ The response will be a JSON object that has a key called kernels.
138 | This will be set to an array of kernel objects, each of which contain the standard kernel attributes
139 |
140 | API documentation: https://developers.digitalocean.com/#list-all-available-kernels-for-a-droplet
141 | */
142 | Droplets.prototype.availableKernelsForDroplet = function(dropletID, callback) {
143 | makeRequest(rest.get, this.baseUri + dropletID + '/kernels', {
144 | headers: {
145 | 'Content-Type': 'application/json',
146 | 'Authorization': 'Bearer ' + this.token
147 | }
148 | }, callback);
149 | };
150 |
151 | /*
152 | Retrieve snapshots for a Droplet
153 |
154 | Retrieve the snapshots that have been created from a Droplet
155 |
156 | @param id {number} [Required]
157 |
158 | @ You will get back a JSON object that has a snapshots key.
159 | This will be set to an array of snapshot objects, each of which contain the standard image attributes
160 |
161 | API documentation: https://developers.digitalocean.com/#retrieve-snapshots-for-a-droplet
162 | */
163 |
164 | Droplets.prototype.getSnapshotsForDroplet = function(dropletID, callback) {
165 | makeRequest(rest.get, this.baseUri + dropletID + '/snapshots', {
166 | headers: {
167 | 'Content-Type': 'application/json',
168 | 'Authorization': 'Bearer ' + this.token
169 | }
170 | }, callback);
171 | };
172 |
173 | /*
174 | Retrieve backups for a Droplet
175 |
176 | Retrieve any backups associated with a Droplet
177 |
178 | @param id {number} [Required]
179 |
180 | @ You will get back a JSON object that has a backups key.
181 | This will be set to an array of backup objects, each of which contain the standard image attributes
182 |
183 | API documentation: https://developers.digitalocean.com/#retrieve-backups-for-a-droplet
184 | */
185 |
186 | Droplets.prototype.getBackupsForDroplet = function(dropletID, callback) {
187 | makeRequest(rest.get, this.baseUri + dropletID + '/backups', {
188 | headers: {
189 | 'Content-Type': 'application/json',
190 | 'Authorization': 'Bearer ' + this.token
191 | }
192 | }, callback);
193 | };
194 |
195 | /*
196 | Retrieve actions for a Droplet
197 |
198 | Retrieve all actions that have been executed on a Droplet
199 |
200 | @param id {number} [Required]
201 |
202 | @ The results will be returned as a JSON object with an actions key.
203 | This will be set to an array filled with action objects containing the standard action attributes
204 |
205 | API documentation: https://developers.digitalocean.com/#retrieve-actions-for-a-droplet
206 | */
207 |
208 | Droplets.prototype.getActionsForDroplet = function(dropletID, callback) {
209 | makeRequest(rest.get, this.baseUri + dropletID + '/actions', {
210 | headers: {
211 | 'Content-Type': 'application/json',
212 | 'Authorization': 'Bearer ' + this.token
213 | }
214 | }, callback);
215 | };
216 |
217 | /*
218 | List Droplet Upgrades
219 |
220 | Retrieve a list of droplets that are scheduled to be upgraded
221 |
222 | @ The results will be returned as a JSON array containing details about the schedule and droplet id
223 |
224 | API documentation: https://developers.digitalocean.com/#retrieve-actions-for-a-droplet
225 | */
226 |
227 | Droplets.prototype.listDropletUpgrades = function(callback) {
228 | makeRequest(rest.get, 'https://api.digitalocean.com/v2/droplet_upgrades', {
229 | headers: {
230 | 'Content-Type': 'application/json',
231 | 'Authorization': 'Bearer ' + this.token
232 | }
233 | }, callback);
234 | };
235 |
236 | exports.Droplets = Droplets;
237 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "dropletapi",
3 | "version": "3.1.0",
4 | "description": "Wrapper for the DigitalOcean API V2",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "https://github.com/oliwerhelsen/dropletapi/tree/V3"
12 | },
13 | "keywords": [
14 | "Digital",
15 | "Ocean",
16 | "API",
17 | "V2"
18 | ],
19 | "dependencies": {
20 | "restler": "2.0.*",
21 | "lodash": "~2.4.1"
22 | },
23 | "author": "Oliwer Helsén",
24 | "license": "ISC",
25 | "bugs": {
26 | "url": "https://github.com/oliwerhelsen/DigitalOcean-API-v2/issues"
27 | },
28 | "homepage": "https://github.com/oliwerhelsen/DigitalOcean-API-v2"
29 | }
30 |
--------------------------------------------------------------------------------