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',
|
2015-11-04 01:40:59 +02:00
|
|
|
* account: <ACCOUNT>, // 'acmecorp'
|
|
|
|
* [user: <RBAC-USER>,] // 'bob'
|
2014-02-20 05:52:58 +02:00
|
|
|
* log: <BUNYAN-LOGGER>,
|
|
|
|
* sign: auth.cliSigner({
|
|
|
|
* keyId: <KEY-ID>, // ssh fingerprint
|
2015-11-04 01:40:59 +02:00
|
|
|
* // Unfortunately node-smartdc-auth uses user/subuser, while
|
|
|
|
* // node-triton uses account/user:
|
|
|
|
* user: <ACCOUNT>, // 'acmecorp'
|
|
|
|
* [subuser: <RBAC-USER>,] // 'bob'
|
2014-02-20 05:52:58 +02:00
|
|
|
* log: <BUNYAN-LOGGER>,
|
|
|
|
* }),
|
|
|
|
* ...
|
|
|
|
* });
|
|
|
|
* client.listImages(function (err, images) { ... });
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
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');
|
2015-11-14 02:19:35 +02:00
|
|
|
var auth = require('smartdc-auth');
|
2014-02-20 05:52:58 +02:00
|
|
|
|
2015-12-08 21:59:45 +02:00
|
|
|
var bunyannoop = require('./bunyannoop');
|
2015-12-09 21:59:47 +02:00
|
|
|
var common = require('./common');
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---- client API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a cloudapi client.
|
|
|
|
*
|
|
|
|
* @param options {Object}
|
|
|
|
* - {String} url (required) Cloud API base url
|
2015-11-04 01:40:59 +02:00
|
|
|
* - {String} account (required) The account login name.
|
2014-02-20 05:52:58 +02:00
|
|
|
* - {Function} sign (required) An http-signature auth signing function
|
2015-11-04 01:40:59 +02:00
|
|
|
* - {String} user (optional) The RBAC user login name.
|
2014-02-20 05:52:58 +02:00
|
|
|
* - {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');
|
2015-11-04 01:40:59 +02:00
|
|
|
assert.string(options.account, 'options.account');
|
2014-02-20 05:52:58 +02:00
|
|
|
assert.func(options.sign, 'options.sign');
|
2015-11-04 01:40:59 +02:00
|
|
|
assert.optionalString(options.user, 'options.user');
|
2014-02-20 05:52:58 +02:00
|
|
|
assert.optionalString(options.version, 'options.version');
|
|
|
|
assert.optionalObject(options.log, 'options.log');
|
|
|
|
|
|
|
|
this.url = options.url;
|
2015-11-04 01:40:59 +02:00
|
|
|
this.account = options.account;
|
|
|
|
this.user = options.user; // optional RBAC subuser
|
2014-02-20 05:52:58 +02:00
|
|
|
this.sign = options.sign;
|
2015-12-08 21:59:45 +02:00
|
|
|
this.log = options.log || new bunyannoop.BunyanNoopLogger();
|
2014-02-20 05:52:58 +02:00
|
|
|
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);
|
|
|
|
//};
|
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
// TODO support token auth
|
2014-02-20 05:52:58 +02:00
|
|
|
//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-12-08 21:59:45 +02:00
|
|
|
CloudApi.prototype.close = function close(callback) {
|
|
|
|
this.log.trace({host: this.client.url && this.client.url.host},
|
|
|
|
'close cloudapi http client');
|
|
|
|
this.client.close();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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 = {};
|
2015-11-14 02:19:35 +02:00
|
|
|
|
|
|
|
var rs = auth.requestSigner({
|
|
|
|
sign: self.sign
|
|
|
|
});
|
|
|
|
headers.date = rs.writeDateHeader();
|
2014-02-20 05:52:58 +02:00
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
// TODO: token auth support
|
2014-02-20 05:52:58 +02:00
|
|
|
//if (this.token !== undefined) {
|
|
|
|
// obj.headers['X-Auth-Token'] = this.token;
|
|
|
|
//}
|
|
|
|
|
2015-11-14 02:19:35 +02:00
|
|
|
rs.sign(function (err, authz) {
|
|
|
|
if (err || !authz) {
|
2014-02-20 05:52:58 +02:00
|
|
|
callback(new errors.SigningError(err));
|
|
|
|
return;
|
|
|
|
}
|
2015-11-14 02:19:35 +02:00
|
|
|
headers.authorization = authz;
|
2014-02-20 05:52:58 +02:00
|
|
|
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-11-04 01:40:59 +02:00
|
|
|
* Return an appropriate full URL *path* given a 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
|
|
|
*
|
2015-12-09 21:59:47 +02:00
|
|
|
* @param {Object|String} opts - object or string for endpoint
|
2015-08-26 01:30:25 +03:00
|
|
|
* - {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-12-09 21:59:47 +02:00
|
|
|
* - {Object} headers - optional additional request headers
|
|
|
|
* @param {Function} cb passed via the restify client
|
2015-08-26 01:30:25 +03:00
|
|
|
*/
|
2015-12-09 21:59:47 +02:00
|
|
|
CloudApi.prototype._request = function _request(opts, cb) {
|
2015-08-26 01:30:25 +03:00
|
|
|
var self = this;
|
2015-12-09 21:59:47 +02:00
|
|
|
if (typeof (opts) === 'string')
|
|
|
|
opts = {path: opts};
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.optionalObject(opts.data, 'opts.data');
|
|
|
|
assert.optionalString(opts.method, 'opts.method');
|
|
|
|
assert.optionalObject(opts.headers, 'opts.headers');
|
|
|
|
assert.func(cb, 'cb');
|
2015-08-26 01:30:25 +03:00
|
|
|
|
2015-12-09 21:59:47 +02:00
|
|
|
var method = (opts.method || 'GET').toLowerCase();
|
2015-11-10 01:09:37 +02:00
|
|
|
assert.ok(['get', 'post', 'put', 'delete', 'head'].indexOf(method) >= 0,
|
2015-08-26 01:30:25 +03:00
|
|
|
'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) {
|
2015-12-09 21:59:47 +02:00
|
|
|
cb(err);
|
2015-08-26 01:30:25 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-12-09 21:59:47 +02:00
|
|
|
if (opts.headers) {
|
|
|
|
common.objMerge(headers, opts.headers);
|
|
|
|
}
|
|
|
|
var reqOpts = {
|
|
|
|
path: opts.path,
|
2015-08-26 01:30:25 +03:00
|
|
|
headers: headers
|
|
|
|
};
|
2015-12-09 21:59:47 +02:00
|
|
|
if (opts.data)
|
|
|
|
self.client[method](reqOpts, opts.data, cb);
|
2015-08-26 04:09:32 +03:00
|
|
|
else
|
2015-12-09 21:59:47 +02:00
|
|
|
self.client[method](reqOpts, cb);
|
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-11-04 01:40:59 +02:00
|
|
|
CloudApi.prototype._passThrough = 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);
|
2016-02-09 22:23:55 +02:00
|
|
|
this._request({path: 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.
|
2016-01-15 08:56:38 +02:00
|
|
|
* TODO: this should be on _request, no? So that PUT, POST, etc. get it.
|
2015-09-24 07:08:26 +03:00
|
|
|
*/
|
|
|
|
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-11-04 01:40:59 +02:00
|
|
|
|
2016-03-19 01:51:22 +02:00
|
|
|
// ---- ping
|
|
|
|
|
|
|
|
CloudApi.prototype.ping = function ping(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var reqOpts = {
|
|
|
|
path: '/--ping',
|
|
|
|
// Ping should be fast. We don't want 15s of retrying.
|
|
|
|
retry: false
|
|
|
|
};
|
|
|
|
this.client.get(reqOpts, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
|
2015-08-26 23:40:50 +03:00
|
|
|
// ---- networks
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get network information
|
|
|
|
*
|
2015-11-06 01:13:14 +02:00
|
|
|
* @param {Function} callback of the form `function (err, networks, res)`
|
2015-08-26 23:40:50 +03:00
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listNetworks = function listNetworks(opts, cb) {
|
2015-11-04 01:40:59 +02:00
|
|
|
var endpoint = format('/%s/networks', this.account);
|
2015-08-26 23:40:50 +03:00
|
|
|
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');
|
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
var endpoint = this._path(format('/%s/networks/%s', this.account, id));
|
2015-08-27 00:09:50 +03:00
|
|
|
this._request(endpoint, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
|
|
|
|
|
2015-08-27 02:56:18 +03:00
|
|
|
// ---- datacenters
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get services information
|
|
|
|
*
|
2015-11-06 01:13:14 +02:00
|
|
|
* @param {Function} callback of the form `function (err, services, res)`
|
2015-08-27 02:56:18 +03:00
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listServices = function listServices(opts, cb) {
|
2015-11-04 01:40:59 +02:00
|
|
|
var endpoint = format('/%s/services', this.account);
|
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-11-06 01:13:14 +02:00
|
|
|
* @param {Function} callback of the form `function (err, datacenters, res)`
|
2015-08-27 02:59:28 +03:00
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listDatacenters = function listDatacenters(opts, cb) {
|
2015-11-04 01:40:59 +02:00
|
|
|
var endpoint = format('/%s/datacenters', this.account);
|
2015-08-27 02:59:28 +03:00
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
|
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-11-06 01:13:14 +02:00
|
|
|
* @param {Function} callback of the form `function (err, account, res)`
|
2014-02-20 05:52:58 +02:00
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.getAccount = function getAccount(opts, cb) {
|
2015-11-04 01:40:59 +02:00
|
|
|
var endpoint = format('/%s', this.account);
|
2015-08-26 23:40:50 +03:00
|
|
|
this._passThrough(endpoint, opts, cb);
|
2014-02-20 05:52:58 +02:00
|
|
|
};
|
|
|
|
|
2015-12-31 04:43:46 +02:00
|
|
|
|
2016-01-15 08:56:38 +02:00
|
|
|
// <updatable account field> -> <expected typeof>
|
|
|
|
CloudApi.prototype.UPDATE_ACCOUNT_FIELDS = {
|
|
|
|
email: 'string',
|
|
|
|
companyName: 'string',
|
|
|
|
firstName: 'string',
|
|
|
|
lastName: 'string',
|
|
|
|
address: 'string',
|
|
|
|
postalCode: 'string',
|
|
|
|
city: 'string',
|
|
|
|
state: 'string',
|
|
|
|
country: 'string',
|
|
|
|
phone: 'string',
|
|
|
|
triton_cns_enabled: 'boolean'
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update account fields.
|
|
|
|
* <https://apidocs.joyent.com/cloudapi/#UpdateAccount>
|
|
|
|
*
|
|
|
|
* @param opts {Object} A key for each account field to update.
|
|
|
|
* @param cb {Function} `function (err, updatedAccount, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.updateAccount = function updateAccount(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var update = {};
|
|
|
|
var unexpectedFields = [];
|
|
|
|
Object.keys(opts).forEach(function (field) {
|
|
|
|
var type = self.UPDATE_ACCOUNT_FIELDS[field];
|
|
|
|
if (type) {
|
|
|
|
assert[type === 'boolean' ? 'bool' : type](opts[field],
|
|
|
|
'opts.'+field);
|
|
|
|
update[field] = opts[field];
|
|
|
|
} else {
|
|
|
|
unexpectedFields.push(field);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (unexpectedFields.length > 0) {
|
|
|
|
throw new Error(format('unknown field(s) for UpdateAccount: %s',
|
|
|
|
unexpectedFields.sort().join(', ')));
|
|
|
|
}
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s', this.account),
|
|
|
|
data: update
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-08-26 07:40:32 +03:00
|
|
|
/**
|
2015-11-06 01:13:14 +02:00
|
|
|
* List account's SSH keys.
|
2015-08-26 07:40:32 +03:00
|
|
|
*
|
2015-12-31 04:43:46 +02:00
|
|
|
* @param {Object} opts (object)
|
2015-11-06 01:13:14 +02:00
|
|
|
* @param {Function} callback of the form `function (err, keys, res)`
|
2015-08-26 07:40:32 +03:00
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listKeys = function listKeys(opts, cb) {
|
2015-12-31 04:43:46 +02:00
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
var endpoint = format('/%s/keys', this.account);
|
2015-12-31 04:43:46 +02:00
|
|
|
this._passThrough(endpoint, {}, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an account's SSH key.
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object)
|
|
|
|
* - {String} fingerprint (required*) The SSH key fingerprint. One of
|
|
|
|
* 'fingerprint' or 'name' is required.
|
|
|
|
* - {String} name (required*) The SSH key name. One of 'fingerprint'
|
|
|
|
* or 'name' is required.
|
2016-01-04 20:26:17 +02:00
|
|
|
* @param {Function} callback of the form `function (err, body, res)`
|
2015-12-31 04:43:46 +02:00
|
|
|
*/
|
|
|
|
CloudApi.prototype.getKey = function getKey(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.optionalString(opts.fingerprint, 'opts.fingerprint');
|
|
|
|
assert.optionalString(opts.name, 'opts.name');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var identifier = opts.fingerprint || opts.name;
|
2016-01-04 20:26:17 +02:00
|
|
|
assert.ok(identifier, 'one of "fingerprint" or "name" is required');
|
2015-12-31 04:43:46 +02:00
|
|
|
|
|
|
|
var endpoint = format('/%s/keys/%s', this.account,
|
|
|
|
encodeURIComponent(identifier));
|
|
|
|
this._passThrough(endpoint, {}, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create/upload a new account SSH public key.
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object)
|
|
|
|
* - {String} key (required) The SSH public key content.
|
|
|
|
* - {String} name (optional) A name for the key. If not given, the
|
|
|
|
* key fingerprint will be used.
|
2016-01-04 20:26:17 +02:00
|
|
|
* @param {Function} callback of the form `function (err, key, res)`
|
2015-12-31 04:43:46 +02:00
|
|
|
*/
|
|
|
|
CloudApi.prototype.createKey = function createKey(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.key, 'opts.key');
|
|
|
|
assert.optionalString(opts.name, 'opts.name');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
name: opts.name,
|
|
|
|
key: opts.key
|
|
|
|
};
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/keys', this.account),
|
|
|
|
data: data
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an account's SSH key.
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object)
|
|
|
|
* - {String} fingerprint (required*) The SSH key fingerprint. One of
|
|
|
|
* 'fingerprint' or 'name' is required.
|
|
|
|
* - {String} name (required*) The SSH key name. One of 'fingerprint'
|
|
|
|
* or 'name' is required.
|
|
|
|
* @param {Function} callback of the form `function (err, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.deleteKey = function deleteKey(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.optionalString(opts.fingerprint, 'opts.fingerprint');
|
|
|
|
assert.optionalString(opts.name, 'opts.name');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var identifier = opts.fingerprint || opts.name;
|
2016-01-04 20:26:17 +02:00
|
|
|
assert.ok(identifier, 'one of "fingerprint" or "name" is required');
|
2015-12-31 04:43:46 +02:00
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'DELETE',
|
|
|
|
path: format('/%s/keys/%s', this.account,
|
|
|
|
encodeURIComponent(identifier))
|
|
|
|
}, function (err, req, res) {
|
|
|
|
cb(err, res);
|
|
|
|
});
|
2015-08-26 07:40:32 +03:00
|
|
|
};
|
2014-02-20 05:52:58 +02:00
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
|
2015-08-26 00:25:30 +03:00
|
|
|
// ---- images
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#ListImages>
|
|
|
|
*
|
2015-11-04 01:40:59 +02:00
|
|
|
* @param {Object} opts (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 :)
|
2015-11-04 01:40:59 +02:00
|
|
|
* @param {Function} cb of the form `function (err, images, res)`
|
2015-08-26 00:25:30 +03:00
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listImages = function listImages(opts, cb) {
|
2015-11-04 01:40:59 +02:00
|
|
|
var endpoint = format('/%s/images', this.account);
|
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
|
|
|
/**
|
2015-11-04 01:40:59 +02:00
|
|
|
* <http://apidocs.joyent.com/cloudapi/#GetImage>
|
2015-08-26 01:47:29 +03:00
|
|
|
*
|
2015-11-04 01:40:59 +02:00
|
|
|
* @param {Object} opts
|
2015-08-26 01:47:29 +03:00
|
|
|
* - id {UUID}
|
2015-11-04 01:40:59 +02:00
|
|
|
* @param {Function} cb of the form `function (err, image, res)`
|
2015-08-26 01:47:29 +03:00
|
|
|
*/
|
2015-11-04 01:40:59 +02:00
|
|
|
CloudApi.prototype.getImage = function getImage(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
2015-08-26 01:47:29 +03:00
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
var endpoint = this._path(format('/%s/images/%s', this.account, opts.id));
|
2015-08-26 01:47:29 +03:00
|
|
|
this._request(endpoint, function (err, req, res, body) {
|
2015-11-04 01:40:59 +02:00
|
|
|
cb(err, body, res);
|
2015-08-26 00:25:30 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-01-26 09:23:36 +02:00
|
|
|
/**
|
|
|
|
* Delete an image by id.
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#DeleteImage>
|
|
|
|
*
|
|
|
|
* @param {String} id (required) The image id.
|
|
|
|
* @param {Function} callback of the form `function (err, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.deleteImage = function deleteImage(id, callback) {
|
|
|
|
var self = this;
|
|
|
|
assert.uuid(id, 'id');
|
|
|
|
assert.func(callback, 'callback');
|
|
|
|
|
|
|
|
var opts = {
|
|
|
|
path: format('/%s/images/%s', self.account, id),
|
|
|
|
method: 'DELETE'
|
|
|
|
};
|
|
|
|
this._request(opts, function (err, req, res) {
|
|
|
|
callback(err, res);
|
|
|
|
});
|
|
|
|
};
|
2015-08-26 00:25:30 +03:00
|
|
|
|
2016-01-19 22:30:46 +02:00
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#CreateImageFromMachine>
|
|
|
|
*
|
|
|
|
* @param {Object} opts
|
|
|
|
* - {UUID} machine Required. The ID of the machine from which to create
|
|
|
|
* the image.
|
|
|
|
* - {String} name Required. The image name.
|
|
|
|
* - {String} version Required. The image version.
|
|
|
|
* - {String} description Optional. A short description.
|
|
|
|
* - {String} homepage Optional. Homepage URL.
|
|
|
|
* - {String} eula Optional. EULA URL.
|
|
|
|
* - {Array} acl Optional. An array of account UUIDs to which to give
|
|
|
|
* access. "Access Control List."
|
|
|
|
* - {Object} tags Optional.
|
|
|
|
* @param {Function} cb of the form `function (err, image, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.createImageFromMachine =
|
|
|
|
function createImageFromMachine(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.machine, 'opts.machine');
|
|
|
|
assert.string(opts.name, 'opts.name');
|
|
|
|
assert.string(opts.version, 'opts.version');
|
|
|
|
assert.optionalString(opts.description, 'opts.description');
|
|
|
|
assert.optionalString(opts.homepage, 'opts.homepage');
|
|
|
|
assert.optionalString(opts.eula, 'opts.eula');
|
|
|
|
assert.optionalArrayOfUuid(opts.acl, 'opts.acl');
|
|
|
|
assert.optionalObject(opts.tags, 'opts.tags');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/images', this.account),
|
|
|
|
data: opts
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for an image to go one of a set of specfic states.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* - {String} id - machine UUID
|
|
|
|
* - {Array of String} states - desired state
|
|
|
|
* - {Number} interval (optional) - Time in ms to poll. Default is 1000ms.
|
|
|
|
* @param {Function} cb - `function (err, image, res)`
|
|
|
|
* Called when state is reached or on error
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.waitForImageStates =
|
|
|
|
function waitForImageStates(opts, cb) {
|
|
|
|
var self = this;
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.arrayOfString(opts.states, 'opts.states');
|
|
|
|
assert.optionalNumber(opts.interval, 'opts.interval');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
var interval = (opts.interval === undefined ? 1000 : opts.interval);
|
|
|
|
assert.ok(interval > 0, 'interval must be a positive number');
|
|
|
|
|
|
|
|
poll();
|
|
|
|
|
|
|
|
function poll() {
|
|
|
|
self.getImage({id: opts.id}, function (err, img, res) {
|
|
|
|
if (err) {
|
|
|
|
cb(err, null, res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (opts.states.indexOf(img.state) !== -1) {
|
|
|
|
cb(null, img, res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(poll, interval);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-08-26 02:12:35 +03:00
|
|
|
// ---- packages
|
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#ListPackages>
|
|
|
|
*
|
|
|
|
* @param {Object} opts (optional)
|
|
|
|
* XXX be more strict about accepted options
|
|
|
|
* XXX document this, see the api doc above :)
|
|
|
|
* @param {Function} callback of the form `function (err, packages, res)`
|
|
|
|
*/
|
2015-09-04 21:04:45 +03:00
|
|
|
CloudApi.prototype.listPackages = function listPackages(opts, cb) {
|
2015-11-04 01:40:59 +02:00
|
|
|
var endpoint = format('/%s/packages', this.account);
|
2015-08-26 23:44:11 +03:00
|
|
|
this._passThrough(endpoint, opts, cb);
|
2015-08-26 02:12:35 +03:00
|
|
|
};
|
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#GetPackage>
|
|
|
|
*
|
|
|
|
* @param {Object} opts
|
|
|
|
* - id {UUID|String} Package ID (a UUID) or name.
|
|
|
|
* @param {Function} cb of the form `function (err, package, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.getPackage = function getPackage(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
2015-08-26 02:12:35 +03:00
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
// XXX use _passThrough?
|
|
|
|
var endpoint = this._path(format('/%s/packages/%s', this.account, opts.id));
|
2015-08-26 02:12:35 +03:00
|
|
|
this._request(endpoint, function (err, req, res, body) {
|
2015-11-04 01:40:59 +02:00
|
|
|
cb(err, body, res);
|
2015-08-26 02:12:35 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
2016-01-19 22:30:46 +02:00
|
|
|
* For backwards compat, calling with `getMachine(id, cb)` is allowed.
|
|
|
|
*
|
|
|
|
* @param {Object} opts
|
|
|
|
* - id {UUID} Required. The machine id.
|
|
|
|
* @param {Function} cb of the form `function (err, machine, res)`
|
2015-07-26 08:45:20 +03:00
|
|
|
*/
|
2016-01-19 22:30:46 +02:00
|
|
|
CloudApi.prototype.getMachine = function getMachine(opts, cb) {
|
|
|
|
if (typeof (opts) === 'string') {
|
|
|
|
opts = {id: opts};
|
|
|
|
}
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
2015-07-26 08:45:20 +03:00
|
|
|
|
2016-01-19 22:30:46 +02:00
|
|
|
var endpoint = format('/%s/machines/%s', this.account, opts.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.
|
|
|
|
*
|
2016-01-26 09:23:36 +02:00
|
|
|
* @param {String} id (required) The machine id.
|
2015-11-05 22:30:06 +02:00
|
|
|
* @param {Function} callback of the form `function (err, res)`
|
2015-08-26 08:25:26 +03:00
|
|
|
*/
|
2016-01-26 09:23:36 +02:00
|
|
|
CloudApi.prototype.deleteMachine = function deleteMachine(id, callback) {
|
2015-08-26 08:25:26 +03:00
|
|
|
var self = this;
|
2016-01-26 09:23:36 +02:00
|
|
|
assert.uuid(id, 'id');
|
2015-08-26 08:25:26 +03:00
|
|
|
assert.func(callback, 'callback');
|
|
|
|
|
|
|
|
var opts = {
|
2016-01-26 09:23:36 +02:00
|
|
|
path: format('/%s/machines/%s', self.account, id),
|
2015-08-26 08:25:26 +03:00
|
|
|
method: 'DELETE'
|
|
|
|
};
|
2015-11-05 22:30:06 +02:00
|
|
|
this._request(opts, function (err, req, res) {
|
|
|
|
callback(err, res);
|
2015-08-26 08:25:26 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-26 04:09:32 +03:00
|
|
|
/**
|
|
|
|
* start a machine by id.
|
|
|
|
*
|
|
|
|
* @param {String} uuid (required) The machine id.
|
2015-11-06 01:13:14 +02:00
|
|
|
* @param {Function} callback of the form `function (err, machine, res)`
|
2015-08-26 04:09:32 +03:00
|
|
|
*/
|
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.
|
2015-11-06 01:13:14 +02:00
|
|
|
* @param {Function} callback of the form `function (err, machine, res)`
|
2015-08-26 04:09:32 +03:00
|
|
|
*/
|
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.
|
2015-11-06 01:13:14 +02:00
|
|
|
* @param {Function} callback of the form `function (err, machine, res)`
|
2015-08-26 04:09:32 +03:00
|
|
|
*/
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-02-04 15:39:50 +02:00
|
|
|
* Enables machine firewall.
|
|
|
|
*
|
|
|
|
* @param {String} id (required) The machine id.
|
2016-03-11 16:07:11 +02:00
|
|
|
* @param {Function} callback of the form `function (err, null, res)`
|
2016-02-04 15:39:50 +02:00
|
|
|
*/
|
|
|
|
CloudApi.prototype.enableMachineFirewall =
|
|
|
|
function enableMachineFirewall(uuid, callback) {
|
|
|
|
return this._doMachine('enable_firewall', uuid, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables machine firewall.
|
|
|
|
*
|
|
|
|
* @param {String} id (required) The machine id.
|
2016-03-11 16:07:11 +02:00
|
|
|
* @param {Function} callback of the form `function (err, null, res)`
|
2016-02-04 15:39:50 +02:00
|
|
|
*/
|
|
|
|
CloudApi.prototype.disableMachineFirewall =
|
|
|
|
function disableMachineFirewall(uuid, callback) {
|
|
|
|
return this._doMachine('disable_firewall', uuid, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* internal function for start/stop/reboot/enable_firewall/disable_firewall
|
2015-08-26 04:09:32 +03:00
|
|
|
*/
|
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-11-04 01:40:59 +02:00
|
|
|
path: format('/%s/machines/%s', self.account, 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 =
|
2016-01-19 22:30:46 +02:00
|
|
|
function waitForMachineStates(opts, callback) {
|
2015-08-26 04:46:14 +03:00
|
|
|
var self = this;
|
|
|
|
assert.object(opts, 'opts');
|
2016-01-19 22:30:46 +02:00
|
|
|
assert.uuid(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);
|
2016-01-19 22:30:46 +02:00
|
|
|
assert.ok(interval > 0, 'interval must be a positive number');
|
2015-08-26 04:46:14 +03:00
|
|
|
|
|
|
|
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
|
|
|
/**
|
2015-11-04 01:40:59 +02:00
|
|
|
* List the account's machines.
|
2014-02-20 05:52:58 +02:00
|
|
|
* <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
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
// If a `limit` is specified, we don't paginate.
|
2015-08-26 02:46:14 +03:00
|
|
|
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;
|
2015-11-04 01:40:59 +02:00
|
|
|
var endpoint = self._path(
|
|
|
|
format('/%s/machines', self.account), options);
|
2015-08-26 02:46:14 +03:00
|
|
|
|
|
|
|
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
|
|
|
/**
|
2015-11-04 01:40:59 +02:00
|
|
|
* List the account's machines.
|
2015-08-26 02:46:14 +03:00
|
|
|
* <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-11-04 01:40:59 +02:00
|
|
|
path: format('/%s/machines', this.account),
|
2015-09-03 07:02:09 +03:00
|
|
|
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-11-06 01:13:14 +02:00
|
|
|
* @param {Function} callback of the form `function (err, audit, res)`
|
2015-07-26 08:45:20 +03:00
|
|
|
*/
|
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-11-04 01:40:59 +02:00
|
|
|
var endpoint = format('/%s/machines/%s/audit', this.account, id);
|
2015-09-08 19:41:31 +03:00
|
|
|
this._request(endpoint, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
2015-07-26 08:45:20 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-03-11 16:07:11 +02:00
|
|
|
/**
|
2016-03-11 21:24:44 +02:00
|
|
|
* Wait for a machine's `firewall_enabled` field to go true/false.
|
2016-03-11 16:07:11 +02:00
|
|
|
*
|
|
|
|
* @param {Object} options
|
2016-03-11 21:24:44 +02:00
|
|
|
* - {String} id: Required. The machine UUID.
|
|
|
|
* - {Boolean} state: Required. The desired `firewall_enabled` state.
|
|
|
|
* - {Number} interval: Optional. Time (in ms) to poll.
|
2016-03-11 16:07:11 +02:00
|
|
|
* @param {Function} callback of the form f(err, machine, res).
|
|
|
|
*/
|
2016-03-11 21:24:44 +02:00
|
|
|
CloudApi.prototype.waitForMachineFirewallEnabled =
|
|
|
|
function waitForMachineFirewallEnabled(opts, cb) {
|
2016-03-11 16:07:11 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.bool(opts.state, 'opts.state');
|
|
|
|
assert.optionalNumber(opts.interval, 'opts.interval');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var interval = opts.interval || 1000;
|
|
|
|
assert.ok(interval > 0, 'interval must be a positive number');
|
|
|
|
|
|
|
|
poll();
|
|
|
|
|
|
|
|
function poll() {
|
|
|
|
self.getMachine({
|
|
|
|
id: opts.id
|
|
|
|
}, function (err, machine, res) {
|
|
|
|
if (err) {
|
|
|
|
cb(err, null, res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.state === machine.firewall_enabled) {
|
|
|
|
cb(null, machine, res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(poll, interval);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-02-09 22:23:55 +02:00
|
|
|
// --- machine tags
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#ListMachineTags>
|
|
|
|
*
|
|
|
|
* @param {Object} opts:
|
|
|
|
* - @param {UUID} id: The machine UUID.
|
|
|
|
* @param {Function} cb - `function (err, tags, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.listMachineTags = function listMachineTags(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/machines/%s/tags', this.account, opts.id);
|
|
|
|
this._passThrough(endpoint, {}, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#GetMachineTag>
|
|
|
|
*
|
|
|
|
* @param {Object} opts:
|
|
|
|
* - @param {UUID} id: The machine UUID. Required.
|
|
|
|
* - @param {UUID} tag: The tag name. Required.
|
|
|
|
* @param {Function} cb - `function (err, value, res)`
|
|
|
|
* On success, `value` is the tag value *as a string*. See note above.
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.getMachineTag = function getMachineTag(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.string(opts.tag, 'opts.tag');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
path: format('/%s/machines/%s/tags/%s', this.account, opts.id,
|
|
|
|
encodeURIComponent(opts.tag))
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#AddMachineTags>
|
|
|
|
*
|
|
|
|
* @param {Object} opts:
|
|
|
|
* - @param {UUID} id: The machine UUID. Required.
|
|
|
|
* - @param {Object} tags: The tag name/value pairs.
|
|
|
|
* @param {Function} cb - `function (err, tags, res)`
|
|
|
|
* On success, `tags` is the updated set of instance tags.
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.addMachineTags = function addMachineTags(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.object(opts.tags, 'opts.tags');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
// TODO: should this strictly guard on opts.tags types?
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/machines/%s/tags', this.account, opts.id),
|
|
|
|
data: opts.tags
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#ReplaceMachineTags>
|
|
|
|
*
|
|
|
|
* @param {Object} opts:
|
|
|
|
* - @param {UUID} id: The machine UUID. Required.
|
|
|
|
* - @param {Object} tags: The tag name/value pairs.
|
|
|
|
* @param {Function} cb - `function (err, tags, res)`
|
|
|
|
* On success, `tags` is the updated set of instance tags.
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.replaceMachineTags = function replaceMachineTags(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.object(opts.tags, 'opts.tags');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
// TODO: should this strictly guard on opts.tags types?
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'PUT',
|
|
|
|
path: format('/%s/machines/%s/tags', this.account, opts.id),
|
|
|
|
data: opts.tags
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-02-04 15:39:50 +02:00
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#DeleteMachineTags>
|
|
|
|
*
|
|
|
|
* @param {Object} opts:
|
|
|
|
* - @param {UUID} id: The machine UUID. Required.
|
|
|
|
* @param {Function} cb - `function (err, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.deleteMachineTags = function deleteMachineTags(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'DELETE',
|
|
|
|
path: format('/%s/machines/%s/tags', this.account, opts.id)
|
|
|
|
}, function (err, req, res) {
|
|
|
|
cb(err, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-02-09 22:23:55 +02:00
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#DeleteMachineTag>
|
|
|
|
*
|
|
|
|
* @param {Object} opts:
|
|
|
|
* - @param {UUID} id: The machine UUID. Required.
|
|
|
|
* - @param {String} tag: The tag name. Required.
|
|
|
|
* @param {Function} cb - `function (err, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.deleteMachineTag = function deleteMachineTag(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.string(opts.tag, 'opts.tag');
|
|
|
|
assert.ok(opts.tag, 'opts.tag cannot be empty');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'DELETE',
|
|
|
|
path: format('/%s/machines/%s/tags/%s', this.account, opts.id,
|
|
|
|
encodeURIComponent(opts.tag))
|
|
|
|
}, function (err, req, res) {
|
|
|
|
cb(err, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-02-04 15:39:50 +02:00
|
|
|
|
|
|
|
// --- snapshots
|
|
|
|
|
2016-02-09 22:23:55 +02:00
|
|
|
/**
|
2016-02-04 15:39:50 +02:00
|
|
|
* Creates a new snapshot for a given machine.
|
2016-02-09 22:23:55 +02:00
|
|
|
*
|
2016-02-04 15:39:50 +02:00
|
|
|
* The machine cannot be a KVM brand.
|
|
|
|
*
|
|
|
|
* Returns a snapshot object.
|
|
|
|
*
|
|
|
|
* @param {Object} options object containing:
|
|
|
|
* - {String} id (required) the machine's id.
|
|
|
|
* - {String} name (optional) name for new snapshot
|
|
|
|
* @param {Function} callback of the form f(err, snapshot, res).
|
2016-02-09 22:23:55 +02:00
|
|
|
*/
|
2016-02-04 15:39:50 +02:00
|
|
|
CloudApi.prototype.createMachineSnapshot =
|
|
|
|
function createMachineSnapshot(opts, cb) {
|
2016-02-09 22:23:55 +02:00
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
2016-02-04 15:39:50 +02:00
|
|
|
assert.optionalString(opts.name, 'opts.name');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var data = {};
|
|
|
|
if (opts.name)
|
|
|
|
data.name = opts.name;
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/machines/%s/snapshots', this.account, opts.id),
|
|
|
|
data: data
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for a machine's snapshot to go one of a set of specfic states.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* - {String} id {required} machine id
|
|
|
|
* - {String} name (optional) name for new snapshot
|
|
|
|
* - {Array of String} states - desired state
|
|
|
|
* - {Number} interval (optional) - time in ms to poll
|
|
|
|
* @param {Function} callback of the form f(err, snapshot, res).
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.waitForSnapshotStates =
|
|
|
|
function waitForSnapshotStates(opts, cb) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.string(opts.name, 'opts.name');
|
|
|
|
assert.arrayOfString(opts.states, 'opts.states');
|
|
|
|
assert.optionalNumber(opts.interval, 'opts.interval');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var interval = opts.interval || 1000;
|
|
|
|
assert.ok(interval > 0, 'interval must be a positive number');
|
|
|
|
|
|
|
|
poll();
|
|
|
|
|
|
|
|
function poll() {
|
|
|
|
self.getMachineSnapshot({
|
|
|
|
id: opts.id,
|
|
|
|
name: opts.name
|
|
|
|
}, function (err, snapshot, res) {
|
|
|
|
if (err) {
|
|
|
|
cb(err, null, res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (opts.states.indexOf(snapshot.state) !== -1) {
|
|
|
|
cb(null, snapshot, res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(poll, interval);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists all snapshots for a given machine.
|
|
|
|
*
|
|
|
|
* Returns a list of snapshot objects.
|
|
|
|
*
|
|
|
|
* @param {Object} options object containing:
|
|
|
|
* - {String} id (required) the machine's id.
|
|
|
|
* @param {Function} callback of the form f(err, snapshot, res).
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.listMachineSnapshots =
|
|
|
|
function listMachineSnapshots(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/machines/%s/snapshots', this.account, opts.id);
|
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a single snapshot for a given machine.
|
|
|
|
*
|
|
|
|
* Returns a snapshot object.
|
|
|
|
*
|
|
|
|
* @param {Object} options object containing:
|
|
|
|
* - {String} id (required) the machine's id.
|
|
|
|
* - {String} name (required) the snapshot's name.
|
|
|
|
* @param {Function} callback of the form f(err, snapshot, res).
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.getMachineSnapshot =
|
|
|
|
function getMachineSnapshot(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.string(opts.name, 'opts.name');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/machines/%s/snapshots/%s', this.account, opts.id,
|
|
|
|
encodeURIComponent(opts.name));
|
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Re/boots a machine from a snapshot.
|
|
|
|
*
|
|
|
|
* @param {Object} options object containing:
|
|
|
|
* - {String} id (required) the machine's id.
|
|
|
|
* - {String} name (required) the snapshot's name.
|
|
|
|
* @param {Function} callback of the form f(err, res).
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.startMachineFromSnapshot =
|
|
|
|
function startMachineFromSnapshot(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.string(opts.name, 'opts.name');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/machines/%s/snapshots/%s', this.account, opts.id,
|
|
|
|
encodeURIComponent(opts.name)),
|
|
|
|
data: opts
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a machine snapshot.
|
|
|
|
*
|
|
|
|
* @param {Object} options object containing:
|
|
|
|
* - {String} id (required) the machine's id.
|
|
|
|
* - {String} name (required) the snapshot's name.
|
|
|
|
* @param {Function} callback of the form f(err, res).
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.deleteMachineSnapshot =
|
|
|
|
function deleteMachineSnapshot(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.string(opts.name, 'opts.name');
|
2016-02-09 22:23:55 +02:00
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'DELETE',
|
2016-02-04 15:39:50 +02:00
|
|
|
path: format('/%s/machines/%s/snapshots/%s', this.account, opts.id,
|
|
|
|
encodeURIComponent(opts.name))
|
2016-02-09 22:23:55 +02:00
|
|
|
}, function (err, req, res) {
|
|
|
|
cb(err, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-02-04 15:39:50 +02:00
|
|
|
// --- firewall rules
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Firewall Rule.
|
|
|
|
*
|
|
|
|
* @param {Object} options object containing:
|
|
|
|
* - {String} rule (required) the fwrule text.
|
|
|
|
* - {Boolean} enabled (optional) default to false.
|
|
|
|
* - {String} description (optional)
|
|
|
|
* @param {Function} callback of the form f(err, fwrule, res).
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.createFirewallRule =
|
|
|
|
function createFirewallRule(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.rule, 'opts.rule');
|
|
|
|
assert.optionalString(opts.description, 'opts.description');
|
|
|
|
assert.optionalBool(opts.enabled, 'opts.enabled');
|
|
|
|
|
|
|
|
var data = {};
|
|
|
|
Object.keys(this.UPDATE_FWRULE_FIELDS).forEach(function (attr) {
|
|
|
|
if (opts[attr] !== undefined)
|
|
|
|
data[attr] = opts[attr];
|
|
|
|
});
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/fwrules', this.account),
|
|
|
|
data: data
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists all your Firewall Rules.
|
|
|
|
*
|
|
|
|
* Returns an array of objects.
|
|
|
|
*
|
|
|
|
* @param opts {Object} Options
|
|
|
|
* @param {Function} callback of the form f(err, fwrules, res).
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.listFirewallRules =
|
|
|
|
function listFirewallRules(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/fwrules', this.account);
|
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a Firewall Rule.
|
|
|
|
*
|
|
|
|
* @param {UUID} id: The firewall rule id.
|
|
|
|
* @param {Function} callback of the form `function (err, fwrule, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.getFirewallRule =
|
|
|
|
function getFirewallRule(id, cb) {
|
|
|
|
assert.uuid(id, 'id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/fwrules/%s', this.account, id);
|
|
|
|
this._request(endpoint, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// <updatable account field> -> <expected typeof>
|
|
|
|
CloudApi.prototype.UPDATE_FWRULE_FIELDS = {
|
|
|
|
enabled: 'boolean',
|
|
|
|
rule: 'string',
|
|
|
|
description: 'string'
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a Firewall Rule.
|
|
|
|
*
|
|
|
|
* Dev Note: That 'rule' is *required* here is lame. Hoping to change that
|
|
|
|
* in cloudapi.
|
|
|
|
*
|
|
|
|
* @param {Object} opts object containing:
|
|
|
|
* - {UUID} id: The fwrule id. Required.
|
|
|
|
* - {String} rule: The fwrule text. Required.
|
|
|
|
* - {Boolean} enabled: Optional.
|
|
|
|
* - {String} description: Description of the rule. Optional.
|
|
|
|
* @param {Function} callback of the form `function (err, fwrule, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.updateFirewallRule =
|
|
|
|
function updateFirewallRule(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.string(opts.rule, 'opts.rule');
|
|
|
|
assert.optionalBool(opts.enabled, 'opts.enabled');
|
|
|
|
assert.optionalString(opts.description, 'opts.description');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var data = {};
|
|
|
|
Object.keys(this.UPDATE_FWRULE_FIELDS).forEach(function (attr) {
|
|
|
|
if (opts[attr] !== undefined)
|
|
|
|
data[attr] = opts[attr];
|
|
|
|
});
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/fwrules/%s', this.account, opts.id),
|
|
|
|
data: data
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable a Firewall Rule.
|
|
|
|
*
|
|
|
|
* @param {Object} opts
|
|
|
|
* - {UUID} id: The firewall id. Required.
|
|
|
|
* @param {Function} callback of the form `function (err, fwrule, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.enableFirewallRule =
|
|
|
|
function enableFirewallRule(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/fwrules/%s/enable', this.account, opts.id),
|
|
|
|
data: {}
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable a Firewall Rule.
|
|
|
|
*
|
|
|
|
* @param {Object} opts
|
|
|
|
* - {UUID} id: The firewall id. Required.
|
|
|
|
* @param {Function} callback of the form `function (err, fwrule, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.disableFirewallRule =
|
|
|
|
function disableFirewallRule(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/fwrules/%s/disable', this.account, opts.id),
|
|
|
|
data: {}
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a Firewall Rule.
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object)
|
|
|
|
* - {UUID} id: The firewall id. Required.
|
|
|
|
* @param {Function} cb of the form `function (err, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.deleteFirewallRule =
|
|
|
|
function deleteFirewallRule(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'DELETE',
|
|
|
|
path: format('/%s/fwrules/%s', this.account, opts.id)
|
|
|
|
}, function (err, req, res) {
|
|
|
|
cb(err, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists all the Firewall Rules affecting a given machine.
|
|
|
|
*
|
|
|
|
* Returns an array of firewall objects.
|
|
|
|
*
|
|
|
|
* @param opts {Object} Options
|
|
|
|
* - {String} id (required) machine id.
|
|
|
|
* @param {Function} callback of the form f(err, fwrules, res).
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.listMachineFirewallRules =
|
|
|
|
function listMachineFirewallRules(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/machines/%s/fwrules', this.account, opts.id);
|
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists all the Machines affected by the given firewall rule.
|
|
|
|
*
|
|
|
|
* Returns an array of machine objects.
|
|
|
|
*
|
|
|
|
* @param opts {Object} Options
|
|
|
|
* - {String} id (required) firewall rule.
|
|
|
|
* @param {Function} callback of the form f(err, machines, res).
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.listFirewallRuleMachines =
|
|
|
|
function listFirewallRuleMachines(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.uuid(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/fwrules/%s/machines', this.account, opts.id);
|
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
// --- rbac
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#ListUsers>
|
|
|
|
*
|
|
|
|
* @param opts {Object} Options (optional)
|
|
|
|
* @param cb {Function} Callback of the form `function (err, users, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.listUsers = function listUsers(opts, cb) {
|
|
|
|
if (cb === undefined) {
|
|
|
|
cb = opts;
|
|
|
|
opts = {};
|
|
|
|
}
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/users', this.account);
|
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#GetUser>
|
|
|
|
*
|
|
|
|
* @param {Object} opts
|
|
|
|
* - id {UUID|String} The user ID or login.
|
|
|
|
* - membership {Boolean} Optional. Whether to includes roles of which
|
|
|
|
* this user is a member. Default false.
|
|
|
|
* @param {Function} callback of the form `function (err, user, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.getUser = function getUser(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.id, 'opts.id');
|
|
|
|
assert.optionalBool(opts.membership, 'opts.membership');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/users/%s', this.account, opts.id);
|
|
|
|
this._passThrough(endpoint, {membership: opts.membership}, cb);
|
|
|
|
};
|
|
|
|
|
2015-11-04 10:11:19 +02:00
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#CreateUser>
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object) user object containing:
|
|
|
|
* - {String} login (required) for your user.
|
|
|
|
* - {String} password (required) for the user.
|
|
|
|
* - {String} email (required) for the user.
|
|
|
|
* - {String} companyName (optional) for the user.
|
|
|
|
* - {String} firstName (optional) for the user.
|
|
|
|
* - {String} lastName (optional) for the user.
|
|
|
|
* - {String} address (optional) for the user.
|
|
|
|
* - {String} postalCode (optional) for the user.
|
|
|
|
* - {String} city (optional) for the user.
|
|
|
|
* - {String} state (optional) for the user.
|
|
|
|
* - {String} country (optional) for the user.
|
|
|
|
* - {String} phone (optional) for the user.
|
|
|
|
* @param {Function} cb of the form `function (err, user, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.createUser = function createUser(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.login, 'opts.login');
|
|
|
|
assert.string(opts.password, 'opts.password');
|
|
|
|
assert.string(opts.email, 'opts.email');
|
|
|
|
// XXX strict on inputs
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
login: opts.login,
|
|
|
|
password: opts.password,
|
|
|
|
email: opts.email,
|
|
|
|
companyName: opts.companyName,
|
|
|
|
firstName: opts.firstName,
|
|
|
|
lastName: opts.lastName,
|
|
|
|
address: opts.address,
|
|
|
|
postalCode: opts.postalCode,
|
|
|
|
city: opts.city,
|
|
|
|
state: opts.state,
|
|
|
|
country: opts.country,
|
|
|
|
phone: opts.phone
|
|
|
|
};
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/users', this.account),
|
|
|
|
data: data
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#UpdateUser>
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object) user object containing:
|
2015-11-18 22:54:44 +02:00
|
|
|
* - {String} id (required) for your user. This can be either the user
|
|
|
|
* login, or the user id (UUID)
|
|
|
|
* - {String} login (optional)
|
|
|
|
* - {String} email (optional)
|
|
|
|
* - {String} companyName (optional)
|
|
|
|
* - {String} firstName (optional)
|
|
|
|
* - {String} lastName (optional)
|
|
|
|
* - {String} address (optional)
|
|
|
|
* - {String} postalCode (optional)
|
|
|
|
* - {String} city (optional)
|
|
|
|
* - {String} state (optional)
|
|
|
|
* - {String} country (optional)
|
|
|
|
* - {String} phone (optional)
|
2015-11-04 10:11:19 +02:00
|
|
|
* @param {Function} cb of the form `function (err, user, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.updateUser = function updateUser(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.id, 'opts.id');
|
|
|
|
// XXX strict on inputs
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var update = {
|
2015-11-18 22:54:44 +02:00
|
|
|
login: opts.login,
|
2015-11-04 10:11:19 +02:00
|
|
|
email: opts.email,
|
|
|
|
companyName: opts.companyName,
|
|
|
|
firstName: opts.firstName,
|
|
|
|
lastName: opts.lastName,
|
|
|
|
address: opts.address,
|
|
|
|
postalCode: opts.postalCode,
|
|
|
|
city: opts.city,
|
|
|
|
state: opts.state,
|
|
|
|
country: opts.country,
|
|
|
|
phone: opts.phone
|
|
|
|
};
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/users/%s', this.account, opts.id),
|
|
|
|
data: update
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#DeleteUser>
|
|
|
|
*
|
2015-11-05 22:30:06 +02:00
|
|
|
* @param {Object} opts (object)
|
2015-11-04 10:11:19 +02:00
|
|
|
* - {String} id (required) for your user.
|
2015-11-05 22:30:06 +02:00
|
|
|
* @param {Function} cb of the form `function (err, res)`
|
2015-11-04 10:11:19 +02:00
|
|
|
*/
|
|
|
|
CloudApi.prototype.deleteUser = function deleteUser(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'DELETE',
|
|
|
|
path: format('/%s/users/%s', this.account, opts.id)
|
2015-11-05 22:30:06 +02:00
|
|
|
}, function (err, req, res) {
|
|
|
|
cb(err, res);
|
2015-11-04 10:11:19 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
|
2015-11-06 01:13:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List RBAC user's SSH keys.
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object)
|
|
|
|
* - {String} userId (required) The user id or login.
|
|
|
|
* @param {Function} callback of the form `function (err, userKeys, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.listUserKeys = function listUserKeys(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.userId, 'opts.userId');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/users/%s/keys', this.account, opts.userId);
|
2015-11-18 22:54:44 +02:00
|
|
|
this._passThrough(endpoint, {}, cb);
|
2015-11-06 01:13:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a RBAC user's SSH key.
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object)
|
|
|
|
* - {String} userId (required) The user id or login.
|
|
|
|
* - {String} fingerprint (required*) The SSH key fingerprint. One of
|
|
|
|
* 'fingerprint' or 'name' is required.
|
|
|
|
* - {String} name (required*) The SSH key name. One of 'fingerprint'
|
|
|
|
* or 'name' is required.
|
|
|
|
* @param {Function} callback of the form `function (err, userKey, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.getUserKey = function getUserKey(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.userId, 'opts.userId');
|
|
|
|
assert.optionalString(opts.fingerprint, 'opts.fingerprint');
|
|
|
|
assert.optionalString(opts.name, 'opts.name');
|
|
|
|
assert.ok(opts.fingerprint || opts.name,
|
2016-01-04 20:26:17 +02:00
|
|
|
'one of "fingerprint" or "name" is required');
|
2015-11-06 01:13:14 +02:00
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/users/%s/keys/%s', this.account, opts.userId,
|
|
|
|
encodeURIComponent(opts.fingerprint || opts.name));
|
|
|
|
this._passThrough(endpoint, {}, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create/upload a new RBAC user SSH public key.
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object)
|
|
|
|
* - {String} userId (required) The user id or login.
|
|
|
|
* - {String} key (required) The SSH public key content.
|
|
|
|
* - {String} name (optional) A name for the key. If not given, the
|
|
|
|
* key fingerprint will be used.
|
|
|
|
* @param {Function} callback of the form `function (err, userKey, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.createUserKey = function createUserKey(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.userId, 'opts.userId');
|
|
|
|
assert.string(opts.key, 'opts.key');
|
|
|
|
assert.optionalString(opts.name, 'opts.name');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
name: opts.name,
|
|
|
|
key: opts.key
|
|
|
|
};
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/users/%s/keys', this.account, opts.userId),
|
|
|
|
data: data
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a RBAC user's SSH key.
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object)
|
|
|
|
* - {String} userId (required) The user id or login.
|
|
|
|
* - {String} fingerprint (required*) The SSH key fingerprint. One of
|
|
|
|
* 'fingerprint' or 'name' is required.
|
|
|
|
* - {String} name (required*) The SSH key name. One of 'fingerprint'
|
|
|
|
* or 'name' is required.
|
|
|
|
* @param {Function} callback of the form `function (err, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.deleteUserKey = function deleteUserKey(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.userId, 'opts.userId');
|
|
|
|
assert.optionalString(opts.fingerprint, 'opts.fingerprint');
|
|
|
|
assert.optionalString(opts.name, 'opts.name');
|
|
|
|
assert.ok(opts.fingerprint || opts.name,
|
2016-01-04 20:26:17 +02:00
|
|
|
'one of "fingerprint" or "name" is required');
|
2015-11-06 01:13:14 +02:00
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'DELETE',
|
|
|
|
path: format('/%s/users/%s/keys/%s', this.account, opts.userId,
|
|
|
|
encodeURIComponent(opts.fingerprint || opts.name))
|
|
|
|
}, function (err, req, res) {
|
|
|
|
cb(err, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-11-05 01:38:38 +02:00
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#ListRoles>
|
|
|
|
*
|
|
|
|
* @param opts {Object} Options (optional)
|
|
|
|
* @param cb {Function} Callback of the form `function (err, roles, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.listRoles = function listRoles(opts, cb) {
|
|
|
|
if (cb === undefined) {
|
|
|
|
cb = opts;
|
|
|
|
opts = {};
|
|
|
|
}
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/roles', this.account);
|
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#GetRole>
|
|
|
|
*
|
|
|
|
* @param {Object} opts
|
|
|
|
* - id {UUID|String} The role ID or name.
|
2015-11-05 22:30:06 +02:00
|
|
|
* @param {Function} callback of the form `function (err, role, res)`
|
2015-11-05 01:38:38 +02:00
|
|
|
*/
|
|
|
|
CloudApi.prototype.getRole = function getRole(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/roles/%s', this.account, opts.id);
|
|
|
|
this._passThrough(endpoint, {}, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#CreateRole>
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object) role object containing:
|
|
|
|
* - {String} name (required) for the role.
|
|
|
|
* - {Array} members (optional) for the role.
|
|
|
|
* - {Array} default_members (optional) for the role.
|
|
|
|
* - {Array} policies (optional) for the role.
|
|
|
|
* @param {Function} cb of the form `function (err, role, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.createRole = function createRole(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.name, 'opts.name');
|
|
|
|
// XXX strict on inputs
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
name: opts.name,
|
|
|
|
default_members: opts.default_members,
|
|
|
|
members: opts.members,
|
|
|
|
policies: opts.policies
|
|
|
|
};
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/roles', this.account),
|
|
|
|
data: data
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#UpdateRole>
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object) role object containing:
|
|
|
|
* - {UUID|String} id (required) The role ID or name.
|
|
|
|
* - {String} name (optional) for the role.
|
|
|
|
* - {Array} members (optional) for the role.
|
|
|
|
* - {Array} default_members (optional) for the role.
|
|
|
|
* - {Array} policies (optional) for the role.
|
|
|
|
* @param {Function} cb of the form `function (err, role, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.updateRole = function updateRole(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.id, 'opts.id');
|
|
|
|
// XXX strict on inputs
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var update = {
|
|
|
|
name: opts.name,
|
|
|
|
members: opts.members,
|
|
|
|
default_members: opts.default_members,
|
|
|
|
policies: opts.policies
|
|
|
|
};
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/roles/%s', this.account, opts.id),
|
|
|
|
data: update
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#DeleteRole>
|
|
|
|
*
|
2015-11-05 22:30:06 +02:00
|
|
|
* @param {Object} opts (object)
|
2015-11-05 01:38:38 +02:00
|
|
|
* - {String} id (required) of the role to delete.
|
2015-11-05 22:30:06 +02:00
|
|
|
* @param {Function} cb of the form `function (err, res)`
|
2015-11-05 01:38:38 +02:00
|
|
|
*/
|
|
|
|
CloudApi.prototype.deleteRole = function deleteRole(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'DELETE',
|
|
|
|
path: format('/%s/roles/%s', this.account, opts.id)
|
2015-11-05 22:30:06 +02:00
|
|
|
}, function (err, req, res) {
|
|
|
|
cb(err, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#ListPolicies>
|
|
|
|
*
|
|
|
|
* @param opts {Object} Options (optional)
|
|
|
|
* @param cb {Function} Callback of the form `function (err, policies, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.listPolicies = function listPolicies(opts, cb) {
|
|
|
|
if (cb === undefined) {
|
|
|
|
cb = opts;
|
|
|
|
opts = {};
|
|
|
|
}
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/policies', this.account);
|
|
|
|
this._passThrough(endpoint, opts, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#GetPolicy>
|
|
|
|
*
|
|
|
|
* @param {Object} opts
|
|
|
|
* - id {UUID|String} The policy ID or name.
|
|
|
|
* @param {Function} callback of the form `function (err, policy, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.getPolicy = function getPolicy(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var endpoint = format('/%s/policies/%s', this.account, opts.id);
|
|
|
|
this._passThrough(endpoint, {}, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#CreatePolicy>
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object) policy object containing:
|
|
|
|
* - {String} name (required) for the policy.
|
|
|
|
* - {Array} description (optional) for the policy.
|
|
|
|
* - {Array} rules (optional) for the policy.
|
|
|
|
* @param {Function} cb of the form `function (err, policy, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.createPolicy = function createPolicy(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.name, 'opts.name');
|
|
|
|
// XXX strict on inputs
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
name: opts.name,
|
|
|
|
description: opts.description,
|
|
|
|
rules: opts.rules
|
|
|
|
};
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/policies', this.account),
|
|
|
|
data: data
|
2015-11-05 01:38:38 +02:00
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-11-05 22:30:06 +02:00
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#UpdatePolicy>
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object) policy object containing:
|
|
|
|
* - {UUID|String} id (required) The policy ID or name.
|
|
|
|
* - {String} name (optional)
|
|
|
|
* - {String} description (optional)
|
|
|
|
* - {Array} rules (optional)
|
|
|
|
* @param {Function} cb of the form `function (err, policy, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.updatePolicy = function updatePolicy(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.id, 'opts.id');
|
|
|
|
// XXX strict on inputs
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var update = {
|
|
|
|
name: opts.name,
|
|
|
|
description: opts.description,
|
|
|
|
rules: opts.rules
|
|
|
|
};
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'POST',
|
|
|
|
path: format('/%s/policies/%s', this.account, opts.id),
|
|
|
|
data: update
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#DeletePolicy>
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object) user object containing:
|
|
|
|
* - {String} id (required) of the policy to delete.
|
|
|
|
* @param {Function} cb of the form `function (err, res)`
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.deletePolicy = function deletePolicy(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.id, 'opts.id');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'DELETE',
|
|
|
|
path: format('/%s/policies/%s', this.account, opts.id)
|
|
|
|
}, function (err, req, res) {
|
|
|
|
cb(err, res);
|
|
|
|
});
|
|
|
|
};
|
2015-11-05 01:38:38 +02:00
|
|
|
|
2015-11-04 01:40:59 +02:00
|
|
|
|
2015-11-13 02:04:12 +02:00
|
|
|
// ---- RBAC role tag support functions
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#role-tags>
|
|
|
|
* Get RBAC role-tags on a resource. Technically there isn't a separate
|
|
|
|
* specific API endpoint for this -- it is the Get$Resource endpoint instead.
|
|
|
|
*
|
|
|
|
* @param {Object} opts:
|
|
|
|
* - {String} resource (required) The resource URL. E.g.
|
|
|
|
* '/:account/machines/:uuid' to tag a particular machine instance.
|
|
|
|
* @param {Function} cb of the form `function (err, roleTags, resource, res)`
|
|
|
|
* @throws {AssertionError, TypeError} on invalid inputs
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.getRoleTags = function getRoleTags(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.resource, 'opts.resource');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
// Validate `resource`.
|
|
|
|
// TODO: share this validation with `setRoleTags`.
|
|
|
|
var resourceRe = new RegExp('^/[^/]{2,}/[^/]+');
|
|
|
|
if (! resourceRe.test(opts.resource)) {
|
|
|
|
throw new TypeError(format('invalid resource "%s": must match ' +
|
|
|
|
'"/:account/:type..."', opts.resource));
|
|
|
|
}
|
|
|
|
|
|
|
|
var validResources = [
|
|
|
|
'machines',
|
|
|
|
'packages',
|
|
|
|
'images',
|
|
|
|
'fwrules',
|
|
|
|
'networks',
|
|
|
|
// TODO: validate/test role tags on these rbac resources
|
|
|
|
'users',
|
|
|
|
'roles',
|
|
|
|
'policies',
|
|
|
|
// TODO: validate, test
|
|
|
|
'keys',
|
|
|
|
'datacenters',
|
|
|
|
'analytics',
|
|
|
|
'instrumentations'
|
|
|
|
];
|
|
|
|
var parts = opts.resource.split('/');
|
|
|
|
if (validResources.indexOf(parts[2]) === -1) {
|
|
|
|
throw new TypeError(format('invalid resource "%s": resource type ' +
|
|
|
|
'must be one of: %s', opts.resource, validResources.join(', ')));
|
|
|
|
}
|
|
|
|
|
|
|
|
function roleTagsFromRes(res) {
|
|
|
|
return (
|
|
|
|
(res.headers['role-tag'] || '')
|
|
|
|
/* JSSTYLED */
|
|
|
|
.split(/\s*,\s*/)
|
|
|
|
.filter(function (r) { return r.trim(); })
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
/*
|
|
|
|
* We use GET instead of HEAD to also be able to return the
|
|
|
|
* resource JSON. Technically we *could* drop support for that from
|
|
|
|
* this API, but `tritonapi.js` is using it.
|
|
|
|
*/
|
|
|
|
method: 'GET',
|
|
|
|
path: opts.resource
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
if (err) {
|
|
|
|
cb(err, null, res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var roleTags = roleTagsFromRes(res);
|
|
|
|
cb(err, roleTags, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-11-10 01:09:37 +02:00
|
|
|
/**
|
|
|
|
* <http://apidocs.joyent.com/cloudapi/#SetRoleTags>
|
|
|
|
* Set RBAC role-tags on a resource.
|
|
|
|
*
|
|
|
|
* @param {Object} opts (object):
|
|
|
|
* - {String} resource (required) The resource URL. E.g.
|
|
|
|
* '/:account/machines/:uuid' to tag a particular machine instance.
|
|
|
|
* - {Array} roleTags (required) the array of role tags to set. Each
|
|
|
|
* role tag string is the name of a RBAC role. See `ListRoles`.
|
|
|
|
* @param {Function} cb of the form `function (err, body, res)`
|
|
|
|
* Where `body` is of the form `{name: <resource url>,
|
|
|
|
* 'role-tag': <array of added role tags>}`.
|
|
|
|
* @throws {AssertionError, TypeError} on invalid inputs
|
|
|
|
*/
|
|
|
|
CloudApi.prototype.setRoleTags = function setRoleTags(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.resource, 'opts.resource');
|
|
|
|
assert.arrayOfString(opts.roleTags, 'opts.roleTags');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
// Validate `resource`.
|
|
|
|
var resourceRe = new RegExp('^/[^/]{2,}/[^/]+');
|
|
|
|
if (! resourceRe.test(opts.resource)) {
|
|
|
|
throw new TypeError(format('invalid resource "%s": must match ' +
|
|
|
|
'"/:account/:type..."', opts.resource));
|
|
|
|
}
|
|
|
|
|
|
|
|
var validResources = [
|
|
|
|
'machines',
|
|
|
|
'packages',
|
|
|
|
'images',
|
|
|
|
'fwrules',
|
|
|
|
'networks',
|
|
|
|
// TODO: validate/test role tags on these rbac resources
|
|
|
|
'users',
|
|
|
|
'roles',
|
|
|
|
'policies',
|
|
|
|
// TODO: validate, test
|
|
|
|
'keys',
|
|
|
|
'datacenters',
|
|
|
|
'analytics',
|
|
|
|
'instrumentations'
|
|
|
|
];
|
|
|
|
var parts = opts.resource.split('/');
|
|
|
|
if (validResources.indexOf(parts[2]) === -1) {
|
|
|
|
throw new TypeError(format('invalid resource "%s": resource type ' +
|
|
|
|
'must be one of: %s', opts.resource, validResources.join(', ')));
|
|
|
|
}
|
|
|
|
|
|
|
|
this._request({
|
|
|
|
method: 'PUT',
|
|
|
|
path: opts.resource,
|
|
|
|
data: {
|
|
|
|
'role-tag': opts.roleTags
|
|
|
|
}
|
|
|
|
}, function (err, req, res, body) {
|
|
|
|
cb(err, body, res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-20 05:52:58 +02:00
|
|
|
// --- Exports
|
|
|
|
|
2016-01-15 08:56:38 +02:00
|
|
|
module.exports = {
|
|
|
|
createClient: function createClient(options) {
|
|
|
|
return new CloudApi(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
CloudApi: CloudApi
|
2016-03-11 16:07:11 +02:00
|
|
|
};
|