blob: 6f9ced4353f9d8fc6f44c95b47c3dd5e50b857b7 [file] [log] [blame]
Yoshie Muranakac11d3892020-02-19 08:07:40 -08001import api from '../../api';
2import i18n from '../../../i18n';
3
Yoshie Muranakafa1512b2020-02-25 15:54:07 -08004/**
5 * Watch for hostStatus changes in GlobalStore module
6 * to set isOperationInProgress state
7 * Stop watching status changes and resolve Promise when
8 * hostStatus value matches passed argument or after 5 minutes
9 * @param {string} hostStatus
10 * @returns {Promise}
11 */
12const checkForHostStatus = function(hostStatus) {
13 return new Promise(resolve => {
14 const timer = setTimeout(() => {
15 resolve();
16 unwatch();
17 }, 300000 /*5mins*/);
18 const unwatch = this.watch(
19 state => state.global.hostStatus,
20 value => {
21 if (value === hostStatus) {
22 resolve();
23 unwatch();
24 clearTimeout(timer);
25 }
26 }
27 );
28 });
29};
30
Yoshie Muranakac11d3892020-02-19 08:07:40 -080031const ControlStore = {
32 namespaced: true,
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080033 state: {
34 isOperationInProgress: false
35 },
36 getters: {
37 isOperationInProgress: state => state.isOperationInProgress
38 },
39 mutations: {
40 setOperationInProgress: (state, inProgress) =>
41 (state.isOperationInProgress = inProgress)
42 },
Yoshie Muranakac11d3892020-02-19 08:07:40 -080043 actions: {
44 async rebootBmc() {
45 const data = { ResetType: 'GracefulRestart' };
46 return await api
47 .post('/redfish/v1/Managers/bmc/Actions/Manager.Reset', data)
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -080048 .then(() => i18n.t('pageRebootBmc.toast.successRebootStart'))
Yoshie Muranakac11d3892020-02-19 08:07:40 -080049 .catch(error => {
50 console.log(error);
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -080051 throw new Error(i18n.t('pageRebootBmc.toast.errorRebootStart'));
Yoshie Muranakac11d3892020-02-19 08:07:40 -080052 });
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080053 },
54 async hostPowerOn({ dispatch, commit }) {
55 const data = { ResetType: 'On' };
56 dispatch('hostPowerChange', data);
57 await checkForHostStatus.bind(this, 'on')();
58 commit('setOperationInProgress', false);
59 },
60 async hostSoftReboot({ dispatch, commit }) {
61 const data = { ResetType: 'GracefulRestart' };
62 dispatch('hostPowerChange', data);
63 await checkForHostStatus.bind(this, 'on')();
64 commit('setOperationInProgress', false);
65 },
66 async hostHardReboot({ dispatch, commit }) {
67 // TODO: Update when ForceWarmReboot property
68 // available
69 dispatch('hostPowerChange', { ResetType: 'ForceOff' });
70 await checkForHostStatus.bind(this, 'off')();
71 dispatch('hostPowerChange', { ResetType: 'On' });
72 await checkForHostStatus.bind(this, 'on')();
73 commit('setOperationInProgress', false);
74 },
75 async hostSoftPowerOff({ dispatch, commit }) {
76 const data = { ResetType: 'GracefulShutdown' };
77 dispatch('hostPowerChange', data);
78 await checkForHostStatus.bind(this, 'off')();
79 commit('setOperationInProgress', false);
80 },
81 async hostHardPowerOff({ dispatch, commit }) {
82 const data = { ResetType: 'ForceOff' };
83 dispatch('hostPowerChange', data);
84 await checkForHostStatus.bind(this, 'off')();
85 commit('setOperationInProgress', false);
86 },
87 hostPowerChange({ commit }, data) {
88 commit('setOperationInProgress', true);
89 api
90 .post('/redfish/v1/Systems/system/Actions/ComputerSystem.Reset', data)
91 .catch(error => {
92 console.log(error);
93 commit('setOperationInProgress', false);
94 });
Yoshie Muranakac11d3892020-02-19 08:07:40 -080095 }
96 }
97};
98
99export default ControlStore;