Skip to content

Commit

Permalink
feat(xo-server): new API method esxi.listVms
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeauchamp authored and julien-f committed Feb 26, 2023
1 parent b69ce3f commit 1f55667
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
31 changes: 30 additions & 1 deletion @xen-orchestra/vmware-explorer/esxi.mjs
Expand Up @@ -188,6 +188,35 @@ export default class Esxi extends EventEmitter {
}
}

async getAllVmMetadata() {
const datas = await this.search('VirtualMachine', ['config', 'storage', 'runtime'])

return Object.keys(datas).map(id => {
const { config, storage, runtime } = datas[id]
const perDatastoreUsage = Array.isArray(storage.perDatastoreUsage)
? storage.perDatastoreUsage
: [storage.perDatastoreUsage]
return {
id,
nameLabel: config.name,
memory: +config.hardware.memoryMB * 1024 * 1024,
nCpus: +config.hardware.numCPU,
guestToolsInstalled: false,
firmware: config.firmware === 'efi' ? 'uefi' : config.firmware, // bios or uefi
powerState: runtime.powerState,
storage: perDatastoreUsage.reduce(
(prev, curr) => {
return {
used: prev.used + +curr.committed,
free: prev.free + +curr.uncommitted,
}
},
{ used: 0, free: 0 }
),
}
})
}

async getTransferableVmMetadata(vmId) {
const search = await this.search('VirtualMachine', ['name', 'config', 'storage', 'runtime', 'snapshot'])
if (search[vmId] === undefined) {
Expand Down Expand Up @@ -253,7 +282,7 @@ export default class Esxi extends EventEmitter {
return {
name_label: config.name,
memory: +config.hardware.memoryMB * 1024 * 1024,
numCpu: +config.hardware.numCPU,
nCpus: +config.hardware.numCPU,
guestToolsInstalled: false,
firmware: config.firmware === 'efi' ? 'uefi' : config.firmware, // bios or uefi
powerState: runtime.powerState,
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.unreleased.md
Expand Up @@ -36,7 +36,7 @@
<!--packages-start-->

- @xen-orchestra/vmware-explorer patch
- @xen-orchestra/vmware-explorer minor
- @xen-orchestra/backups minor
- xo-cli minor
- xo-server-auth-oidc minor
Expand Down
12 changes: 12 additions & 0 deletions packages/xo-server/src/api/esxi.mjs
@@ -0,0 +1,12 @@
export function listVms({ host, password, sslVerify = true, user }) {
return this.connectToEsxiAndList({ host, user, password, sslVerify })
}

listVms.params = {
host: { type: 'string' },
user: { type: 'string' },
password: { type: 'string' },
sslVerify: { type: 'boolean', optional: true },
}

listVms.permission = 'admin'
11 changes: 8 additions & 3 deletions packages/xo-server/src/xo-mixins/migrate-vm.mjs
Expand Up @@ -161,6 +161,11 @@ export default class MigrateVm {
})
}

async connectToEsxiAndList({ host, user, password, sslVerify }) {
const esxi = await this.#connectToEsxi(host, user, password, sslVerify)
return esxi.getAllVmMetadata()
}

@decorateWith(deferrable)
async migrationfromEsxi(
$defer,
Expand All @@ -173,7 +178,7 @@ export default class MigrateVm {
return esxi.getTransferableVmMetadata(vmId)
})

const { disks, firmware, memory, name_label, networks, numCpu, powerState, snapshots } = esxiVmMetadata
const { disks, firmware, memory, name_label, networks, nCpus, powerState, snapshots } = esxiVmMetadata
const isRunning = powerState !== 'poweredOff'

const chainsByNodes = await new Task({ name: `build disks and snapshots chains for ${vmId}` }).run(async () => {
Expand All @@ -194,8 +199,8 @@ export default class MigrateVm {
memory_static_min: memory,
name_description: 'from esxi',
name_label,
VCPUs_at_startup: numCpu,
VCPUs_max: numCpu,
VCPUs_at_startup: nCpus,
VCPUs_max: nCpus,
})
)
await Promise.all([
Expand Down

0 comments on commit 1f55667

Please sign in to comment.