Add raw REST API calls to MaxCtrl

Being able to perform raw REST API calls that leverage the value
extraction capabilities of Node.js gives more control to the end user. It
also doubles as a handy tool for creating scripts that only require one
particular value from the REST API.
This commit is contained in:
Markus Mäkelä 2018-04-16 09:54:40 +03:00
parent 41626202ed
commit 5855b307bd
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
2 changed files with 43 additions and 0 deletions

42
maxctrl/lib/api.js Normal file
View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2020-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
require('./common.js')()
exports.command = 'api <command>'
exports.desc = 'Raw REST API access'
exports.handler = function() {}
exports.builder = function(yargs) {
yargs
.command('get <resource> [path]', 'Get raw JSON', function(yargs) {
return yargs.epilog('Perform a raw REST API call. ' +
'The path definition uses JavaScript syntax to extract values.')
.usage('Usage: get <resource> [path]')
}, function(argv) {
maxctrl(argv, function(host) {
return doRequest(host, argv.resource, (res) => {
if (argv.path) {
res = _.getPath(res, argv.path, '')
}
return JSON.stringify(res)
})
})
})
.usage('Usage: api <command>')
.help()
.command('*', 'the default command', {}, function(argv) {
maxctrl(argv, function(host) {
return error('Unknown command. See output of `help api` for a list of commands.')
})
})
}

View File

@ -100,6 +100,7 @@ program
.command(require('./rotate.js'))
.command(require('./call.js'))
.command(require('./cluster.js'))
.command(require('./api.js'))
.help()
.demandCommand(1, 'At least one command is required')
.command('*', 'the default command', {}, function(argv) {