From 61e1f83a9f2e2e7f34af6bc8df047883d2f7977e Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 28 Nov 2023 17:54:10 +0100 Subject: [PATCH] feat(xo-server/rest-api): possibility to import a VM --- CHANGELOG.unreleased.md | 2 ++ docs/restapi.md | 17 +++++++++++++++++ packages/xo-server/docs/rest-api.md | 17 +++++++++++++++++ packages/xo-server/src/xo-mixins/rest-api.mjs | 16 ++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index ff0f032cd..dd36a1526 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -8,6 +8,7 @@ > Users must be able to say: “Nice enhancement, I'm eager to test it” - [VM/Disks] Display task information when importing VDIs (PR [#7197](https://github.com/vatesfr/xen-orchestra/pull/7197)) +- [REST API] Support VM import using the XVA format ### Bug fixes @@ -30,6 +31,7 @@ - @xen-orchestra/backups patch +- xo-server minor - xo-web minor diff --git a/docs/restapi.md b/docs/restapi.md index b29be161c..78fcd97f5 100644 --- a/docs/restapi.md +++ b/docs/restapi.md @@ -199,6 +199,23 @@ curl \ > myDisk.vhd ``` +## VM Import + +A VM can be imported by posting to `/rest/v0/pools/:id/vms`. + +```sh +curl \ + -X POST \ + -b authenticationToken=KQxQdm2vMiv7jBIK0hgkmgxKzemd8wSJ7ugFGKFkTbs \ + -T myDisk.raw \ + 'https://xo.example.org/rest/v0/pools/355ee47d-ff4c-4924-3db2-fd86ae629676/vms?sr=357bd56c-71f9-4b2a-83b8-3451dec04b8f' \ + | cat +``` + +The `sr` query parameter can be used to specify on which SR the VM should be imported, if not specified, the default SR will be used. + +> Note: the final `| cat` ensures cURL's standard output is not a TTY, which is necessary for upload stats to be dislayed. + ## VDI Import ### Existing VDI diff --git a/packages/xo-server/docs/rest-api.md b/packages/xo-server/docs/rest-api.md index 7de3736ac..52d34af14 100644 --- a/packages/xo-server/docs/rest-api.md +++ b/packages/xo-server/docs/rest-api.md @@ -180,6 +180,23 @@ curl \ > myVM.xva ``` +## VM Import + +A VM can be imported by posting to `/rest/v0/pools/:id/vms`. + +```sh +curl \ + -X POST \ + -b authenticationToken=KQxQdm2vMiv7jBIK0hgkmgxKzemd8wSJ7ugFGKFkTbs \ + -T myDisk.raw \ + 'https://xo.company.lan/rest/v0/pools/355ee47d-ff4c-4924-3db2-fd86ae629676/vms?sr=357bd56c-71f9-4b2a-83b8-3451dec04b8f' \ + | cat +``` + +The `sr` query parameter can be used to specify on which SR the VM should be imported, if not specified, the default SR will be used. + +> Note: the final `| cat` ensures cURL's standard output is not a TTY, which is necessary for upload stats to be dislayed. + ## VDI destruction ```sh diff --git a/packages/xo-server/src/xo-mixins/rest-api.mjs b/packages/xo-server/src/xo-mixins/rest-api.mjs index d3627b36a..b56efb39d 100644 --- a/packages/xo-server/src/xo-mixins/rest-api.mjs +++ b/packages/xo-server/src/xo-mixins/rest-api.mjs @@ -576,6 +576,22 @@ export default class RestApi { }) ) + api.post( + '/:collection(pools)/:object/vms', + wrap(async (req, res) => { + let srRef + const { sr } = req.params + if (sr !== undefined) { + srRef = app.getXapiObject(sr, 'SR').$ref + } + + const { $xapi } = req.xapiObject + const ref = await $xapi.VM_import(req, srRef) + + res.end(await $xapi.getField('VM', ref, 'uuid')) + }) + ) + api.post( '/:collection(srs)/:object/vdis', wrap(async (req, res) => {