1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const PoliciesStore = {
5  namespaced: true,
6  state: {
7    sshProtocolEnabled: false,
8    ipmiProtocolEnabled: false,
9  },
10  getters: {
11    sshProtocolEnabled: (state) => state.sshProtocolEnabled,
12    ipmiProtocolEnabled: (state) => state.ipmiProtocolEnabled,
13  },
14  mutations: {
15    setSshProtocolEnabled: (state, sshProtocolEnabled) =>
16      (state.sshProtocolEnabled = sshProtocolEnabled),
17    setIpmiProtocolEnabled: (state, ipmiProtocolEnabled) =>
18      (state.ipmiProtocolEnabled = ipmiProtocolEnabled),
19  },
20  actions: {
21    async getNetworkProtocolStatus({ commit }) {
22      return await api
23        .get('/redfish/v1/Managers/bmc/NetworkProtocol')
24        .then((response) => {
25          const sshProtocol = response.data.SSH.ProtocolEnabled;
26          const ipmiProtocol = response.data.IPMI.ProtocolEnabled;
27          commit('setSshProtocolEnabled', sshProtocol);
28          commit('setIpmiProtocolEnabled', ipmiProtocol);
29        })
30        .catch((error) => console.log(error));
31    },
32    async saveIpmiProtocolState({ commit }, protocolEnabled) {
33      commit('setIpmiProtocolEnabled', protocolEnabled);
34      const ipmi = {
35        IPMI: {
36          ProtocolEnabled: protocolEnabled,
37        },
38      };
39      return await api
40        .patch('/redfish/v1/Managers/bmc/NetworkProtocol', ipmi)
41        .then(() => {
42          if (protocolEnabled) {
43            return i18n.t('pagePolicies.toast.successIpmiEnabled');
44          } else {
45            return i18n.t('pagePolicies.toast.successIpmiDisabled');
46          }
47        })
48        .catch((error) => {
49          console.log(error);
50          commit('setIpmiProtocolEnabled', !protocolEnabled);
51          if (protocolEnabled) {
52            throw new Error(i18n.t('pagePolicies.toast.errorIpmiEnabled'));
53          } else {
54            throw new Error(i18n.t('pagePolicies.toast.errorIpmiDisabled'));
55          }
56        });
57    },
58    async saveSshProtocolState({ commit }, protocolEnabled) {
59      commit('setSshProtocolEnabled', protocolEnabled);
60      const ssh = {
61        SSH: {
62          ProtocolEnabled: protocolEnabled,
63        },
64      };
65      return await api
66        .patch('/redfish/v1/Managers/bmc/NetworkProtocol', ssh)
67        .then(() => {
68          if (protocolEnabled) {
69            return i18n.t('pagePolicies.toast.successSshEnabled');
70          } else {
71            return i18n.t('pagePolicies.toast.successSshDisabled');
72          }
73        })
74        .catch((error) => {
75          console.log(error);
76          commit('setSshProtocolEnabled', !protocolEnabled);
77          if (protocolEnabled) {
78            throw new Error(i18n.t('pagePolicies.toast.errorSshEnabled'));
79          } else {
80            throw new Error(i18n.t('pagePolicies.toast.errorSshDisabled'));
81          }
82        });
83    },
84  },
85};
86
87export default PoliciesStore;
88