From 86e390f70f6fc2ef2ac7149126356076c4e2ccf8 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 24 Feb 2022 16:34:48 +0100 Subject: [PATCH] feat(xo-server/vm.import): support from URL --- packages/xo-server/src/api/vm.mjs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/xo-server/src/api/vm.mjs b/packages/xo-server/src/api/vm.mjs index 5426b943d..e81ab4b62 100644 --- a/packages/xo-server/src/api/vm.mjs +++ b/packages/xo-server/src/api/vm.mjs @@ -3,6 +3,7 @@ import assignWith from 'lodash/assignWith.js' import asyncMapSettled from '@xen-orchestra/async-map/legacy.js' import concat from 'lodash/concat.js' import getStream from 'get-stream' +import hrp from 'http-request-plus' import { createLogger } from '@xen-orchestra/log' import { defer } from 'golike-defer' import { FAIL_ON_QUEUE } from 'limit-concurrency-decorator' @@ -1102,22 +1103,28 @@ async function handleVmImport(req, res, { data, srId, type, xapi }) { } // TODO: "sr_id" can be passed in URL to target a specific SR -async function import_({ data, sr, type }) { +async function import_({ data, sr, type = 'xva', url }) { if (data && type === 'xva') { throw invalidParameters('unsupported field data for the file type xva') } + const xapi = this.getXapi(sr) + const srId = sr._xapiId + + if (url !== undefined) { + if (type !== 'xva') { + throw invalidParameters('URL import is only compatible with XVA') + } + + return (await xapi.importVm(await hrp(url), { srId, type })).$id + } + return { $sendTo: await this.registerApiHttpRequest( 'vm.import', this.session, handleVmImport, - { - data, - srId: sr._xapiId, - type, - xapi: this.getXapi(sr), - }, + { data, srId, type, xapi }, { exposeAllErrors: true } ), } @@ -1155,6 +1162,7 @@ import_.params = { }, type: { type: 'string', optional: true }, sr: { type: 'string' }, + url: { type: 'string', optional: true }, } import_.resolve = {