From 5855b307bd428be7b5d8d31eb8217e6329463c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Mon, 16 Apr 2018 09:54:40 +0300 Subject: [PATCH] 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. --- maxctrl/lib/api.js | 42 ++++++++++++++++++++++++++++++++++++++++++ maxctrl/lib/core.js | 1 + 2 files changed, 43 insertions(+) create mode 100644 maxctrl/lib/api.js diff --git a/maxctrl/lib/api.js b/maxctrl/lib/api.js new file mode 100644 index 000000000..716924110 --- /dev/null +++ b/maxctrl/lib/api.js @@ -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 ' +exports.desc = 'Raw REST API access' +exports.handler = function() {} +exports.builder = function(yargs) { + yargs + .command('get [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 [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 ') + .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.') + }) + }) +} diff --git a/maxctrl/lib/core.js b/maxctrl/lib/core.js index c3bd62a1a..74868a16f 100644 --- a/maxctrl/lib/core.js +++ b/maxctrl/lib/core.js @@ -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) {