SurenNeware | 6185909 | 2020-10-01 09:37:32 +0530 | [diff] [blame] | 1 | import api from '@/store/api'; |
| 2 | import i18n from '@/i18n'; |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 3 | |
| 4 | const BootSettingsStore = { |
| 5 | namespaced: true, |
| 6 | state: { |
| 7 | bootSourceOptions: [], |
| 8 | bootSource: null, |
| 9 | overrideEnabled: null, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 10 | tpmEnabled: null, |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 11 | }, |
| 12 | getters: { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 13 | bootSourceOptions: (state) => state.bootSourceOptions, |
| 14 | bootSource: (state) => state.bootSource, |
| 15 | overrideEnabled: (state) => state.overrideEnabled, |
| 16 | tpmEnabled: (state) => state.tpmEnabled, |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 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 | }, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 30 | setTpmPolicy: (state, tpmEnabled) => (state.tpmEnabled = tpmEnabled), |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 31 | }, |
| 32 | actions: { |
Yoshie Muranaka | 5c97797 | 2020-04-30 09:48:23 -0700 | [diff] [blame] | 33 | async getBootSettings({ commit }) { |
| 34 | return await api |
Derick Montague | 4904126 | 2021-05-11 16:55:09 -0500 | [diff] [blame] | 35 | .get('/redfish/v1/Systems/system') |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 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 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 44 | .catch((error) => console.log(error)); |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 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) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 60 | .then((response) => { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 61 | // If request success, commit the values |
| 62 | commit('setBootSource', data.Boot.BootSourceOverrideTarget); |
| 63 | commit('setOverrideEnabled', data.Boot.BootSourceOverrideEnabled); |
| 64 | return response; |
| 65 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 66 | .catch((error) => { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 67 | console.log(error); |
| 68 | // If request error, GET saved options |
| 69 | dispatch('getBootSettings'); |
| 70 | return error; |
| 71 | }); |
| 72 | }, |
Yoshie Muranaka | 5c97797 | 2020-04-30 09:48:23 -0700 | [diff] [blame] | 73 | async getTpmPolicy({ commit }) { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 74 | // TODO: switch to Redfish when available |
Yoshie Muranaka | 5c97797 | 2020-04-30 09:48:23 -0700 | [diff] [blame] | 75 | return await api |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 76 | .get('/xyz/openbmc_project/control/host0/TPMEnable') |
| 77 | .then(({ data: { data: { TPMEnable } } }) => |
| 78 | commit('setTpmPolicy', TPMEnable) |
| 79 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 80 | .catch((error) => console.log(error)); |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 81 | }, |
| 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 Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 90 | .then((response) => { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 91 | // If request success, commit the values |
| 92 | commit('setTpmPolicy', tpmEnabled); |
| 93 | return response; |
| 94 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 95 | .catch((error) => { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 96 | 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 Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 122 | responses.forEach((response) => { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 123 | if (response instanceof Error) { |
| 124 | throw new Error( |
| 125 | i18n.t('pageServerPowerOperations.toast.errorSaveSettings') |
| 126 | ); |
| 127 | } |
| 128 | }); |
| 129 | return message; |
| 130 | }) |
| 131 | ); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 132 | }, |
| 133 | }, |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | export default BootSettingsStore; |