1import api from '@/store/api';
2import i18n from '@/i18n';
3
4/**
5 * Watch for serverStatus changes in GlobalStore module
6 * to set isOperationInProgress state
7 * Stop watching status changes and resolve Promise when
8 * serverStatus value matches passed argument or after 5 minutes
9 * @param {string} serverStatus
10 * @returns {Promise}
11 */
12const checkForServerStatus = function (serverStatus) {
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.serverStatus,
20      (value) => {
21        if (value === serverStatus) {
22          resolve();
23          unwatch();
24          clearTimeout(timer);
25        }
26      }
27    );
28  });
29};
30
31const ControlStore = {
32  namespaced: true,
33  state: {
34    isOperationInProgress: false,
35    lastPowerOperationTime: null,
36    lastBmcRebootTime: null,
37  },
38  getters: {
39    isOperationInProgress: (state) => state.isOperationInProgress,
40    lastPowerOperationTime: (state) => state.lastPowerOperationTime,
41    lastBmcRebootTime: (state) => state.lastBmcRebootTime,
42  },
43  mutations: {
44    setOperationInProgress: (state, inProgress) =>
45      (state.isOperationInProgress = inProgress),
46    setLastPowerOperationTime: (state, lastPowerOperationTime) =>
47      (state.lastPowerOperationTime = lastPowerOperationTime),
48    setLastBmcRebootTime: (state, lastBmcRebootTime) =>
49      (state.lastBmcRebootTime = lastBmcRebootTime),
50  },
51  actions: {
52    async getLastPowerOperationTime({ commit }) {
53      return await api
54        .get('/redfish/v1/Systems/system')
55        .then((response) => {
56          const lastReset = response.data.LastResetTime;
57          if (lastReset) {
58            const lastPowerOperationTime = new Date(lastReset);
59            commit('setLastPowerOperationTime', lastPowerOperationTime);
60          }
61        })
62        .catch((error) => console.log(error));
63    },
64    getLastBmcRebootTime({ commit }) {
65      return api
66        .get('/redfish/v1/Managers/bmc')
67        .then((response) => {
68          const lastBmcReset = response.data.LastResetTime;
69          const lastBmcRebootTime = new Date(lastBmcReset);
70          commit('setLastBmcRebootTime', lastBmcRebootTime);
71        })
72        .catch((error) => console.log(error));
73    },
74    async rebootBmc({ dispatch }) {
75      const data = { ResetType: 'GracefulRestart' };
76      return await api
77        .post('/redfish/v1/Managers/bmc/Actions/Manager.Reset', data)
78        .then(() => dispatch('getLastBmcRebootTime'))
79        .then(() => i18n.t('pageRebootBmc.toast.successRebootStart'))
80        .catch((error) => {
81          console.log(error);
82          throw new Error(i18n.t('pageRebootBmc.toast.errorRebootStart'));
83        });
84    },
85    async serverPowerOn({ dispatch, commit }) {
86      const data = { ResetType: 'On' };
87      dispatch('serverPowerChange', data);
88      await checkForServerStatus.bind(this, 'on')();
89      commit('setOperationInProgress', false);
90      dispatch('getLastPowerOperationTime');
91    },
92    async serverSoftReboot({ dispatch, commit }) {
93      const data = { ResetType: 'GracefulRestart' };
94      dispatch('serverPowerChange', data);
95      await checkForServerStatus.bind(this, 'on')();
96      commit('setOperationInProgress', false);
97      dispatch('getLastPowerOperationTime');
98    },
99    async serverHardReboot({ dispatch, commit }) {
100      const data = { ResetType: 'ForceRestart' };
101      dispatch('serverPowerChange', data);
102      await checkForServerStatus.bind(this, 'on')();
103      commit('setOperationInProgress', false);
104      dispatch('getLastPowerOperationTime');
105    },
106    async serverSoftPowerOff({ dispatch, commit }) {
107      const data = { ResetType: 'GracefulShutdown' };
108      dispatch('serverPowerChange', data);
109      await checkForServerStatus.bind(this, 'off')();
110      commit('setOperationInProgress', false);
111      dispatch('getLastPowerOperationTime');
112    },
113    async serverHardPowerOff({ dispatch, commit }) {
114      const data = { ResetType: 'ForceOff' };
115      dispatch('serverPowerChange', data);
116      await checkForServerStatus.bind(this, 'off')();
117      commit('setOperationInProgress', false);
118      dispatch('getLastPowerOperationTime');
119    },
120    serverPowerChange({ commit }, data) {
121      commit('setOperationInProgress', true);
122      api
123        .post('/redfish/v1/Systems/system/Actions/ComputerSystem.Reset', data)
124        .catch((error) => {
125          console.log(error);
126          commit('setOperationInProgress', false);
127        });
128    },
129  },
130};
131
132export default ControlStore;
133