xref: /openbmc/webui-vue/src/store/modules/Settings/NetworkStore.js (revision b34349d4139230fb4ca99bf89a6b0e1f707e58e2)
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          HostName,
33          IPv4Addresses,
34          IPv4StaticAddresses,
35          LinkStatus,
36        } = data;
37        return {
38          defaultGateway: IPv4StaticAddresses[0]?.Gateway, //First static gateway is the default gateway
39          dhcpAddress: IPv4Addresses.filter(
40            (ipv4) => ipv4.AddressOrigin === 'DHCP'
41          ),
42          hostname: HostName,
43          linkStatus: LinkStatus,
44          staticAddress: IPv4StaticAddresses[0]?.Address, // Display first static address on overview page
45          useDnsEnabled: DHCPv4.UseDNSServers,
46          useDomainNameEnabled: DHCPv4.UseDomainName,
47          useNtpEnabled: DHCPv4.UseNTPServers,
48        };
49      });
50    },
51    setNtpState: (state, ntpState) => (state.ntpState = ntpState),
52    setSelectedInterfaceId: (state, selectedInterfaceId) =>
53      (state.selectedInterfaceId = selectedInterfaceId),
54    setSelectedInterfaceIndex: (state, selectedInterfaceIndex) =>
55      (state.selectedInterfaceIndex = selectedInterfaceIndex),
56  },
57  actions: {
58    async getEthernetData({ commit }) {
59      return await api
60        .get('/redfish/v1/Managers/bmc/EthernetInterfaces')
61        .then((response) =>
62          response.data.Members.map(
63            (ethernetInterface) => ethernetInterface['@odata.id']
64          )
65        )
66        .then((ethernetInterfaceIds) =>
67          api.all(
68            ethernetInterfaceIds.map((ethernetInterface) =>
69              api.get(ethernetInterface)
70            )
71          )
72        )
73        .then((ethernetInterfaces) => {
74          const ethernetData = ethernetInterfaces.map(
75            (ethernetInterface) => ethernetInterface.data
76          );
77          const firstInterfaceId = ethernetData[0].Id;
78
79          commit('setEthernetData', ethernetData);
80          commit('setFirstInterfaceId', firstInterfaceId);
81          commit('setSelectedInterfaceId', firstInterfaceId);
82          commit('setGlobalNetworkSettings', ethernetInterfaces);
83        })
84        .catch((error) => {
85          console.log('Network Data:', error);
86        });
87    },
88    async saveDomainNameState({ commit, state }, domainState) {
89      commit('setDomainNameState', domainState);
90      const data = {
91        DHCPv4: {
92          UseDomainName: domainState,
93        },
94      };
95      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
96      // on all interfaces
97      return api
98        .patch(
99          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
100          data
101        )
102        .then(() => {
103          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
104            setting: i18n.t('pageNetwork.domainName'),
105          });
106        })
107        .catch((error) => {
108          console.log(error);
109          commit('setDomainNameState', !domainState);
110          throw new Error(
111            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
112              setting: i18n.t('pageNetwork.domainName'),
113            })
114          );
115        });
116    },
117    async saveDnsState({ commit, state }, dnsState) {
118      commit('setDnsState', dnsState);
119      const data = {
120        DHCPv4: {
121          UseDNSServers: dnsState,
122        },
123      };
124      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
125      // on all interfaces
126      return api
127        .patch(
128          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
129          data
130        )
131        .then(() => {
132          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
133            setting: i18n.t('pageNetwork.dns'),
134          });
135        })
136        .catch((error) => {
137          console.log(error);
138          commit('setDnsState', !dnsState);
139          throw new Error(
140            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
141              setting: i18n.t('pageNetwork.dns'),
142            })
143          );
144        });
145    },
146    async saveNtpState({ commit, state }, ntpState) {
147      commit('setNtpState', ntpState);
148      const data = {
149        DHCPv4: {
150          UseDNSServers: ntpState,
151        },
152      };
153      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
154      // on all interfaces
155      return api
156        .patch(
157          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
158          data
159        )
160        .then(() => {
161          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
162            setting: i18n.t('pageNetwork.ntp'),
163          });
164        })
165        .catch((error) => {
166          console.log(error);
167          commit('setNtpState', !ntpState);
168          throw new Error(
169            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
170              setting: i18n.t('pageNetwork.ntp'),
171            })
172          );
173        });
174    },
175    async setSelectedTabIndex({ commit }, tabIndex) {
176      commit('setSelectedInterfaceIndex', tabIndex);
177    },
178    async setSelectedTabId({ commit }, tabId) {
179      commit('setSelectedInterfaceId', tabId);
180    },
181    async saveIpv4Address({ dispatch, state }, ipv4Form) {
182      const originalAddresses = state.ethernetData[
183        state.selectedInterfaceIndex
184      ].IPv4StaticAddresses.map((ipv4) => {
185        const { Address, SubnetMask, Gateway } = ipv4;
186        return {
187          Address,
188          SubnetMask,
189          Gateway,
190        };
191      });
192      const newAddress = [ipv4Form];
193      return api
194        .patch(
195          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
196          { IPv4StaticAddresses: originalAddresses.concat(newAddress) }
197        )
198        .then(dispatch('getEthernetData'))
199        .then(() => {
200          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
201            setting: i18n.t('pageNetwork.ipv4'),
202          });
203        })
204        .catch((error) => {
205          console.log(error);
206          throw new Error(
207            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
208              setting: i18n.t('pageNetwork.ipv4'),
209            })
210          );
211        });
212    },
213    async editIpv4Address({ dispatch, state }, ipv4TableData) {
214      return api
215        .patch(
216          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
217          { IPv4StaticAddresses: ipv4TableData }
218        )
219        .then(dispatch('getEthernetData'))
220        .then(() => {
221          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
222            setting: i18n.t('pageNetwork.ipv4'),
223          });
224        })
225        .catch((error) => {
226          console.log(error);
227          throw new Error(
228            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
229              setting: i18n.t('pageNetwork.ipv4'),
230            })
231          );
232        });
233    },
234    async saveDnsAddress({ dispatch, state }, dnsForm) {
235      const newAddress = dnsForm;
236      const originalAddresses =
237        state.ethernetData[state.selectedInterfaceIndex].StaticNameServers;
238      const newDnsArray = originalAddresses.concat(newAddress);
239      return api
240        .patch(
241          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
242          { StaticNameServers: newDnsArray }
243        )
244        .then(dispatch('getEthernetData'))
245        .then(() => {
246          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
247            setting: i18n.t('pageNetwork.dns'),
248          });
249        })
250        .catch((error) => {
251          console.log(error);
252          throw new Error(
253            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
254              setting: i18n.t('pageNetwork.dns'),
255            })
256          );
257        });
258    },
259    async editDnsAddress({ dispatch, state }, dnsTableData) {
260      return api
261        .patch(
262          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
263          { StaticNameServers: dnsTableData }
264        )
265        .then(dispatch('getEthernetData'))
266        .then(() => {
267          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
268            setting: i18n.t('pageNetwork.dns'),
269          });
270        })
271        .catch((error) => {
272          console.log(error);
273          throw new Error(
274            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
275              setting: i18n.t('pageNetwork.dns'),
276            })
277          );
278        });
279    },
280  },
281};
282
283export default NetworkStore;
284