From c1893783894fe1c1424472fc9a0ccfbccfd166ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Fri, 30 Jun 2017 12:12:39 +0300 Subject: [PATCH] MXS-1330: Pair header names with resource paths The getCollection and getResource now use similar mechanisms to declare the names and values for the tables. --- client/maxctrl/common.js | 29 ++++++++++++---- client/maxctrl/lib/list.js | 71 +++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 42 deletions(-) diff --git a/client/maxctrl/common.js b/client/maxctrl/common.js index 976012d7e..6844f0f23 100644 --- a/client/maxctrl/common.js +++ b/client/maxctrl/common.js @@ -59,16 +59,23 @@ module.exports = function() { .help() // Request a resource collection and format it as a table - this.getCollection = function (resource, headers, parts) { + this.getCollection = function (resource, fields) { doRequest(resource, function(res) { - var table = getTable(headers) + + var header = [] + + fields.forEach(function(i) { + header.push(Object.keys(i)) + }) + + var table = getTable(header) res.data.forEach(function(i) { row = [] - parts.forEach(function(p) { - var v = _.getPath(i, p, "") + fields.forEach(function(p) { + var v = _.getPath(i, p[Object.keys(p)[0]], "") if (Array.isArray(v)) { v = v.join(", ") @@ -87,7 +94,7 @@ module.exports = function() { this.getResource = function (resource, fields) { doRequest(resource, function(res) { - var table = new Table() + var table = getList() fields.forEach(function(i) { var k = Object.keys(i)[0] @@ -128,11 +135,17 @@ module.exports = function() { uri: getUri(resource), json: true }, function(err, resp, res) { - if (resp.statusCode == 200) { + if (err) { + // Failed to request + console.log("Error:", JSON.stringify(err, null, 4)) + } else if (resp.statusCode == 200) { + // Reuqest OK, returns data cb(res) } else if (resp.statusCode == 204) { + // Request OK, no data console.log(colors.green("OK")) } else { + // Unexpected return code, probably an error console.log("Error:", resp.statusCode, resp.statusMessage) if (res) { console.log(res) @@ -142,6 +155,10 @@ module.exports = function() { } } +function getList() { + return new Table({ style: { head: ['cyan'] } }) +} + // Creates a table-like array for output. The parameter is an array of header names function getTable(headobj) { diff --git a/client/maxctrl/lib/list.js b/client/maxctrl/lib/list.js index 212afdb3c..fe0a4fc7c 100644 --- a/client/maxctrl/lib/list.js +++ b/client/maxctrl/lib/list.js @@ -19,52 +19,51 @@ exports.handler = function() {} exports.builder = function(yargs) { yargs .command('servers', 'List servers', {}, function() { - - getCollection('servers', - ['Server', 'Address', 'Port', 'Connections', 'Status'], - ['id', - 'attributes.parameters.address', - 'attributes.parameters.port', - 'attributes.statistics.connections', - 'attributes.status']) + getCollection('servers', [ + {'Server': 'id'}, + {'Address': 'attributes.parameters.address'}, + {'Port': 'attributes.parameters.port'}, + {'Connections': 'attributes.statistics.connections'}, + {'Status': 'attributes.status'} + ]) }) .command('services', 'List services', {}, function() { - getCollection('services', - ['Service', 'Router', 'Connections', 'Total Connections', 'Servers'], - ['id', - 'attributes.router', - 'attributes.connections', - 'attributes.total_connections', - 'relationships.servers.data[].id']) + getCollection('services',[ + {'Service': 'id'}, + {'Router': 'attributes.router'}, + {'Connections': 'attributes.connections'}, + {'Total Connections': 'attributes.total_connections'}, + {'Servers': 'relationships.servers.data[].id'} + ]) }) .command('monitors', 'List monitors', {}, function() { - getCollection('monitors', - ['Monitor', 'Status', 'Servers'], - ['id', - 'attributes.state', - 'relationships.servers.data[].id']) + getCollection('monitors', [ + {'Monitor': 'id'}, + {'Status': 'attributes.state'}, + {'Servers': 'relationships.servers.data[].id'} + ]) }) .command('sessions', 'List sessions', {}, function() { - getCollection('sessions', - ['Id', 'Service', 'User', 'Host'], - ['id', - 'relationships.services.data[].id', - 'attributes.user', - 'attributes.remote']) + getCollection('sessions',[ + {'Id': 'id'}, + {'Service': 'relationships.services.data[].id'}, + {'User': 'attributes.user'}, + {'Host': 'attributes.remote'} + ]) }) .command('filters', 'List filters', {}, function() { - getCollection('filters', - ['Filter', 'Service', 'Module'], - ['id', - 'relationships.services.data[].id', - 'attributes.module']) + getCollection('filters', [ + {'Filter': 'id'}, + {'Service': 'relationships.services.data[].id'}, + {'Module': 'attributes.module'} + ]) }) .command('modules', 'List loaded modules', {}, function() { - getCollection('maxscale/modules', - ['Module', 'Type', 'Version'], - ['id', - 'attributes.module_type', - 'attributes.version']) + getCollection('maxscale/modules',[ + {'Module':'id'}, + {'Type':'attributes.module_type'}, + {'Version': 'attributes.version'} + ]) }) .help() }