fix: XAPI record types are now properly cased

This commit is contained in:
Julien Fontanet 2019-02-25 12:45:50 +01:00
parent 59106aa29e
commit 420f1c77a1
8 changed files with 21 additions and 64 deletions

View File

@ -95,7 +95,7 @@ root@xen1.company.net> xapi.pool.$master.name_label
To ease searches, `find()` and `findAll()` functions are available:
```
root@xen1.company.net> findAll({ $type: 'vm' }).length
root@xen1.company.net> findAll({ $type: 'VM' }).length
183
```

View File

@ -673,8 +673,9 @@ ${entry.listItem}
}
}
async getRrd(xoObject, secondsAgo) {
const host = xoObject.$type === 'host' ? xoObject : xoObject.$resident_on
async getRrd(xapiObject, secondsAgo) {
const host =
xapiObject.$type === 'host' ? xapiObject : xapiObject.$resident_on
if (host == null) {
return null
}
@ -685,13 +686,13 @@ ${entry.listItem}
host,
query: {
cf: 'AVERAGE',
host: (xoObject.$type === 'host').toString(),
host: (xapiObject.$type === 'host').toString(),
json: 'true',
start: serverTimestamp - secondsAgo,
},
}
if (xoObject.$type === 'vm') {
payload['vm_uuid'] = xoObject.uuid
if (xapiObject.$type === 'VM') {
payload['vm_uuid'] = xapiObject.uuid
}
// JSON is not well formed, can't use the default node parser
return JSON5.parse(

View File

@ -60,7 +60,6 @@ import {
asInteger,
extractOpaqueRef,
filterUndefineds,
getNamespaceForType,
getVmDisks,
canSrHaveNewVdiOfSize,
isVmHvm,
@ -227,7 +226,7 @@ export default class Xapi extends XapiBase {
_setObjectProperty(object, name, value) {
return this.call(
`${getNamespaceForType(object.$type)}.set_${camelToSnakeCase(name)}`,
`${object.$type}.set_${camelToSnakeCase(name)}`,
object.$ref,
prepareXapiParam(value)
)
@ -236,15 +235,13 @@ export default class Xapi extends XapiBase {
_setObjectProperties(object, props) {
const { $ref: ref, $type: type } = object
const namespace = getNamespaceForType(type)
// TODO: the thrown error should contain the name of the
// properties that failed to be set.
return Promise.all(
mapToArray(props, (value, name) => {
if (value != null) {
return this.call(
`${namespace}.set_${camelToSnakeCase(name)}`,
`${type}.set_${camelToSnakeCase(name)}`,
ref,
prepareXapiParam(value)
)
@ -258,9 +255,8 @@ export default class Xapi extends XapiBase {
prop = camelToSnakeCase(prop)
const namespace = getNamespaceForType(type)
const add = `${namespace}.add_to_${prop}`
const remove = `${namespace}.remove_from_${prop}`
const add = `${type}.add_to_${prop}`
const remove = `${type}.remove_from_${prop}`
await Promise.all(
mapToArray(values, (value, name) => {
@ -327,15 +323,13 @@ export default class Xapi extends XapiBase {
async addTag(id, tag) {
const { $ref: ref, $type: type } = this.getObject(id)
const namespace = getNamespaceForType(type)
await this.call(`${namespace}.add_tags`, ref, tag)
await this.call(`${type}.add_tags`, ref, tag)
}
async removeTag(id, tag) {
const { $ref: ref, $type: type } = this.getObject(id)
const namespace = getNamespaceForType(type)
await this.call(`${namespace}.remove_tags`, ref, tag)
await this.call(`${type}.remove_tags`, ref, tag)
}
// =================================================================
@ -1708,7 +1702,7 @@ export default class Xapi extends XapiBase {
find(
this.objects.all,
obj =>
obj.$type === 'vm' &&
obj.$type === 'VM' &&
obj.is_a_template &&
obj.name_label === templateNameLabel
)
@ -2208,7 +2202,7 @@ export default class Xapi extends XapiBase {
const physPif = find(
this.objects.all,
obj =>
obj.$type === 'pif' &&
obj.$type === 'PIF' &&
(obj.physical || !isEmpty(obj.bond_master_of)) &&
obj.$pool === pif.$pool &&
obj.device === pif.device
@ -2444,7 +2438,7 @@ export default class Xapi extends XapiBase {
return find(
this.objects.all,
obj =>
obj.$type === 'sr' && obj.shared && canSrHaveNewVdiOfSize(obj, minSize)
obj.$type === 'SR' && obj.shared && canSrHaveNewVdiOfSize(obj, minSize)
)
}

View File

@ -166,7 +166,7 @@ export default {
async _ejectToolsIsos(hostRef) {
return Promise.all(
mapFilter(this.objects.all, vm => {
if (vm.$type !== 'vm' || (hostRef && vm.resident_on !== hostRef)) {
if (vm.$type !== 'VM' || (hostRef && vm.resident_on !== hostRef)) {
return
}

View File

@ -71,44 +71,6 @@ export const extractOpaqueRef = str => {
// -------------------------------------------------------------------
const TYPE_TO_NAMESPACE = { __proto__: null }
forEach(
[
'Bond',
'DR_task',
'GPU_group',
'PBD',
'PCI',
'PGPU',
'PIF',
'PIF_metrics',
'SM',
'SR',
'VBD',
'VBD_metrics',
'VDI',
'VGPU',
'VGPU_type',
'VIF',
'VLAN',
'VM',
'VM_appliance',
'VM_guest_metrics',
'VM_metrics',
'VMPP',
'VTPM',
],
namespace => {
TYPE_TO_NAMESPACE[namespace.toLowerCase()] = namespace
}
)
// Object types given by `xen-api` are always lowercase but the
// namespaces in the Xen API can have a different casing.
export const getNamespaceForType = type => TYPE_TO_NAMESPACE[type] || type
// -------------------------------------------------------------------
export const getVmDisks = vm => {
const disks = { __proto__: null }
forEach(vm.$VBDs, vbd => {
@ -288,7 +250,7 @@ export const makeEditObject = specs => {
const object = this.getObject(id)
const _objectRef = object.$ref
const _setMethodPrefix = `${getNamespaceForType(object.$type)}.set_`
const _setMethodPrefix = `${object.$type}.set_`
// Context used to execute functions.
const context = {

View File

@ -192,7 +192,7 @@ const listReplicatedVms = (
const object = all[key]
const oc = object.other_config
if (
object.$type === 'vm' &&
object.$type === 'VM' &&
!object.is_a_snapshot &&
!object.is_a_template &&
'start' in object.blocked_operations &&

View File

@ -427,7 +427,7 @@ export default class {
let toRemove = filter(
targetXapi.objects.all,
obj => obj.$type === 'vm' && obj.other_config[TAG_SOURCE_VM] === uuid
obj => obj.$type === 'VM' && obj.other_config[TAG_SOURCE_VM] === uuid
)
const { length } = toRemove
const deleteBase = length === 0 // old replications are not captured in toRemove

View File

@ -329,7 +329,7 @@ export default class {
let id
let set
if (
object.$type !== 'vm' ||
object.$type !== 'VM' ||
object.is_a_snapshot ||
('start' in object.blocked_operations &&
(object.tags.includes('Disaster Recovery') ||