MXS-1656: Split resource collection functions into smaller parts

Split the getCollection function into smaller, reusable parts. This is
needed to display the GTID information in `list servers`.
This commit is contained in:
Markus Mäkelä 2018-02-15 15:47:28 +02:00
parent 145ab64a4a
commit 558ea4babe
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19

View File

@ -70,35 +70,53 @@ module.exports = function() {
})
}
this.filterResource = function (res, fields) {
table = []
res.data.forEach(function(i) {
row = []
fields.forEach(function(p) {
var v = _.getPath(i, p[Object.keys(p)[0]], '')
if (Array.isArray(v)) {
v = v.join(', ')
}
row.push(v)
})
table.push(row)
})
return table
}
// Get a resource as raw collection; a matrix of strings
this.getRawCollection = function (host, resource, fields) {
return getResource(host, resource)
.then((res) => filterResource(res, fields))
}
this.rawCollectionAsTable = function (arr, fields) {
var header = []
fields.forEach(function(i) {
header.push(Object.keys(i))
})
var table = getTable(header)
arr.forEach((row) => {
table.push(row)
})
return table.toString()
}
// Request a resource collection and format it as a table
this.getCollection = function (host, resource, fields) {
return doRequest(host, resource, function(res) {
var header = []
fields.forEach(function(i) {
header.push(Object.keys(i))
})
var table = getTable(header)
res.data.forEach(function(i) {
row = []
fields.forEach(function(p) {
var v = _.getPath(i, p[Object.keys(p)[0]], '')
if (Array.isArray(v)) {
v = v.join(', ')
}
row.push(v)
})
table.push(row)
})
return table.toString()
})
return getRawCollection(host, resource, fields)
.then((res) => rawCollectionAsTable(res, fields))
}
// Request a part of a resource as a collection
@ -250,6 +268,12 @@ module.exports = function() {
return doAsyncRequest(host, resource, cb, obj)
}
this.getResource = function(host, resource) {
return doAsyncRequest(host, resource, (res) => {
return res
})
}
this.error = function(err) {
return Promise.reject(colors.red('Error: ') + err)
}