This repository was archived by the owner on Feb 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvultr.js
87 lines (75 loc) · 2.41 KB
/
vultr.js
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
50
51
52
53
54
55
56
57
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
'use strict';
var Promise = require('bluebird'),
request = require('request-promise');
var config = require('./config');
var account = require(__dirname + '/lib/account'),
dns = require(__dirname + '/lib/dns'),
iso = require(__dirname + '/lib/iso'),
backup = require(__dirname + '/lib/backup'),
os = require(__dirname + '/lib/os'),
plans = require(__dirname + '/lib/plans'),
regions = require(__dirname + '/lib/regions'),
server = require(__dirname + '/lib/server'),
snapshot = require(__dirname + '/lib/snapshot'),
sshkey = require(__dirname + '/lib/sshkey'),
startupscript = require(__dirname + '/lib/startupscript');
/**
* Vultr instance constructor
* @prototype
* @class Vultr
*/
function Vultr(apiKey) {
this.version = 'v1';
this.endpoint = 'https://api.vultr.com/' + this.version + '/';
this.apiKey = (apiKey ? apiKey : config.vultr.apiKey);
this.account = new account(this);
this.dns = new dns(this);
this.os = new os(this);
this.iso = new iso(this);
this.backup = new backup(this);
this.plans = new plans(this);
this.regions = new regions(this);
this.server = new server(this);
this.snapshot = new snapshot(this);
this.sshkey = new sshkey(this);
this.startupscript = new startupscript(this);
}
/**
* Handle communicating with the Vultr REST in one call
* @param {String} service
* @param {String} method
* @param {Array} data
* @param {Mixed} formattedData Can be a hash or array
* @return {Promise}
*/
Vultr.prototype.communicate = function communicate(service, method, data, type) {
data = data || {};
var options = {
'url': this.endpoint + service + '/' + method,
'qs': {
'api_key': this.apiKey
},
'method': 'GET',
'json': true
};
/** Use POST when we have DATA */
if(Object.keys(data).length > 0) {
options.method = 'POST';
options.form = data;
}
/** Sometimes we have to do a GET, but still with data */
if(type !== void 0 && type !== '') {
if(options.method === 'POST' && type.toUpperCase() === 'GET') {
options.form.api_key = this.apiKey;
options.qs = options.form;
options.form = {};
}
options.method = type.toUpperCase();
}
//console.log('options', options);
/** We have to deal with a rate limit of 1 req/sec */
return Promise.delay(1000).then(function() {
return request(options);
});
};
module.exports = Vultr;