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.
This commit is contained in:
Markus Mäkelä
2017-06-30 12:12:39 +03:00
parent 1148ed9876
commit c189378389
2 changed files with 58 additions and 42 deletions

View File

@ -59,16 +59,23 @@ module.exports = function() {
.help() .help()
// Request a resource collection and format it as a table // 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) { 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) { res.data.forEach(function(i) {
row = [] row = []
parts.forEach(function(p) { fields.forEach(function(p) {
var v = _.getPath(i, p, "") var v = _.getPath(i, p[Object.keys(p)[0]], "")
if (Array.isArray(v)) { if (Array.isArray(v)) {
v = v.join(", ") v = v.join(", ")
@ -87,7 +94,7 @@ module.exports = function() {
this.getResource = function (resource, fields) { this.getResource = function (resource, fields) {
doRequest(resource, function(res) { doRequest(resource, function(res) {
var table = new Table() var table = getList()
fields.forEach(function(i) { fields.forEach(function(i) {
var k = Object.keys(i)[0] var k = Object.keys(i)[0]
@ -128,11 +135,17 @@ module.exports = function() {
uri: getUri(resource), uri: getUri(resource),
json: true json: true
}, function(err, resp, res) { }, 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) cb(res)
} else if (resp.statusCode == 204) { } else if (resp.statusCode == 204) {
// Request OK, no data
console.log(colors.green("OK")) console.log(colors.green("OK"))
} else { } else {
// Unexpected return code, probably an error
console.log("Error:", resp.statusCode, resp.statusMessage) console.log("Error:", resp.statusCode, resp.statusMessage)
if (res) { if (res) {
console.log(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 // Creates a table-like array for output. The parameter is an array of header names
function getTable(headobj) { function getTable(headobj) {

View File

@ -19,52 +19,51 @@ exports.handler = function() {}
exports.builder = function(yargs) { exports.builder = function(yargs) {
yargs yargs
.command('servers', 'List servers', {}, function() { .command('servers', 'List servers', {}, function() {
getCollection('servers', [
getCollection('servers', {'Server': 'id'},
['Server', 'Address', 'Port', 'Connections', 'Status'], {'Address': 'attributes.parameters.address'},
['id', {'Port': 'attributes.parameters.port'},
'attributes.parameters.address', {'Connections': 'attributes.statistics.connections'},
'attributes.parameters.port', {'Status': 'attributes.status'}
'attributes.statistics.connections', ])
'attributes.status'])
}) })
.command('services', 'List services', {}, function() { .command('services', 'List services', {}, function() {
getCollection('services', getCollection('services',[
['Service', 'Router', 'Connections', 'Total Connections', 'Servers'], {'Service': 'id'},
['id', {'Router': 'attributes.router'},
'attributes.router', {'Connections': 'attributes.connections'},
'attributes.connections', {'Total Connections': 'attributes.total_connections'},
'attributes.total_connections', {'Servers': 'relationships.servers.data[].id'}
'relationships.servers.data[].id']) ])
}) })
.command('monitors', 'List monitors', {}, function() { .command('monitors', 'List monitors', {}, function() {
getCollection('monitors', getCollection('monitors', [
['Monitor', 'Status', 'Servers'], {'Monitor': 'id'},
['id', {'Status': 'attributes.state'},
'attributes.state', {'Servers': 'relationships.servers.data[].id'}
'relationships.servers.data[].id']) ])
}) })
.command('sessions', 'List sessions', {}, function() { .command('sessions', 'List sessions', {}, function() {
getCollection('sessions', getCollection('sessions',[
['Id', 'Service', 'User', 'Host'], {'Id': 'id'},
['id', {'Service': 'relationships.services.data[].id'},
'relationships.services.data[].id', {'User': 'attributes.user'},
'attributes.user', {'Host': 'attributes.remote'}
'attributes.remote']) ])
}) })
.command('filters', 'List filters', {}, function() { .command('filters', 'List filters', {}, function() {
getCollection('filters', getCollection('filters', [
['Filter', 'Service', 'Module'], {'Filter': 'id'},
['id', {'Service': 'relationships.services.data[].id'},
'relationships.services.data[].id', {'Module': 'attributes.module'}
'attributes.module']) ])
}) })
.command('modules', 'List loaded modules', {}, function() { .command('modules', 'List loaded modules', {}, function() {
getCollection('maxscale/modules', getCollection('maxscale/modules',[
['Module', 'Type', 'Version'], {'Module':'id'},
['id', {'Type':'attributes.module_type'},
'attributes.module_type', {'Version': 'attributes.version'}
'attributes.version']) ])
}) })
.help() .help()
} }