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:
@ -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
|
// Request a resource collection and format it as a table
|
||||||
this.getCollection = function (host, resource, fields) {
|
this.getCollection = function (host, resource, fields) {
|
||||||
return doRequest(host, resource, function(res) {
|
return getRawCollection(host, resource, fields)
|
||||||
|
.then((res) => rawCollectionAsTable(res, fields))
|
||||||
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()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request a part of a resource as a collection
|
// Request a part of a resource as a collection
|
||||||
@ -250,6 +268,12 @@ module.exports = function() {
|
|||||||
return doAsyncRequest(host, resource, cb, obj)
|
return doAsyncRequest(host, resource, cb, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.getResource = function(host, resource) {
|
||||||
|
return doAsyncRequest(host, resource, (res) => {
|
||||||
|
return res
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
this.error = function(err) {
|
this.error = function(err) {
|
||||||
return Promise.reject(colors.red('Error: ') + err)
|
return Promise.reject(colors.red('Error: ') + err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user