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

M #~: fix upcast in array commands (#2163)

This commit is contained in:
Jorge Miguel Lobo Escalona 2022-06-21 11:38:06 +02:00 committed by GitHub
parent 5ff7b9a7fc
commit 0f27d0bb1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 43 deletions

View File

@ -59,35 +59,36 @@ const executeWorker = ({
next,
res,
}) => {
if (user && password && rpc && command && paramsCommand) {
const worker = useWorker()
worker.onmessage = function (result) {
worker.terminate()
const err = result && result.data && result.data.err
const value = result && result.data && result.data.value
writeInLogger([command, paramsCommand, JSON.stringify(value)], {
format: 'worker: %s, [%s]: %s',
level: 2,
})
if (!err) {
fillResourceforHookConnection(user, command, paramsCommand)
res.locals.httpCode = parseReturnWorker(value)
}
next()
}
worker.postMessage({
type: 'xmlrpc',
config: {
globalState: (global && global.paths) || {},
user,
password,
rpc,
command,
paramsCommand,
},
})
if (!(user && password && rpc && command && paramsCommand)) {
return
}
const worker = useWorker()
worker.onmessage = function (result) {
worker.terminate()
const err = result && result.data && result.data.err
const value = result && result.data && result.data.value
writeInLogger([command, paramsCommand, JSON.stringify(value)], {
format: 'worker: %s, [%s]: %s',
level: 2,
})
if (!err) {
fillResourceforHookConnection(user, command, paramsCommand)
res.locals.httpCode = parseReturnWorker(value)
}
next()
}
worker.postMessage({
type: 'xmlrpc',
config: {
globalState: (global && global.paths) || {},
user,
password,
rpc,
command,
paramsCommand,
},
})
}
/**
@ -100,22 +101,33 @@ const executeWorker = ({
*/
const getCommandParams = (config) => {
const { params, serverDataSource } = config
if (params && serverDataSource) {
return Object.entries(params).map(([key, value]) => {
if (key && value && value.from && typeof value.default !== 'undefined') {
// `value == null` checks against undefined and null
return serverDataSource[value.from] &&
serverDataSource[value.from][key] != null
? upcast.to(
serverDataSource[value.from][key],
upcast.type(value.default)
)
: value.default
}
return ''
})
if (!(params && serverDataSource)) {
return
}
return Object.entries(params).map(([key, value]) => {
if (!(key && value && value.from && typeof value.default !== 'undefined')) {
return ''
}
// `value == null` checks against undefined and null
if (
serverDataSource[value.from] &&
serverDataSource[value.from][key] != null
) {
const upcastedData = upcast.to(
serverDataSource[value.from][key],
upcast.type(value.default)
)
return value.arrayDefault !== undefined && Array.isArray(upcastedData)
? upcastedData.map((internalValue) =>
upcast.to(internalValue, upcast.type(value.arrayDefault))
)
: upcastedData
} else {
return value.default
}
})
}
/**

View File

@ -74,6 +74,7 @@ module.exports = {
group: {
from: postBody,
default: [],
arrayDefault: 0, // this is for the upcast of the internal values of the array
},
},
},