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()
// 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) {

View File

@ -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()
}