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(`${await this.dispatch('global/getSystemPath')}`)
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    async saveBootSettings(
47      { commit, dispatch },
48      { bootSource, overrideEnabled },
49    ) {
50      const data = { Boot: {} };
51      data.Boot.BootSourceOverrideTarget = bootSource;
52
53      if (overrideEnabled) {
54        data.Boot.BootSourceOverrideEnabled = 'Once';
55      } else if (bootSource === 'None') {
56        data.Boot.BootSourceOverrideEnabled = 'Disabled';
57      } else {
58        data.Boot.BootSourceOverrideEnabled = 'Continuous';
59      }
60
61      return api
62        .patch(`${await this.dispatch('global/getSystemPath')}`, data)
63        .then((response) => {
64          // If request success, commit the values
65          commit('setBootSource', data.Boot.BootSourceOverrideTarget);
66          commit('setOverrideEnabled', data.Boot.BootSourceOverrideEnabled);
67          return response;
68        })
69        .catch((error) => {
70          console.log(error);
71          // If request error, GET saved options
72          dispatch('getBootSettings');
73          return error;
74        });
75    },
76    async getTpmPolicy({ commit }) {
77      // TODO: switch to Redfish when available
78      return await api
79        .get('/xyz/openbmc_project/control/host0/TPMEnable')
80        .then(
81          ({
82            data: {
83              data: { TPMEnable },
84            },
85          }) => commit('setTpmPolicy', TPMEnable),
86        )
87        .catch((error) => console.log(error));
88    },
89    saveTpmPolicy({ commit, dispatch }, tpmEnabled) {
90      // TODO: switch to Redfish when available
91      const data = { data: tpmEnabled };
92      return api
93        .put(
94          '/xyz/openbmc_project/control/host0/TPMEnable/attr/TPMEnable',
95          data,
96        )
97        .then((response) => {
98          // If request success, commit the values
99          commit('setTpmPolicy', tpmEnabled);
100          return response;
101        })
102        .catch((error) => {
103          console.log(error);
104          // If request error, GET saved policy
105          dispatch('getTpmPolicy');
106          return error;
107        });
108    },
109    async saveSettings(
110      { dispatch },
111      { bootSource, overrideEnabled, tpmEnabled },
112    ) {
113      const promises = [];
114
115      if (bootSource !== null || overrideEnabled !== null) {
116        promises.push(
117          dispatch('saveBootSettings', { bootSource, overrideEnabled }),
118        );
119      }
120      if (tpmEnabled !== null) {
121        promises.push(dispatch('saveTpmPolicy', tpmEnabled));
122      }
123
124      return await api.all(promises).then(
125        api.spread((...responses) => {
126          let message = i18n.t(
127            'pageServerPowerOperations.toast.successSaveSettings',
128          );
129          responses.forEach((response) => {
130            if (response instanceof Error) {
131              throw new Error(
132                i18n.t('pageServerPowerOperations.toast.errorSaveSettings'),
133              );
134            }
135          });
136          return message;
137        }),
138      );
139    },
140  },
141};
142
143export default BootSettingsStore;
144