feat(xapi/VTPM): ability to create, destroy VTPM (#7074)

This commit is contained in:
Mathieu 2023-10-12 07:19:38 +00:00 committed by GitHub
parent bea771ca90
commit 2e634a9d1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 0 deletions

View File

@ -5,3 +5,4 @@ export { default as VBD } from './vbd.mjs'
export { default as VDI } from './vdi.mjs'
export { default as VIF } from './vif.mjs'
export { default as VM } from './vm.mjs'
export { default as VTPM } from './vtpm.mjs'

View File

@ -0,0 +1,37 @@
import upperFirst from 'lodash/upperFirst.js'
import { incorrectState } from 'xo-common/api-errors.js'
export default class Vtpm {
async create({ is_unique = false, VM }) {
const pool = this.pool
// If VTPM.create is called on a pool that doesn't support VTPM, the errors aren't explicit.
// See https://github.com/xapi-project/xen-api/issues/5186
if (pool.restrictions.restrict_vtpm !== 'false') {
throw incorrectState({
actual: pool.restrictions.restrict_vtpm,
expected: 'false',
object: pool.uuid,
property: 'restrictions.restrict_vtpm',
})
}
try {
return await this.call('VTPM.create', VM, is_unique)
} catch (error) {
const { code, params } = error
if (code === 'VM_BAD_POWER_STATE') {
const [, expected, actual] = params
// In `VM_BAD_POWER_STATE` errors, the power state is lowercased
throw incorrectState({
actual: upperFirst(actual),
expected: upperFirst(expected),
object: await this.getField('VM', VM, 'uuid'),
property: 'power_state',
})
}
throw error
}
}
}

View File

@ -0,0 +1,29 @@
export async function create({ vm }) {
const xapi = this.getXapi(vm)
const vtpmRef = await xapi.VTPM_create({ VM: vm._xapiRef })
return xapi.getField('VTPM', vtpmRef, 'uuid')
}
create.description = 'create a VTPM'
create.params = {
id: { type: 'string' },
}
create.resolve = {
vm: ['id', 'VM', 'administrate'],
}
export async function destroy({ vtpm }) {
await this.getXapi(vtpm).call('VTPM.destroy', vtpm._xapiRef)
}
destroy.description = 'destroy a VTPM'
destroy.params = {
id: { type: 'string' },
}
destroy.resolve = {
vtpm: ['id', 'VTPM', 'administrate'],
}

View File

@ -118,6 +118,7 @@ const TRANSFORMS = {
},
suspendSr: link(obj, 'suspend_image_SR'),
zstdSupported: obj.restrictions.restrict_zstd_export === 'false',
vtpmSupported: obj.restrictions.restrict_vtpm === 'false',
// TODO
// - ? networks = networksByPool.items[pool.id] (network.$pool.id)
@ -413,6 +414,7 @@ const TRANSFORMS = {
suspendSr: link(obj, 'suspend_SR'),
tags: obj.tags,
VIFs: link(obj, 'VIFs'),
VTPMs: link(obj, 'VTPMs'),
virtualizationMode: domainType,
// deprecated, use pvDriversVersion instead
@ -841,6 +843,14 @@ const TRANSFORMS = {
vgpus: link(obj, 'VGPUs'),
}
},
vtpm(obj) {
return {
type: 'VTPM',
vm: link(obj, 'VM'),
}
},
}
// ===================================================================