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