chore(xen-api): various changes
This commit is contained in:
parent
b34688043f
commit
82247d7422
@ -88,12 +88,7 @@ const isSessionInvalid = ({ code }) => code === 'SESSION_INVALID'
|
|||||||
|
|
||||||
// ===================================================================
|
// ===================================================================
|
||||||
|
|
||||||
const {
|
const { defineProperties, freeze, keys: getKeys } = Object
|
||||||
create: createObject,
|
|
||||||
defineProperties,
|
|
||||||
freeze: freezeObject,
|
|
||||||
keys: getKeys,
|
|
||||||
} = Object
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
@ -132,19 +127,21 @@ export class Xapi extends EventEmitter {
|
|||||||
constructor(opts) {
|
constructor(opts) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
this._allowUnauthorized = opts.allowUnauthorized
|
|
||||||
this._callTimeout = makeCallSetting(opts.callTimeout, 0)
|
this._callTimeout = makeCallSetting(opts.callTimeout, 0)
|
||||||
this._httpInactivityTimeout = opts.httpInactivityTimeout ?? 5 * 60 * 1e3 // 5 mins
|
this._httpInactivityTimeout = opts.httpInactivityTimeout ?? 5 * 60 * 1e3 // 5 mins
|
||||||
this._pool = null
|
this._pool = null
|
||||||
this._readOnly = Boolean(opts.readOnly)
|
this._readOnly = Boolean(opts.readOnly)
|
||||||
this._RecordsByType = createObject(null)
|
this._RecordsByType = { __proto__: null }
|
||||||
this._sessionId = null
|
this._sessionId = null
|
||||||
|
|
||||||
this._auth = opts.auth
|
this._auth = opts.auth
|
||||||
const url = (this._url = parseUrl(opts.url))
|
const url = parseUrl(opts.url)
|
||||||
if (this._auth === undefined) {
|
if (this._auth === undefined) {
|
||||||
const user = url.username
|
const user = url.username
|
||||||
if (user !== undefined) {
|
if (user === undefined) {
|
||||||
|
throw new TypeError('missing credentials')
|
||||||
|
}
|
||||||
|
|
||||||
this._auth = {
|
this._auth = {
|
||||||
user,
|
user,
|
||||||
password: url.password,
|
password: url.password,
|
||||||
@ -152,8 +149,9 @@ export class Xapi extends EventEmitter {
|
|||||||
delete url.username
|
delete url.username
|
||||||
delete url.password
|
delete url.password
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
this._allowUnauthorized = opts.allowUnauthorized
|
||||||
|
this._setUrl(url)
|
||||||
;(this._objects = new Collection()).getKey = getKey
|
;(this._objects = new Collection()).getKey = getKey
|
||||||
this._debounce = opts.debounce == null ? 200 : opts.debounce
|
this._debounce = opts.debounce == null ? 200 : opts.debounce
|
||||||
this._watchedTypes = undefined
|
this._watchedTypes = undefined
|
||||||
@ -164,7 +162,7 @@ export class Xapi extends EventEmitter {
|
|||||||
|
|
||||||
const { watchEvents } = opts
|
const { watchEvents } = opts
|
||||||
if (watchEvents !== false) {
|
if (watchEvents !== false) {
|
||||||
if (Array.isArray(watchEvents)) {
|
if (isArray(watchEvents)) {
|
||||||
this._watchedTypes = watchEvents
|
this._watchedTypes = watchEvents
|
||||||
}
|
}
|
||||||
this.watchEvents()
|
this.watchEvents()
|
||||||
@ -215,9 +213,6 @@ export class Xapi extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const auth = this._auth
|
const auth = this._auth
|
||||||
if (auth === undefined) {
|
|
||||||
throw new Error('missing credentials')
|
|
||||||
}
|
|
||||||
|
|
||||||
this._sessionId = CONNECTING
|
this._sessionId = CONNECTING
|
||||||
|
|
||||||
@ -254,22 +249,20 @@ export class Xapi extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnect() {
|
async disconnect() {
|
||||||
return Promise.resolve().then(() => {
|
|
||||||
const { status } = this
|
const { status } = this
|
||||||
|
|
||||||
if (status === DISCONNECTED) {
|
if (status === DISCONNECTED) {
|
||||||
return Promise.reject(new Error('already disconnected'))
|
throw new Error('already disconnected')
|
||||||
}
|
}
|
||||||
|
|
||||||
this._transportCall('session.logout', [this._sessionId]).catch(noop)
|
ignoreErrors.call(this._transportCall('session.logout', [this._sessionId]))
|
||||||
|
|
||||||
this._sessionId = null
|
this._sessionId = null
|
||||||
|
|
||||||
debug('%s: disconnected', this._humanId)
|
debug('%s: disconnected', this._humanId)
|
||||||
|
|
||||||
this.emit(DISCONNECTED)
|
this.emit(DISCONNECTED)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===========================================================================
|
// ===========================================================================
|
||||||
@ -629,9 +622,9 @@ export class Xapi extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
watchEvents() {
|
watchEvents() {
|
||||||
this._eventWatchers = createObject(null)
|
this._eventWatchers = { __proto__: null }
|
||||||
|
|
||||||
this._taskWatchers = Object.create(null)
|
this._taskWatchers = { __proto__: null }
|
||||||
|
|
||||||
if (this.status === CONNECTED) {
|
if (this.status === CONNECTED) {
|
||||||
this._watchEventsWrapper()
|
this._watchEventsWrapper()
|
||||||
@ -672,24 +665,8 @@ export class Xapi extends EventEmitter {
|
|||||||
// Private
|
// Private
|
||||||
// ===========================================================================
|
// ===========================================================================
|
||||||
|
|
||||||
get _humanId() {
|
|
||||||
return `${this._auth.user}@${this._url.hostname}`
|
|
||||||
}
|
|
||||||
|
|
||||||
get _url() {
|
|
||||||
return this.__url
|
|
||||||
}
|
|
||||||
|
|
||||||
set _url(url) {
|
|
||||||
this.__url = url
|
|
||||||
this._call = autoTransport({
|
|
||||||
allowUnauthorized: this._allowUnauthorized,
|
|
||||||
url,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
_clearObjects() {
|
_clearObjects() {
|
||||||
;(this._objectsByRef = createObject(null))[NULL_REF] = undefined
|
;(this._objectsByRef = { __proto__: null })[NULL_REF] = undefined
|
||||||
this._nTasks = 0
|
this._nTasks = 0
|
||||||
this._objects.clear()
|
this._objects.clear()
|
||||||
this.objectsFetched = new Promise(resolve => {
|
this.objectsFetched = new Promise(resolve => {
|
||||||
@ -743,11 +720,20 @@ export class Xapi extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_setUrl(url) {
|
||||||
|
this._humanId = `${this._auth.user}@${url.hostname}`
|
||||||
|
this._call = autoTransport({
|
||||||
|
allowUnauthorized: this._allowUnauthorized,
|
||||||
|
url,
|
||||||
|
})
|
||||||
|
this._url = url
|
||||||
|
}
|
||||||
|
|
||||||
_addRecordToCache(type, ref, object) {
|
_addRecordToCache(type, ref, object) {
|
||||||
object = this._wrapRecord(type, ref, object)
|
object = this._wrapRecord(type, ref, object)
|
||||||
|
|
||||||
// Finally freezes the object.
|
// Finally freezes the object.
|
||||||
freezeObject(object)
|
freeze(object)
|
||||||
|
|
||||||
const objects = this._objects
|
const objects = this._objects
|
||||||
const objectsByRef = this._objectsByRef
|
const objectsByRef = this._objectsByRef
|
||||||
@ -791,7 +777,7 @@ export class Xapi extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_processEvents(events) {
|
_processEvents(events) {
|
||||||
forEach(events, event => {
|
events.forEach(event => {
|
||||||
let type = event.class
|
let type = event.class
|
||||||
const lcToTypes = this._lcToTypes
|
const lcToTypes = this._lcToTypes
|
||||||
if (type in lcToTypes) {
|
if (type in lcToTypes) {
|
||||||
@ -1014,7 +1000,7 @@ export class Xapi extends EventEmitter {
|
|||||||
|
|
||||||
Record = function(ref, data) {
|
Record = function(ref, data) {
|
||||||
defineProperties(this, {
|
defineProperties(this, {
|
||||||
$id: { value: data.uuid || ref },
|
$id: { value: data.uuid ?? ref },
|
||||||
$ref: { value: ref },
|
$ref: { value: ref },
|
||||||
$xapi: { value: xapi },
|
$xapi: { value: xapi },
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user