1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const PoliciesStore = {
5  namespaced: true,
6  state: {
7    sshProtocolEnabled: false,
8    ipmiProtocolEnabled: false,
9    rtadEnabled: 'Disabled',
10    vtpmEnabled: 'Disabled',
11  },
12  getters: {
13    sshProtocolEnabled: (state) => state.sshProtocolEnabled,
14    ipmiProtocolEnabled: (state) => state.ipmiProtocolEnabled,
15    rtadEnabled: (state) => state.rtadEnabled,
16    vtpmEnabled: (state) => state.vtpmEnabled,
17  },
18  mutations: {
19    setSshProtocolEnabled: (state, sshProtocolEnabled) =>
20      (state.sshProtocolEnabled = sshProtocolEnabled),
21    setIpmiProtocolEnabled: (state, ipmiProtocolEnabled) =>
22      (state.ipmiProtocolEnabled = ipmiProtocolEnabled),
23    setRtadEnabled: (state, rtadEnabled) => (state.rtadEnabled = rtadEnabled),
24    setVtpmEnabled: (state, vtpmEnabled) => (state.vtpmEnabled = vtpmEnabled),
25  },
26  actions: {
27    async getNetworkProtocolStatus({ commit }) {
28      return await api
29        .get('/redfish/v1/Managers/bmc/NetworkProtocol')
30        .then((response) => {
31          const sshProtocol = response.data.SSH.ProtocolEnabled;
32          const ipmiProtocol = response.data.IPMI.ProtocolEnabled;
33          commit('setSshProtocolEnabled', sshProtocol);
34          commit('setIpmiProtocolEnabled', ipmiProtocol);
35        })
36        .catch((error) => console.log(error));
37    },
38    async getBiosStatus({ commit }) {
39      return await api
40        .get('/redfish/v1/Systems/system/Bios')
41        .then((response) => {
42          commit('setRtadEnabled', response.data.Attributes.pvm_rtad);
43          commit('setVtpmEnabled', response.data.Attributes.pvm_vtpm);
44        })
45        .catch((error) => console.log(error));
46    },
47    async saveIpmiProtocolState({ commit }, protocolEnabled) {
48      commit('setIpmiProtocolEnabled', protocolEnabled);
49      const ipmi = {
50        IPMI: {
51          ProtocolEnabled: protocolEnabled,
52        },
53      };
54      return await api
55        .patch('/redfish/v1/Managers/bmc/NetworkProtocol', ipmi)
56        .then(() => {
57          if (protocolEnabled) {
58            return i18n.t('pagePolicies.toast.successIpmiEnabled');
59          } else {
60            return i18n.t('pagePolicies.toast.successIpmiDisabled');
61          }
62        })
63        .catch((error) => {
64          console.log(error);
65          commit('setIpmiProtocolEnabled', !protocolEnabled);
66          if (protocolEnabled) {
67            throw new Error(i18n.t('pagePolicies.toast.errorIpmiEnabled'));
68          } else {
69            throw new Error(i18n.t('pagePolicies.toast.errorIpmiDisabled'));
70          }
71        });
72    },
73    async saveSshProtocolState({ commit }, protocolEnabled) {
74      commit('setSshProtocolEnabled', protocolEnabled);
75      const ssh = {
76        SSH: {
77          ProtocolEnabled: protocolEnabled,
78        },
79      };
80      return await api
81        .patch('/redfish/v1/Managers/bmc/NetworkProtocol', ssh)
82        .then(() => {
83          if (protocolEnabled) {
84            return i18n.t('pagePolicies.toast.successSshEnabled');
85          } else {
86            return i18n.t('pagePolicies.toast.successSshDisabled');
87          }
88        })
89        .catch((error) => {
90          console.log(error);
91          commit('setSshProtocolEnabled', !protocolEnabled);
92          if (protocolEnabled) {
93            throw new Error(i18n.t('pagePolicies.toast.errorSshEnabled'));
94          } else {
95            throw new Error(i18n.t('pagePolicies.toast.errorSshDisabled'));
96          }
97        });
98    },
99    async saveRtadState({ commit }, updatedRtad) {
100      commit('setRtadEnabled', updatedRtad);
101      return await api
102        .patch('/redfish/v1/Systems/system/Bios/Settings', {
103          Attributes: {
104            pvm_rtad: updatedRtad,
105          },
106        })
107        .then(() => {
108          if (updatedRtad === 'Enabled') {
109            return i18n.t('pagePolicies.toast.successRtadEnabled');
110          } else {
111            return i18n.t('pagePolicies.toast.successRtadDisabled');
112          }
113        })
114        .catch((error) => {
115          console.log(error);
116          if (updatedRtad === 'Enabled') {
117            throw new Error(i18n.t('pagePolicies.toast.errorRtadEnabled'));
118          } else {
119            throw new Error(i18n.t('pagePolicies.toast.errorRtadDisabled'));
120          }
121        });
122    },
123    async saveVtpmState({ commit }, updatedVtpm) {
124      commit('setVtpmEnabled', updatedVtpm);
125      return await api
126        .patch('/redfish/v1/Systems/system/Bios/Settings', {
127          Attributes: {
128            pvm_vtpm: updatedVtpm,
129          },
130        })
131        .then(() => {
132          if (updatedVtpm === 'Enabled') {
133            return i18n.t('pagePolicies.toast.successVtpmEnabled');
134          } else {
135            return i18n.t('pagePolicies.toast.successVtpmDisabled');
136          }
137        })
138        .catch((error) => {
139          console.log(error);
140          if (updatedVtpm === 'Enabled') {
141            throw new Error(i18n.t('pagePolicies.toast.errorVtpmEnabled'));
142          } else {
143            throw new Error(i18n.t('pagePolicies.toast.errorVtpmDisabled'));
144          }
145        });
146    },
147  },
148};
149
150export default PoliciesStore;
151