1import api from '@/store/api'; 2import i18n from '@/i18n'; 3import { find, remove } from 'lodash'; 4 5const NetworkStore = { 6 namespaced: true, 7 state: { 8 defaultGateway: '', 9 ethernetData: [], 10 interfaceOptions: [], 11 }, 12 getters: { 13 defaultGateway: (state) => state.defaultGateway, 14 ethernetData: (state) => state.ethernetData, 15 interfaceOptions: (state) => state.interfaceOptions, 16 }, 17 mutations: { 18 setDefaultGateway: (state, defaultGateway) => 19 (state.defaultGateway = defaultGateway), 20 setEthernetData: (state, ethernetData) => 21 (state.ethernetData = ethernetData), 22 setInterfaceOptions: (state, interfaceOptions) => 23 (state.interfaceOptions = interfaceOptions), 24 }, 25 actions: { 26 async getEthernetData({ commit }) { 27 return await api 28 .get('/redfish/v1/Managers/bmc/EthernetInterfaces') 29 .then((response) => 30 response.data.Members.map( 31 (ethernetInterface) => ethernetInterface['@odata.id'] 32 ) 33 ) 34 .then((ethernetInterfaceIds) => 35 api.all( 36 ethernetInterfaceIds.map((ethernetInterface) => 37 api.get(ethernetInterface) 38 ) 39 ) 40 ) 41 .then((ethernetInterfaces) => { 42 const ethernetData = ethernetInterfaces.map( 43 (ethernetInterface) => ethernetInterface.data 44 ); 45 const interfaceOptions = ethernetInterfaces.map( 46 (ethernetName) => ethernetName.data.Id 47 ); 48 const addresses = ethernetData[0].IPv4StaticAddresses; 49 50 // Default gateway manually set to first gateway saved on the first interface. Default gateway property is WIP on backend 51 const defaultGateway = addresses.map((ipv4) => { 52 return ipv4.Gateway; 53 }); 54 55 commit('setDefaultGateway', defaultGateway[0]); 56 commit('setEthernetData', ethernetData); 57 commit('setInterfaceOptions', interfaceOptions); 58 }) 59 .catch((error) => { 60 console.log('Network Data:', error); 61 }); 62 }, 63 64 async updateInterfaceSettings({ dispatch, state }, networkSettingsForm) { 65 const updatedAddresses = networkSettingsForm.staticIpv4; 66 const originalAddresses = 67 state.ethernetData[networkSettingsForm.selectedInterfaceIndex] 68 .IPv4StaticAddresses; 69 70 const addressArray = originalAddresses.map((item) => { 71 const address = item.Address; 72 if (find(updatedAddresses, { Address: address })) { 73 remove(updatedAddresses, (item) => { 74 return item.Address === address; 75 }); 76 return {}; 77 } else { 78 return null; 79 } 80 }); 81 82 const data = { 83 HostName: networkSettingsForm.hostname, 84 MACAddress: networkSettingsForm.macAddress, 85 DHCPv4: { 86 DHCPEnabled: networkSettingsForm.isDhcpEnabled, 87 }, 88 }; 89 90 // If DHCP disabled, update static DNS or static ipv4 91 if (!networkSettingsForm.isDhcpEnabled) { 92 data.IPv4StaticAddresses = [...addressArray, ...updatedAddresses]; 93 data.StaticNameServers = networkSettingsForm.staticNameServers; 94 } 95 96 return await api 97 .patch( 98 `/redfish/v1/Managers/bmc/EthernetInterfaces/${networkSettingsForm.interfaceId}`, 99 data 100 ) 101 .then(() => dispatch('getEthernetData')) 102 .then(() => { 103 return i18n.t('pageNetwork.toast.successSaveNetworkSettings'); 104 }) 105 .catch((error) => { 106 console.log(error); 107 throw new Error(i18n.t('pageNetwork.toast.errorSaveNetworkSettings')); 108 }); 109 }, 110 }, 111}; 112 113export default NetworkStore; 114