blob: 9740da7f900c549ec6160ce4a18ecb21f06c3a98 [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
Sean Zhang8841b7d2024-06-15 08:42:41 +030035 .get(`${await this.dispatch('global/getSystemPath')}`)
Yoshie Muranakac05ff642020-02-26 14:23:15 -080036 .then(({ data: { Boot } }) => {
37 commit(
38 'setBootSourceOptions',
Ed Tanous81323992024-02-27 11:26:24 -080039 Boot['BootSourceOverrideTarget@Redfish.AllowableValues'],
Yoshie Muranakac05ff642020-02-26 14:23:15 -080040 );
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 },
Sean Zhang8841b7d2024-06-15 08:42:41 +030046 async saveBootSettings(
47 { commit, dispatch },
48 { bootSource, overrideEnabled },
49 ) {
Yoshie Muranakac05ff642020-02-26 14:23:15 -080050 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
Sean Zhang8841b7d2024-06-15 08:42:41 +030062 .patch(`${await this.dispatch('global/getSystemPath')}`, data)
Derick Montague602e98a2020-10-21 16:20:00 -050063 .then((response) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -080064 // If request success, commit the values
65 commit('setBootSource', data.Boot.BootSourceOverrideTarget);
66 commit('setOverrideEnabled', data.Boot.BootSourceOverrideEnabled);
67 return response;
68 })
Derick Montague602e98a2020-10-21 16:20:00 -050069 .catch((error) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -080070 console.log(error);
71 // If request error, GET saved options
72 dispatch('getBootSettings');
73 return error;
74 });
75 },
Yoshie Muranaka5c977972020-04-30 09:48:23 -070076 async getTpmPolicy({ commit }) {
Yoshie Muranakac05ff642020-02-26 14:23:15 -080077 // TODO: switch to Redfish when available
Yoshie Muranaka5c977972020-04-30 09:48:23 -070078 return await api
Yoshie Muranakac05ff642020-02-26 14:23:15 -080079 .get('/xyz/openbmc_project/control/host0/TPMEnable')
Ed Tanous81323992024-02-27 11:26:24 -080080 .then(
81 ({
82 data: {
83 data: { TPMEnable },
84 },
85 }) => commit('setTpmPolicy', TPMEnable),
Yoshie Muranakac05ff642020-02-26 14:23:15 -080086 )
Derick Montague602e98a2020-10-21 16:20:00 -050087 .catch((error) => console.log(error));
Yoshie Muranakac05ff642020-02-26 14:23:15 -080088 },
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',
Ed Tanous81323992024-02-27 11:26:24 -080095 data,
Yoshie Muranakac05ff642020-02-26 14:23:15 -080096 )
Derick Montague602e98a2020-10-21 16:20:00 -050097 .then((response) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -080098 // If request success, commit the values
99 commit('setTpmPolicy', tpmEnabled);
100 return response;
101 })
Derick Montague602e98a2020-10-21 16:20:00 -0500102 .catch((error) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800103 console.log(error);
104 // If request error, GET saved policy
105 dispatch('getTpmPolicy');
106 return error;
107 });
108 },
109 async saveSettings(
110 { dispatch },
Ed Tanous81323992024-02-27 11:26:24 -0800111 { bootSource, overrideEnabled, tpmEnabled },
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800112 ) {
113 const promises = [];
114
115 if (bootSource !== null || overrideEnabled !== null) {
116 promises.push(
Ed Tanous81323992024-02-27 11:26:24 -0800117 dispatch('saveBootSettings', { bootSource, overrideEnabled }),
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800118 );
119 }
120 if (tpmEnabled !== null) {
121 promises.push(dispatch('saveTpmPolicy', tpmEnabled));
122 }
123
124 return await api.all(promises).then(
125 api.spread((...responses) => {
Surya V603cfbf2024-07-11 15:19:46 +0530126 let message = i18n.global.t(
Ed Tanous81323992024-02-27 11:26:24 -0800127 'pageServerPowerOperations.toast.successSaveSettings',
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800128 );
Derick Montague602e98a2020-10-21 16:20:00 -0500129 responses.forEach((response) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800130 if (response instanceof Error) {
131 throw new Error(
Surya V603cfbf2024-07-11 15:19:46 +0530132 i18n.global.t(
133 'pageServerPowerOperations.toast.errorSaveSettings',
134 ),
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800135 );
136 }
137 });
138 return message;
Ed Tanous81323992024-02-27 11:26:24 -0800139 }),
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800140 );
Derick Montague602e98a2020-10-21 16:20:00 -0500141 },
142 },
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800143};
144
145export default BootSettingsStore;