2015-08-26 01:30:25 +03: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.
|
2015-08-26 01:30:25 +03:00
|
|
|
*
|
|
|
|
* `triton cloudapi ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var http = require('http');
|
|
|
|
|
2015-12-09 21:59:47 +02:00
|
|
|
var errors = require('./errors');
|
|
|
|
|
|
|
|
|
2015-09-02 20:47:06 +03:00
|
|
|
function do_cloudapi(subcmd, opts, args, callback) {
|
2015-08-26 01:30:25 +03:00
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
2015-09-04 21:49:26 +03:00
|
|
|
} else if (args.length < 1) {
|
2015-08-26 01:30:25 +03:00
|
|
|
callback(new Error('invalid arguments'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-09 21:59:47 +02:00
|
|
|
// Get `reqOpts` from given options.
|
|
|
|
var method = opts.method;
|
|
|
|
if (!method) {
|
|
|
|
if (opts.data) {
|
|
|
|
method = 'PUT';
|
|
|
|
} else {
|
|
|
|
method = 'GET';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var reqOpts = {
|
|
|
|
method: method.toLowerCase(),
|
2015-09-04 21:49:26 +03:00
|
|
|
headers: {},
|
2015-12-09 21:59:47 +02:00
|
|
|
path: args[0]
|
2015-08-26 01:30:25 +03:00
|
|
|
};
|
2015-12-09 21:59:47 +02:00
|
|
|
if (opts.header) {
|
|
|
|
for (var i = 0; i < opts.header.length; i++) {
|
|
|
|
var raw = opts.header[i];
|
|
|
|
var j = raw.indexOf(':');
|
|
|
|
if (j < 0) {
|
|
|
|
callback(new errors.TritonError(
|
|
|
|
'failed to parse header: ' + raw));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var header = raw.substr(0, j);
|
|
|
|
var value = raw.substr(j + 1).trimLeft();
|
|
|
|
reqOpts.headers[header] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opts.data) {
|
|
|
|
try {
|
|
|
|
reqOpts.data = JSON.parse(opts.data);
|
|
|
|
} catch (parseErr) {
|
|
|
|
callback(new errors.TritonError(parseErr,
|
|
|
|
'given <data> is not valid JSON: ' + parseErr.message));
|
2015-09-04 21:49:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 21:59:47 +02:00
|
|
|
this.tritonapi.cloudapi._request(reqOpts, function (err, req, res, body) {
|
2015-08-26 01:30:25 +03:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-09 21:59:47 +02:00
|
|
|
if (opts.headers || reqOpts.method === 'head') {
|
2015-08-26 01:30:25 +03:00
|
|
|
console.error('%s/%s %d %s',
|
|
|
|
req.connection.encrypted ? 'HTTPS' : 'HTTP',
|
|
|
|
res.httpVersion,
|
|
|
|
res.statusCode,
|
|
|
|
http.STATUS_CODES[res.statusCode]);
|
|
|
|
Object.keys(res.headers).forEach(function (key) {
|
|
|
|
console.error('%s: %s', key, res.headers[key]);
|
|
|
|
});
|
|
|
|
console.error();
|
|
|
|
}
|
|
|
|
|
2015-12-09 21:59:47 +02:00
|
|
|
if (reqOpts.method !== 'head')
|
2015-08-26 01:30:25 +03:00
|
|
|
console.log(JSON.stringify(body, null, 4));
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
do_cloudapi.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
2015-09-04 21:49:26 +03:00
|
|
|
{
|
|
|
|
names: ['method', 'X'],
|
|
|
|
type: 'string',
|
2015-12-09 21:59:47 +02:00
|
|
|
helpArg: '<method>',
|
2015-09-04 21:49:26 +03:00
|
|
|
help: 'Request method to use. Default is "GET".'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['header', 'H'],
|
|
|
|
type: 'arrayOfString',
|
2015-12-09 21:59:47 +02:00
|
|
|
helpArg: '<header>',
|
2015-09-04 21:49:26 +03:00
|
|
|
help: 'Headers to send with request.'
|
|
|
|
},
|
2015-08-26 01:30:25 +03:00
|
|
|
{
|
|
|
|
names: ['headers', 'i'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Print response headers to stderr.'
|
2015-12-09 21:59:47 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['data', 'd'],
|
|
|
|
type: 'string',
|
|
|
|
helpArg: '<data>',
|
|
|
|
help: 'Add POST data. This must be valid JSON.'
|
2015-08-26 01:30:25 +03:00
|
|
|
}
|
|
|
|
];
|
|
|
|
do_cloudapi.help = (
|
|
|
|
'Raw cloudapi request.\n'
|
|
|
|
+ '\n'
|
|
|
|
+ 'Usage:\n'
|
2015-12-09 21:59:47 +02:00
|
|
|
+ ' {{name}} cloudapi [-X <method>] [-H <header=value>] \\\n'
|
|
|
|
+ ' [-d <data>] <endpoint>\n'
|
2015-08-26 01:30:25 +03:00
|
|
|
+ '\n'
|
|
|
|
+ '{{options}}'
|
|
|
|
);
|
|
|
|
|
2015-08-26 02:12:35 +03:00
|
|
|
do_cloudapi.hidden = true;
|
|
|
|
|
2015-08-26 01:30:25 +03:00
|
|
|
|
|
|
|
module.exports = do_cloudapi;
|