1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const BootSettingsStore = {
5  namespaced: true,
6  state: {
7    bootSourceOptions: [],
8    bootSource: null,
9    overrideEnabled: null,
10    tpmEnabled: null,
11  },
12  getters: {
13    bootSourceOptions: (state) => state.bootSourceOptions,
14    bootSource: (state) => state.bootSource,
15    overrideEnabled: (state) => state.overrideEnabled,
16    tpmEnabled: (state) => state.tpmEnabled,
17  },
18  mutations: {
19    setBootSourceOptions: (state, bootSourceOptions) =>
20      (state.bootSourceOptions = bootSourceOptions),
21    setBootSource: (state, bootSource) => (state.bootSource = bootSource),
22    setOverrideEnabled: (state, overrideEnabled) => {
23      if (overrideEnabled === 'Once') {
24        state.overrideEnabled = true;
25      } else {
26        // 'Continuous' or 'Disabled'
27        state.overrideEnabled = false;
28      }
29    },
30    setTpmPolicy: (state, tpmEnabled) => (state.tpmEnabled = tpmEnabled),
31  },
32  actions: {
33    async getBootSettings({ commit }) {
34      return await api
35        .get('/redfish/v1/Systems/system')
36        .then(({ data: { Boot } }) => {
37          commit(
38            'setBootSourceOptions',
39            Boot['BootSourceOverrideTarget@Redfish.AllowableValues']
40          );
41          commit('setOverrideEnabled', Boot.BootSourceOverrideEnabled);
42          commit('setBootSource', Boot.BootSourceOverrideTarget);
43        })
44        .catch((error) => console.log(error));
45    },
46    saveBootSettings({ commit, dispatch }, { bootSource, overrideEnabled }) {
47      const data = { Boot: {} };
48      data.Boot.BootSourceOverrideTarget = bootSource;
49
50      if (overrideEnabled) {
51        data.Boot.BootSourceOverrideEnabled = 'Once';
52      } else if (bootSource === 'None') {
53        data.Boot.BootSourceOverrideEnabled = 'Disabled';
54      } else {
55        data.Boot.BootSourceOverrideEnabled = 'Continuous';
56      }
57
58      return api
59        .patch('/redfish/v1/Systems/system', data)
60        .then((response) => {
61          // If request success, commit the values
62          commit('setBootSource', data.Boot.BootSourceOverrideTarget);
63          commit('setOverrideEnabled', data.Boot.BootSourceOverrideEnabled);
64          return response;
65        })
66        .catch((error) => {
67          console.log(error);
68          // If request error, GET saved options
69          dispatch('getBootSettings');
70          return error;
71        });
72    },
73    async getTpmPolicy({ commit }) {
74      // TODO: switch to Redfish when available
75      return await api
76        .get('/xyz/openbmc_project/control/host0/TPMEnable')
77        .then(({ data: { data: { TPMEnable } } }) =>
78          commit('setTpmPolicy', TPMEnable)
79        )
80        .catch((error) => console.log(error));
81    },
82    saveTpmPolicy({ commit, dispatch }, tpmEnabled) {
83      // TODO: switch to Redfish when available
84      const data = { data: tpmEnabled };
85      return api
86        .put(
87          '/xyz/openbmc_project/control/host0/TPMEnable/attr/TPMEnable',
88          data
89        )
90        .then((response) => {
91          // If request success, commit the values
92          commit('setTpmPolicy', tpmEnabled);
93          return response;
94        })
95        .catch((error) => {
96          console.log(error);
97          // If request error, GET saved policy
98          dispatch('getTpmPolicy');
99          return error;
100        });
101    },
102    async saveSettings(
103      { dispatch },
104      { bootSource, overrideEnabled, tpmEnabled }
105    ) {
106      const promises = [];
107
108      if (bootSource !== null || overrideEnabled !== null) {
109        promises.push(
110          dispatch('saveBootSettings', { bootSource, overrideEnabled })
111        );
112      }
113      if (tpmEnabled !== null) {
114        promises.push(dispatch('saveTpmPolicy', tpmEnabled));
115      }
116
117      return await api.all(promises).then(
118        api.spread((...responses) => {
119          let message = i18n.t(
120            'pageServerPowerOperations.toast.successSaveSettings'
121          );
122          responses.forEach((response) => {
123            if (response instanceof Error) {
124              throw new Error(
125                i18n.t('pageServerPowerOperations.toast.errorSaveSettings')
126              );
127            }
128          });
129          return message;
130        })
131      );
132    },
133  },
134};
135
136export default BootSettingsStore;
137