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    sessionTimeoutValue: null,
12  },
13  getters: {
14    sshProtocolEnabled: (state) => state.sshProtocolEnabled,
15    ipmiProtocolEnabled: (state) => state.ipmiProtocolEnabled,
16    rtadEnabled: (state) => state.rtadEnabled,
17    vtpmEnabled: (state) => state.vtpmEnabled,
18    getSessionTimeoutValue: (state) => state.sessionTimeoutValue,
19  },
20  mutations: {
21    setSshProtocolEnabled: (state, sshProtocolEnabled) =>
22      (state.sshProtocolEnabled = sshProtocolEnabled),
23    setIpmiProtocolEnabled: (state, ipmiProtocolEnabled) =>
24      (state.ipmiProtocolEnabled = ipmiProtocolEnabled),
25    setRtadEnabled: (state, rtadEnabled) => (state.rtadEnabled = rtadEnabled),
26    setVtpmEnabled: (state, vtpmEnabled) => (state.vtpmEnabled = vtpmEnabled),
27    setSessionTimeoutValue(state, sessionTimeoutValue) {
28      state.sessionTimeoutValue = sessionTimeoutValue;
29    },
30  },
31  actions: {
32    async getNetworkProtocolStatus({ commit }) {
33      return await api
34        .get(`${await this.dispatch('global/getBmcPath')}/NetworkProtocol`)
35        .then((response) => {
36          const sshProtocol = response.data.SSH.ProtocolEnabled;
37          const ipmiProtocol = response.data.IPMI.ProtocolEnabled;
38          commit('setSshProtocolEnabled', sshProtocol);
39          commit('setIpmiProtocolEnabled', ipmiProtocol);
40        })
41        .catch((error) => console.log(error));
42    },
43    async getBiosStatus({ commit }) {
44      return await api
45        .get(`${await this.dispatch('global/getSystemPath')}/Bios`)
46        .then((response) => {
47          commit('setRtadEnabled', response.data.Attributes.pvm_rtad);
48          commit('setVtpmEnabled', response.data.Attributes.pvm_vtpm);
49        })
50        .catch((error) => console.log(error));
51    },
52    async getSessionTimeout({ commit }) {
53      return await api
54        .get('/redfish/v1/SessionService')
55        .then((response) => {
56          const sessionTimeoutValue = response.data.SessionTimeout;
57          commit('setSessionTimeoutValue', sessionTimeoutValue);
58        })
59        .catch((error) => console.log(error));
60    },
61    async saveIpmiProtocolState({ commit }, protocolEnabled) {
62      commit('setIpmiProtocolEnabled', protocolEnabled);
63      const ipmi = {
64        IPMI: {
65          ProtocolEnabled: protocolEnabled,
66        },
67      };
68      return await api
69        .patch(
70          `${await this.dispatch('global/getBmcPath')}/NetworkProtocol`,
71          ipmi,
72        )
73        .then(() => {
74          if (protocolEnabled) {
75            return i18n.global.t('pagePolicies.toast.successIpmiEnabled');
76          } else {
77            return i18n.global.t('pagePolicies.toast.successIpmiDisabled');
78          }
79        })
80        .catch((error) => {
81          console.log(error);
82          commit('setIpmiProtocolEnabled', !protocolEnabled);
83          if (protocolEnabled) {
84            throw new Error(
85              i18n.global.t('pagePolicies.toast.errorIpmiEnabled'),
86            );
87          } else {
88            throw new Error(
89              i18n.global.t('pagePolicies.toast.errorIpmiDisabled'),
90            );
91          }
92        });
93    },
94    async saveSshProtocolState({ commit }, protocolEnabled) {
95      commit('setSshProtocolEnabled', protocolEnabled);
96      const ssh = {
97        SSH: {
98          ProtocolEnabled: protocolEnabled,
99        },
100      };
101      return await api
102        .patch(
103          `${await this.dispatch('global/getBmcPath')}/NetworkProtocol`,
104          ssh,
105        )
106        .then(() => {
107          if (protocolEnabled) {
108            return i18n.global.t('pagePolicies.toast.successSshEnabled');
109          } else {
110            return i18n.global.t('pagePolicies.toast.successSshDisabled');
111          }
112        })
113        .catch((error) => {
114          console.log(error);
115          commit('setSshProtocolEnabled', !protocolEnabled);
116          if (protocolEnabled) {
117            throw new Error(
118              i18n.global.t('pagePolicies.toast.errorSshEnabled'),
119            );
120          } else {
121            throw new Error(
122              i18n.global.t('pagePolicies.toast.errorSshDisabled'),
123            );
124          }
125        });
126    },
127    async saveRtadState({ commit }, updatedRtad) {
128      commit('setRtadEnabled', updatedRtad);
129      return await api
130        .patch(`${await this.dispatch('global/getSystemPath')}/Bios/Settings`, {
131          Attributes: {
132            pvm_rtad: updatedRtad,
133          },
134        })
135        .then(() => {
136          if (updatedRtad === 'Enabled') {
137            return i18n.global.t('pagePolicies.toast.successRtadEnabled');
138          } else {
139            return i18n.global.t('pagePolicies.toast.successRtadDisabled');
140          }
141        })
142        .catch((error) => {
143          console.log(error);
144          if (updatedRtad === 'Enabled') {
145            throw new Error(
146              i18n.global.t('pagePolicies.toast.errorRtadEnabled'),
147            );
148          } else {
149            throw new Error(
150              i18n.global.t('pagePolicies.toast.errorRtadDisabled'),
151            );
152          }
153        });
154    },
155    async saveVtpmState({ commit }, updatedVtpm) {
156      commit('setVtpmEnabled', updatedVtpm);
157      return await api
158        .patch(`${await this.dispatch('global/getSystemPath')}/Bios/Settings`, {
159          Attributes: {
160            pvm_vtpm: updatedVtpm,
161          },
162        })
163        .then(() => {
164          if (updatedVtpm === 'Enabled') {
165            return i18n.global.t('pagePolicies.toast.successVtpmEnabled');
166          } else {
167            return i18n.global.t('pagePolicies.toast.successVtpmDisabled');
168          }
169        })
170        .catch((error) => {
171          console.log(error);
172          if (updatedVtpm === 'Enabled') {
173            throw new Error(
174              i18n.global.t('pagePolicies.toast.errorVtpmEnabled'),
175            );
176          } else {
177            throw new Error(
178              i18n.global.t('pagePolicies.toast.errorVtpmDisabled'),
179            );
180          }
181        });
182    },
183    async saveSessionTimeoutValue({ dispatch }, sessionTimeoutNewValue) {
184      const sessionValue = {
185        SessionTimeout: sessionTimeoutNewValue,
186      };
187      return await api
188        .patch('/redfish/v1/SessionService', sessionValue)
189        .then(() => dispatch('getSessionTimeout'))
190        .then(() => {
191          return i18n.global.t('pagePolicies.toast.successSessionTimeout');
192        })
193        .catch((error) => {
194          console.log(error);
195          throw new Error(
196            i18n.global.t('pagePolicies.toast.errorSessionTimeout'),
197          );
198        });
199    },
200  },
201};
202
203export default PoliciesStore;
204