From 87ce1f6dab3026f18bc9aa3056ef7ad37511659f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Sun, 15 Apr 2018 11:08:59 +0300 Subject: [PATCH] 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.). --- maxctrl/lib/common.js | 50 ++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/maxctrl/lib/common.js b/maxctrl/lib/common.js index cab6d9057..6e00bb83c 100644 --- a/maxctrl/lib/common.js +++ b/maxctrl/lib/common.js @@ -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 this.getResource = function (host, resource, fields) { + return doRequest(host, resource, (res) => { + return formatResource(fields, res.data) + }) + } - return doRequest(host, resource, function(res) { - var table = getList() - - fields.forEach(function(i) { - 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) + 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') }) }