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(
78          ({
79            data: {
80              data: { TPMEnable },
81            },
82          }) => commit('setTpmPolicy', TPMEnable),
83        )
84        .catch((error) => console.log(error));
85    },
86    saveTpmPolicy({ commit, dispatch }, tpmEnabled) {
87      // TODO: switch to Redfish when available
88      const data = { data: tpmEnabled };
89      return api
90        .put(
91          '/xyz/openbmc_project/control/host0/TPMEnable/attr/TPMEnable',
92          data,
93        )
94        .then((response) => {
95          // If request success, commit the values
96          commit('setTpmPolicy', tpmEnabled);
97          return response;
98        })
99        .catch((error) => {
100          console.log(error);
101          // If request error, GET saved policy
102          dispatch('getTpmPolicy');
103          return error;
104        });
105    },
106    async saveSettings(
107      { dispatch },
108      { bootSource, overrideEnabled, tpmEnabled },
109    ) {
110      const promises = [];
111
112      if (bootSource !== null || overrideEnabled !== null) {
113        promises.push(
114          dispatch('saveBootSettings', { bootSource, overrideEnabled }),
115        );
116      }
117      if (tpmEnabled !== null) {
118        promises.push(dispatch('saveTpmPolicy', tpmEnabled));
119      }
120
121      return await api.all(promises).then(
122        api.spread((...responses) => {
123          let message = i18n.t(
124            'pageServerPowerOperations.toast.successSaveSettings',
125          );
126          responses.forEach((response) => {
127            if (response instanceof Error) {
128              throw new Error(
129                i18n.t('pageServerPowerOperations.toast.errorSaveSettings'),
130              );
131            }
132          });
133          return message;
134        }),
135      );
136    },
137  },
138};
139
140export default BootSettingsStore;
141