MXS-1782: Separate resource fetching and processing

The requesting of a resource and the processing was integrated into one
function. Moving the processing part into a separate function allows easy
processing of resource collections.

This refactoring made the creation of the getCollectionAsResource function
possible. It enables `show` type commands for resouce collections
(servers, services etc.).
This commit is contained in:
Markus Mäkelä
2018-04-15 11:08:59 +03:00
parent 32fdc3d454
commit 87ce1f6dab

View File

@ -169,16 +169,13 @@ module.exports = function() {
}) })
} }
// Request a single resource and format it as a key-value list this.formatResource = function (fields, data) {
this.getResource = function (host, resource, fields) {
return doRequest(host, resource, function(res) {
var table = getList() var table = getList()
fields.forEach(function(i) { fields.forEach(function(i) {
var k = Object.keys(i)[0] var k = Object.keys(i)[0]
var path = i[k] var path = i[k]
var v = _.getPath(res.data, path, '') var v = _.getPath(data, path, '')
if (Array.isArray(v) && typeof(v[0]) != 'object') { if (Array.isArray(v) && typeof(v[0]) != 'object') {
v = v.join(', ') v = v.join(', ')
@ -192,6 +189,19 @@ module.exports = function() {
}) })
return tableToString(table) return tableToString(table)
}
// Request a single resource and format it as a key-value list
this.getResource = function (host, resource, fields) {
return doRequest(host, resource, (res) => {
return formatResource(fields, res.data)
})
}
this.getCollectionAsResource = function(host, resource, fields) {
return doRequest(host, resource, (res) => {
//return formatResource(fields, res.data[0])
return res.data.map((i) => formatResource(fields, i)).join('\n')
}) })
} }