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