diff --git a/src/fireedge/src/server/routes/api/oneprovision/index.js b/src/fireedge/src/server/routes/api/oneprovision/index.js index 95c11231a2..dd531ff0dc 100644 --- a/src/fireedge/src/server/routes/api/oneprovision/index.js +++ b/src/fireedge/src/server/routes/api/oneprovision/index.js @@ -26,6 +26,8 @@ const { deleteProvision, hostCommand, hostCommandSSH, + hostAdd, + ipAdd, createProvision, configureProvision, configureHost, @@ -86,6 +88,8 @@ const { PROVISION_DELETE_PROVISION, PROVISION_UPDATE_CONFIGURE, PROVISION_UPDATE_HOST, + PROVISION_ADD_HOST, + PROVISION_ADD_IP, } = ActionsProvision const { @@ -215,6 +219,14 @@ module.exports = [ ...CommandsProvision[PROVISION_UPDATE_HOST], action: configureHost, }, + { + ...CommandsProvision[PROVISION_ADD_HOST], + action: hostAdd, + }, + { + ...CommandsProvision[PROVISION_ADD_IP], + action: ipAdd, + }, // Template { diff --git a/src/fireedge/src/server/routes/api/oneprovision/provision/functions.js b/src/fireedge/src/server/routes/api/oneprovision/provision/functions.js index eb15d8b186..9f54e939ba 100644 --- a/src/fireedge/src/server/routes/api/oneprovision/provision/functions.js +++ b/src/fireedge/src/server/routes/api/oneprovision/provision/functions.js @@ -612,13 +612,14 @@ const deleteProvision = ( const relFileYML = `${relFile}.${ext}` const relFileLOCK = `${relFile}.lock` const { user, password } = userData - const { id, cleanup } = params + const { id, cleanup, force } = params const rtn = httpInternalError if (Number.isInteger(parseInt(id, 10)) && user && password) { const command = 'delete' const endpoint = getEndpoint() const authCommand = ['--user', user, '--password', password] const cleanUpTag = cleanup ? ['--cleanup'] : [] + const forceTag = force ? ['--force'] : [] const paramsCommand = [ command, id, @@ -626,6 +627,7 @@ const deleteProvision = ( '--debug', '--json', ...cleanUpTag, + ...forceTag, ...authCommand, ...endpoint, ] @@ -1321,6 +1323,116 @@ const getLogProvisions = ( next() } +/** + * Execute Command sync and return http response. + * + * @param {any[]} params - params for command. + * @returns {object} httpResponse + */ +const addResourceSync = (params) => { + if (params && Array.isArray(params)) { + const executedCommand = executeCommand( + defaultCommandProvision, + params, + getSpecificConfig('oneprovision_prepend_command') + ) + try { + const response = executedCommand.success ? ok : internalServerError + + return httpResponse( + response, + executedCommand.data ? JSON.parse(executedCommand.data) : params.id + ) + } catch (error) { + return httpResponse(internalServerError, '', executedCommand.data) + } + } +} + +/** + * Add Host to provision. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {string} params.resource - resource + * @param {object} userData - user of http request + */ +const hostAdd = ( + res = {}, + next = defaultEmptyFunction, + params = {}, + userData = {} +) => { + let rtn = httpInternalError + const { id, amount } = params + const { user, password } = userData + if ( + Number.isInteger(parseInt(id, 10)) && + Number.isInteger(parseInt(amount, 10)) && + user && + password + ) { + const authCommand = ['--user', user, '--password', password] + const endpoint = getEndpoint() + + rtn = + addResourceSync([ + 'host', + 'add', + id, + 'amount', + amount, + ...authCommand, + ...endpoint, + ]) || httpInternalError + } + res.locals.httpCode = rtn + next() +} + +/** + * Add Ips to provision. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {string} params.resource - resource + * @param {object} userData - user of http request + */ +const ipAdd = ( + res = {}, + next = defaultEmptyFunction, + params = {}, + userData = {} +) => { + let rtn = httpInternalError + const { id, amount } = params + const { user, password } = userData + if ( + Number.isInteger(parseInt(id, 10)) && + Number.isInteger(parseInt(amount, 10)) && + user && + password + ) { + const authCommand = ['--user', user, '--password', password] + const endpoint = getEndpoint() + + rtn = + addResourceSync([ + 'ip', + 'add', + id, + 'amount', + amount, + ...authCommand, + ...endpoint, + ]) || httpInternalError + } + res.locals.httpCode = rtn + next() +} + const provisionFunctionsApi = { getProvisionDefaults, getLogProvisions, @@ -1334,5 +1446,7 @@ const provisionFunctionsApi = { configureProvision, configureHost, validate, + hostAdd, + ipAdd, } module.exports = provisionFunctionsApi diff --git a/src/fireedge/src/server/routes/api/oneprovision/provision/routes.js b/src/fireedge/src/server/routes/api/oneprovision/provision/routes.js index ea22672f9c..36bf3c708e 100644 --- a/src/fireedge/src/server/routes/api/oneprovision/provision/routes.js +++ b/src/fireedge/src/server/routes/api/oneprovision/provision/routes.js @@ -51,6 +51,8 @@ const PROVISION_DELETE_CLUSTER_RESOURCE = 'provision.deleteclusterresource' const PROVISION_DELETE_PROVISION = 'provision.deleteprovision' const PROVISION_UPDATE_CONFIGURE = 'provision.updateconfigure' const PROVISION_UPDATE_HOST = 'provision.updatehost' +const PROVISION_ADD_HOST = 'provison.addhost' +const PROVISION_ADD_IP = 'provision.addip' const Actions = { PROVISION_CLUSTER_RESOURCE, @@ -80,6 +82,8 @@ const Actions = { PROVISION_DELETE_PROVISION, PROVISION_UPDATE_CONFIGURE, PROVISION_UPDATE_HOST, + PROVISION_ADD_HOST, + PROVISION_ADD_IP, } module.exports = { @@ -371,6 +375,9 @@ module.exports = { cleanup: { from: postBody, }, + force: { + from: postBody, + }, }, }, [PROVISION_UPDATE_CONFIGURE]: { @@ -393,5 +400,31 @@ module.exports = { }, }, }, + [PROVISION_ADD_HOST]: { + path: `${basepath}/host/:id`, + httpMethod: PUT, + auth: true, + params: { + id: { + from: resource, + }, + amount: { + from: postBody, + }, + }, + }, + [PROVISION_ADD_IP]: { + path: `${basepath}/ip/:id`, + httpMethod: PUT, + auth: true, + params: { + id: { + from: resource, + }, + amount: { + from: postBody, + }, + }, + }, }, }