2015-08-26 01:30:25 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Joyent Inc.
|
|
|
|
*
|
|
|
|
* `triton cloudapi ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var http = require('http');
|
|
|
|
|
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-08-26 02:46:14 +03:00
|
|
|
} else if (args.length < 1 || args.length > 2) {
|
2015-08-26 01:30:25 +03:00
|
|
|
callback(new Error('invalid arguments'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-26 02:46:14 +03:00
|
|
|
var method = args[0];
|
|
|
|
var path = args[1];
|
|
|
|
if (path === undefined) {
|
|
|
|
path = method;
|
|
|
|
method = 'GET';
|
|
|
|
}
|
|
|
|
|
2015-08-26 01:30:25 +03:00
|
|
|
var reqopts = {
|
2015-08-26 02:46:14 +03:00
|
|
|
method: method.toLowerCase(),
|
|
|
|
path: path
|
2015-08-26 01:30:25 +03:00
|
|
|
};
|
|
|
|
|
2015-08-26 02:46:14 +03:00
|
|
|
this.triton.cloudapi._request(reqopts, function (err, req, res, body) {
|
2015-08-26 01:30:25 +03:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (opts.headers || reqopts.method === 'head') {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reqopts.method !== 'head')
|
|
|
|
console.log(JSON.stringify(body, null, 4));
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
do_cloudapi.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['headers', 'i'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Print response headers to stderr.'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
do_cloudapi.help = (
|
|
|
|
'Raw cloudapi request.\n'
|
|
|
|
+ '\n'
|
|
|
|
+ 'Usage:\n'
|
|
|
|
+ ' {{name}} <method> <endpoint>\n'
|
|
|
|
+ '\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;
|