blob: 7a36dd8460ca7874e50069b3fefeb0cb2f775af8 [file] [log] [blame]
SurenNeware61859092020-10-01 09:37:32 +05301import api from '@/store/api';
2import i18n from '@/i18n';
Yoshie Muranakac05ff642020-02-26 14:23:15 -08003
4const BootSettingsStore = {
5 namespaced: true,
6 state: {
7 bootSourceOptions: [],
8 bootSource: null,
9 overrideEnabled: null,
Derick Montague602e98a2020-10-21 16:20:00 -050010 tpmEnabled: null,
Yoshie Muranakac05ff642020-02-26 14:23:15 -080011 },
12 getters: {
Derick Montague602e98a2020-10-21 16:20:00 -050013 bootSourceOptions: (state) => state.bootSourceOptions,
14 bootSource: (state) => state.bootSource,
15 overrideEnabled: (state) => state.overrideEnabled,
16 tpmEnabled: (state) => state.tpmEnabled,
Yoshie Muranakac05ff642020-02-26 14:23:15 -080017 },
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 },
Derick Montague602e98a2020-10-21 16:20:00 -050030 setTpmPolicy: (state, tpmEnabled) => (state.tpmEnabled = tpmEnabled),
Yoshie Muranakac05ff642020-02-26 14:23:15 -080031 },
32 actions: {
Yoshie Muranaka5c977972020-04-30 09:48:23 -070033 async getBootSettings({ commit }) {
34 return await api
Derick Montague49041262021-05-11 16:55:09 -050035 .get('/redfish/v1/Systems/system')
Yoshie Muranakac05ff642020-02-26 14:23:15 -080036 .then(({ data: { Boot } }) => {
37 commit(
38 'setBootSourceOptions',
39 Boot['BootSourceOverrideTarget@Redfish.AllowableValues']
40 );
41 commit('setOverrideEnabled', Boot.BootSourceOverrideEnabled);
42 commit('setBootSource', Boot.BootSourceOverrideTarget);
43 })
Derick Montague602e98a2020-10-21 16:20:00 -050044 .catch((error) => console.log(error));
Yoshie Muranakac05ff642020-02-26 14:23:15 -080045 },
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)
Derick Montague602e98a2020-10-21 16:20:00 -050060 .then((response) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -080061 // If request success, commit the values
62 commit('setBootSource', data.Boot.BootSourceOverrideTarget);
63 commit('setOverrideEnabled', data.Boot.BootSourceOverrideEnabled);
64 return response;
65 })
Derick Montague602e98a2020-10-21 16:20:00 -050066 .catch((error) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -080067 console.log(error);
68 // If request error, GET saved options
69 dispatch('getBootSettings');
70 return error;
71 });
72 },
Yoshie Muranaka5c977972020-04-30 09:48:23 -070073 async getTpmPolicy({ commit }) {
Yoshie Muranakac05ff642020-02-26 14:23:15 -080074 // TODO: switch to Redfish when available
Yoshie Muranaka5c977972020-04-30 09:48:23 -070075 return await api
Yoshie Muranakac05ff642020-02-26 14:23:15 -080076 .get('/xyz/openbmc_project/control/host0/TPMEnable')
77 .then(({ data: { data: { TPMEnable } } }) =>
78 commit('setTpmPolicy', TPMEnable)
79 )
Derick Montague602e98a2020-10-21 16:20:00 -050080 .catch((error) => console.log(error));
Yoshie Muranakac05ff642020-02-26 14:23:15 -080081 },
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 )
Derick Montague602e98a2020-10-21 16:20:00 -050090 .then((response) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -080091 // If request success, commit the values
92 commit('setTpmPolicy', tpmEnabled);
93 return response;
94 })
Derick Montague602e98a2020-10-21 16:20:00 -050095 .catch((error) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -080096 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 );
Derick Montague602e98a2020-10-21 16:20:00 -0500122 responses.forEach((response) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800123 if (response instanceof Error) {
124 throw new Error(
125 i18n.t('pageServerPowerOperations.toast.errorSaveSettings')
126 );
127 }
128 });
129 return message;
130 })
131 );
Derick Montague602e98a2020-10-21 16:20:00 -0500132 },
133 },
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800134};
135
136export default BootSettingsStore;