From bf06efd1a149003d4063377a701aed25945af6d0 Mon Sep 17 00:00:00 2001 From: Sergio Betanzos Date: Thu, 20 Jan 2022 17:38:39 +0100 Subject: [PATCH] F #5623: Add host boilerplate (#1711) (cherry picked from commit cd533b685c65b6371f5ceb61b523eca575e269ba) --- src/fireedge/src/client/constants/actions.js | 1 + src/fireedge/src/client/constants/host.js | 13 ++ src/fireedge/src/client/constants/vm.js | 2 +- .../src/client/constants/vmTemplate.js | 2 +- .../src/client/features/One/host/actions.js | 16 ++ .../src/client/features/One/host/hooks.js | 12 ++ .../src/client/features/One/host/services.js | 195 ++++++++++++++++++ .../features/One/vmTemplate/services.js | 2 +- .../server/utils/constants/commands/host.js | 11 +- 9 files changed, 248 insertions(+), 6 deletions(-) diff --git a/src/fireedge/src/client/constants/actions.js b/src/fireedge/src/client/constants/actions.js index 8adeb35ff8..e7d83a6e10 100644 --- a/src/fireedge/src/client/constants/actions.js +++ b/src/fireedge/src/client/constants/actions.js @@ -15,6 +15,7 @@ * ------------------------------------------------------------------------- */ // INFORMATION +export const REFRESH = 'refresh' export const RENAME = 'rename' // ATTRIBUTES diff --git a/src/fireedge/src/client/constants/host.js b/src/fireedge/src/client/constants/host.js index 23cb6161b8..f1a3b702c8 100644 --- a/src/fireedge/src/client/constants/host.js +++ b/src/fireedge/src/client/constants/host.js @@ -14,6 +14,7 @@ * limitations under the License. * * ------------------------------------------------------------------------- */ import * as STATES from 'client/constants/states' +import * as ACTIONS from 'client/constants/actions' import COLOR from 'client/constants/color' /** @@ -80,3 +81,15 @@ export const HOST_STATES = [ color: COLOR.error.dark, }, ] + +/** @enum {string} Host actions */ +export const HOST_ACTIONS = { + REFRESH: ACTIONS.REFRESH, + CREATE_DIALOG: 'create_dialog', + RENAME: ACTIONS.RENAME, + ADD_TO_CLUSTER: 'addtocluster', + ENABLE: 'enable', + DISABLE: 'disable', + OFFLINE: 'offline', + DELETE: 'delete', +} diff --git a/src/fireedge/src/client/constants/vm.js b/src/fireedge/src/client/constants/vm.js index fb890f965d..adb379f8ec 100644 --- a/src/fireedge/src/client/constants/vm.js +++ b/src/fireedge/src/client/constants/vm.js @@ -513,7 +513,7 @@ export const VM_LCM_STATES = [ /** @enum {string} Virtual machine actions */ export const VM_ACTIONS = { - REFRESH: 'refresh', + REFRESH: ACTIONS.REFRESH, CREATE_DIALOG: 'create_dialog', CREATE_APP_DIALOG: 'create_app_dialog', DEPLOY: 'deploy', diff --git a/src/fireedge/src/client/constants/vmTemplate.js b/src/fireedge/src/client/constants/vmTemplate.js index 93e4abd0ef..d6b465af42 100644 --- a/src/fireedge/src/client/constants/vmTemplate.js +++ b/src/fireedge/src/client/constants/vmTemplate.js @@ -16,7 +16,7 @@ import * as ACTIONS from 'client/constants/actions' export const VM_TEMPLATE_ACTIONS = { - REFRESH: 'refresh', + REFRESH: ACTIONS.REFRESH, CREATE_DIALOG: 'create_dialog', IMPORT_DIALOG: 'import_dialog', UPDATE_DIALOG: 'update_dialog', diff --git a/src/fireedge/src/client/features/One/host/actions.js b/src/fireedge/src/client/features/One/host/actions.js index 826d9e30f8..2d14d46d75 100644 --- a/src/fireedge/src/client/features/One/host/actions.js +++ b/src/fireedge/src/client/features/One/host/actions.js @@ -27,3 +27,19 @@ export const getHosts = createAction( hostService.getHosts, (response) => ({ [RESOURCES.host]: response }) ) + +export const allocate = createAction(`${HOST}/allocate`, hostService.allocate) +export const remove = createAction(`${HOST}/delete`, hostService.delete) +export const enable = createAction(`${HOST}/enable`, hostService.enable) +export const disable = createAction(`${HOST}/disable`, hostService.disable) +export const offline = createAction(`${HOST}/offline`, hostService.offline) +export const update = createAction(`${HOST}/update`, hostService.update) +export const rename = createAction(`${HOST}/rename`, hostService.rename) +export const monitoring = createAction( + `${HOST}/monitoring`, + hostService.monitoring +) +export const monitoringPool = createAction( + `${HOST}/monitoring-pool`, // ends with "-pool" to differentiate with resource pool + hostService.monitoringPool +) diff --git a/src/fireedge/src/client/features/One/host/hooks.js b/src/fireedge/src/client/features/One/host/hooks.js index 14b32795fa..b587a540b8 100644 --- a/src/fireedge/src/client/features/One/host/hooks.js +++ b/src/fireedge/src/client/features/One/host/hooks.js @@ -35,5 +35,17 @@ export const useHostApi = () => { return { getHost: (id) => unwrapDispatch(actions.getHost({ id })), getHosts: (options) => unwrapDispatch(actions.getHosts(options)), + allocate: (data) => unwrapDispatch(actions.allocate(data)), + remove: (id) => unwrapDispatch(actions.remove({ id })), + enable: (id) => unwrapDispatch(actions.enable({ id })), + disable: (id) => unwrapDispatch(actions.disable({ id })), + offline: (id) => unwrapDispatch(actions.offline({ id })), + update: (id, template, replace) => + unwrapDispatch(actions.update({ id, template, replace })), + rename: (id, newName) => + unwrapDispatch(actions.rename({ id, name: newName })), + getMonitoring: (id) => unwrapDispatch(actions.monitoring({ id })), + getMonitoringPool: (seconds) => + unwrapDispatch(actions.monitoringPool({ seconds })), } } diff --git a/src/fireedge/src/client/features/One/host/services.js b/src/fireedge/src/client/features/One/host/services.js index 2231585f70..f2fd0cec31 100644 --- a/src/fireedge/src/client/features/One/host/services.js +++ b/src/fireedge/src/client/features/One/host/services.js @@ -55,4 +55,199 @@ export const hostService = { return [res?.data?.HOST_POOL?.HOST ?? []].flat() }, + /** + * Allocates a new host in OpenNebula. + * + * @param {object} params - Request params + * @param {string} params.hostname - Hostname of the machine we want to add + * @param {string} params.imMad + * - The name of the information manager (im_mad_name), + * this values are taken from the oned.conf with the tag name IM_MAD (name) + * @param {string} params.vmmMad + * - The name of the virtual machine manager mad name (vmm_mad_name), + * this values are taken from the oned.conf with the tag name VM_MAD (name) + * @param {string|number} [params.cluster] - The cluster ID + * @returns {number} Host id + * @throws Fails when response isn't code 200 + */ + allocate: async (params) => { + const name = Actions.HOST_ALLOCATE + const command = { name, ...Commands[name] } + const config = requestConfig(params, command) + + const res = await RestClient.request(config) + + if (!res?.id || res?.id !== httpCodes.ok.id) throw res + + return res?.data + }, + + /** + * Deletes the given host from the pool. + * + * @param {object} params - Request params + * @param {number|string} params.id - Host id + * @returns {number} Host id + * @throws Fails when response isn't code 200 + */ + delete: async (params) => { + const name = Actions.HOST_DELETE + const command = { name, ...Commands[name] } + const config = requestConfig(params, command) + + const res = await RestClient.request(config) + + if (!res?.id || res?.id !== httpCodes.ok.id) throw res + + return res?.data + }, + + /** + * Sets the status of the host to enabled. + * + * @param {object} params - Request params + * @param {number|string} params.id - Host id + * @returns {number} Host id + * @throws Fails when response isn't code 200 + */ + enable: async (params) => { + const name = Actions.HOST_STATUS + const command = { name, ...Commands[name] } + const config = requestConfig({ ...params, status: 0 }, command) + + const res = await RestClient.request(config) + + if (!res?.id || res?.id !== httpCodes.ok.id) throw res + + return res?.data + }, + + /** + * Sets the status of the host to disabled. + * + * @param {object} params - Request params + * @param {number|string} params.id - Host id + * @returns {number} Host id + * @throws Fails when response isn't code 200 + */ + disable: async (params) => { + const name = Actions.HOST_STATUS + const command = { name, ...Commands[name] } + const config = requestConfig({ ...params, status: 1 }, command) + + const res = await RestClient.request(config) + + if (!res?.id || res?.id !== httpCodes.ok.id) throw res + + return res?.data + }, + + /** + * Sets the status of the host to offline. + * + * @param {object} params - Request params + * @param {number|string} params.id - Host id + * @returns {number} Host id + * @throws Fails when response isn't code 200 + */ + offline: async (params) => { + const name = Actions.HOST_STATUS + const command = { name, ...Commands[name] } + const config = requestConfig({ ...params, status: 2 }, command) + + const res = await RestClient.request(config) + + if (!res?.id || res?.id !== httpCodes.ok.id) throw res + + return res?.data + }, + + /** + * Replaces the host’s template contents.. + * + * @param {object} params - Request params + * @param {number|string} params.id - Host id + * @param {string} params.template - The new template contents + * @param {0|1} params.replace + * - Update type: + * ``0``: Replace the whole template. + * ``1``: Merge new template with the existing one. + * @returns {number} Host id + * @throws Fails when response isn't code 200 + */ + update: async (params) => { + const name = Actions.HOST_UPDATE + const command = { name, ...Commands[name] } + const config = requestConfig(params, command) + + const res = await RestClient.request(config) + + if (!res?.id || res?.id !== httpCodes.ok.id) throw res + + return res?.data + }, + + /** + * Renames a host. + * + * @param {object} params - Request parameters + * @param {string|number} params.id - Host id + * @param {string} params.name - New name + * @returns {number} Host id + * @throws Fails when response isn't code 200 + */ + rename: async (params) => { + const name = Actions.HOST_RENAME + const command = { name, ...Commands[name] } + const config = requestConfig(params, command) + + const res = await RestClient.request(config) + + if (!res?.id || res?.id !== httpCodes.ok.id) throw res?.data + + return res?.data + }, + + /** + * Returns the host monitoring records. + * + * @param {object} params - Request parameters + * @param {string|number} params.id - Host id + * @returns {string} The monitoring information string / The error string + * @throws Fails when response isn't code 200 + */ + monitoring: async (params) => { + const name = Actions.HOST_MONITORING + const command = { name, ...Commands[name] } + const config = requestConfig(params, command) + + const res = await RestClient.request(config) + + if (!res?.id || res?.id !== httpCodes.ok.id) throw res?.data + + return res?.data + }, + + /** + * Returns all the host monitoring records. + * + * @param {object} params - Request parameters + * @param {string|number} [params.seconds] + * - Retrieve monitor records in the last num seconds. + * ``0``: Only the last record. + * ``-1``: All records. + * @returns {string} The monitoring information string / The error string + * @throws Fails when response isn't code 200 + */ + monitoringPool: async (params) => { + const name = Actions.HOST_POOL_MONITORING + const command = { name, ...Commands[name] } + const config = requestConfig(params, command) + + const res = await RestClient.request(config) + + if (!res?.id || res?.id !== httpCodes.ok.id) throw res?.data + + return res?.data + }, } diff --git a/src/fireedge/src/client/features/One/vmTemplate/services.js b/src/fireedge/src/client/features/One/vmTemplate/services.js index 3c4cf6821c..1b03c6977c 100644 --- a/src/fireedge/src/client/features/One/vmTemplate/services.js +++ b/src/fireedge/src/client/features/One/vmTemplate/services.js @@ -134,7 +134,7 @@ export const vmTemplateService = { * * @param {object} params - Request params * @param {number|string} params.id - Template id - * @param {boolean} params.template - The new template contents + * @param {string} params.template - The new template contents * @param {0|1} params.replace * - Update type: * ``0``: Replace the whole template. diff --git a/src/fireedge/src/server/utils/constants/commands/host.js b/src/fireedge/src/server/utils/constants/commands/host.js index 1f195c4ce8..fbfb693587 100644 --- a/src/fireedge/src/server/utils/constants/commands/host.js +++ b/src/fireedge/src/server/utils/constants/commands/host.js @@ -52,11 +52,11 @@ module.exports = { from: postBody, default: '', }, - information: { + imMad: { from: postBody, default: '', }, - manager: { + vmmMad: { from: postBody, default: '', }, @@ -154,7 +154,12 @@ module.exports = { [HOST_POOL_MONITORING]: { // inspected httpMethod: GET, - params: {}, + params: { + seconds: { + from: query, + default: -1, + }, + }, }, }, }