chore(xen-api/*setField*): take separate type and ref

This commit is contained in:
Julien Fontanet 2019-02-22 18:30:43 +01:00
parent 5eb1454e67
commit 58e8d75935
2 changed files with 21 additions and 28 deletions

View File

@ -82,21 +82,17 @@ ${cliName} v${pkg.version}
) )
await Promise.all([ await Promise.all([
srcXapi.setFieldEntries(srcSnapshot, 'other_config', metadata), srcSnapshot.update_other_config(metadata),
srcXapi.setFieldEntries(srcSnapshot, 'other_config', { srcSnapshot.update_other_config({
'xo:backup:exported': 'true', 'xo:backup:exported': 'true',
}), }),
tgtXapi.setField( tgtVm.set_name_label(`${srcVm.name_label} (${srcSnapshot.snapshot_time})`),
tgtVm, tgtVm.update_other_config(metadata),
'name_label', tgtVm.update_other_config({
`${srcVm.name_label} (${srcSnapshot.snapshot_time})`
),
tgtXapi.setFieldEntries(tgtVm, 'other_config', metadata),
tgtXapi.setFieldEntries(tgtVm, 'other_config', {
'xo:backup:sr': tgtSr.uuid, 'xo:backup:sr': tgtSr.uuid,
'xo:copy_of': srcSnapshotUuid, 'xo:copy_of': srcSnapshotUuid,
}), }),
tgtXapi.setFieldEntries(tgtVm, 'blocked_operations', { tgtVm.update_blocked_operations({
start: start:
'Start operation for this vm is blocked, clone it if you want to use it.', 'Start operation for this vm is blocked, clone it if you want to use it.',
}), }),
@ -105,12 +101,9 @@ ${cliName} v${pkg.version}
const srcDisk = srcDisks[userDevice] const srcDisk = srcDisks[userDevice]
const tgtDisk = tgtDisks[userDevice] const tgtDisk = tgtDisks[userDevice]
return tgtXapi.setFieldEntry( return tgtDisk.update_other_config({
tgtDisk, 'xo:copy_of': srcDisk.uuid,
'other_config', })
'xo:copy_of',
srcDisk.uuid
)
}) })
), ),
]) ])

View File

@ -724,39 +724,39 @@ export class Xapi extends EventEmitter {
) )
} }
setField({ $type, $ref }, field, value) { setField(type, ref, field, value) {
return this.call(`${$type}.set_${field}`, $ref, value).then(noop) return this.call(`${type}.set_${field}`, ref, value).then(noop)
} }
setFieldEntries(record, field, entries) { setFieldEntries(type, ref, field, entries) {
return Promise.all( return Promise.all(
getKeys(entries).map(entry => { getKeys(entries).map(entry => {
const value = entries[entry] const value = entries[entry]
if (value !== undefined) { if (value !== undefined) {
return value === null return value === null
? this.unsetFieldEntry(record, field, entry) ? this.unsetFieldEntry(type, ref, field, entry)
: this.setFieldEntry(record, field, entry, value) : this.setFieldEntry(type, ref, field, entry, value)
} }
}) })
).then(noop) ).then(noop)
} }
async setFieldEntry({ $type, $ref }, field, entry, value) { async setFieldEntry(type, ref, field, entry, value) {
while (true) { while (true) {
try { try {
await this.call(`${$type}.add_to_${field}`, $ref, entry, value) await this.call(`${type}.add_to_${field}`, ref, entry, value)
return return
} catch (error) { } catch (error) {
if (error == null || error.code !== 'MAP_DUPLICATE_KEY') { if (error == null || error.code !== 'MAP_DUPLICATE_KEY') {
throw error throw error
} }
} }
await this.unsetFieldEntry({ $type, $ref }, field, entry) await this.unsetFieldEntry(type, ref, field, entry)
} }
} }
unsetFieldEntry({ $type, $ref }, field, entry) { unsetFieldEntry(type, ref, field, entry) {
return this.call(`${$type}.remove_from_${field}`, $ref, entry) return this.call(`${type}.remove_from_${field}`, ref, entry)
} }
watchTask(ref) { watchTask(ref) {
@ -1073,7 +1073,7 @@ export class Xapi extends EventEmitter {
const props = { $type: type } const props = { $type: type }
fields.forEach(field => { fields.forEach(field => {
props[`set_${field}`] = function(value) { props[`set_${field}`] = function(value) {
return xapi.setField(this, field, value) return xapi.setField(this.$type, this.$ref, field, value)
} }
const $field = (field in RESERVED_FIELDS ? '$$' : '$') + field const $field = (field in RESERVED_FIELDS ? '$$' : '$') + field
@ -1102,7 +1102,7 @@ export class Xapi extends EventEmitter {
return result return result
} }
props[`update_${field}`] = function(entries) { props[`update_${field}`] = function(entries) {
return xapi.setFieldEntries(this, field, entries) return xapi.setFieldEntries(this.$type, this.$ref, field, entries)
} }
} else if (value === '' || isOpaqueRef(value)) { } else if (value === '' || isOpaqueRef(value)) {
// 2019-02-07 - JFT: even if `value` should not be an empty string for // 2019-02-07 - JFT: even if `value` should not be an empty string for