virtualizorjs 1.0.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,491 +0,0 @@
1
- const https = require("https");
2
- const { URLSearchParams } = require("url");
3
- const EventEmitter = require("events");
4
-
5
- /**
6
- * @description - This class contains some the actions supported by Virtualizor API
7
- * @class Actions
8
- * @static
9
- * @readonly
10
- * @memberof VirtualizorClient
11
- * @enum {string}
12
- */
13
-
14
- const Actions = require("./actions");
15
-
16
- /**
17
- * @class VirtualizorClient
18
- * @description - This class is used to make http requests to Virtualizor API
19
- * @author kkMihai <kkmihai@duck.com>
20
- * @param {String} host - Hostname of the Virtualizor server (IP or domain)
21
- * @param {String} port - Port of the Virtualizor server (default: 4085)
22
- * @param {String} adminapikey - API admin api key
23
- * @param {String} adminapipass - API admin api pass
24
- * @param {Boolean} isRawResponse - If true, the response will be the raw response from the API, Recommended to set this to false
25
- * @returns {VirtualizorClient} VirtualizorClient
26
- */
27
-
28
- class VirtualizorClient extends EventEmitter {
29
- constructor({ host, port, adminapikey, adminapipass, isRawResponse = false }) {
30
- super();
31
- this.host = host;
32
- this.port = port;
33
- this.adminapikey = adminapikey;
34
- this.adminapipass = adminapipass;
35
- this.isRawResponse = isRawResponse
36
-
37
- /**
38
- * @description - Bind Methods
39
- * @memberof VirtualizorClient
40
- */
41
- this.CreateVPS = this.CreateVPS.bind(this);
42
- this.GetVPS = this.GetVPS.bind(this);
43
- this.ListVPS = this.ListVPS.bind(this);
44
- this.StartVPS = this.StartVPS.bind(this);
45
- this.StopVPS = this.StopVPS.bind(this);
46
- this.RestartVPS = this.RestartVPS.bind(this);
47
- this.GetVPSRam = this.GetVPSRam.bind(this);
48
- this.GetVPSCPU = this.GetVPSCPU.bind(this);
49
- this.GetVPSDisk = this.GetVPSDisk.bind(this);
50
- this.GetVPSBandwidth = this.GetVPSBandwidth.bind(this);
51
- this.GetPlans = this.GetPlans.bind(this);
52
- }
53
-
54
- /**
55
- * @description - This method is used to build query string and should not be used externally
56
- * @param {Object} params
57
- * @returns {String} query string
58
- * @memberof VirtualizorClient
59
- * @private
60
- */
61
- buildQueryString(params) {
62
- params.api = "json";
63
- Object.keys(params).forEach((key) => {
64
- if (params[key] === undefined) {
65
- delete params[key];
66
- }
67
- });
68
- const queryParams = new URLSearchParams(params);
69
- return `?${queryParams.toString()}`;
70
- }
71
-
72
- /**
73
- * @description - This method is used to make http/s request and should not be used externally
74
- * @param {String} path - Path of the request
75
- * @param {String} method - Method of the request
76
- * @param {String} postData - This is used to send data in POST request in the form of query string
77
- * @returns {Promise} Promise
78
- * @memberof VirtualizorClient
79
- * @private
80
- */
81
- makeHttpRequest(path, method = "GET", postData) {
82
- const options = {
83
- host: this.host,
84
- port: this.port,
85
- protocol: "https:",
86
- path: path,
87
- method: method,
88
- headers: {
89
- "Content-Type": "application/x-www-form-urlencoded",
90
- },
91
- agent: new https.Agent({ rejectUnauthorized: false ,requestCert: false }),
92
- };
93
-
94
- return new Promise((resolve, reject) => {
95
- const req = https.request(options, (res) => {
96
- let data = "";
97
-
98
- console.log(data);
99
-
100
-
101
- res.on("data", (chunk) => {
102
- data += chunk;
103
- });
104
-
105
- res.on("end", () => {
106
- try {
107
- const parsedData = JSON.parse(data);
108
- resolve(parsedData);
109
- } catch (error) {
110
- reject(error);
111
- }
112
- });
113
- });
114
-
115
- req.on("error", (error) => {
116
- reject(error);
117
- });
118
-
119
- if (postData) {
120
- req.write(postData);
121
- }
122
-
123
- req.end();
124
- });
125
- }
126
-
127
- /**
128
- * @description - This method is used to create a new virtual server
129
- * @param {Object} params - Parameters for creating a new virtual server
130
- * @param {Object | String} params.storageSpace - Format { Size: Number, st_uuid: String }
131
- * @returns {Promise} Promise
132
- * @memberof VirtualizorClient
133
- */
134
- async CreateVPS({
135
- virtualizationType,
136
- nodeSelection,
137
- userEmail,
138
- userpassword,
139
- serverHostname,
140
- rootpassword,
141
- osId,
142
- ipAddress,
143
- storageSpace,
144
- serverRam,
145
- bandwidthLimit,
146
- cpuCores,
147
- }) {
148
- /**
149
- * @description - This method is used to handle storage space parameter, This in GB
150
- * @param {Array|Number} space
151
- * @returns {Number | Promise} Promise
152
- * @memberof VirtualizorClient
153
- * @private
154
- */
155
-
156
- function handleDiskSpace(space) {
157
- if (Array.isArray(space)) {
158
- return space.reduce((acc, curr) => acc + curr, 0);
159
- }
160
- return space;
161
- }
162
-
163
- const queryParams = {
164
- action: Actions.AddVPS,
165
- virt: virtualizationType,
166
- node_select: nodeSelection,
167
- user_email: userEmail,
168
- user_pass: userpassword,
169
- hostname: serverHostname,
170
- root_pass: rootpassword,
171
- os_id: osId,
172
- ips: ipAddress,
173
- space: handleDiskSpace(storageSpace),
174
- ram: serverRam,
175
- bandwidth: bandwidthLimit,
176
- cores: cpuCores,
177
- adminapikey: this.adminapikey,
178
- adminapipass: this.adminapipass,
179
- };
180
-
181
- const path = `/index.php${this.buildQueryString(queryParams)}`;
182
-
183
- try {
184
- const response = await this.makeHttpRequest(path, "POST");
185
- this.emit("vpsCreated", response);
186
- return Promise.resolve({
187
- message: response.done && response.done.msg,
188
- data: response,
189
- });
190
- } catch (error) {
191
- return Promise.reject(error);
192
- }
193
- }
194
-
195
- /**
196
- * @description - This method is used to get information about a virtual server
197
- * @param {String} id - ID of the virtual server
198
- * @returns {Promise} Promise
199
- * @memberof VirtualizorClient
200
- * @Tested - Yes
201
- */
202
- async GetVPS(id) {
203
- const queryParams = {
204
- act: Actions.GetVPS,
205
- vpsid: id,
206
- adminapikey: this.adminapikey,
207
- adminapipass: this.adminapipass,
208
- };
209
-
210
- const path = `/index.php${this.buildQueryString(queryParams)}`;
211
-
212
- try {
213
-
214
- if (!id) {
215
- return Promise.reject(new Error("vpsid is required"));
216
- }
217
-
218
- const res = await this.makeHttpRequest(path, "POST");
219
- let resData = res;
220
-
221
- if (!this.isRawResponse) {
222
- resData = res.vs[id];
223
- }
224
-
225
- return Promise.resolve(resData);
226
-
227
- } catch (err) {
228
- return Promise.reject(err);
229
- }
230
- }
231
-
232
- /**
233
- * @description - This method is used to list all virtual servers
234
- * @returns {Promise} Promise
235
- * @memberof VirtualizorClient
236
- * @Tested - Yes
237
- */
238
- async ListVPS() {
239
- const queryParams = {
240
- act: Actions.GetVPS,
241
- adminapikey: this.adminapikey,
242
- adminapipass: this.adminapipass,
243
- };
244
-
245
- const path = `/index.php${this.buildQueryString(queryParams)}`;
246
-
247
- try {
248
-
249
- const res = await this.makeHttpRequest(path, "GET");
250
- let resData = res;
251
-
252
- if (!this.isRawResponse) {
253
- resData = res.vs
254
- }
255
-
256
- return Promise.resolve(resData);
257
-
258
- } catch (err) {
259
- return Promise.reject(err);
260
- }
261
- }
262
-
263
-
264
- /**
265
- * @description - This method is used to start a virtual server
266
- * @param {String} vpsId - ID of the virtual server
267
- * @returns {Promise} Promise
268
- * @memberof VirtualizorClient
269
- * @Tested - Yes
270
- */
271
- async StartVPS(vpsId) {
272
- const queryParams = {
273
- action: Actions.StartVPS,
274
- vpsid: vpsId,
275
- adminapikey: this.adminapikey,
276
- adminapipass: this.adminapipass,
277
- };
278
-
279
- const path = `/index.php${this.buildQueryString(queryParams)}`;
280
-
281
- try {
282
-
283
- if (!vpsId) {
284
- return Promise.reject(new Error("vpsid is required"));
285
- }
286
-
287
- const res = await this.makeHttpRequest(path, "GET", `act=vs`);
288
- this.emit("vpsStarted", res);
289
-
290
- return Promise.resolve({
291
- message: res.done && res.done_msg,
292
- error: res.error_msg || false
293
- })
294
-
295
- } catch (err) {
296
- return Promise.reject(err);
297
- }
298
- }
299
-
300
- /**
301
- * @description - This method is used to stop a virtual server
302
- * @param {String} vpsId - ID of the virtual server
303
- * @returns {Promise} Promise
304
- * @memberof VirtualizorClient
305
- * @Tested - Yes
306
- */
307
- async StopVPS(vpsId) {
308
- const queryParams = {
309
- action: Actions.StopVPS,
310
- vpsid: vpsId,
311
- adminapikey: this.adminapikey,
312
- adminapipass: this.adminapipass,
313
- };
314
-
315
- const path = `/index.php${this.buildQueryString(queryParams)}`;
316
-
317
- try {
318
- const res = await this.makeHttpRequest(path, "GET", `act=vs`);
319
- this.emit("vpsStopped", res);
320
- return Promise.resolve({
321
- message: res.done && res.done_msg,
322
- error: res.error_msg || false,
323
- })
324
- } catch (err) {
325
- return Promise.reject(err);
326
- }
327
- }
328
-
329
- /**
330
- * @description - This method is used to restart a virtual server
331
- * @param {String} vpsId - ID of the virtual server
332
- * @returns {Promise} Promise
333
- * @memberof VirtualizorClient
334
- */
335
- async RestartVPS(vpsId) {
336
- const queryParams = {
337
- action: Actions.RestartVPS,
338
- vpsid: vpsId,
339
- adminapikey: this.adminapikey,
340
- adminapipass: this.adminapipass,
341
- };
342
-
343
- const path = `/index.php${this.buildQueryString(queryParams)}`;
344
-
345
- try {
346
- const res = await this.makeHttpRequest(path, "GET", `act=vs`);
347
- this.emit("vpsRestarted", res);
348
- return Promise.resolve({
349
- message: res.done && res.done_msg,
350
- error: res.error_msg || false
351
- })
352
- } catch (err) {
353
- return Promise.reject(err);
354
- }
355
- }
356
-
357
- /**
358
- * @description - This method is used to get RAM information of a virtual server
359
- * @param {String} vpsId - ID of the virtual server
360
- * @returns {Promise} Promise
361
- * @memberof VirtualizorClient
362
- */
363
- async GetVPSRam(vpsId) {
364
- const queryParams = {
365
- act: Actions.GetVPSRam,
366
- svs: vpsId,
367
- adminapikey: this.adminapikey,
368
- adminapipass: this.adminapipass,
369
- };
370
-
371
- const path = `/index.php${this.buildQueryString(queryParams)}`;
372
-
373
- try {
374
- const res = await this.makeHttpRequest(path);
375
- return Promise.resolve({
376
- ram: res.ram,
377
- time_taken: res.time_taken,
378
- });
379
- } catch (err) {
380
- return Promise.reject(err);
381
- }
382
- }
383
-
384
- /**
385
- * @description - This method is used to get CPU information of a virtual server
386
- * @param {String} vpsId - ID of the virtual server
387
- * @returns {Promise} Promise
388
- * @memberof VirtualizorClient
389
- */
390
- async GetVPSCPU(vpsId) {
391
- const queryParams = {
392
- act: Actions.GetVPSCPU,
393
- svs: vpsId,
394
- adminapikey: this.adminapikey,
395
- adminapipass: this.adminapipass,
396
- };
397
-
398
- const path = `/index.php${this.buildQueryString(queryParams)}`;
399
-
400
- try {
401
- const res = await this.makeHttpRequest(path);
402
-
403
- return Promise.resolve({
404
- cpu: res.cpu,
405
- time_taken: res.time_taken,
406
- });
407
- } catch (err) {
408
- return Promise.reject(err);
409
- }
410
- }
411
-
412
- /**
413
- * @description - This method is used to get disk information of a virtual server
414
- * @param {String} vpsId - ID of the virtual server
415
- * @returns {Promise} Promise
416
- * @memberof VirtualizorClient
417
- */
418
- async GetVPSDisk(vpsId) {
419
- const queryParams = {
420
- act: Actions.GetVPSDisk,
421
- changeserid: vpsId,
422
- adminapikey: this.adminapikey,
423
- adminapipass: this.adminapipass,
424
- };
425
-
426
- const path = `/index.php${this.buildQueryString(queryParams)}`;
427
-
428
- try {
429
- const res = await this.makeHttpRequest(path);
430
-
431
- return Promise.resolve({
432
- info: res.disk,
433
- time_taken: res.time_taken,
434
- });
435
-
436
-
437
- } catch (err) {
438
- return Promise.reject(err);
439
- }
440
- }
441
-
442
- /**
443
- * @description - This method is used to get bandwidth information of a virtual server
444
- * @param {String} vpsId - ID of the virtual server
445
- * @param {String} month - Month for which bandwidth information is required (YYYY-MM)
446
- * @returns {Promise} Promise
447
- * @memberof VirtualizorClient
448
- */
449
- async GetVPSBandwidth({vpsId, month}) {
450
- const queryParams = {
451
- act: Actions.GetServerBandwidth,
452
- changeserid: vpsId,
453
- adminapikey: this.adminapikey,
454
- adminapipass: this.adminapipass,
455
- };
456
-
457
- const path = `/index.php${this.buildQueryString(queryParams)}`;
458
-
459
- try {
460
- const res = await this.makeHttpRequest(path, "GET", `show=${month}`);
461
- return Promise.resolve({
462
- bandwidth: res.bandwidth,
463
- time_taken: res.time_taken,
464
- });
465
- } catch (err) {
466
- return Promise.reject(err);
467
- }
468
- }
469
-
470
- async GetPlans() {
471
- const queryParams = {
472
- act: Actions.GetPlans,
473
- adminapikey: this.adminapikey,
474
- adminapipass: this.adminapipass,
475
- };
476
-
477
- const path = `/index.php${this.buildQueryString(queryParams)}`;
478
-
479
- try {
480
- const res = await this.makeHttpRequest(path);
481
- return Promise.resolve({
482
- plans: res.plans,
483
- time_taken: res.time_taken,
484
- });
485
- } catch (err) {
486
- return Promise.reject(err);
487
- }
488
- }
489
- }
490
-
491
- module.exports = VirtualizorClient;