UNPKG

@stordata/vsphere-soapify

Version:

A NodeJS abstraction layer for the vSphere SOAP API

61 lines (49 loc) 1.95 kB
'use strict'; const _ = require('lodash'), ManagedEntity = require('./ManagedEntity'), RetrieveResult = require('../data/RetrieveResult'), PropertyFilterSpec = require('../data/PropertyFilterSpec'), PropertySpec = require('../data/PropertySpec'), ObjectSpec = require('../data/ObjectSpec'); module.exports = class PropertyCollector extends ManagedEntity { /** * Retrieves the specified properties of the specified managed objects. * * @param specSet {PropertyFilterSpec} The specification of properties to retrieve * @param [options={}] {RetrieveOptions} Options for the call * * @returns {Promise<Array<ObjectContent>>} A Promise resolving with an array of retrieved objects */ async retrievePropertiesEx(specSet, options = {}) { return this.call('RetrievePropertiesExAsync', { specSet, options }, RetrieveResult) .then(result => this._keepOnCollecting(result)); } /** * Retrieves additional results from a retrieval started by RetrievePropertiesEx on the same session. * * @param token {String} The token obtained during the previous call * * @returns {Promise<RetrieveResult>} A Promise resolving when the call completes */ async continueRetrievePropertiesEx(token) { return this.call('ContinueRetrievePropertiesExAsync', { token }, RetrieveResult); } async retrieveObjectWithProperties(object, properties) { const { constructor } = object; return this.retrievePropertiesEx(new PropertyFilterSpec( new PropertySpec(constructor.name, false, properties || constructor.properties()), new ObjectSpec(object) )) .then(_.first); } async _keepOnCollecting(result) { if (!result) { return []; } const { objects = [], token } = result; if (token) { objects.push(...await this.continueRetrievePropertiesEx(token).then(next => this._keepOnCollecting(next))); } return objects; } };