summaryrefslogtreecommitdiff
path: root/src/store/modules/Settings/NetworkStore.js
blob: 4040658a6a5a820a25fa16e13f9ce64b0bbf7cfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import api from '@/store/api';
import i18n from '@/i18n';
import { find, remove } from 'lodash';

const NetworkStore = {
  namespaced: true,
  state: {
    defaultGateway: '',
    ethernetData: [],
    interfaceOptions: [],
  },
  getters: {
    defaultGateway: (state) => state.defaultGateway,
    ethernetData: (state) => state.ethernetData,
    interfaceOptions: (state) => state.interfaceOptions,
  },
  mutations: {
    setDefaultGateway: (state, defaultGateway) =>
      (state.defaultGateway = defaultGateway),
    setEthernetData: (state, ethernetData) =>
      (state.ethernetData = ethernetData),
    setInterfaceOptions: (state, interfaceOptions) =>
      (state.interfaceOptions = interfaceOptions),
  },
  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 interfaceOptions = ethernetInterfaces.map(
            (ethernetName) => ethernetName.data.Id
          );
          const addresses = ethernetData[0].IPv4StaticAddresses;

          // Default gateway manually set to first gateway saved on the first interface. Default gateway property is WIP on backend
          const defaultGateway = addresses.map((ipv4) => {
            return ipv4.Gateway;
          });

          commit('setDefaultGateway', defaultGateway[0]);
          commit('setEthernetData', ethernetData);
          commit('setInterfaceOptions', interfaceOptions);
        })
        .catch((error) => {
          console.log('Network Data:', error);
        });
    },

    async updateInterfaceSettings({ dispatch, state }, networkSettingsForm) {
      const updatedAddresses = networkSettingsForm.staticIpv4;
      const originalAddresses =
        state.ethernetData[networkSettingsForm.selectedInterfaceIndex]
          .IPv4StaticAddresses;

      const addressArray = originalAddresses.map((item) => {
        const address = item.Address;
        if (find(updatedAddresses, { Address: address })) {
          remove(updatedAddresses, (item) => {
            return item.Address === address;
          });
          return {};
        } else {
          return null;
        }
      });

      const data = {
        HostName: networkSettingsForm.hostname,
        MACAddress: networkSettingsForm.macAddress,
        DHCPv4: {
          DHCPEnabled: networkSettingsForm.isDhcpEnabled,
        },
      };

      // If DHCP disabled, update static DNS or static ipv4
      if (!networkSettingsForm.isDhcpEnabled) {
        data.IPv4StaticAddresses = [...addressArray, ...updatedAddresses];
        data.StaticNameServers = networkSettingsForm.staticNameServers;
      }

      return await api
        .patch(
          `/redfish/v1/Managers/bmc/EthernetInterfaces/${networkSettingsForm.interfaceId}`,
          data
        )
        .then(() => dispatch('getEthernetData'))
        .then(() => {
          return i18n.t('pageNetwork.toast.successSaveNetworkSettings');
        })
        .catch((error) => {
          console.log(error);
          throw new Error(i18n.t('pageNetwork.toast.errorSaveNetworkSettings'));
        });
    },
  },
};

export default NetworkStore;