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:
@ -169,29 +169,39 @@ module.exports = function() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.formatResource = function (fields, data) {
|
||||||
|
var table = getList()
|
||||||
|
|
||||||
|
fields.forEach(function(i) {
|
||||||
|
var k = Object.keys(i)[0]
|
||||||
|
var path = i[k]
|
||||||
|
var v = _.getPath(data, path, '')
|
||||||
|
|
||||||
|
if (Array.isArray(v) && typeof(v[0]) != 'object') {
|
||||||
|
v = v.join(', ')
|
||||||
|
} else if (typeof(v) == 'object') {
|
||||||
|
v = JSON.stringify(v, null, 4)
|
||||||
|
}
|
||||||
|
|
||||||
|
var o = {}
|
||||||
|
o[k] = v
|
||||||
|
table.push(o)
|
||||||
|
})
|
||||||
|
|
||||||
|
return tableToString(table)
|
||||||
|
}
|
||||||
|
|
||||||
// Request a single resource and format it as a key-value list
|
// Request a single resource and format it as a key-value list
|
||||||
this.getResource = function (host, resource, fields) {
|
this.getResource = function (host, resource, fields) {
|
||||||
|
return doRequest(host, resource, (res) => {
|
||||||
|
return formatResource(fields, res.data)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return doRequest(host, resource, function(res) {
|
this.getCollectionAsResource = function(host, resource, fields) {
|
||||||
var table = getList()
|
return doRequest(host, resource, (res) => {
|
||||||
|
//return formatResource(fields, res.data[0])
|
||||||
fields.forEach(function(i) {
|
return res.data.map((i) => formatResource(fields, i)).join('\n')
|
||||||
var k = Object.keys(i)[0]
|
|
||||||
var path = i[k]
|
|
||||||
var v = _.getPath(res.data, path, '')
|
|
||||||
|
|
||||||
if (Array.isArray(v) && typeof(v[0]) != 'object') {
|
|
||||||
v = v.join(', ')
|
|
||||||
} else if (typeof(v) == 'object') {
|
|
||||||
v = JSON.stringify(v, null, 4)
|
|
||||||
}
|
|
||||||
|
|
||||||
var o = {}
|
|
||||||
o[k] = v
|
|
||||||
table.push(o)
|
|
||||||
})
|
|
||||||
|
|
||||||
return tableToString(table)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user