import api from '@/store/api'; import i18n from '@/i18n'; const NetworkStore = { namespaced: true, state: { ethernetData: [], firstInterfaceId: '', //used for setting global DHCP settings globalNetworkSettings: [], }, getters: { ethernetData: (state) => state.ethernetData, firstInterfaceId: (state) => state.firstInterfaceId, globalNetworkSettings: (state) => state.globalNetworkSettings, }, mutations: { setEthernetData: (state, ethernetData) => (state.ethernetData = ethernetData), setFirstInterfaceId: (state, firstInterfaceId) => (state.firstInterfaceId = firstInterfaceId), setGlobalNetworkSettings: (state, data) => { state.globalNetworkSettings = data.map(({ data }) => { const { DHCPv4, HostName, IPv4Addresses, IPv4StaticAddresses, LinkStatus, } = data; return { dhcpAddress: IPv4Addresses.filter( (ipv4) => ipv4.AddressOrigin === 'DHCP' ), hostname: HostName, linkStatus: LinkStatus, staticAddress: IPv4StaticAddresses[0]?.Address, // Display first static address on overview page useDnsEnabled: DHCPv4.UseDNSServers, useDomainNameEnabled: DHCPv4.UseDomainName, useNtpEnabled: DHCPv4.UseNTPServers, }; }); }, }, actions: { async getEthernetData({ commit }) { return await api .get('/redfish/v1/Managers/bmc/EthernetInterfaces') .then((response) => response.data.Members.map( (ethernetInterface) => ethernetInterface['@odata.id'] ) ) .then((ethernetInterfaceIds) => api.all( ethernetInterfaceIds.map((ethernetInterface) => api.get(ethernetInterface) ) ) ) .then((ethernetInterfaces) => { const ethernetData = ethernetInterfaces.map( (ethernetInterface) => ethernetInterface.data ); const firstInterfaceId = ethernetData[0].Id; commit('setEthernetData', ethernetData); commit('setFirstInterfaceId', firstInterfaceId); commit('setGlobalNetworkSettings', ethernetInterfaces); }) .catch((error) => { console.log('Network Data:', error); }); }, async saveDomainNameState({ commit, state }, domainState) { commit('setDomainNameState', domainState); const data = { DHCPv4: { UseDomainName: domainState, }, }; // Saving to the first interface automatically updates DHCPv4 and DHCPv6 // on all interfaces return api .patch( `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`, data ) .then(() => { return i18n.t('pageNetwork.toast.successSaveNetworkSettings', { setting: i18n.t('pageNetwork.domainName'), }); }) .catch((error) => { console.log(error); commit('setDomainNameState', !domainState); throw new Error( i18n.t('pageNetwork.toast.errorSaveNetworkSettings', { setting: i18n.t('pageNetwork.domainName'), }) ); }); }, async saveDnsState({ commit, state }, dnsState) { commit('setDnsState', dnsState); const data = { DHCPv4: { UseDNSServers: dnsState, }, }; // Saving to the first interface automatically updates DHCPv4 and DHCPv6 // on all interfaces return api .patch( `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`, data ) .then(() => { return i18n.t('pageNetwork.toast.successSaveNetworkSettings', { setting: i18n.t('pageNetwork.dns'), }); }) .catch((error) => { console.log(error); commit('setDnsState', !dnsState); throw new Error( i18n.t('pageNetwork.toast.errorSaveNetworkSettings', { setting: i18n.t('pageNetwork.dns'), }) ); }); }, async saveNtpState({ commit, state }, ntpState) { commit('setNtpState', ntpState); const data = { DHCPv4: { UseDNSServers: ntpState, }, }; // Saving to the first interface automatically updates DHCPv4 and DHCPv6 // on all interfaces return api .patch( `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`, data ) .then(() => { return i18n.t('pageNetwork.toast.successSaveNetworkSettings', { setting: i18n.t('pageNetwork.ntp'), }); }) .catch((error) => { console.log(error); commit('setNtpState', !ntpState); throw new Error( i18n.t('pageNetwork.toast.errorSaveNetworkSettings', { setting: i18n.t('pageNetwork.ntp'), }) ); }); }, }, }; export default NetworkStore;