diff --git a/packages/xen-api/README.md b/packages/xen-api/README.md index dcfb33882..4a71c4ddc 100644 --- a/packages/xen-api/README.md +++ b/packages/xen-api/README.md @@ -51,7 +51,9 @@ xapi.objects.on('add', function (objects) { }) ``` -Custom fields on objects (hidden and read-only): +> Note: all objects are frozen and cannot be altered! + +Custom fields on objects (hidden − ie. non enumerable): - `$type`: the type of the object (`VM`, `task`, …); - `$ref`: the (opaque) reference of the object; - `$id`: the identifier of this object (its UUID if any, otherwise its reference); @@ -80,6 +82,13 @@ root@xen1.company.net> xapi.pool.$master.name_label 'xen1' ``` +To ease searches, `find()` and `findAll()` functions are available: + +``` +root@xen1.company.net> findAll({ $type: 'VM' }).length +183 +``` + ## Development ### Installing dependencies diff --git a/packages/xen-api/package.json b/packages/xen-api/package.json index 2c217d51d..5694726b9 100644 --- a/packages/xen-api/package.json +++ b/packages/xen-api/package.json @@ -37,6 +37,7 @@ "exec-promise": "^0.5.1", "kindof": "^2.0.0", "lodash.filter": "^3.1.1", + "lodash.find": "^3.2.1", "lodash.foreach": "^3.0.2", "lodash.isarray": "^3.0.2", "lodash.isobject": "^3.0.1", diff --git a/packages/xen-api/src/cli.js b/packages/xen-api/src/cli.js index 93afb56f4..486c8653e 100755 --- a/packages/xen-api/src/cli.js +++ b/packages/xen-api/src/cli.js @@ -4,6 +4,8 @@ import blocked from 'blocked' import Bluebird, {coroutine} from 'bluebird' import eventToPromise from 'event-to-promise' import execPromise from 'exec-promise' +import filter from 'lodash.filter' +import find from 'lodash.find' import minimist from 'minimist' import pw from 'pw' import {start as createRepl} from 'repl' @@ -84,6 +86,9 @@ const main = coroutine(function * (args) { }) repl.context.xapi = xapi + repl.context.find = predicate => find(xapi.objects.all, predicate) + repl.context.findAll = predicate => filter(xapi.objects.all, predicate) + // Make the REPL waits for promise completion. { const evaluate = Bluebird.promisify(repl.eval)