From e6aae8fcfa3ffeed722d0e06ecf4da3089c766c9 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 28 Mar 2019 12:18:51 +0100 Subject: [PATCH] chore(xen-api): regroup object handling helpers --- packages/xen-api/src/index.js | 120 ++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 58 deletions(-) diff --git a/packages/xen-api/src/index.js b/packages/xen-api/src/index.js index 47bc58f62..b82e6f2ae 100644 --- a/packages/xen-api/src/index.js +++ b/packages/xen-api/src/index.js @@ -370,6 +370,68 @@ export class Xapi extends EventEmitter { return promise } + // =========================================================================== + // Objects handling helpers + // =========================================================================== + + async getAllRecords(type) { + return map( + await this._sessionCall(`${type}.get_all_records`), + (record, ref) => this._wrapRecord(type, ref, record) + ) + } + + async getRecord(type, ref) { + return this._wrapRecord( + type, + ref, + await this._sessionCall(`${type}.get_record`, [ref]) + ) + } + + async getRecordByUuid(type, uuid) { + return this.getRecord( + type, + await this._sessionCall(`${type}.get_by_uuid`, [uuid]) + ) + } + + getRecords(type, refs) { + return Promise.all(refs.map(ref => this.getRecord(type, ref))) + } + + setField(type, ref, field, value) { + return this.call(`${type}.set_${field}`, ref, value).then(noop) + } + + setFieldEntries(type, ref, field, entries) { + return Promise.all( + getKeys(entries).map(entry => { + const value = entries[entry] + if (value !== undefined) { + return this.setFieldEntry(type, ref, field, entry, value) + } + }) + ).then(noop) + } + + async setFieldEntry(type, ref, field, entry, value) { + if (value === null) { + return this.call(`${type}.remove_from_${field}`, ref, entry).then(noop) + } + while (true) { + try { + await this.call(`${type}.add_to_${field}`, ref, entry, value) + return + } catch (error) { + if (error?.code !== 'MAP_DUPLICATE_KEY') { + throw error + } + } + await this.call(`${type}.remove_from_${field}`, ref, entry) + } + } + // create a task and automatically destroy it when settled // // allowed even in read-only mode because it does not have impact on the @@ -436,32 +498,6 @@ export class Xapi extends EventEmitter { throw new Error('no object with UUID: ' + uuid) } - async getRecord(type, ref) { - return this._wrapRecord( - type, - ref, - await this._sessionCall(`${type}.get_record`, [ref]) - ) - } - - getRecords(type, refs) { - return Promise.all(refs.map(ref => this.getRecord(type, ref))) - } - - async getAllRecords(type) { - return map( - await this._sessionCall(`${type}.get_all_records`), - (record, ref) => this._wrapRecord(type, ref, record) - ) - } - - async getRecordByUuid(type, uuid) { - return this.getRecord( - type, - await this._sessionCall(`${type}.get_by_uuid`, [uuid]) - ) - } - @cancelable getResource($cancelToken, pathname, { host, query, task } = {}) { return this._autoTask(task, `Xapi#getResource ${pathname}`).then( @@ -616,38 +652,6 @@ export class Xapi extends EventEmitter { ) } - setField(type, ref, field, value) { - return this.call(`${type}.set_${field}`, ref, value).then(noop) - } - - setFieldEntries(type, ref, field, entries) { - return Promise.all( - getKeys(entries).map(entry => { - const value = entries[entry] - if (value !== undefined) { - return this.setFieldEntry(type, ref, field, entry, value) - } - }) - ).then(noop) - } - - async setFieldEntry(type, ref, field, entry, value) { - if (value === null) { - return this.call(`${type}.remove_from_${field}`, ref, entry).then(noop) - } - while (true) { - try { - await this.call(`${type}.add_to_${field}`, ref, entry, value) - return - } catch (error) { - if (error == null || error.code !== 'MAP_DUPLICATE_KEY') { - throw error - } - } - await this.call(`${type}.remove_from_${field}`, ref, entry) - } - } - watchTask(ref) { const watchers = this._taskWatchers if (watchers === undefined) {