diff --git a/src/api/data-sources/index.js b/src/api/data-sources/index.js index 920cc7e..86ce31c 100644 --- a/src/api/data-sources/index.js +++ b/src/api/data-sources/index.js @@ -7,6 +7,9 @@ const mapObj = require("map-obj"); const lvm = require("../../packages/exec-lvm"); const All = require("../../packages/graphql-interface/symbols/all"); +const nvmeCLI = require("../../packages/exec-nvme-cli"); +const smartctl = require("../../packages/exec-smartctl"); +const dlayerWrap = require("../../packages/dlayer-wrap"); // This generates a (memoized) source function for commands that always produce an entire list, that needs to be filtered for the desired item(s) function makeListCommand({ command, selectResult, selectID }) { @@ -26,43 +29,77 @@ function makeListCommand({ command, selectResult, selectID }) { if (id === All) { return items; } else { - return items.find((item) => selectID(item, id)); + return items.find((item) => selectID(item) === id); } }); }); }; } +function makeSingleCommand({ command, selectResult }) { + return function (ids) { + return Promise.map(ids, (id) => { + if (id === All) { + // FIXME: Have some sort of mechanism for making this possible? + throw new Error(`This data source does not support fetching all entries`); + } else { + return command(id); + } + }).map((result) => { + if (selectResult != null) { + return selectResult(result); + } else { + return result; + } + }); + }; +} + module.exports = function createSources() { let sources = { lvmLogicalVolumes: makeListCommand({ command: lvm.getLogicalVolumes, selectResult: (result) => result.volumes, - selectID: (volume, path) => (volume.path === path) + selectID: (volume) => volume.path }), lvmPhysicalVolumes: makeListCommand({ command: lvm.getPhysicalVolumes, selectResult: (result) => result.volumes, - selectID: (device, path) => (device.path === path) + selectID: (device) => device.path }), lvmVolumeGroups: makeListCommand({ command: lvm.getVolumeGroups, selectResult: (result) => result.groups, - selectID: (group, name) => (group.name === name) + selectID: (group) => group.name + }), + nvmeIdentifyController: makeSingleCommand({ + command: (path) => nvmeCLI.identifyController({ devicePath: path }) + }), + nvmeListNamespaces: makeSingleCommand({ + command: (path) => nvmeCLI.listNamespaces({ devicePath: path }) + }), + smartctlScan: makeListCommand({ + command: smartctl.scan, + selectID: (device) => device.path + }), + smartctlInfo: makeSingleCommand({ + command: (path) => dlayerWrap(() => smartctl.info({ devicePath: path }), { + allowedErrors: [ smartctl.InfoError ] + }) + }), + smartctlAttributes: makeSingleCommand({ + command: (path) => dlayerWrap(() => smartctl.attributes({ devicePath: path }), { + allowedErrors: [ smartctl.AttributesError ] + }) }), }; let factoryModules = { lsblk: require("./lsblk")(), - findmnt: require("./findmnt")(), - smartctlInfo: require("./smartctl/info")(), - smartctlScan: require("./smartctl/scan")(), - smartctlAttributes: require("./smartctl/attributes")(), - nvmeListNamespaces: require("./nvme/list-namespaces")(), - nvmeIdentifyController: require("./nvme/identify-controller")(), + findmnt: require("./findmnt")() }; - return mapObj({ ... sources, ... factoryModules }, (name, factory) => { + return mapObj({ ... factoryModules, ... sources }, (name, factory) => { return [ name, new DataLoader(factory) diff --git a/src/api/data-sources/nvme/identify-controller.js b/src/api/data-sources/nvme/identify-controller.js deleted file mode 100644 index ece8eac..0000000 --- a/src/api/data-sources/nvme/identify-controller.js +++ /dev/null @@ -1,13 +0,0 @@ -"use strict"; - -const Promise = require("bluebird"); - -const nvmeCli = require("../../../packages/exec-nvme-cli"); - -module.exports = function () { - return function (controllerPaths) { - return Promise.map(controllerPaths, (path) => { - return nvmeCli.identifyController({ devicePath: path }); - }); - }; -}; diff --git a/src/api/data-sources/nvme/list-namespaces.js b/src/api/data-sources/nvme/list-namespaces.js deleted file mode 100644 index 7553163..0000000 --- a/src/api/data-sources/nvme/list-namespaces.js +++ /dev/null @@ -1,13 +0,0 @@ -"use strict"; - -const Promise = require("bluebird"); - -const nvmeCli = require("../../../packages/exec-nvme-cli"); - -module.exports = function () { - return function (controllerPaths) { - return Promise.map(controllerPaths, (path) => { - return nvmeCli.listNamespaces({ devicePath: path }); - }); - }; -};