1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

F #5422: marketapp export (#1592)

Co-authored-by: Sergio Betanzos <sbetanzos@opennebula.io>
This commit is contained in:
Jorge Miguel Lobo Escalona 2021-11-22 10:52:03 +01:00 committed by GitHub
parent b07daa78d9
commit 5efa6b9b6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 229 additions and 0 deletions

View File

@ -0,0 +1,78 @@
/* ------------------------------------------------------------------------- *
* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
* not use this file except in compliance with the License. You may obtain *
* a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* ------------------------------------------------------------------------- */
const {
defaultEmptyFunction,
defaultCommandMarketApp
} = require('server/utils/constants/defaults')
const {
ok,
internalServerError,
badRequest
} = require('server/utils/constants/http-codes')
const { httpResponse, executeCommand } = require('server/utils/server')
const { getSunstoneConfig } = require('server/utils/yml')
const httpBadRequest = httpResponse(badRequest, '', '')
const appConfig = getSunstoneConfig()
const prependCommand = appConfig.sunstone_prepend || ''
/**
* Exports the marketplace app to the OpenNebula cloud.
*
* @param {object} res - http response
* @param {Function} next - express stepper
* @param {object} params - params of http request
* @param {object} userData - user of http request
*/
const exportApp = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => {
let rtn = httpBadRequest
const { id, name, datastore, file, associated, tag, template, vmname } = params
if (id && name && datastore) {
let message = ''
const paramsCommand = ['export', `${id}`, `${name}`, '--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)
}
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()
}
const functionRoutes = {
exportApp
}
module.exports = functionRoutes

View File

@ -0,0 +1,35 @@
/* ------------------------------------------------------------------------- *
* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
* not use this file except in compliance with the License. You may obtain *
* a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* ------------------------------------------------------------------------- */
const { addFunctionAsRoute, setApiRoutes } = require('server/utils/server')
const { routes: marketappRoutes } = require('./marketapp')
const { MARKETAPP } = require('./string-routes')
/**
* Add routes.
*
* @returns {Array} routes
*/
const generatePrivateRoutes = () => {
return setApiRoutes(marketappRoutes, MARKETAPP, addFunctionAsRoute)
}
const functionRoutes = {
private: generatePrivateRoutes(),
public: []
}
module.exports = functionRoutes

View File

@ -0,0 +1,66 @@
/* ------------------------------------------------------------------------- *
* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
* not use this file except in compliance with the License. You may obtain *
* a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* ------------------------------------------------------------------------- */
const { httpMethod, from: fromData } = require('server/utils/constants/defaults')
const { exportApp } = require('./functions')
const { POST } = httpMethod
const routes = {
[POST]: {
export: {
action: exportApp,
params: {
id: {
from: fromData.resource,
name: 'id'
},
name: {
from: fromData.postBody,
name: 'name'
},
datastore: {
from: fromData.postBody,
name: 'datastore'
},
file: {
from: fromData.postBody,
name: 'file'
},
associated: {
from: fromData.postBody,
name: 'associated'
},
tag: {
from: fromData.postBody,
name: 'tag'
},
template: {
from: fromData.postBody,
name: 'template'
},
vmname: {
from: fromData.postBody,
name: 'vmname'
}
}
}
}
}
const authApi = {
routes
}
module.exports = authApi

View File

@ -0,0 +1,23 @@
/* ------------------------------------------------------------------------- *
* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
* not use this file except in compliance with the License. You may obtain *
* a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* ------------------------------------------------------------------------- */
const MARKETAPP = 'marketapp'
const Actions = {
MARKETAPP
}
module.exports = Actions

View File

@ -68,6 +68,7 @@ const defaults = {
'2fa',
'auth',
'files',
'marketapp',
'oneflow',
'support',
'vcenter',
@ -116,6 +117,7 @@ const defaults = {
defaultCommandProvider: 'oneprovider',
defaultCommandVcenter: 'onevcenter',
defaultCommandVM: 'onevm',
defaultCommandMarketApp: 'onemarketapp',
defaultFolderTmpProvision: 'tmp',
defaultHideCredentials: true,
defaultHideCredentialReplacer: '****',

View File

@ -73,6 +73,30 @@ const setFunctionRoute = (method, endpoint, action) => ({
action
})
/**
* Set functions to API routes.
*
* @param {object} routes - object of routes
* @param {string} path - principal route
* @param {Function} action - function of route
* @returns {Array} parsed routes
*/
const setApiRoutes = (routes = {}, path = '', action = () => undefined) => {
const rtn = []
if (Object.keys(routes).length > 0 && routes.constructor === Object) {
Object.keys(routes).forEach((route) => {
rtn.push(
setFunctionRoute(route, path,
(req, res, next, connection, userId, user) => {
action(req, res, next, routes[route], user, connection)
}
)
)
})
}
return rtn
}
/**
* Execute actions Function routes
*
@ -907,6 +931,7 @@ const executeCommandAsync = (
module.exports = {
setFunctionRoute,
setApiRoutes,
addFunctionAsRoute,
encrypt,
decrypt,