diff --git a/src/fireedge/src/server/routes/api/marketapp/functions.js b/src/fireedge/src/server/routes/api/marketapp/functions.js new file mode 100644 index 0000000000..dda6c6d2d9 --- /dev/null +++ b/src/fireedge/src/server/routes/api/marketapp/functions.js @@ -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 diff --git a/src/fireedge/src/server/routes/api/marketapp/index.js b/src/fireedge/src/server/routes/api/marketapp/index.js new file mode 100644 index 0000000000..9ed3e8bf38 --- /dev/null +++ b/src/fireedge/src/server/routes/api/marketapp/index.js @@ -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 diff --git a/src/fireedge/src/server/routes/api/marketapp/marketapp.js b/src/fireedge/src/server/routes/api/marketapp/marketapp.js new file mode 100644 index 0000000000..62cd9b1c03 --- /dev/null +++ b/src/fireedge/src/server/routes/api/marketapp/marketapp.js @@ -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 diff --git a/src/fireedge/src/server/routes/api/marketapp/string-routes.js b/src/fireedge/src/server/routes/api/marketapp/string-routes.js new file mode 100644 index 0000000000..1ec91fe40c --- /dev/null +++ b/src/fireedge/src/server/routes/api/marketapp/string-routes.js @@ -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 diff --git a/src/fireedge/src/server/utils/constants/defaults.js b/src/fireedge/src/server/utils/constants/defaults.js index c3da56b53b..6a516a0c4e 100644 --- a/src/fireedge/src/server/utils/constants/defaults.js +++ b/src/fireedge/src/server/utils/constants/defaults.js @@ -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: '****', diff --git a/src/fireedge/src/server/utils/server.js b/src/fireedge/src/server/utils/server.js index 3ac0580d83..75476b17f2 100644 --- a/src/fireedge/src/server/utils/server.js +++ b/src/fireedge/src/server/utils/server.js @@ -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,