1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-19 06:50:07 +03:00

F #5422: Add marketapp import (#1629)

This commit is contained in:
Jorge Miguel Lobo Escalona 2021-11-30 16:10:03 +01:00 committed by GitHub
parent c4e131164d
commit 5ff147b27c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 15 deletions

View File

@ -38,6 +38,14 @@ const prependCommand = appConfig.sunstone_prepend || ''
* @param {object} res - http response
* @param {Function} next - express stepper
* @param {object} params - params of http request
* @param {number} params.id - app id
* @param {string} params.name - app name
* @param {number} params.datastore - app datastore
* @param {string} [params.file] - app file
* @param {string} [params.associated] - app associated resource
* @param {string} [params.tag] - app tag
* @param {string} [params.template] - app template
* @param {string} [params.vmname] - app vm name
* @param {object} userData - user of http request
*/
const exportApp = (
@ -58,21 +66,53 @@ const exportApp = (
'--datastore',
datastore,
]
if (file) {
paramsCommand.push('--file-datastore', file)
}
if (associated && associated === 'true') {
paramsCommand.push('--no')
}
if (tag) {
paramsCommand.push('--tag', tag)
}
if (template) {
paramsCommand.push('--template', template)
}
if (vmname) {
paramsCommand.push('--vmname', vmname)
file && paramsCommand.push('--file-datastore', file)
associated && associated === 'true' && paramsCommand.push('--no')
tag && paramsCommand.push('--tag', tag)
template && paramsCommand.push('--template', template)
vmname && paramsCommand.push('--vmname', vmname)
const executedCommand = executeCommand(
defaultCommandMarketApp,
paramsCommand,
prependCommand
)
const response = executedCommand.success ? ok : internalServerError
if (executedCommand.data) {
message = executedCommand.data
}
rtn = httpResponse(response, message)
}
res.locals.httpCode = rtn
next()
}
/**
* Import the marketplace VM or VM TEPLATE to the OpenNebula cloud.
*
* @param {object} res - http response
* @param {Function} next - express stepper
* @param {object} params - params of http request
* @param {number} [params.vmId] - vm id
* @param {number} [params.templateId] - template id
* @param {number} [params.marketId] - market id
* @param {string} [params.associated=''] - associated resource
* @param {number} [params.vmname] - vm name
*/
const importMarket = (res = {}, next = defaultEmptyFunction, params = {}) => {
let rtn = httpBadRequest
const { vmId, templateId, marketId, associated, vmname } = params
const resource = vmId ? 'vm' : 'vm-template'
const id = vmId || templateId
if (id) {
let message = ''
const paramsCommand = [resource, 'import', `${id}`]
paramsCommand.push(associated && associated === 'true' ? '--yes' : '--no')
marketId && paramsCommand.push('--market', marketId)
vmname && paramsCommand.push('--vmname', vmname)
const executedCommand = executeCommand(
defaultCommandMarketApp,
paramsCommand,
@ -90,5 +130,6 @@ const exportApp = (
const functionRoutes = {
exportApp,
importMarket,
}
module.exports = functionRoutes

View File

@ -18,7 +18,7 @@ const {
httpMethod,
from: fromData,
} = require('server/utils/constants/defaults')
const { exportApp } = require('./functions')
const { exportApp, importMarket } = require('./functions')
const { POST } = httpMethod
const routes = {
@ -60,6 +60,56 @@ const routes = {
},
},
},
vmimport: {
action: importMarket,
params: {
vmId: {
from: fromData.resource,
name: 'id',
},
name: {
from: fromData.postBody,
name: 'name',
},
associated: {
from: fromData.postBody,
name: 'associated',
},
marketId: {
from: fromData.postBody,
name: 'marketId',
},
vmname: {
from: fromData.postBody,
name: 'vmname',
},
},
},
templateimport: {
action: importMarket,
params: {
templateId: {
from: fromData.resource,
name: 'id',
},
name: {
from: fromData.postBody,
name: 'name',
},
associated: {
from: fromData.postBody,
name: 'associated',
},
marketId: {
from: fromData.postBody,
name: 'marketId',
},
vmname: {
from: fromData.postBody,
name: 'vmname',
},
},
},
},
}