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 |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame] | 35 | .get(`${await this.dispatch('global/getSystemPath')}`) |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 36 | .then(({ data: { Boot } }) => { |
| 37 | commit( |
| 38 | 'setBootSourceOptions', |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 39 | Boot['BootSourceOverrideTarget@Redfish.AllowableValues'], |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 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 | }, |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame] | 46 | async saveBootSettings( |
| 47 | { commit, dispatch }, |
| 48 | { bootSource, overrideEnabled }, |
| 49 | ) { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 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 |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame] | 62 | .patch(`${await this.dispatch('global/getSystemPath')}`, data) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 63 | .then((response) => { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 64 | // If request success, commit the values |
| 65 | commit('setBootSource', data.Boot.BootSourceOverrideTarget); |
| 66 | commit('setOverrideEnabled', data.Boot.BootSourceOverrideEnabled); |
| 67 | return response; |
| 68 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 69 | .catch((error) => { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 70 | console.log(error); |
| 71 | // If request error, GET saved options |
| 72 | dispatch('getBootSettings'); |
| 73 | return error; |
| 74 | }); |
| 75 | }, |
Yoshie Muranaka | 5c97797 | 2020-04-30 09:48:23 -0700 | [diff] [blame] | 76 | async getTpmPolicy({ commit }) { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 77 | // TODO: switch to Redfish when available |
Yoshie Muranaka | 5c97797 | 2020-04-30 09:48:23 -0700 | [diff] [blame] | 78 | return await api |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 79 | .get('/xyz/openbmc_project/control/host0/TPMEnable') |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 80 | .then( |
| 81 | ({ |
| 82 | data: { |
| 83 | data: { TPMEnable }, |
| 84 | }, |
| 85 | }) => commit('setTpmPolicy', TPMEnable), |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 86 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 87 | .catch((error) => console.log(error)); |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 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', |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 95 | data, |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 96 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 97 | .then((response) => { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 98 | // If request success, commit the values |
| 99 | commit('setTpmPolicy', tpmEnabled); |
| 100 | return response; |
| 101 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 102 | .catch((error) => { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 103 | console.log(error); |
| 104 | // If request error, GET saved policy |
| 105 | dispatch('getTpmPolicy'); |
| 106 | return error; |
| 107 | }); |
| 108 | }, |
| 109 | async saveSettings( |
| 110 | { dispatch }, |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 111 | { bootSource, overrideEnabled, tpmEnabled }, |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 112 | ) { |
| 113 | const promises = []; |
| 114 | |
| 115 | if (bootSource !== null || overrideEnabled !== null) { |
| 116 | promises.push( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 117 | dispatch('saveBootSettings', { bootSource, overrideEnabled }), |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 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) => { |
Surya V | 603cfbf | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 126 | let message = i18n.global.t( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 127 | 'pageServerPowerOperations.toast.successSaveSettings', |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 128 | ); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 129 | responses.forEach((response) => { |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 130 | if (response instanceof Error) { |
| 131 | throw new Error( |
Surya V | 603cfbf | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 132 | i18n.global.t( |
| 133 | 'pageServerPowerOperations.toast.errorSaveSettings', |
| 134 | ), |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 135 | ); |
| 136 | } |
| 137 | }); |
| 138 | return message; |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 139 | }), |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 140 | ); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 141 | }, |
| 142 | }, |
Yoshie Muranaka | c05ff64 | 2020-02-26 14:23:15 -0800 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | export default BootSettingsStore; |