From 922273324363834736e19933e1ec9bcf4dc0c10b Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Wed, 6 May 2015 14:06:17 +0200 Subject: [PATCH] Xapi#getObject() --- packages/xen-api/src/index.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/xen-api/src/index.js b/packages/xen-api/src/index.js index ace2a0165..12d2ff3f5 100644 --- a/packages/xen-api/src/index.js +++ b/packages/xen-api/src/index.js @@ -212,6 +212,23 @@ export class Xapi extends EventEmitter { return this._sessionCall(method, args) } + // Nice getter which returns the object for a given $id (internal to + // this lib), UUID (unique identifier that some objects have) or + // opaque reference (internal to XAPI). + getObject (idOrUuidOrRef, defaultValue) { + const object = ( + // if there is an UUID, it is also the $id. + this.objects.all[idOrUuidOrRef] || + this._objectsByRefs[idOrUuidOrRef] + ) + + if (object) return object + + if (arguments.length > 1) return defaultValue + + throw new Error('there is not object can be matched to ' + idOrUuidOrRef) + } + getObjectByRef (ref, defaultValue) { const { _objectsByRefs: objectsByRefs