1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const transferProtocolType = {
5  CIFS: 'CIFS',
6  FTP: 'FTP',
7  SFTP: 'SFTP',
8  HTTP: 'HTTP',
9  HTTPS: 'HTTPS',
10  NFS: 'NFS',
11  SCP: 'SCP',
12  TFTP: 'TFTP',
13  OEM: 'OEM',
14};
15
16const VirtualMediaStore = {
17  namespaced: true,
18  state: {
19    proxyDevices: [],
20    legacyDevices: [],
21    connections: [],
22  },
23  getters: {
24    proxyDevices: (state) => state.proxyDevices,
25    legacyDevices: (state) => state.legacyDevices,
26  },
27  mutations: {
28    setProxyDevicesData: (state, deviceData) =>
29      (state.proxyDevices = deviceData),
30    setLegacyDevicesData: (state, deviceData) =>
31      (state.legacyDevices = deviceData),
32  },
33  actions: {
34    async getData({ commit }) {
35      const virtualMediaListEnabled =
36        process.env.VUE_APP_VIRTUAL_MEDIA_LIST_ENABLED === 'true'
37          ? true
38          : false;
39      if (!virtualMediaListEnabled) {
40        const device = {
41          id: i18n.t('pageVirtualMedia.defaultDeviceName'),
42          websocket: '/vm/0/0',
43          file: null,
44          transferProtocolType: transferProtocolType.OEM,
45          isActive: false,
46        };
47        commit('setProxyDevicesData', [device]);
48        return;
49      }
50
51      return await api
52        .get('/redfish/v1/Managers/bmc/VirtualMedia')
53        .then((response) =>
54          response.data.Members.map((virtualMedia) => virtualMedia['@odata.id'])
55        )
56        .then((devices) => api.all(devices.map((device) => api.get(device))))
57        .then((devices) => {
58          const deviceData = devices.map((device) => {
59            const isActive = device.data?.Inserted === true ? true : false;
60            return {
61              id: device.data?.Id,
62              transferProtocolType: device.data?.TransferProtocolType,
63              websocket: device.data?.Oem?.OpenBMC?.WebSocketEndpoint,
64              isActive: isActive,
65            };
66          });
67          const proxyDevices = deviceData
68            .filter((d) => d.transferProtocolType === transferProtocolType.OEM)
69            .map((device) => {
70              return {
71                ...device,
72                file: null,
73              };
74            });
75          const legacyDevices = deviceData
76            .filter((d) => d.transferProtocolType !== transferProtocolType.OEM)
77            .map((device) => {
78              return {
79                ...device,
80                serverUri: '',
81                username: '',
82                password: '',
83                isRW: false,
84              };
85            });
86          commit('setProxyDevicesData', proxyDevices);
87          commit('setLegacyDevicesData', legacyDevices);
88        })
89        .catch((error) => {
90          console.log('Virtual Media:', error);
91        });
92    },
93    async mountImage(_, { id, data }) {
94      return await api
95        .post(
96          `/redfish/v1/Managers/bmc/VirtualMedia/${id}/Actions/VirtualMedia.InsertMedia`,
97          data
98        )
99        .catch((error) => {
100          console.log('Mount image:', error);
101          throw new Error();
102        });
103    },
104    async unmountImage(_, id) {
105      return await api
106        .post(
107          `/redfish/v1/Managers/bmc/VirtualMedia/${id}/Actions/VirtualMedia.EjectMedia`
108        )
109        .catch((error) => {
110          console.log('Unmount image:', error);
111          throw new Error();
112        });
113    },
114  },
115};
116
117export default VirtualMediaStore;
118