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

F #5422: delete duplicate functions

Signed-off-by: Jorge Lobo <jlobo@opennebula.io>
This commit is contained in:
Jorge Lobo 2021-07-02 10:45:39 +02:00
parent 505e65d693
commit 17b79abfff
No known key found for this signature in database
GPG Key ID: 9C21660F7B06905E
12 changed files with 67 additions and 196 deletions

View File

@ -13,27 +13,22 @@
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
const { main: service, routes: serviceRoutes } = require('./service')
const { main: serviceTemplate, routes: serviceTemplateRoutes } = require('./service_template')
const { addFunctionToRoute, fillFunctionRoute } = require('server/utils/server')
const { routes: serviceRoutes } = require('./service')
const { routes: serviceTemplateRoutes } = require('./service_template')
const { SERVICE, SERVICE_TEMPLATE } = require('./string-routes')
const privateRoutes = []
const publicRoutes = []
const fillRoute = (method, endpoint, action) => ({
httpMethod: method,
endpoint,
action
})
const fillPrivateRoutes = (methods = {}, path = '', action = () => undefined) => {
if (Object.keys(methods).length > 0 && methods.constructor === Object) {
Object.keys(methods).forEach((method) => {
privateRoutes.push(
fillRoute(method, path,
fillFunctionRoute(method, path,
(req, res, next, connection, userId, user) => {
action(req, res, next, methods[method], user)
action(req, res, next, methods[method], user, connection)
})
)
})
@ -41,8 +36,8 @@ const fillPrivateRoutes = (methods = {}, path = '', action = () => undefined) =>
}
const generatePrivateRoutes = () => {
fillPrivateRoutes(serviceRoutes, SERVICE, service)
fillPrivateRoutes(serviceTemplateRoutes, SERVICE_TEMPLATE, serviceTemplate)
fillPrivateRoutes(serviceRoutes, SERVICE, addFunctionToRoute)
fillPrivateRoutes(serviceTemplateRoutes, SERVICE_TEMPLATE, addFunctionToRoute)
return privateRoutes
}

View File

@ -64,31 +64,8 @@ const routes = {
}
}
}
const main = (req = {}, res = {}, next = () => undefined, routes = {}, user = {}, index = 0) => {
const resources = Object.keys(req[fromData.resource])
if (req && res && next && routes) {
const route = routes[`${req[fromData.resource][resources[index]]}`.toLowerCase()]
if (req && fromData && fromData.resource && req[fromData.resource] && route) {
if (Object.keys(route).length > 0 && route.constructor === Object) {
if (route.action && route.params && typeof route.action === 'function') {
const params = getParamsForObject(route.params, req)
route.action(res, next, params, user)
} else {
main(req, res, next, route, user, index + 1)
}
} else {
next()
}
} else {
next()
}
} else {
next()
}
}
const serviceApi = {
main,
routes
}

View File

@ -14,7 +14,6 @@
/* -------------------------------------------------------------------------- */
const { httpMethod, from: fromData } = require('server/utils/constants/defaults')
const { getParamsForObject } = require('server/utils/server')
const {
serviceTemplate,
serviceTemplateDelete,
@ -67,31 +66,7 @@ const routes = {
}
}
const main = (req = {}, res = {}, next = () => undefined, routes = {}, user = {}, index = 0) => {
const resources = Object.keys(req[fromData.resource])
if (req && res && next && routes) {
const route = routes[`${req[fromData.resource][resources[index]]}`.toLowerCase()]
if (req && fromData && fromData.resource && req[fromData.resource] && route) {
if (Object.keys(route).length > 0 && route.constructor === Object) {
if (route.action && route.params && typeof route.action === 'function') {
const params = getParamsForObject(route.params, req)
route.action(res, next, params, user)
} else {
main(req, res, next, route, user, index + 1)
}
} else {
next()
}
} else {
next()
}
} else {
next()
}
}
const serviceTemplateApi = {
main,
routes
}

View File

@ -12,26 +12,21 @@
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
const { main: provision, routes: provisionRoutes } = require('./provision')
const { main: provisionTemplate, routes: provisionTemplateRoutes } = require('./provision_template')
const { main: provider, routes: providerRoutes } = require('./provider')
const { addFunctionToRoute, fillFunctionRoute } = require('server/utils/server')
const { routes: provisionRoutes } = require('./provision')
const { routes: provisionTemplateRoutes } = require('./provision_template')
const { routes: providerRoutes } = require('./provider')
const { PROVIDER, PROVISION, PROVISION_TEMPLATE } = require('./string-routes')
const privateRoutes = []
const publicRoutes = []
const fillRoute = (method, endpoint, action) => ({
httpMethod: method,
endpoint,
action
})
const fillPrivateRoutes = (methods = {}, path = '', action = () => undefined) => {
if (Object.keys(methods).length > 0 && methods.constructor === Object) {
Object.keys(methods).forEach((method) => {
privateRoutes.push(
fillRoute(method, path,
fillFunctionRoute(method, path,
(req, res, next, connection, userId, user) => {
action(req, res, next, methods[method], user, connection)
})
@ -41,9 +36,9 @@ const fillPrivateRoutes = (methods = {}, path = '', action = () => undefined) =>
}
const generatePrivateRoutes = () => {
fillPrivateRoutes(provisionRoutes, PROVISION, provision)
fillPrivateRoutes(provisionTemplateRoutes, PROVISION_TEMPLATE, provisionTemplate)
fillPrivateRoutes(providerRoutes, PROVIDER, provider)
fillPrivateRoutes(provisionRoutes, PROVISION, addFunctionToRoute)
fillPrivateRoutes(provisionTemplateRoutes, PROVISION_TEMPLATE, addFunctionToRoute)
fillPrivateRoutes(providerRoutes, PROVIDER, addFunctionToRoute)
return privateRoutes
}

View File

@ -67,31 +67,7 @@ const routes = {
}
}
const main = (req = {}, res = {}, next = () => undefined, routes = {}, user = {}, oneConnection = defaultEmptyFunction, index = 0) => {
const resources = Object.keys(req[fromData.resource])
if (req && res && next && routes) {
const route = routes[`${req[fromData.resource][resources[index]]}`.toLowerCase()]
if (req && fromData && fromData.resource && req[fromData.resource] && route) {
if (Object.keys(route).length > 0 && route.constructor === Object) {
if (route.action && route.params && typeof route.action === 'function') {
const params = getParamsForObject(route.params, req)
route.action(res, next, params, user)
} else {
main(req, res, next, route, user, index + 1)
}
} else {
next()
}
} else {
next()
}
} else {
next()
}
}
const providerApi = {
main,
routes
}
module.exports = providerApi

View File

@ -12,8 +12,7 @@
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
const { httpMethod, from: fromData, defaultEmptyFunction } = require('server/utils/constants/defaults')
const { getParamsForObject } = require('server/utils/server')
const { httpMethod, from: fromData } = require('server/utils/constants/defaults')
const {
getList,
getListProvisions,
@ -219,31 +218,7 @@ const routes = {
}
}
const main = (req = {}, res = {}, next = defaultEmptyFunction, routes = {}, user = {}, oneConnection = defaultEmptyFunction, index = 0) => {
const resources = Object.keys(req[fromData.resource])
if (req && res && next && routes) {
const route = routes[`${req[fromData.resource][resources[index]]}`.toLowerCase()]
if (req && fromData && fromData.resource && req[fromData.resource] && route) {
if (Object.keys(route).length > 0 && route.constructor === Object) {
if (route.action && route.params && typeof route.action === 'function') {
const params = getParamsForObject(route.params, req)
route.action(res, next, params, user, oneConnection)
} else {
main(req, res, next, route, user, oneConnection, index + 1)
}
} else {
next()
}
} else {
next()
}
} else {
next()
}
}
const provisionApi = {
main,
routes
}
module.exports = provisionApi

View File

@ -12,8 +12,7 @@
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
const { from: fromData, defaultEmptyFunction } = require('server/utils/constants/defaults')
const { getParamsForObject } = require('server/utils/server')
const { from: fromData } = require('server/utils/constants/defaults')
const {
getListProvisionTemplates,
createProvisionTemplate,
@ -67,31 +66,7 @@ const routes = {
}
}
const main = (req = {}, res = {}, next = defaultEmptyFunction, routes = {}, user = {}, oneConnection = defaultEmptyFunction, index = 0) => {
const resources = Object.keys(req[fromData.resource])
if (req && res && next && routes) {
const route = routes[`${req[fromData.resource][resources[index]]}`.toLowerCase()]
if (req && fromData && fromData.resource && req[fromData.resource] && route) {
if (Object.keys(route).length > 0 && route.constructor === Object) {
if (route.action && route.params && typeof route.action === 'function') {
const params = getParamsForObject(route.params, req)
route.action(res, next, params, user)
} else {
main(req, res, next, route, user, index + 1)
}
} else {
next()
}
} else {
next()
}
} else {
next()
}
}
const provisionTemplateApi = {
main,
routes
}
module.exports = provisionTemplateApi

View File

@ -12,25 +12,19 @@
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
const { main: sunstone, routes: sunstoneRoutes } = require('./sunstone')
const { addFunctionToRoute, fillFunctionRoute } = require('server/utils/server')
const { routes: sunstoneRoutes } = require('./sunstone')
const { SUNSTONE } = require('./string-routes')
const privateRoutes = []
const publicRoutes = []
const fillRoute = (method, endpoint, action) => ({
httpMethod: method,
endpoint,
action
})
const fillPrivateRoutes = (methods = {}, path = '', action = () => undefined) => {
if (Object.keys(methods).length > 0 && methods.constructor === Object) {
Object.keys(methods).forEach((method) => {
privateRoutes.push(
fillRoute(method, path,
fillFunctionRoute(method, path,
(req, res, next, connection, userId, user) => {
action(req, res, next, methods[method], user, connection)
})
@ -40,7 +34,7 @@ const fillPrivateRoutes = (methods = {}, path = '', action = () => undefined) =>
}
const generatePrivateRoutes = () => {
fillPrivateRoutes(sunstoneRoutes, SUNSTONE, sunstone)
fillPrivateRoutes(sunstoneRoutes, SUNSTONE, addFunctionToRoute)
return privateRoutes
}

View File

@ -47,39 +47,39 @@ const getViews = (res = {}, next = () => undefined, params = {}, userData = {},
const { user, password } = userData
if (user && password && global && global.SUNSTONE_VIEWS && global.SUNSTONE_PATH) {
const connect = oneConnection(user, password)
// get user data
connect(
ActionsUser.USER_INFO,
[-1, false],
(err = {}, userData = {}) => {
if (userData && userData.USER && userData.USER.GID) {
// get group data for user
getInfoZone(
connect,
userData.USER.GID,
(err = {}, vmgroupData = {}) => {
if (vmgroupData && vmgroupData.GROUP && vmgroupData.GROUP.NAME) {
// get info for file sunstone-views.yamls
existsFile(
global.SUNSTONE_VIEWS,
filedata => {
const jsonFileData = parse(filedata) || {}
if (jsonFileData && jsonFileData.groups && jsonFileData.default) {
// get info for views
const views = jsonFileData.groups[vmgroupData.GROUP.NAME] || jsonFileData.default
const rtn = {}
views.forEach(view => {
// aca se tiene que ir armando la respuesta del http
console.log('-->', getFiles(
`${global.SUNSTONE_PATH}${view}`
))
getFiles(
`${global.SUNSTONE_PATH}${view}`
).forEach(viewPath => {
existsFile(
viewPath,
(viewData = '') => {
// get file content of view
if (!rtn[view]) {
rtn[view] = []
}
const jsonViewData = parse(viewData) || {}
if (jsonViewData && jsonViewData.resource_name) {
rtn[view] = jsonViewData
rtn[view].push(jsonViewData)
}
}
)
@ -114,6 +114,7 @@ const getViews = (res = {}, next = () => undefined, params = {}, userData = {},
const getConfig = (res = {}, next = () => undefined, params = {}, userData = {}) => {
if (global && global.SUNSTONE_CONFIG) {
console.log('asdasd: ', global.SUNSTONE_CONFIG)
existsFile(
global.SUNSTONE_CONFIG,
filedata => {

View File

@ -12,9 +12,7 @@
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
const { defaultEmptyFunction } = require('server/utils/constants/defaults')
const { httpMethod, from: fromData } = require('server/utils/constants/defaults')
const { getParamsForObject } = require('server/utils/server')
const { httpMethod } = require('server/utils/constants/defaults')
const {
getConfig,
getViews
@ -33,31 +31,8 @@ const routes = {
}
}
}
const main = (req = {}, res = {}, next = defaultEmptyFunction, routes = {}, user = {}, oneConnection = defaultEmptyFunction, index = 0) => {
const resources = Object.keys(req[fromData.resource])
if (req && res && next && routes) {
const route = routes[`${req[fromData.resource][resources[index]]}`.toLowerCase()]
if (req && fromData && fromData.resource && req[fromData.resource] && route) {
if (Object.keys(route).length > 0 && route.constructor === Object) {
if (route.action && route.params && typeof route.action === 'function') {
const params = getParamsForObject(route.params, req)
route.action(res, next, params, user, oneConnection)
} else {
main(req, res, next, route, user, oneConnection, index + 1)
}
} else {
next()
}
} else {
next()
}
} else {
next()
}
}
const sunstoneApi = {
main,
routes
}

View File

@ -56,7 +56,7 @@ const clientCallbacks = {
const appConfig = getConfig()
const guacd = appConfig.guacd || {}
const guacdPort = guacd.port || 4822
const guacdHost = guacd.host || '127.0.0.1'
const guacdHost = guacd.host || 'localhost'
const guacamole = appServer => {
if (

View File

@ -23,6 +23,7 @@ const { internalServerError } = require('./constants/http-codes')
const { messageTerminal } = require('server/utils/general')
const { validateAuth } = require('server/utils/jwt')
const {
from: fromData,
defaultAppName,
defaultConfigFile,
defaultLogFilename,
@ -39,12 +40,42 @@ const {
defaultHash,
defaultSunstonePath,
defaultSunstoneViews,
defaultSunstoneConfig
defaultSunstoneConfig,
defaultEmptyFunction
} = require('./constants/defaults')
let cert = ''
let key = ''
const fillFunctionRoute = (method, endpoint, action) => ({
httpMethod: method,
endpoint,
action
})
const addFunctionToRoute = (req = {}, res = {}, next = defaultEmptyFunction, routes = {}, user = {}, oneConnection = defaultEmptyFunction, index = 0) => {
const resources = Object.keys(req[fromData.resource])
if (req && res && next && routes) {
const route = routes[`${req[fromData.resource][resources[index]]}`.toLowerCase()]
if (req && fromData && fromData.resource && req[fromData.resource] && route) {
if (Object.keys(route).length > 0 && route.constructor === Object) {
if (route.action && route.params && typeof route.action === 'function') {
const params = getParamsForObject(route.params, req)
route.action(res, next, params, user, oneConnection)
} else {
addFunctionToRoute(req, res, next, route, user, oneConnection, index + 1)
}
} else {
next()
}
} else {
next()
}
} else {
next()
}
}
const validateServerIsSecure = () => {
const folder = '../cert/'
const pathfile = env && env.NODE_ENV === defaultWebpackMode ? `${__dirname}/../../${folder}` : `${__dirname}/${folder}`
@ -172,8 +203,8 @@ const existsFile = (path = '', success = () => undefined, error = () => undefine
let errorData
try {
const fileData = readFileSync(path, 'utf8')
if (path && fileData) {
file = fileData
if (path) {
file = fileData || ''
rtn = true
}
} catch (err) {
@ -409,6 +440,8 @@ const parsePostData = (postData = {}) => {
return rtn
}
module.exports = {
fillFunctionRoute,
addFunctionToRoute,
encrypt,
decrypt,
getDataZone,