2014-02-20 05:52:58 +02:00
|
|
|
/*
|
2015-09-04 21:12:20 +03:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2015 Joyent, Inc.
|
2014-02-20 05:52:58 +02:00
|
|
|
*
|
|
|
|
* Client library for the SmartDataCenter Cloud API (cloudapi).
|
|
|
|
* http://apidocs.joyent.com/cloudapi/
|
|
|
|
*
|
|
|
|
* Usage example::
|
|
|
|
*
|
|
|
|
* var auth = require('smartdc-auth');
|
|
|
|
* var cloudapi = require('./lib/cloudapi2');
|
|
|
|
* var client = cloudapi.createClient({
|
2015-05-19 00:07:45 +03:00
|
|
|
* url: <URL>, // 'https://us-sw-1.api.joyent.com',
|
2014-02-20 05:52:58 +02:00
|
|
|
* user: <USER>, // 'bob'
|
|
|
|
* log: <BUNYAN-LOGGER>,
|
|
|
|
* sign: auth.cliSigner({
|
|
|
|
* keyId: <KEY-ID>, // ssh fingerprint
|
|
|
|
* user: <USER>, // 'bob'
|
|
|
|
* log: <BUNYAN-LOGGER>,
|
|
|
|
* }),
|
|
|
|
* ...
|
|
|
|
* });
|
|
|
|
* client.listImages(function (err, images) { ... });
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
|
|
|
var auth = require('smartdc-auth');
|
2015-08-26 00:25:30 +03:00
|
|
|
var format = require('util').format;
|
2015-08-26 02:46:14 +03:00
|
|
|
var LOMStream = require('lomstream').LOMStream;
|
2014-02-20 05:52:58 +02:00
|
|
|
var os = require('os');
|
|
|
|
var querystring = require('querystring');
|
2015-08-25 23:11:40 +03:00
|
|
|
var vasync = require('vasync');
|
2014-02-20 05:52:58 +02:00
|
|
|
|
|
|
|
var errors = require('./errors');
|
2015-09-23 22:32:09 +03:00
|
|
|
var SaferJsonClient = require('./SaferJsonClient');
|
2014-02-20 05:52:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---- globals
|
|
|
|
|
2015-08-25 23:11:40 +03:00
|
|
|
var VERSION = require('../package.json').version;
|
2014-02-20 05:52:58 +02:00
|
|
|
var OS_ARCH = os.arch();
|
|
|
|
var OS_PLATFORM = os.platform();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---- internal support stuff
|
|
|
|
|
|
|
|
// A no-op bunyan logger shim.
|
|
|
|
function BunyanNoopLogger() {}
|
|
|
|
BunyanNoopLogger.prototype.trace = function () {};
|
|
|
|
BunyanNoopLogger.prototype.debug = function () {};
|
|
|
|
BunyanNoopLogger.prototype.info = function () {};
|
|
|
|
BunyanNoopLogger.prototype.warn = function () {};
|
|
|
|
BunyanNoopLogger.prototype.error = function () {};
|
|
|
|
BunyanNoopLogger.prototype.fatal = function () {};
|
|
|
|
BunyanNoopLogger.prototype.child = function () { return this; };
|
|
|
|
BunyanNoopLogger.prototype.end = function () {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---- client API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a cloudapi client.
|
|
|
|
*
|
|
|
|
* @param options {Object}
|
|
|
|
* - {String} url (required) Cloud API base url
|
|
|
|
* - {String} user (required) The user login name.
|
|
|
|
* For backward compat, 'options.account' is accepted as a synonym.
|
|
|
|
* - {Function} sign (required) An http-signature auth signing function
|
|
|
|
* - {String} version (optional) Used for the accept-version header. This
|
|
|
|
* defaults to '*', meaning that over time you could experience breaking
|
|
|
|
* changes. Specifying a value is strongly recommended. E.g. '~7.1'.
|
|
|
|
* - {Bunyan Logger} log (optional)
|
|
|
|
* - ... and any other standard restify client options, e.g.:
|
|
|
|
* {String} userAgent
|
|
|
|
* {Boolean} rejectUnauthorized
|
|
|
|
* {Boolean} agent Set to `false` to not get KeepAlive. You want
|
|
|
|
* this for CLIs.
|
|
|
|
* TODO doc the backoff/retry available options
|
|
|
|
* @throws {TypeError} on bad input.
|
|
|
|
* @constructor
|
|
|
|
*
|
|
|
|
* TODO: caching options (copy node-manta/node-moray/node-smartdc?)
|
|
|
|
* - {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).
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
function CloudApi(options) {
|
2014-02-20 05:52:58 +02:00
|
|
|
assert.object(options, 'options');
|
|
|
|
assert.string(options.url, 'options.url');
|
|
|
|
assert.string(options.user || options.account, 'options.user');
|
|
|
|
assert.func(options.sign, 'options.sign');
|
|
|
|
assert.optionalString(options.version, 'options.version');
|
|
|
|
assert.optionalObject(options.log, 'options.log');
|
|
|
|
|
|
|
|
this.url = options.url;
|
|
|
|
this.user = options.user || options.account;
|
|
|
|
this.sign = options.sign;
|
|
|
|
this.log = options.log || new BunyanNoopLogger();
|
|
|
|
if (!options.version) {
|
|
|
|
options.version = '*';
|
|
|
|
}
|
|
|
|
if (!options.userAgent) {
|
2015-09-03 07:02:40 +03:00
|
|
|
options.userAgent = format('triton/%s (%s-%s; node/%s)',
|
2015-08-25 23:11:40 +03:00
|
|
|
VERSION, OS_ARCH, OS_PLATFORM, process.versions.node);
|
2014-02-20 05:52:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX relevant?
|
|
|
|
//options.retryCallback = function checkFor500(code) {
|
|
|
|
// return (code === 500);
|
|
|
|
//};
|
|
|
|
|
|
|
|
// XXX relevant?
|
|
|
|
//this.token = options.token;
|
|
|
|
|
2015-09-23 22:32:09 +03:00
|
|
|
this.client = new SaferJsonClient(options);
|
2014-02-20 05:52:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype._getAuthHeaders = function _getAuthHeaders(callback) {
|
2014-02-20 05:52:58 +02:00
|
|
|
assert.func(callback, 'callback');
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var headers = {};
|
|
|
|
headers.date = new Date().toUTCString();
|
|
|
|
var sigstr = 'date: ' + headers.date;
|
|
|
|
|
|
|
|
//XXX
|
|
|
|
//if (this.token !== undefined) {
|
|
|
|
// obj.headers['X-Auth-Token'] = this.token;
|
|
|
|
//}
|
|
|
|
|
|
|
|
self.sign(sigstr, function (err, sig) {
|
|
|
|
if (err || !sig) {
|
|
|
|
callback(new errors.SigningError(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:02:40 +03:00
|
|
|
headers.authorization = format(
|
2014-02-20 05:52:58 +02:00
|
|
|
'Signature keyId="/%s/keys/%s",algorithm="%s",signature="%s"',
|
|
|
|
self.user, sig.keyId, sig.algorithm, sig.signature);
|
|
|
|
callback(null, headers);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-26 00:25:30 +03:00
|
|
|
/**
|
|
|
|
* Return an appropriate query string *with the leading '?'* from the given
|
|
|
|
* fields. If any of the field values are undefined or null, then they will
|
|
|
|
* be excluded.
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype._qs = function _qs(/* fields1, ...*/) {
|
2015-08-26 01:30:25 +03:00
|
|
|
var fields = Array.prototype.slice.call(arguments);
|
2015-08-26 00:25:30 +03:00
|
|
|
|
|
|
|
var query = {};
|
2015-08-26 01:30:25 +03:00
|
|
|
fields.forEach(function (field) {
|
|
|
|
Object.keys(field).forEach(function (key) {
|
|
|
|
var value = field[key];
|
2015-08-26 00:25:30 +03:00
|
|
|
if (value !== undefined && value !== null) {
|
|
|
|
query[key] = value;
|
|
|
|
}
|
|
|
|
});
|
2015-08-26 01:30:25 +03:00
|
|
|
});
|
2015-08-26 00:25:30 +03:00
|
|
|
|
|
|
|
if (Object.keys(query).length === 0) {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return '?' + querystring.stringify(query);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-04 21:04:45 +03:00
|
|
|
* Return an appropriate full URL *path* given an CloudApi subpath.
|
2015-08-26 00:25:30 +03:00
|
|
|
* This handles prepending the API's base path, if any: e.g. if the configured
|
|
|
|
* URL is "https://example.com/base/path".
|
|
|
|
*
|
|
|
|
* Optionally an object of query params can be passed in to include a query
|
|
|
|
* string. This just calls `this._qs(...)`.
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype._path = function _path(subpath /* , qparams, ... */) {
|
2015-08-26 00:25:30 +03:00
|
|
|
assert.string(subpath, 'subpath');
|
|
|
|
assert.ok(subpath[0] === '/');
|
|
|
|
|
|
|
|
var path = subpath;
|
2015-08-26 01:30:25 +03:00
|
|
|
var qparams = Array.prototype.slice.call(arguments, 1);
|
|
|
|
path += this._qs.apply(this, qparams);
|
2015-08-26 00:25:30 +03:00
|
|
|
return path;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-08-26 01:30:25 +03:00
|
|
|
/**
|
2015-08-26 01:47:29 +03:00
|
|
|
* Cloud API request wrapper - modeled after http.request
|
2015-08-26 01:30:25 +03:00
|
|
|
*
|
|
|
|
* @param {Object|String} options - object or string for endpoint
|
|
|
|
* - {String} path - URL endpoint to hit
|
|
|
|
* - {String} method - HTTP(s) request method
|
2015-08-26 04:09:32 +03:00
|
|
|
* - {Object} data - data to be passed
|
2015-08-26 01:30:25 +03:00
|
|
|
* @param {Function} callback passed via the restify client
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype._request = function _request(options, callback) {
|
2015-08-26 01:30:25 +03:00
|
|
|
var self = this;
|
2015-09-01 22:03:52 +03:00
|
|
|
if (typeof (options) === 'string')
|
2015-08-26 01:30:25 +03:00
|
|
|
options = {path: options};
|
|
|
|
assert.object(options, 'options');
|
|
|
|
assert.func(callback, 'callback');
|
2015-08-26 04:09:32 +03:00
|
|
|
assert.optionalObject(options.data, 'options.data');
|
2015-08-26 01:30:25 +03:00
|
|
|
|
|
|
|
var method = (options.method || 'GET').toLowerCase();
|
|
|
|
assert.ok(['get', 'post', 'delete', 'head'].indexOf(method) >= 0,
|
|
|
|
'invalid method given');
|
2015-08-26 08:25:26 +03:00
|
|
|
switch (method) {
|
2015-09-01 22:03:52 +03:00
|
|
|
case 'delete':
|
|
|
|
method = 'del';
|
2015-09-01 19:00:45 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-08-26 08:25:26 +03:00
|
|
|
}
|
2015-09-01 19:00:45 +03:00
|
|
|
|
2015-08-26 01:30:25 +03:00
|
|
|
self._getAuthHeaders(function (err, headers) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var opts = {
|
|
|
|
path: options.path,
|
|
|
|
headers: headers
|
|
|
|
};
|
2015-08-26 04:09:32 +03:00
|
|
|
if (options.data)
|
|
|
|
self.client[method](opts, options.data, callback);
|
|
|
|
else
|
|
|
|
self.client[method](opts, callback);
|
2015-08-26 01:30:25 +03:00
|
|
|
});
|
|
|
|
};
|
2014-02-20 05:52:58 +02:00
|
|
|
|
2015-08-26 23:40:50 +03:00
|
|
|
/**
|
|
|
|
* A simple wrapper around making a GET request to an endpoint and
|
|
|
|
* passing back the body returned
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype._passThrough =
|
2015-08-26 23:40:50 +03:00
|
|
|
function _passThrough(endpoint, opts, cb) {
|
2015-09-24 07:08:26 +03:00
|
|
|
var self = this;
|
2015-08-26 23:40:50 +03:00
|
|
|
if (typeof (opts) === 'function') {
|
|
|
|
cb = opts;
|
|
|
|
opts = null;
|
|
|
|
}
|
|
|
|
opts = opts || {};
|
2015-08-26 01:47:29 +03:00
|
|
|
|
2015-08-26 23:40:50 +03:00
|
|
|
assert.string(endpoint, 'endpoint');
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.func(cb, 'cb');
|
2015-08-26 01:47:29 +03:00
|
|
|
|
2015-08-26 23:40:50 +03:00
|
|
|
var p = this._path(endpoint, opts);
|
|
|
|
this._request(p, function (err, req, res, body) {
|
2015-09-24 07:08:26 +03:00
|
|
|
/*
|
|
|
|
* Improve this kind of error message:
|
|
|
|
*
|
|
|
|
* Error: DEPTH_ZERO_SELF_SIGNED_CERT
|
|
|
|
* at SecurePair.<anonymous> (tls.js:1381:32)
|
|
|
|
* at SecurePair.emit (events.js:92:17)
|
|
|
|
*
|
|
|
|
* TODO: could generalize this into a wrapErr method.
|
|
|
|
*/
|
|
|
|
if (err && err.message === 'DEPTH_ZERO_SELF_SIGNED_CERT' &&
|
|
|
|
self.client.rejectUnauthorized)
|
|
|
|
{
|
|
|
|
err = new errors.SelfSignedCertError(err, self.url);
|
|
|
|
}
|
|
|
|
|
2015-08-26 23:40:50 +03:00
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
2014-02-20 05:52:58 +02:00
|
|
|
|
2015-08-26 23:40:50 +03:00
|
|
|
// ---- networks
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get network information
|
|
|
|
*
|
|
|
|
* @param {Function} callback of the form `function (err, networks, response)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listNetworks = function listNetworks(opts, cb) {
|
2015-08-26 23:40:50 +03:00
|
|
|
var endpoint = format('/%s/networks', this.user);
|
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
2015-08-27 00:09:50 +03:00
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#GetNetwork>
|
|
|
|
*
|
|
|
|
* @param {String} - UUID
|
|
|
|
* @param {Function} callback of the form `function (err, network, res)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.getNetwork = function getNetwork(id, cb) {
|
2015-08-27 00:09:50 +03:00
|
|
|
assert.uuid(id, 'id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = this._path(format('/%s/networks/%s', this.user, id));
|
|
|
|
this._request(endpoint, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-27 02:56:18 +03:00
|
|
|
// ---- datacenters
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get services information
|
|
|
|
*
|
|
|
|
* @param {Function} callback of the form `function (err, services, response)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listServices = function listServices(opts, cb) {
|
2015-09-03 07:02:40 +03:00
|
|
|
var endpoint = format('/%s/services', this.user);
|
2015-08-27 02:56:18 +03:00
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
2015-08-27 02:59:28 +03:00
|
|
|
/**
|
|
|
|
* Get datacenters information
|
|
|
|
*
|
2015-09-02 20:47:06 +03:00
|
|
|
* @param {Function} callback of the form
|
|
|
|
* `function (err, datacenters, response)`
|
2015-08-27 02:59:28 +03:00
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listDatacenters = function listDatacenters(opts, cb) {
|
2015-09-03 07:02:40 +03:00
|
|
|
var endpoint = format('/%s/datacenters', this.user);
|
2015-08-27 02:59:28 +03:00
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
2015-08-26 23:40:50 +03:00
|
|
|
// ---- accounts
|
2015-08-26 06:44:08 +03:00
|
|
|
|
2014-02-20 05:52:58 +02:00
|
|
|
/**
|
2015-08-26 06:44:08 +03:00
|
|
|
* Get account information
|
2014-02-20 05:52:58 +02:00
|
|
|
*
|
2015-08-26 06:44:08 +03:00
|
|
|
* @param {Function} callback of the form `function (err, account, response)`
|
2014-02-20 05:52:58 +02:00
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.getAccount = function getAccount(opts, cb) {
|
2015-09-03 07:02:40 +03:00
|
|
|
var endpoint = format('/%s', this.user);
|
2015-08-26 23:40:50 +03:00
|
|
|
this._passThrough(endpoint, opts, cb);
|
2014-02-20 05:52:58 +02:00
|
|
|
};
|
|
|
|
|
2015-08-26 07:40:32 +03:00
|
|
|
/**
|
|
|
|
* Get public key information
|
|
|
|
*
|
|
|
|
* @param {Function} callback of the form `function (err, keys, response)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listKeys = function listKeys(opts, cb) {
|
2015-09-03 07:02:40 +03:00
|
|
|
var endpoint = format('/%s/keys', this.user);
|
2015-08-26 23:40:50 +03:00
|
|
|
this._passThrough(endpoint, opts, cb);
|
2015-08-26 07:40:32 +03:00
|
|
|
};
|
2014-02-20 05:52:58 +02:00
|
|
|
|
2015-08-26 00:25:30 +03:00
|
|
|
// ---- images
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#ListImages>
|
|
|
|
*
|
|
|
|
* @param {Object} options (optional)
|
2015-09-21 20:33:42 +03:00
|
|
|
* XXX be more strict about accepted options
|
2015-08-26 00:25:30 +03:00
|
|
|
* XXX document this, see the api doc above :)
|
|
|
|
* @param {Function} callback of the form `function (err, images, res)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listImages = function listImages(opts, cb) {
|
2015-09-03 07:02:40 +03:00
|
|
|
var endpoint = format('/%s/images', this.user);
|
2015-08-26 23:44:11 +03:00
|
|
|
this._passThrough(endpoint, opts, cb);
|
2015-08-26 01:47:29 +03:00
|
|
|
};
|
2015-08-26 00:25:30 +03:00
|
|
|
|
2015-08-26 01:47:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#ListImages>
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* - id {UUID}
|
|
|
|
* @param {Function} callback of the form `function (err, image, res)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.getImage = function getImage(options, callback) {
|
2015-08-26 01:47:29 +03:00
|
|
|
if (callback === undefined) {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
assert.object(options, 'options');
|
2015-08-26 02:12:35 +03:00
|
|
|
assert.uuid(options.id, 'options.id');
|
2015-08-26 01:47:29 +03:00
|
|
|
assert.func(callback, 'callback');
|
|
|
|
|
|
|
|
var endpoint = this._path(format('/%s/images/%s', this.user, options.id));
|
|
|
|
this._request(endpoint, function (err, req, res, body) {
|
|
|
|
callback(err, body, res);
|
2015-08-26 00:25:30 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-08-26 02:12:35 +03:00
|
|
|
// ---- packages
|
|
|
|
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listPackages = function listPackages(opts, cb) {
|
2015-09-03 07:02:40 +03:00
|
|
|
var endpoint = format('/%s/packages', this.user);
|
2015-08-26 23:44:11 +03:00
|
|
|
this._passThrough(endpoint, opts, cb);
|
2015-08-26 02:12:35 +03:00
|
|
|
};
|
|
|
|
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.getPackage = function getPackage(options, callback) {
|
2015-08-26 02:12:35 +03:00
|
|
|
if (callback === undefined) {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
assert.object(options, 'options');
|
|
|
|
assert.uuid(options.id, 'options.id');
|
|
|
|
assert.func(callback, 'callback');
|
|
|
|
|
|
|
|
var endpoint = this._path(format('/%s/packages/%s', this.user, options.id));
|
|
|
|
this._request(endpoint, function (err, req, res, body) {
|
|
|
|
callback(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-20 05:52:58 +02:00
|
|
|
// ---- machines
|
|
|
|
|
2015-07-26 08:45:20 +03:00
|
|
|
/**
|
|
|
|
* Get a machine by id.
|
|
|
|
*
|
|
|
|
* XXX add getCredentials equivalent
|
|
|
|
* XXX cloudapi docs don't doc the credentials=true option
|
|
|
|
*
|
2015-08-26 04:09:32 +03:00
|
|
|
* @param {String} uuid (required) The machine id.
|
2015-07-26 08:45:20 +03:00
|
|
|
* @param {Function} callback of the form `function (err, machine, response)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.getMachine = function getMachine(id, cb) {
|
2015-08-27 00:09:50 +03:00
|
|
|
assert.uuid(id, 'id');
|
|
|
|
assert.func(cb, 'cb');
|
2015-07-26 08:45:20 +03:00
|
|
|
|
2015-09-03 07:02:40 +03:00
|
|
|
var endpoint = format('/%s/machines/%s', this.user, id);
|
2015-08-26 03:27:46 +03:00
|
|
|
this._request(endpoint, function (err, req, res, body) {
|
2015-08-27 03:08:52 +03:00
|
|
|
cb(err, body, res);
|
2015-07-26 08:45:20 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-26 08:25:26 +03:00
|
|
|
/**
|
|
|
|
* delete a machine by id.
|
|
|
|
*
|
|
|
|
* @param {String} uuid (required) The machine id.
|
|
|
|
* @param {Function} callback of the form `function (err, machine, response)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.deleteMachine = function deleteMachine(uuid, callback) {
|
2015-08-26 08:25:26 +03:00
|
|
|
var self = this;
|
|
|
|
assert.string(uuid, 'uuid');
|
|
|
|
assert.func(callback, 'callback');
|
|
|
|
|
|
|
|
var opts = {
|
2015-09-03 07:02:40 +03:00
|
|
|
path: format('/%s/machines/%s', self.user, uuid),
|
2015-08-26 08:25:26 +03:00
|
|
|
method: 'DELETE'
|
|
|
|
};
|
|
|
|
this._request(opts, function (err, req, res, body) {
|
|
|
|
callback(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-26 04:09:32 +03:00
|
|
|
/**
|
|
|
|
* start a machine by id.
|
|
|
|
*
|
|
|
|
* @param {String} uuid (required) The machine id.
|
|
|
|
* @param {Function} callback of the form `function (err, machine, response)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.startMachine = function startMachine(uuid, callback) {
|
2015-08-26 04:09:32 +03:00
|
|
|
return this._doMachine('start', uuid, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stop a machine by id.
|
|
|
|
*
|
|
|
|
* @param {String} uuid (required) The machine id.
|
|
|
|
* @param {Function} callback of the form `function (err, machine, response)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.stopMachine = function stopMachine(uuid, callback) {
|
2015-08-26 04:09:32 +03:00
|
|
|
return this._doMachine('stop', uuid, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* reboot a machine by id.
|
|
|
|
*
|
|
|
|
* @param {String} uuid (required) The machine id.
|
|
|
|
* @param {Function} callback of the form `function (err, machine, response)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.rebootMachine = function rebootMachine(uuid, callback) {
|
2015-08-26 04:09:32 +03:00
|
|
|
return this._doMachine('reboot', uuid, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* internal function for start/stop/reboot
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype._doMachine = function _doMachine(action, uuid, callback) {
|
2015-08-26 04:09:32 +03:00
|
|
|
var self = this;
|
2015-08-26 04:46:14 +03:00
|
|
|
assert.string(action, 'action');
|
2015-08-26 04:09:32 +03:00
|
|
|
assert.string(uuid, 'uuid');
|
|
|
|
assert.func(callback, 'callback');
|
|
|
|
|
|
|
|
var opts = {
|
2015-09-03 07:02:40 +03:00
|
|
|
path: format('/%s/machines/%s', self.user, uuid),
|
2015-08-26 04:09:32 +03:00
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
action: action
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this._request(opts, function (err, req, res, body) {
|
|
|
|
callback(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-26 04:46:14 +03:00
|
|
|
/**
|
2015-08-26 06:53:48 +03:00
|
|
|
* Wait for a machine to go one of a set of specfic states.
|
2015-08-26 04:46:14 +03:00
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* - {String} id - machine UUID
|
2015-08-26 06:53:48 +03:00
|
|
|
* - {Array of String} states - desired state
|
2015-08-26 04:46:14 +03:00
|
|
|
* - {Number} interval (optional) - time in ms to poll
|
|
|
|
* @param {Function} callback - called when state is reached or on error
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.waitForMachineStates =
|
2015-09-02 20:47:06 +03:00
|
|
|
function waitForMachineStates(opts, callback) {
|
2015-08-26 04:46:14 +03:00
|
|
|
var self = this;
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.id, 'opts.id');
|
2015-08-26 06:53:48 +03:00
|
|
|
assert.arrayOfString(opts.states, 'opts.states');
|
2015-08-26 04:46:14 +03:00
|
|
|
assert.optionalNumber(opts.interval, 'opts.interval');
|
|
|
|
assert.func(callback, 'callback');
|
|
|
|
var interval = (opts.interval === undefined ? 1000 : opts.interval);
|
|
|
|
|
|
|
|
poll();
|
|
|
|
|
|
|
|
function poll() {
|
2015-08-26 08:25:26 +03:00
|
|
|
self.getMachine(opts.id, function (err, machine, res) {
|
2015-08-26 04:46:14 +03:00
|
|
|
if (err) {
|
2015-08-26 08:25:26 +03:00
|
|
|
callback(err, null, res);
|
2015-08-26 04:46:14 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-08-26 06:53:48 +03:00
|
|
|
if (opts.states.indexOf(machine.state) !== -1) {
|
2015-08-26 08:25:26 +03:00
|
|
|
callback(null, machine, res);
|
2015-08-26 04:46:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(poll, interval);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2015-07-26 08:45:20 +03:00
|
|
|
|
2014-02-20 05:52:58 +02:00
|
|
|
/**
|
|
|
|
* List the user's machines.
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#ListMachines>
|
|
|
|
*
|
2015-08-26 02:46:14 +03:00
|
|
|
* @param {Object} options
|
|
|
|
* See document above
|
|
|
|
* @return {LOMStream} a stream for each machine entry
|
2014-02-20 05:52:58 +02:00
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.createListMachinesStream =
|
2015-08-26 02:46:14 +03:00
|
|
|
function createListMachinesStream(options) {
|
|
|
|
var self = this;
|
2015-08-26 07:16:41 +03:00
|
|
|
options = options || {};
|
2015-08-26 02:46:14 +03:00
|
|
|
|
|
|
|
// if the user specifies an offset we don't paginate
|
|
|
|
var once = options.limit !== undefined;
|
|
|
|
|
|
|
|
return new LOMStream({
|
|
|
|
fetch: fetch,
|
|
|
|
limit: 1000,
|
|
|
|
offset: true
|
|
|
|
});
|
|
|
|
|
|
|
|
function fetch(fetcharg, limitObj, datacb, donecb) {
|
|
|
|
options.limit = limitObj.limit;
|
|
|
|
options.offset = limitObj.offset;
|
|
|
|
var endpoint = self._path(format('/%s/machines', self.user), options);
|
|
|
|
|
|
|
|
self._request(endpoint, function (err, req, res, body) {
|
2015-09-21 20:33:42 +03:00
|
|
|
if (err) {
|
|
|
|
return donecb(err);
|
|
|
|
}
|
2015-08-26 02:46:14 +03:00
|
|
|
var resourcecount = res.headers['x-resource-count'];
|
|
|
|
var done = once || resourcecount < options.limit;
|
2015-09-21 20:33:42 +03:00
|
|
|
donecb(null, {done: done, results: body});
|
2015-08-26 02:46:14 +03:00
|
|
|
});
|
2014-02-20 05:52:58 +02:00
|
|
|
}
|
2015-08-26 02:46:14 +03:00
|
|
|
};
|
2014-02-20 05:52:58 +02:00
|
|
|
|
2015-08-26 02:46:14 +03:00
|
|
|
/**
|
|
|
|
* List the user's machines.
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#ListMachines>
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* See document above
|
|
|
|
* @param {Function} callback - called like `function (err, machines)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listMachines = function listMachines(options, callback) {
|
2015-08-26 07:16:41 +03:00
|
|
|
if (typeof (options) === 'function') {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2015-08-26 02:46:14 +03:00
|
|
|
var machines = [];
|
|
|
|
var s = this.createListMachinesStream(options);
|
|
|
|
s.on('error', function (e) {
|
|
|
|
callback(e);
|
|
|
|
});
|
|
|
|
s.on('readable', function () {
|
|
|
|
var machine;
|
|
|
|
while ((machine = s.read()) !== null) {
|
|
|
|
machines.push(machine);
|
2014-02-20 05:52:58 +02:00
|
|
|
}
|
2015-08-26 02:46:14 +03:00
|
|
|
});
|
|
|
|
s.on('end', function () {
|
|
|
|
callback(null, machines);
|
|
|
|
});
|
2014-02-20 05:52:58 +02:00
|
|
|
};
|
|
|
|
|
2015-08-26 06:53:48 +03:00
|
|
|
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.createMachine = function createMachine(options, callback) {
|
2015-08-26 06:53:48 +03:00
|
|
|
assert.object(options, 'options');
|
|
|
|
assert.optionalString(options.name, 'options.name');
|
|
|
|
assert.uuid(options.image, 'options.image');
|
|
|
|
assert.uuid(options.package, 'options.package');
|
|
|
|
assert.optionalArrayOfUuid(options.networks, 'options.networks');
|
|
|
|
// TODO: assert the other fields
|
|
|
|
assert.func(callback, 'callback');
|
|
|
|
|
|
|
|
// XXX how does options.networks array work here?
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
2015-09-03 07:02:09 +03:00
|
|
|
path: format('/%s/machines', this.user),
|
|
|
|
data: options
|
2015-08-26 06:53:48 +03:00
|
|
|
}, function (err, req, res, body) {
|
|
|
|
callback(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-07-26 08:45:20 +03:00
|
|
|
/**
|
|
|
|
* List machine audit (successful actions on the machine).
|
|
|
|
*
|
|
|
|
* XXX IMO this endpoint should be called ListMachineAudit in cloudapi.
|
|
|
|
*
|
2015-09-08 19:41:31 +03:00
|
|
|
* @param {String} id (required) The machine id.
|
2015-07-26 08:45:20 +03:00
|
|
|
* @param {Function} callback of the form `function (err, audit, response)`
|
|
|
|
*/
|
2015-09-08 19:41:31 +03:00
|
|
|
CloudApi.prototype.machineAudit = function machineAudit(id, cb) {
|
|
|
|
assert.uuid(id, 'id');
|
|
|
|
assert.func(cb, 'cb');
|
2015-07-26 08:45:20 +03:00
|
|
|
|
2015-09-08 19:41:31 +03:00
|
|
|
var endpoint = format('/%s/machines/%s/audit', this.user, id);
|
|
|
|
this._request(endpoint, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
2015-07-26 08:45:20 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-20 05:52:58 +02:00
|
|
|
// --- Exports
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createClient: function (options) {
|
2015-09-04 21:04:45 +03:00
|
|
|
return new CloudApi(options);
|
2014-02-20 05:52:58 +02:00
|
|
|
}
|
|
|
|
};
|