blob: f88f2aa70cf59139859c9f4cc93c5a5ab5a0a176 [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 }) {
Gunnar Mills54941122020-07-27 13:22:05 -050067 const data = { ResetType: 'ForceRestart' };
68 dispatch('hostPowerChange', data);
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080069 await checkForHostStatus.bind(this, 'on')();
70 commit('setOperationInProgress', false);
71 },
72 async hostSoftPowerOff({ dispatch, commit }) {
73 const data = { ResetType: 'GracefulShutdown' };
74 dispatch('hostPowerChange', data);
75 await checkForHostStatus.bind(this, 'off')();
76 commit('setOperationInProgress', false);
77 },
78 async hostHardPowerOff({ dispatch, commit }) {
79 const data = { ResetType: 'ForceOff' };
80 dispatch('hostPowerChange', data);
81 await checkForHostStatus.bind(this, 'off')();
82 commit('setOperationInProgress', false);
83 },
84 hostPowerChange({ commit }, data) {
85 commit('setOperationInProgress', true);
86 api
87 .post('/redfish/v1/Systems/system/Actions/ComputerSystem.Reset', data)
88 .catch(error => {
89 console.log(error);
90 commit('setOperationInProgress', false);
91 });
Yoshie Muranakac11d3892020-02-19 08:07:40 -080092 }
93 }
94};
95
96export default ControlStore;