blob: 1f5a628f6f2ce77052736f2a2858b7a4e0708cb9 [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',
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 },
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')
Ed Tanous81323992024-02-27 11:26:24 -080077 .then(
78 ({
79 data: {
80 data: { TPMEnable },
81 },
82 }) => commit('setTpmPolicy', TPMEnable),
Yoshie Muranakac05ff642020-02-26 14:23:15 -080083 )
Derick Montague602e98a2020-10-21 16:20:00 -050084 .catch((error) => console.log(error));
Yoshie Muranakac05ff642020-02-26 14:23:15 -080085 },
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',
Ed Tanous81323992024-02-27 11:26:24 -080092 data,
Yoshie Muranakac05ff642020-02-26 14:23:15 -080093 )
Derick Montague602e98a2020-10-21 16:20:00 -050094 .then((response) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -080095 // If request success, commit the values
96 commit('setTpmPolicy', tpmEnabled);
97 return response;
98 })
Derick Montague602e98a2020-10-21 16:20:00 -050099 .catch((error) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800100 console.log(error);
101 // If request error, GET saved policy
102 dispatch('getTpmPolicy');
103 return error;
104 });
105 },
106 async saveSettings(
107 { dispatch },
Ed Tanous81323992024-02-27 11:26:24 -0800108 { bootSource, overrideEnabled, tpmEnabled },
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800109 ) {
110 const promises = [];
111
112 if (bootSource !== null || overrideEnabled !== null) {
113 promises.push(
Ed Tanous81323992024-02-27 11:26:24 -0800114 dispatch('saveBootSettings', { bootSource, overrideEnabled }),
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800115 );
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(
Ed Tanous81323992024-02-27 11:26:24 -0800124 'pageServerPowerOperations.toast.successSaveSettings',
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800125 );
Derick Montague602e98a2020-10-21 16:20:00 -0500126 responses.forEach((response) => {
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800127 if (response instanceof Error) {
128 throw new Error(
Ed Tanous81323992024-02-27 11:26:24 -0800129 i18n.t('pageServerPowerOperations.toast.errorSaveSettings'),
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800130 );
131 }
132 });
133 return message;
Ed Tanous81323992024-02-27 11:26:24 -0800134 }),
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800135 );
Derick Montague602e98a2020-10-21 16:20:00 -0500136 },
137 },
Yoshie Muranakac05ff642020-02-26 14:23:15 -0800138};
139
140export default BootSettingsStore;