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

F #3951: fix provider API (#443)

Signed-off-by: Jorge Lobo <jlobo@opennebula.io>

Co-authored-by: Jorge Lobo <jlobo@opennebula.systems>
This commit is contained in:
Jorge Miguel Lobo Escalona 2020-11-18 18:35:14 +01:00 committed by GitHub
parent 3aee222422
commit 9983b79a5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 49 deletions

View File

@ -233,15 +233,14 @@ const executeCommandAsync = (
const executeCommand = (command = '', resource = '') => {
const rsc = Array.isArray(resource) ? resource : [resource]
const rtn = { success: false, data: null }
let rtn = { success: false, data: null }
const execute = spawnSync(command, [...rsc])
if (execute) {
if (execute.stdout) {
rtn.success = true
rtn.data = execute.stdout.toString()
rtn = { success: true, data: execute.stdout.toString() }
}
if (execute.stderr && execute.stderr.length > 0) {
rtn.data = execute.stderr.toString()
rtn = { success: false, data: execute.stderr.toString() }
messageTerminal(defaultError(execute.stderr.toString(), 'Error command: %s'))
}
}

View File

@ -23,7 +23,7 @@ const {
} = require('server/utils/constants/http-codes')
const { httpResponse, existsFile, parsePostData } = require('server/utils/server')
const { executeCommand, getFiles, createTemporalFile, createYMLContent, removeFile } = require('./functions')
const { provider } = require('./schemas')
const { provider, providerUpdate } = require('./schemas')
const httpInternalError = httpResponse(internalServerError, '', '')
@ -108,9 +108,15 @@ const createProviders = (res = {}, next = () => undefined, params = {}, userData
const paramsCommand = ['create', file.path, ...authCommand]
const executedCommand = executeCommand(command, paramsCommand)
res.locals.httpCode = httpResponse(internalServerError)
if (executedCommand && executedCommand.success && executedCommand.data) {
res.locals.httpCode = httpResponse(ok, executedCommand.data)
removeFile(file.path)
if (executedCommand && executedCommand.data) {
if (executedCommand.success) {
const data = executedCommand.data
const dataInternal = data && Array.isArray(data.match('\\d+')) ? data.match('\\d+').join() : data
res.locals.httpCode = httpResponse(ok, dataInternal)
removeFile(file.path)
} else {
res.locals.httpCode = httpResponse(internalServerError, '', executedCommand.data)
}
}
next()
return
@ -130,29 +136,26 @@ const createProviders = (res = {}, next = () => undefined, params = {}, userData
next()
}
const updateProviders = (res = {}, next = () => undefined, params = {}, userData = {}) => { //
const updateProviders = (res = {}, next = () => undefined, params = {}, userData = {}) => {
const { user, password } = userData
let rtn = httpInternalError
if (params && params.resource && params.id && user && password) {
const authCommand = ['--user', user, '--password', password]
const schemaValidator = new Validator()
const resource = parsePostData(params.resource)
const valSchema = schemaValidator.validate(resource, provider)
const valSchema = schemaValidator.validate(resource, providerUpdate)
if (valSchema.valid) {
const content = createYMLContent(resource)
if (content) {
const file = createTemporalFile(tmpPath, 'yaml', content)
if (file && file.name && file.path) {
const paramsCommand = ['update', params.id, file.path, ...authCommand]
const executedCommand = executeCommand(command, paramsCommand)
res.locals.httpCode = httpResponse(internalServerError)
if (executedCommand && executedCommand.success && executedCommand.data) {
res.locals.httpCode = httpResponse(ok, executedCommand.data)
removeFile(file.path)
}
next()
return
const file = createTemporalFile(tmpPath, 'json', JSON.stringify(resource))
if (file && file.name && file.path) {
const paramsCommand = ['update', params.id, file.path, ...authCommand]
const executedCommand = executeCommand(command, paramsCommand)
res.locals.httpCode = httpResponse(internalServerError)
if (executedCommand && executedCommand.success) {
res.locals.httpCode = httpResponse(ok)
removeFile(file.path)
}
next()
return
}
} else {
const errors = []
@ -175,13 +178,17 @@ const deleteProvider = (res = {}, next = () => undefined, params = {}, userData
const authCommand = ['--user', user, '--password', password]
const paramsCommand = ['delete', `${params.id}`.toLowerCase(), ...authCommand]
const executedCommand = executeCommand(command, paramsCommand)
const data = executedCommand.data || ''
try {
const response = executedCommand.success ? ok : internalServerError
res.locals.httpCode = httpResponse(response, JSON.parse(executedCommand.data))
if (executedCommand) {
res.locals.httpCode = httpResponse(ok)
} else {
res.locals.httpCode = httpResponse(internalServerError, data)
}
next()
return
} catch (error) {
rtn = httpResponse(internalServerError, '', executedCommand.data)
rtn = httpResponse(internalServerError, '', data)
}
}
res.locals.httpCode = rtn

View File

@ -109,21 +109,33 @@ const deleteResource = (res = {}, next = () => undefined, params = {}, userData
next()
}
const deleteProvision = (res = {}, next = () => undefined, params = {}, userData = {}) => { // falta
const deleteProvision = (res = {}, next = () => undefined, params = {}, userData = {}) => {
const { user, password } = userData
let rtn = httpInternalError
const rtn = httpInternalError
if (params && params.id && user && password) {
const authCommand = ['--user', user, '--password', password]
const paramsCommand = ['delete', `${params.id}`.toLowerCase(), ...authCommand]
const executedCommand = executeCommand(command, paramsCommand)
try {
const response = executedCommand.success ? ok : internalServerError
res.locals.httpCode = httpResponse(response, JSON.parse(executedCommand.data))
next()
return
} catch (error) {
rtn = httpResponse(internalServerError, '', executedCommand.data)
const paramsCommand = ['delete', params.id, '--batch', '--debug', ...authCommand]
let lastLine = ''
const emit = message => {
lastLine = message.toString()
publish(command, { id: params.id, message: lastLine })
}
executeCommandAsync(
command,
paramsCommand,
{
err: emit,
out: emit,
close: success => {
if (success) {
console.log('borrado con exito')
}
}
}
)
res.locals.httpCode = httpResponse(accepted, params.id)
next()
return
}
res.locals.httpCode = rtn
next()
@ -301,7 +313,17 @@ const validate = (res = {}, next = () => undefined, params = {}, userData = {})
next()
}
const getLogProvisions = (res = {}, next = () => undefined, params = {}) => {
const rtn = httpInternalError
if (params && params.id) {
console.log('PASO')
}
res.locals.httpCode = rtn
next()
}
const provisionFunctionsApi = {
getLogProvisions,
getList,
getListProvisions,
deleteResource,

View File

@ -17,6 +17,7 @@ const { getParamsForObject } = require('server/utils/server')
const {
getList,
getListProvisions,
getLogProvisions,
deleteResource,
deleteProvision,
hostCommand,
@ -77,6 +78,12 @@ const routes = {
params: {
resource: { from: fromData.resource, name: 'method' }
}
},
log: {
action: getLogProvisions,
params: {
id: { from: fromData.resource, name: 'id', front: true }
}
}
},
[POST]: {
@ -126,8 +133,8 @@ const routes = {
}
},
[DELETE]: {
delete: { // este puede tardar Websocket //FALTA
action: deleteProvision, // el comando tiene que ser asincrono
delete: {
action: deleteProvision,
params: {
id: { from: fromData.resource, name: 'id', front: true }
},

View File

@ -12,6 +12,7 @@
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
const providers = ['aws', 'packet']
const provider = {
id: '/Provider',
@ -24,6 +25,29 @@ const provider = {
connection: {
type: 'object',
required: true
},
provider: {
type: 'string',
enum: providers
}
}
}
const providerUpdate = {
id: '/Provider',
type: 'object',
properties: {
provider: {
type: 'string',
enum: providers
},
connection: {
type: 'object',
required: true
},
registration_time: {
type: 'integer',
required: true
}
}
}
@ -47,20 +71,16 @@ const provision = {
type: 'string'
},
packet_token: {
type: 'string',
required: true
type: 'string'
},
packet_project: {
type: 'string',
required: true
type: 'string'
},
facility: {
type: 'string',
required: true
type: 'string'
},
plan: {
type: 'string',
required: true
type: 'string'
},
os: {
type: 'string'
@ -109,7 +129,7 @@ const provision = {
}
}
},
clusters:{
clusters: {
type: 'array',
required: true
},
@ -181,7 +201,8 @@ const provision = {
}
const schemas = {
provider,
provision
provision,
providerUpdate
}
module.exports = schemas