xref: /openbmc/webui-vue/src/store/modules/Settings/NetworkStore.js (revision c4b8757ed88ecea369e6044548d2fbe072d5bd4a)
1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const NetworkStore = {
5  namespaced: true,
6  state: {
7    ethernetData: [],
8    firstInterfaceId: '', //used for setting global DHCP settings
9    globalNetworkSettings: [],
10  },
11  getters: {
12    ethernetData: (state) => state.ethernetData,
13    firstInterfaceId: (state) => state.firstInterfaceId,
14    globalNetworkSettings: (state) => state.globalNetworkSettings,
15  },
16  mutations: {
17    setEthernetData: (state, ethernetData) =>
18      (state.ethernetData = ethernetData),
19    setFirstInterfaceId: (state, firstInterfaceId) =>
20      (state.firstInterfaceId = firstInterfaceId),
21    setGlobalNetworkSettings: (state, data) => {
22      state.globalNetworkSettings = data.map(({ data }) => {
23        const {
24          DHCPv4,
25          HostName,
26          IPv4Addresses,
27          IPv4StaticAddresses,
28          LinkStatus,
29        } = data;
30        return {
31          dhcpAddress: IPv4Addresses.filter(
32            (ipv4) => ipv4.AddressOrigin === 'DHCP'
33          ),
34          hostname: HostName,
35          linkStatus: LinkStatus,
36          staticAddress: IPv4StaticAddresses[0]?.Address, // Display first static address on overview page
37          useDnsEnabled: DHCPv4.UseDNSServers,
38          useDomainNameEnabled: DHCPv4.UseDomainName,
39          useNtpEnabled: DHCPv4.UseNTPServers,
40        };
41      });
42    },
43  },
44  actions: {
45    async getEthernetData({ commit }) {
46      return await api
47        .get('/redfish/v1/Managers/bmc/EthernetInterfaces')
48        .then((response) =>
49          response.data.Members.map(
50            (ethernetInterface) => ethernetInterface['@odata.id']
51          )
52        )
53        .then((ethernetInterfaceIds) =>
54          api.all(
55            ethernetInterfaceIds.map((ethernetInterface) =>
56              api.get(ethernetInterface)
57            )
58          )
59        )
60        .then((ethernetInterfaces) => {
61          const ethernetData = ethernetInterfaces.map(
62            (ethernetInterface) => ethernetInterface.data
63          );
64          const firstInterfaceId = ethernetData[0].Id;
65
66          commit('setEthernetData', ethernetData);
67          commit('setFirstInterfaceId', firstInterfaceId);
68          commit('setGlobalNetworkSettings', ethernetInterfaces);
69        })
70        .catch((error) => {
71          console.log('Network Data:', error);
72        });
73    },
74    async saveDomainNameState({ commit, state }, domainState) {
75      commit('setDomainNameState', domainState);
76      const data = {
77        DHCPv4: {
78          UseDomainName: domainState,
79        },
80      };
81      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
82      // on all interfaces
83      return api
84        .patch(
85          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
86          data
87        )
88        .then(() => {
89          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
90            setting: i18n.t('pageNetwork.domainName'),
91          });
92        })
93        .catch((error) => {
94          console.log(error);
95          commit('setDomainNameState', !domainState);
96          throw new Error(
97            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
98              setting: i18n.t('pageNetwork.domainName'),
99            })
100          );
101        });
102    },
103    async saveDnsState({ commit, state }, dnsState) {
104      commit('setDnsState', dnsState);
105      const data = {
106        DHCPv4: {
107          UseDNSServers: dnsState,
108        },
109      };
110      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
111      // on all interfaces
112      return api
113        .patch(
114          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
115          data
116        )
117        .then(() => {
118          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
119            setting: i18n.t('pageNetwork.dns'),
120          });
121        })
122        .catch((error) => {
123          console.log(error);
124          commit('setDnsState', !dnsState);
125          throw new Error(
126            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
127              setting: i18n.t('pageNetwork.dns'),
128            })
129          );
130        });
131    },
132    async saveNtpState({ commit, state }, ntpState) {
133      commit('setNtpState', ntpState);
134      const data = {
135        DHCPv4: {
136          UseDNSServers: ntpState,
137        },
138      };
139      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
140      // on all interfaces
141      return api
142        .patch(
143          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
144          data
145        )
146        .then(() => {
147          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
148            setting: i18n.t('pageNetwork.ntp'),
149          });
150        })
151        .catch((error) => {
152          console.log(error);
153          commit('setNtpState', !ntpState);
154          throw new Error(
155            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
156              setting: i18n.t('pageNetwork.ntp'),
157            })
158          );
159        });
160    },
161  },
162};
163
164export default NetworkStore;
165