xref: /openbmc/webui-vue/src/store/modules/Settings/NetworkStore.js (revision d36ac8a8be8636ddd0e64ce005d507b21bcdeb00)
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    selectedInterfaceId: '', // which tab is selected
11    selectedInterfaceIndex: 0, // which tab is selected
12  },
13  getters: {
14    ethernetData: (state) => state.ethernetData,
15    firstInterfaceId: (state) => state.firstInterfaceId,
16    globalNetworkSettings: (state) => state.globalNetworkSettings,
17    selectedInterfaceId: (state) => state.selectedInterfaceId,
18    selectedInterfaceIndex: (state) => state.selectedInterfaceIndex,
19  },
20  mutations: {
21    setDomainNameState: (state, domainState) =>
22      (state.domainState = domainState),
23    setDnsState: (state, dnsState) => (state.dnsState = dnsState),
24    setEthernetData: (state, ethernetData) =>
25      (state.ethernetData = ethernetData),
26    setFirstInterfaceId: (state, firstInterfaceId) =>
27      (state.firstInterfaceId = firstInterfaceId),
28    setGlobalNetworkSettings: (state, data) => {
29      state.globalNetworkSettings = data.map(({ data }) => {
30        const {
31          DHCPv4,
32          DHCPv6,
33          HostName,
34          IPv4Addresses,
35          IPv4StaticAddresses,
36          IPv6Addresses,
37          IPv6StaticAddresses,
38          LinkStatus,
39          MACAddress,
40          IPv6DefaultGateway,
41        } = data;
42        return {
43          defaultGateway: IPv4StaticAddresses[0]?.Gateway, //First static gateway is the default gateway
44          ipv6DefaultGateway: IPv6DefaultGateway,
45          dhcpAddress: IPv4Addresses.filter(
46            (ipv4) => ipv4.AddressOrigin === 'DHCP',
47          ),
48          dhcpv6Address: IPv6Addresses.filter(
49            (ipv6) =>
50              ipv6.AddressOrigin === 'SLAAC' || ipv6.AddressOrigin === 'DHCPv6',
51          ),
52          dhcpEnabled: DHCPv4.DHCPEnabled,
53          dhcp6Enabled: DHCPv6.OperatingMode,
54          hostname: HostName,
55          macAddress: MACAddress,
56          linkStatus: LinkStatus,
57          staticAddress: IPv4StaticAddresses[0]?.Address, // Display first static address on overview page
58          ipv6StaticAddress: IPv6StaticAddresses[0]?.Address,
59          useDnsEnabled: DHCPv4.UseDNSServers,
60          useDomainNameEnabled: DHCPv4.UseDomainName,
61          useNtpEnabled: DHCPv4.UseNTPServers,
62          useDnsEnabledIpv6: DHCPv6.UseDNSServers,
63          useDomainNameEnabledIpv6: DHCPv6.UseDomainName,
64          useNtpEnabledIpv6: DHCPv6.UseNTPServers,
65        };
66      });
67    },
68    setNtpState: (state, ntpState) => (state.ntpState = ntpState),
69    setDomainNameStateIpv6: (state, domainState) =>
70      (state.domainStateIpv6 = domainState),
71    setDnsStateIpv6: (state, dnsState) => (state.dnsStateIpv6 = dnsState),
72    setNtpStateIpv6: (state, ntpState) => (state.ntpStateIpv6 = ntpState),
73    setSelectedInterfaceId: (state, selectedInterfaceId) =>
74      (state.selectedInterfaceId = selectedInterfaceId),
75    setSelectedInterfaceIndex: (state, selectedInterfaceIndex) =>
76      (state.selectedInterfaceIndex = selectedInterfaceIndex),
77  },
78  actions: {
79    async getEthernetData({ commit }) {
80      return await api
81        .get(`${await this.dispatch('global/getBmcPath')}/EthernetInterfaces`)
82        .then((response) =>
83          response.data.Members.map(
84            (ethernetInterface) => ethernetInterface['@odata.id'],
85          ),
86        )
87        .then((ethernetInterfaceIds) =>
88          api.all(
89            ethernetInterfaceIds.map((ethernetInterface) =>
90              api.get(ethernetInterface),
91            ),
92          ),
93        )
94        .then((ethernetInterfaces) => {
95          const ethernetData = ethernetInterfaces.map(
96            (ethernetInterface) => ethernetInterface.data,
97          );
98          const firstInterfaceId = ethernetData[0].Id;
99
100          commit('setEthernetData', ethernetData);
101          commit('setFirstInterfaceId', firstInterfaceId);
102          commit('setSelectedInterfaceId', firstInterfaceId);
103          commit('setGlobalNetworkSettings', ethernetInterfaces);
104        })
105        .catch((error) => {
106          console.log('Network Data:', error);
107        });
108    },
109    async saveDhcpEnabledState({ state, dispatch }, dhcpState) {
110      const data = {
111        DHCPv4: {
112          DHCPEnabled: dhcpState,
113        },
114      };
115      return api
116        .patch(
117          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
118          data,
119        )
120        .then(dispatch('getEthernetData'))
121        .then(() => {
122          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
123            setting: i18n.global.t('pageNetwork.dhcp'),
124          });
125        })
126        .catch((error) => {
127          console.log(error);
128          throw new Error(
129            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
130              setting: i18n.global.t('pageNetwork.dhcp'),
131            }),
132          );
133        });
134    },
135    async saveDhcp6EnabledState({ state, dispatch }, dhcpState) {
136      const data = {
137        DHCPv6: {
138          OperatingMode: dhcpState ? 'Enabled' : 'Disabled',
139        },
140      };
141      return api
142        .patch(
143          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
144          data,
145        )
146        .then(dispatch('getEthernetData'))
147        .then(() => {
148          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
149            setting: i18n.global.t('pageNetwork.dhcp6'),
150          });
151        })
152        .catch((error) => {
153          console.log(error);
154          throw new Error(
155            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
156              setting: i18n.global.t('pageNetwork.dhcp6'),
157            }),
158          );
159        });
160    },
161    async saveDomainNameState({ commit, state }, { domainState, ipVersion }) {
162      var data;
163      if (ipVersion === 'IPv4') {
164        commit('setDomainNameState', domainState);
165        data = {
166          DHCPv4: {
167            UseDomainName: domainState,
168          },
169        };
170      } else if (ipVersion === 'IPv6') {
171        commit('setDomainNameStateIpv6', domainState);
172        data = {
173          DHCPv6: {
174            UseDomainName: domainState,
175          },
176        };
177      }
178      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
179      // on all interfaces
180      return api
181        .patch(
182          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.firstInterfaceId}`,
183          data,
184        )
185        .then(() => {
186          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
187            setting: i18n.global.t('pageNetwork.domainName'),
188          });
189        })
190        .catch((error) => {
191          console.log(error);
192          if (ipVersion === 'IPv4') commit('setDomainNameState', !domainState);
193          else if (ipVersion === 'IPv6')
194            commit('setDomainNameStateIpv6', !domainState);
195          throw new Error(
196            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
197              setting: i18n.global.t('pageNetwork.domainName'),
198            }),
199          );
200        });
201    },
202    async saveDnsState({ commit, state }, { dnsState, ipVersion }) {
203      var data;
204      if (ipVersion === 'IPv4') {
205        commit('setDnsState', dnsState);
206        data = {
207          DHCPv4: {
208            UseDNSServers: dnsState,
209          },
210        };
211      } else if (ipVersion === 'IPv6') {
212        commit('setDnsStateIpv6', dnsState);
213        data = {
214          DHCPv6: {
215            UseDNSServers: dnsState,
216          },
217        };
218      }
219      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
220      // on all interfaces
221      return api
222        .patch(
223          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.firstInterfaceId}`,
224          data,
225        )
226        .then(() => {
227          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
228            setting: i18n.global.t('pageNetwork.dns'),
229          });
230        })
231        .catch((error) => {
232          console.log(error);
233          if (ipVersion === 'IPv4') commit('setDnsState', !dnsState);
234          else if (ipVersion === 'IPv6') commit('setDnsStateIpv6', !dnsState);
235          throw new Error(
236            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
237              setting: i18n.global.t('pageNetwork.dns'),
238            }),
239          );
240        });
241    },
242    async saveNtpState({ commit, state }, { ntpState, ipVersion }) {
243      var data;
244      if (ipVersion === 'IPv4') {
245        commit('setNtpState', ntpState);
246        data = {
247          DHCPv4: {
248            UseNTPServers: ntpState,
249          },
250        };
251      } else if (ipVersion === 'IPv6') {
252        commit('setNtpStateIpv6', ntpState);
253        data = {
254          DHCPv6: {
255            UseNTPServers: ntpState,
256          },
257        };
258      }
259      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
260      // on all interfaces
261      return api
262        .patch(
263          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.firstInterfaceId}`,
264          data,
265        )
266        .then(() => {
267          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
268            setting: i18n.global.t('pageNetwork.ntp'),
269          });
270        })
271        .catch((error) => {
272          console.log(error);
273          if (ipVersion === 'IPv4') commit('setNtpState', !ntpState);
274          else if (ipVersion === 'IPv6') commit('setNtpStateIpv6', !ntpState);
275          throw new Error(
276            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
277              setting: i18n.global.t('pageNetwork.ntp'),
278            }),
279          );
280        });
281    },
282    async setSelectedTabIndex({ commit }, tabIndex) {
283      commit('setSelectedInterfaceIndex', tabIndex);
284    },
285    async setSelectedTabId({ commit }, tabId) {
286      commit('setSelectedInterfaceId', tabId);
287    },
288    async saveIpv4Address({ dispatch, state }, ipv4Form) {
289      const originalAddresses = state.ethernetData[
290        state.selectedInterfaceIndex
291      ].IPv4StaticAddresses.map((ipv4) => {
292        const { Address, SubnetMask, Gateway } = ipv4;
293        return {
294          Address,
295          SubnetMask,
296          Gateway,
297        };
298      });
299      const newAddress = [ipv4Form];
300      return api
301        .patch(
302          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
303          {
304            IPv4StaticAddresses: originalAddresses.concat(newAddress),
305          },
306        )
307        .then(dispatch('getEthernetData'))
308        .then(() => {
309          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
310            setting: i18n.global.t('pageNetwork.ipv4'),
311          });
312        })
313        .catch((error) => {
314          console.log(error);
315          throw new Error(
316            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
317              setting: i18n.global.t('pageNetwork.ipv4'),
318            }),
319          );
320        });
321    },
322    async saveIpv6Address({ dispatch, state }, ipv6Form) {
323      const originalAddresses = state.ethernetData[
324        state.selectedInterfaceIndex
325      ].IPv6StaticAddresses.map((ipv6) => {
326        const { Address, PrefixLength } = ipv6;
327        return {
328          Address,
329          PrefixLength,
330        };
331      });
332      const newAddress = [ipv6Form];
333      return api
334        .patch(
335          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
336          {
337            IPv6StaticAddresses: originalAddresses.concat(newAddress),
338          },
339        )
340        .then(dispatch('getEthernetData'))
341        .then(() => {
342          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
343            setting: i18n.global.t('pageNetwork.ipv6'),
344          });
345        })
346        .catch((error) => {
347          console.log(error);
348          throw new Error(
349            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
350              setting: i18n.global.t('pageNetwork.ipv6'),
351            }),
352          );
353        });
354    },
355    async editIpv4Address({ dispatch, state }, ipv4TableData) {
356      return api
357        .patch(
358          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
359          { IPv4StaticAddresses: ipv4TableData },
360        )
361        .then(dispatch('getEthernetData'))
362        .then(() => {
363          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
364            setting: i18n.global.t('pageNetwork.ipv4'),
365          });
366        })
367        .catch((error) => {
368          console.log(error);
369          throw new Error(
370            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
371              setting: i18n.global.t('pageNetwork.ipv4'),
372            }),
373          );
374        });
375    },
376    async editIpv6Address({ dispatch, state }, ipv6TableData) {
377      return api
378        .patch(
379          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
380          { IPv6StaticAddresses: ipv6TableData },
381        )
382        .then(dispatch('getEthernetData'))
383        .then(() => {
384          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
385            setting: i18n.global.t('pageNetwork.ipv6'),
386          });
387        })
388        .catch((error) => {
389          console.log(error);
390          throw new Error(
391            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
392              setting: i18n.global.t('pageNetwork.ipv6'),
393            }),
394          );
395        });
396    },
397    async saveSettings({ state, dispatch }, interfaceSettingsForm) {
398      return api
399        .patch(
400          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
401          interfaceSettingsForm,
402        )
403        .then(dispatch('getEthernetData'))
404        .then(() => {
405          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
406            setting: i18n.global.t('pageNetwork.network'),
407          });
408        })
409        .catch((error) => {
410          console.log(error);
411          throw new Error(
412            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
413              setting: i18n.global.t('pageNetwork.network'),
414            }),
415          );
416        });
417    },
418    async saveDnsAddress({ dispatch, state }, dnsForm) {
419      const newAddress = dnsForm;
420      const originalAddresses =
421        state.ethernetData[state.selectedInterfaceIndex].StaticNameServers;
422      const newDnsArray = originalAddresses.concat(newAddress);
423      return api
424        .patch(
425          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
426          { StaticNameServers: newDnsArray },
427        )
428        .then(dispatch('getEthernetData'))
429        .then(() => {
430          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
431            setting: i18n.global.t('pageNetwork.dns'),
432          });
433        })
434        .catch((error) => {
435          console.log(error);
436          throw new Error(
437            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
438              setting: i18n.global.t('pageNetwork.dns'),
439            }),
440          );
441        });
442    },
443    async editDnsAddress({ dispatch, state }, dnsTableData) {
444      return api
445        .patch(
446          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
447          { StaticNameServers: dnsTableData },
448        )
449        .then(dispatch('getEthernetData'))
450        .then(() => {
451          return i18n.global.t('pageNetwork.toast.successSaveNetworkSettings', {
452            setting: i18n.global.t('pageNetwork.dns'),
453          });
454        })
455        .catch((error) => {
456          console.log(error);
457          throw new Error(
458            i18n.global.t('pageNetwork.toast.errorSaveNetworkSettings', {
459              setting: i18n.global.t('pageNetwork.dns'),
460            }),
461          );
462        });
463    },
464  },
465};
466
467export default NetworkStore;
468