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.t('pagePolicies.toast.successIpmiEnabled');
76          } else {
77            return i18n.t('pagePolicies.toast.successIpmiDisabled');
78          }
79        })
80        .catch((error) => {
81          console.log(error);
82          commit('setIpmiProtocolEnabled', !protocolEnabled);
83          if (protocolEnabled) {
84            throw new Error(i18n.t('pagePolicies.toast.errorIpmiEnabled'));
85          } else {
86            throw new Error(i18n.t('pagePolicies.toast.errorIpmiDisabled'));
87          }
88        });
89    },
90    async saveSshProtocolState({ commit }, protocolEnabled) {
91      commit('setSshProtocolEnabled', protocolEnabled);
92      const ssh = {
93        SSH: {
94          ProtocolEnabled: protocolEnabled,
95        },
96      };
97      return await api
98        .patch(
99          `${await this.dispatch('global/getBmcPath')}/NetworkProtocol`,
100          ssh,
101        )
102        .then(() => {
103          if (protocolEnabled) {
104            return i18n.t('pagePolicies.toast.successSshEnabled');
105          } else {
106            return i18n.t('pagePolicies.toast.successSshDisabled');
107          }
108        })
109        .catch((error) => {
110          console.log(error);
111          commit('setSshProtocolEnabled', !protocolEnabled);
112          if (protocolEnabled) {
113            throw new Error(i18n.t('pagePolicies.toast.errorSshEnabled'));
114          } else {
115            throw new Error(i18n.t('pagePolicies.toast.errorSshDisabled'));
116          }
117        });
118    },
119    async saveRtadState({ commit }, updatedRtad) {
120      commit('setRtadEnabled', updatedRtad);
121      return await api
122        .patch(`${await this.dispatch('global/getSystemPath')}/Bios/Settings`, {
123          Attributes: {
124            pvm_rtad: updatedRtad,
125          },
126        })
127        .then(() => {
128          if (updatedRtad === 'Enabled') {
129            return i18n.t('pagePolicies.toast.successRtadEnabled');
130          } else {
131            return i18n.t('pagePolicies.toast.successRtadDisabled');
132          }
133        })
134        .catch((error) => {
135          console.log(error);
136          if (updatedRtad === 'Enabled') {
137            throw new Error(i18n.t('pagePolicies.toast.errorRtadEnabled'));
138          } else {
139            throw new Error(i18n.t('pagePolicies.toast.errorRtadDisabled'));
140          }
141        });
142    },
143    async saveVtpmState({ commit }, updatedVtpm) {
144      commit('setVtpmEnabled', updatedVtpm);
145      return await api
146        .patch(`${await this.dispatch('global/getSystemPath')}/Bios/Settings`, {
147          Attributes: {
148            pvm_vtpm: updatedVtpm,
149          },
150        })
151        .then(() => {
152          if (updatedVtpm === 'Enabled') {
153            return i18n.t('pagePolicies.toast.successVtpmEnabled');
154          } else {
155            return i18n.t('pagePolicies.toast.successVtpmDisabled');
156          }
157        })
158        .catch((error) => {
159          console.log(error);
160          if (updatedVtpm === 'Enabled') {
161            throw new Error(i18n.t('pagePolicies.toast.errorVtpmEnabled'));
162          } else {
163            throw new Error(i18n.t('pagePolicies.toast.errorVtpmDisabled'));
164          }
165        });
166    },
167    async saveSessionTimeoutValue({ dispatch }, sessionTimeoutNewValue) {
168      const sessionValue = {
169        SessionTimeout: sessionTimeoutNewValue,
170      };
171      return await api
172        .patch('/redfish/v1/SessionService', sessionValue)
173        .then(() => dispatch('getSessionTimeout'))
174        .then(() => {
175          return i18n.t('pagePolicies.toast.successSessionTimeout');
176        })
177        .catch((error) => {
178          console.log(error);
179          throw new Error(i18n.t('pagePolicies.toast.errorSessionTimeout'));
180        });
181    },
182  },
183};
184
185export default PoliciesStore;
186