-
Notifications
You must be signed in to change notification settings - Fork 42
/
cloudapi.js
4526 lines (3885 loc) · 142 KB
/
cloudapi.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014, Joyent, Inc.
*/
var assert = require('assert-plus');
var crypto = require('crypto');
var qs = require('querystring');
var util = require('util');
var sprintf = util.format;
var os = require('os');
var createCache = require('lru-cache');
var restify = require('restify');
var bunyan = require('bunyan');
var clone = require('clone');
var auth = require('smartdc-auth');
// --- Globals
var log = bunyan.createLogger({
level: 'fatal',
name: 'smartdc',
stream: process.stderr,
serializers: restify.bunyan.serializers
});
var VERSION = require('../package.json').version;
var RESTIFY_VERSION = 'unknown';
try {
RESTIFY_VERSION = require('../node_modules/restify/package.json').version;
} catch (e) {}
var SIGNATURE = 'Signature keyId="/%s/keys/%s",algorithm="%s" %s';
var ROOT = '/%s';
var KEYS = ROOT + '/keys';
var KEY = KEYS + '/%s';
var PACKAGES = ROOT + '/packages';
var PACKAGE = PACKAGES + '/%s';
var DATASETS = ROOT + '/datasets';
var DATASET = DATASETS + '/%s';
var IMAGES = ROOT + '/images';
var IMAGE = IMAGES + '/%s';
var DATACENTERS = ROOT + '/datacenters';
var MACHINES = ROOT + '/machines';
var MACHINE = MACHINES + '/%s';
var METADATA = MACHINE + '/metadata';
var NICS = MACHINE + '/nics';
var NIC = NICS + '/%s';
var METADATA_KEY = MACHINE + '/metadata/%s';
var SNAPSHOTS = MACHINE + '/snapshots';
var SNAPSHOT = SNAPSHOTS + '/%s';
var TAGS = MACHINE + '/tags';
var TAG = TAGS + '/%s';
var ANALYTICS = ROOT + '/analytics';
var INSTS = ANALYTICS + '/instrumentations';
var INST = INSTS + '/%s';
var INST_RAW = INST + '/value/raw';
var INST_HMAP = INST + '/value/heatmap/image';
var INST_HMAP_DETAILS = INST + '/value/heatmap/details';
var USAGE = ROOT + '/usage/%s';
var MACHINE_USAGE = MACHINE + '/usage/%s';
var AUDIT = MACHINE + '/audit';
var FWRULES = ROOT + '/fwrules';
var FWRULE = FWRULES + '/%s';
var NETWORKS = ROOT + '/networks';
var NETWORK = NETWORKS + '/%s';
var USERS = ROOT + '/users';
var USER = USERS + '/%s';
var POLICIES = ROOT + '/policies';
var POLICY = POLICIES + '/%s';
var ROLES = ROOT + '/roles';
var ROLE = ROLES + '/%s';
var SUB_KEYS = USER + '/keys';
var SUB_KEY = SUB_KEYS + '/%s';
// --- Internal Helpers
function _encodeURI(path) {
assert.ok(path);
var ret = '';
var str = '';
var esc = false;
function append() {
if (str.length)
ret += '/' + qs.escape(str);
str = '';
return ret;
}
for (var i = 0; i < path.length; i++) {
if (!esc) {
switch (path[i]) {
case '\\':
esc = true;
break;
case '/':
append();
break;
default:
str += path[i];
break;
}
} else {
str += path[i];
esc = false;
}
}
return append();
}
function _signRequest(opts, cb) {
assert.object(opts, 'options');
assert.object(opts.headers, 'options.headers');
assert.func(cb, 'callback');
if (!opts.sign) {
return (cb(null));
}
assert.func(opts.sign, 'options.sign');
opts.sign(opts.headers.date, function signedCb(err, obj) {
if (err) {
return (cb(err));
}
if (obj === null) {
return (cb(null));
}
opts.headers.authorization = sprintf(SIGNATURE,
obj.user,
obj.keyId,
obj.algorithm,
obj.signature);
return (cb(null));
});
return (null);
}
function _addToQuery(req, obj) {
var query = req.query;
if (typeof (obj) !== 'undefined') {
assert.object(obj, 'req.options');
Object.keys(obj).forEach(function (key) {
query[key] = obj[key];
});
}
}
// --- Exported CloudAPI Client
/**
* Constructor.
*
* Note that in options you can pass in any parameters that the restify
* RestClient constructor takes (for example retry/backoff settings).
*
* In order to create a client, you either have to specify username and
* password, in which case HTTP Basic Authentication will be used, or
* preferably keyId and key, in which case HTTP Signature Authentication will
* be used (much more secure).
*
* @param {Object} options object (required):
* - {String} url (required) CloudAPI location.
* - {String} account (optional) the login name to use (default my).
* - {Number} logLevel (optional) an enum value for the logging level.
* - {String} version (optional) api version (default ~7.0).
* - {Function} sign (required) callback function to use for signing
* (authenticated requests)
* - {Boolean} noCache (optional) disable client caching (default false).
* - {Boolean} cacheSize (optional) number of cache entries (default 1k).
* - {Boolean} cacheExpiry (optional) entry age in seconds (default 60).
* - {String} userAgent (optional)
* ...
* @throws {TypeError} on bad input.
* @constructor
*/
function CloudAPI(options) {
assert.object(options, 'options');
assert.string(options.url, 'options.url');
assert.func(options.sign, 'options.sign');
assert.optionalString(options.account, 'options.account');
if (options.logLevel)
log.level(options.logLevel);
options.log = log;
if (!options.version)
options.version = '~7.2';
this.account = options.account || 'my';
this.sign = options.sign;
options.contentType = 'application/json';
options.retryCallback = function checkFor500(code) {
return (code === 500);
};
if (options.asRole) {
this.asRole = options.asRole;
}
if (!options.userAgent) {
options.userAgent = 'restify/' + RESTIFY_VERSION + ' (' + os.arch() +
'-' + os.platform() +
'; v8/' + process.versions.v8 + '; ' +
'OpenSSL/' + process.versions.openssl + ') ' +
'node/' + process.versions.node +
'; node-smartdc/' + VERSION;
}
this.token = options.token;
if (process.env.SDC_TESTING) {
options.rejectUnauthorized = false;
}
this.client = restify.createJsonClient(options);
this.options = clone(options);
// Initialize the cache
if (!options.noCache) {
this.cacheSize = options.cacheSize || 1000;
this.cacheExpiry = (options.cacheExpiry || 60) * 1000;
this.cache = createCache(this.cacheSize);
}
}
/**
* Looks up your account record.
*
* Returns an object.
*
* @param {String} account (optional) the login name of the account.
* @param {Function} callback of the form f(err, account).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function getAccount(account, callback, noCache) {
var self = this;
if (typeof (account) === 'function') {
callback = account;
account = this.account;
}
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
return self._request(sprintf(ROOT, account), null, function reqCb(req) {
return self._get(req, callback, noCache);
});
}
CloudAPI.prototype.getAccount = getAccount;
CloudAPI.prototype.GetAccount = getAccount;
/**
* Modifies your account record.
*
* Returns an object.
*
* @param {String} account (optional) the login name of the account.
* @param {Object} opts (object) account object.
* @param {Function} callback of the form f(err, account).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function updateAccount(account, opts, callback, noCache) {
var self = this;
if (typeof (opts) === 'function') {
callback = opts;
opts = account;
account = this.account;
}
if (!opts || typeof (opts) !== 'object')
throw new TypeError('opts (object) required');
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
return self._request(sprintf(ROOT, account), opts, function reqCb(req) {
return self._post(req, callback);
});
}
CloudAPI.prototype.updateAccount = updateAccount;
CloudAPI.prototype.UpdateAccount = updateAccount;
/**
* Creates an SSH key on your account.
*
* Returns a JS object (the created key). Note that options can actually
* be just the key PEM, if you don't care about names.
*
* @param {String} account (optional) the login name of the account.
* @param {Object} options object containing:
* - {String} name (optional) name for your ssh key.
* - {String} key SSH public key.
* @param {Function} callback of the form f(err, key).
* @throws {TypeError} on bad input.
*/
function createKey(account, options, callback) {
var self = this;
if (typeof (options) === 'function') {
callback = options;
options = account;
account = this.account;
}
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (!options ||
(typeof (options) !== 'string' && typeof (options) !== 'object'))
throw new TypeError('options (object) required');
if (typeof (account) === 'object')
account = account.login;
if (typeof (options) === 'string') {
options = {
key: options
};
}
return self._request(sprintf(KEYS, account), options, function reqCb(req) {
return self._post(req, callback);
});
}
CloudAPI.prototype.createKey = createKey;
CloudAPI.prototype.CreateKey = createKey;
/**
* Lists all SSH keys on file for your account.
*
* Returns an array of objects.
*
* @param {String} account (optional) the login name of the account.
* @param {Function} callback of the form f(err, keys).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function listKeys(account, callback, noCache) {
var self = this;
if (typeof (account) === 'function') {
noCache = callback;
callback = account;
account = this.account;
}
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
return self._request(sprintf(KEYS, account), null, function reqCb(req) {
if (noCache)
_addToQuery(req, { sync: noCache });
return self._get(req, callback, noCache);
});
}
CloudAPI.prototype.listKeys = listKeys;
CloudAPI.prototype.ListKeys = listKeys;
/**
* Retrieves an SSH key from your account.
*
* Returns a JS object.
*
* @param {String} account (optional) the login name of the account.
* @param {String} key can be either the string name of the key, or the object
* returned from create/get.
* @param {Function} callback of the form f(err, key).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function getKey(account, key, callback, noCache) {
var self = this;
if (typeof (key) === 'function') {
noCache = callback;
callback = key;
key = account;
account = this.account;
}
if (!key || (typeof (key) !== 'object' && typeof (key) !== 'string'))
throw new TypeError('key (object|string) required');
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
var name = (typeof (key) === 'object' ? key.name : key);
var path = sprintf(KEY, account, name);
return self._request(path, null, function reqCb(req) {
if (noCache)
_addToQuery(req, { sync: noCache });
return self._get(req, callback, noCache);
});
}
CloudAPI.prototype.getKey = getKey;
CloudAPI.prototype.GetKey = getKey;
/**
* Deletes an SSH key from your account.
*
* @param {String} account (optional) the login name of the account.
* @param {String} key can be either the string name of the key, or the object
* returned from create/get.
* @param {Function} callback of the form f(err).
* @throws {TypeError} on bad input.
*/
function deleteKey(account, key, callback) {
var self = this;
if (typeof (key) === 'function') {
callback = key;
key = account;
account = this.account;
}
if (!key || (typeof (key) !== 'object' && typeof (key) !== 'string'))
throw new TypeError('key (object|string) required');
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
var name = (typeof (key) === 'object' ? key.name : key);
var path = sprintf(KEY, account, name);
return self._request(path, null, function reqCb(req) {
return self._del(req, callback);
});
}
CloudAPI.prototype.deleteKey = deleteKey;
CloudAPI.prototype.DeleteKey = deleteKey;
/**
* Lists all packages available to your account.
*
* Returns an array of objects.
*
* @param {String} account (optional) the login name of the account.
* @param {Function} callback of the form f(err, packages).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function listPackages(account, callback, noCache) {
var self = this;
if (typeof (account) === 'function') {
callback = account;
account = this.account;
}
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
return self._request(sprintf(PACKAGES, account), null, function reqCb(req) {
return self._get(req, callback, noCache);
});
}
CloudAPI.prototype.listPackages = listPackages;
CloudAPI.prototype.ListPackages = listPackages;
/**
* Retrieves a single package available to your account.
*
* Returns a JS object.
*
* @param {String} account (optional) the login name of the account.
* @param {String} pkg can be either the string name of the package, or an
* object returned from listPackages.
* @param {Function} callback of the form f(err, package).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function getPackage(account, pkg, callback, noCache) {
var self = this;
if (typeof (pkg) === 'function') {
callback = pkg;
pkg = account;
account = this.account;
}
if (!pkg || (typeof (pkg) !== 'object' && typeof (pkg) !== 'string'))
throw new TypeError('key (object|string) required');
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
var name = (typeof (pkg) === 'object' ? pkg.name : pkg);
var path = sprintf(PACKAGE, account, name);
return self._request(path, null, function reqCb(req) {
return self._get(req, callback, noCache);
});
}
CloudAPI.prototype.getPackage = getPackage;
CloudAPI.prototype.GetPackage = getPackage;
/**
* Lists all datasets available to your account.
*
* Returns an array of objects.
*
* @param {String} account (optional) the login name of the account.
* @param {Function} callback of the form f(err, datasets).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function listDatasets(account, callback, noCache) {
var self = this;
if (typeof (account) === 'function') {
callback = account;
account = this.account;
}
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
return self._request(sprintf(DATASETS, account), null, function reqCb(req) {
return self._get(req, callback, noCache);
});
}
CloudAPI.prototype.listDatasets = listDatasets;
CloudAPI.prototype.ListDatasets = listDatasets;
/**
* Retrieves a single dataset available to your account.
*
* Returns a JS object.
*
* @param {String} account (optional) the login name of the account.
* @param {String} dataset can be either the string name of the dataset, or an
* object returned from listDatasets.
* @param {Function} callback of the form f(err, package).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function getDataset(account, dataset, callback, noCache) {
var self = this;
if (typeof (dataset) === 'function') {
callback = dataset;
dataset = account;
account = this.account;
}
if (!dataset ||
(typeof (dataset) !== 'object' && typeof (dataset) !== 'string'))
throw new TypeError('dataset (object|string) required');
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
var name = (typeof (dataset) === 'object' ? dataset.id : dataset);
var path = sprintf(DATASET, account, name);
return self._request(path, null, function reqCb(req) {
return self._get(req, callback, noCache);
});
}
CloudAPI.prototype.getDataset = getDataset;
CloudAPI.prototype.GetDataset = getDataset;
/**
* Lists all images available to your account.
*
* Returns an array of objects.
*
* @param {String} account (optional) the login name of the account.
* @param {Object} options (optional) additional filter for images:
* - {String} public (optional) filter public/private images.
* - {String} state (optional) filter by state.
* - {String} type (optional) filter by type.
* @param {Function} callback of the form f(err, images).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function listImages(account, options, callback, noCache) {
var self = this;
// Dev Note: This optional `account`, `options` and `noCache` before
// and after handling is insanity. Time for a refactor with bwcompat
// breakage.
if (typeof (account) === 'function') {
callback = account;
account = this.account;
} else if (typeof (options) === 'function') {
callback = options;
}
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
// Backward compat: account is optional, callback is not last arg
if (typeof (account) === 'object') {
if (account.login) {
account = account.login;
} else {
options = account;
account = this.account;
}
}
// console.log(
// 'listImages: account=%j, options=%j, callback=%s, noCache=%j',
// account, options, util.inspect(callback), noCache);
return self._request(sprintf(IMAGES, account), null, function reqCb(req) {
_addToQuery(req, options);
return self._get(req, callback, noCache);
});
}
CloudAPI.prototype.listImages = listImages;
CloudAPI.prototype.ListImages = listImages;
/**
* Retrieves a single image available to your account.
*
* Returns a JS object.
*
* @param {String} account (optional) the login name of the account.
* @param {String} image can be either the string name of the image, or an
* object returned from listImages.
* @param {Function} callback of the form f(err, image).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function getImage(account, image, callback, noCache) {
var self = this;
if (typeof (image) === 'function') {
callback = image;
image = account;
account = this.account;
}
if (!image ||
(typeof (image) !== 'object' && typeof (image) !== 'string'))
throw new TypeError('image (object|string) required');
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
var name = (typeof (image) === 'object' ? image.id : image);
var path = sprintf(IMAGE, account, name);
return self._request(path, null, function reqCb(req) {
return self._get(req, callback, noCache);
});
}
CloudAPI.prototype.getImage = getImage;
CloudAPI.prototype.GetImage = getImage;
/**
* Creates an image from a machine
*
* Returns a JS object
*
* @param {String} account (optional) the login name of the account.
* @param {Object} options object containing:
* - {UUID} machine The prepared and stopped machine UUID
* from which the image is to be created.
* - {String} name The name for your new image
* - {String} version The version of the custom image,
* e.g. "1.0.0". See the IMGAPI docs for details.
* - {String} description The image description.
* @param {Function} callback of the form f(err, machine).
* @throws {TypeError} on bad input.
*/
function createImageFromMachine(account, options, callback) {
var self = this;
if (typeof (options) === 'function') {
callback = options;
options = account;
account = this.account;
}
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (!options ||
(typeof (options) !== 'object'))
throw new TypeError('options (object) required');
if (typeof (account) === 'object')
account = account.login;
if (options['machine'] && options['name'] && options['version']) {
switch (typeof (options['machine'])) {
case 'string':
// noop
break;
case 'object':
options['machine'] = options['machine'].id;
break;
default:
throw new TypeError('options.machine must be a string or object');
}
} else {
throw new TypeError('options missing machine, name, or version field');
}
var path = sprintf(IMAGES, account);
return self._request(path, options, function reqCb(req) {
return self._post(req, callback);
});
}
CloudAPI.prototype.createImageFromMachine = createImageFromMachine;
CloudAPI.prototype.CreateImageFromMachine = createImageFromMachine;
/**
* Updates an image
*
* Returns a JS object
*
* @param {String} account (optional) the login name of the account.
* @param {String} image can be either the string name of the image, or an
* object returned from listImages.
* @param {Object} params object containing any optional image attribute to
* be udpated:
* - {String} name Name of the image
* - {String} version Version of the image,
* - {String} description The image description.
* - {String} homepage The image homepage.
* - {String} eula The image EULA.
* - {Array} acl The image ACL.
* - {Object} tags The image tags.
* @param {Function} callback of the form f(err, machine).
* @throws {TypeError} on bad input.
*/
function updateImage(account, image, params, callback) {
var self = this;
if (typeof (params) === 'function') {
callback = params;
params = image;
image = account;
account = this.account;
}
if (!image || (typeof (image) !== 'object' && typeof (image) !== 'string'))
throw new TypeError('image (object|string) required');
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (!params || (typeof (params) !== 'object'))
throw new TypeError('params (object) required');
if (typeof (account) === 'object')
account = account.login;
var name = (typeof (image) === 'object' ? image.id : image);
var path = sprintf(IMAGE, account, name);
return self._request(path, null, function reqCb(req) {
_addToQuery(req, { action: 'update' });
req.body = params;
return self._post(req, callback);
});
}
CloudAPI.prototype.updateImage = updateImage;
CloudAPI.prototype.UpdateImage = updateImage;
/**
* Removes an image available to your account.
*
* @param {String} account (optional) the login name of the account.
* @param {String} image can be either the string name of the image, or an
* object returned from listImages.
* @param {Function} callback of the form f(err).
* @throws {TypeError} on bad input.
*/
function deleteImage(account, image, callback) {
var self = this;
if (typeof (image) === 'function') {
callback = image;
image = account;
account = this.account;
}
if (!image || (typeof (image) !== 'object' && typeof (image) !== 'string'))
throw new TypeError('image (object|string) required');
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
var name = (typeof (image) === 'object' ? image.id : image);
var path = sprintf(IMAGE, account, name);
return self._request(path, null, function reqCb(req) {
return self._del(req, callback);
});
}
CloudAPI.prototype.deleteImage = deleteImage;
CloudAPI.prototype.DeleteImage = deleteImage;
/**
* Exports an Image to the specified Manta path.
*
* Returns a JS object.
*
* @param {String} account (optional) the login name of the account.
* @param {String} image can be either the string name of the image, or an
* object returned from listImages.
* @param {String} mantaPath is a path prefix that must resolve to a location
* that is owned by the user account. If mantaPath is a
* directory, then the image file and manifest are saved with
* NAME-VER.zfs[.EXT] and NAME-VER.imgmanifest as filename
* formats. If the basename of mantaPath is not a directory,
* then "MANTA_PATH.imgmanifest" and "MANTA_PATH.zfs[.EXT]"
* are filename formats.
* @param {Function} callback of the form f(err, obj).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function exportImage(account, image, mantaPath, callback, noCache) {
var self = this;
if (typeof (mantaPath) === 'function') {
callback = mantaPath;
mantaPath = image;
image = account;
account = this.account;
}
if (!image ||
(typeof (image) !== 'object' && typeof (image) !== 'string'))
throw new TypeError('image (object|string) required');
if (!mantaPath || typeof (mantaPath) !== 'string')
throw new TypeError('mantaPath (string) required');
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
var name = (typeof (image) === 'object' ? image.id : image);
var path = sprintf(IMAGE, account, name);
return self._request(path, null, function reqCb(req) {
_addToQuery(req, { action: 'export', manta_path: mantaPath });
return self._post(req, callback, noCache);
});
}
CloudAPI.prototype.exportImage = exportImage;
CloudAPI.prototype.ExportImage = exportImage;
/**
* Lists all datacenters available to your account.
*
* Returns an array of objects.
*
* @param {String} account (optional) the login name of the account.
* @param {Function} callback of the form f(err, datacenters).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function listDatacenters(account, callback, noCache) {
var self = this;
if (typeof (account) === 'function') {
callback = account;
account = this.account;
}
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
var path = sprintf(DATACENTERS, account);
return self._request(path, null, function reqCb(req) {
return self._get(req, callback, noCache);
});
}
CloudAPI.prototype.listDatacenters = listDatacenters;
CloudAPI.prototype.ListDatacenters = listDatacenters;
/**
* Creates a new CloudAPI client connected to the specified datacenter.
*
* Returns a JS object.
*
* @param {String} account (optional) the login name of the account.
* @param {String} datacenter can be either the string name of the datacenter,
* or an object returned from listDatacenters.
* @param {Function} callback of the form f(err, client).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
function createClientForDatacenter(account, datacenter, callback, noCache) {
var self = this;
if (typeof (datacenter) === 'function') {
callback = datacenter;
datacenter = account;
account = this.account;
}
if (typeof (datacenter) !== 'string')
throw new TypeError('datacenter (string) required');
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (account) === 'object')
account = account.login;
return self.listDatacenters(account, function listCb(err, datacenters) {
if (err)
return callback(self._error(err));
if (!datacenters[datacenter]) {
var e = new restify.ResourceNotFoundError(
'datacenter ' + datacenter + ' not found');
e.name = 'CloudApiError';
return callback(e);
}
var opts = clone(self.options);
opts.url = datacenters[datacenter];
return callback(null, new CloudAPI(opts));
});
}
CloudAPI.prototype.createClientForDatacenter = createClientForDatacenter;
CloudAPI.prototype.CreateClientForDatacenter = createClientForDatacenter;
/**
* Provisions a new smartmachine or virtualmachine.
*
* Returns a JS object (the created machine). Note that the options
* object parameters like image/package can actually be the JS objects
* returned from the respective APIs.
*
* @param {String} account (optional) the login name of the account.
* @param {Object} options (optional) object containing:
* - {String} name (optional) name for your machine.
* - {String} image (optional) image to provision.
* - {String} package (optional) package to provision.
* ...
* @param {Function} callback of the form f(err, machine).
* @throws {TypeError} on bad input.
*/
function createMachine(account, options, callback) {
var self = this;
if (typeof (account) === 'function') {
callback = account;
options = {};
account = this.account;
}
if (typeof (options) === 'function') {
callback = options;
options = account;
account = this.account;
}
if (!callback || typeof (callback) !== 'function')
throw new TypeError('callback (function) required');
if (typeof (options) !== 'object')
throw new TypeError('options must be an object');
if (options.name && typeof (options.name) !== 'string')
throw new TypeError('options.name must be a string');
if (typeof (account) === 'object')
account = account.login;
if (!options.image && options.dataset) {