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.global.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(`${await this.dispatch('global/getBmcPath')}/VirtualMedia`)
53        .then((response) =>
54          response.data.Members.map(
55            (virtualMedia) => virtualMedia['@odata.id'],
56          ),
57        )
58        .then((devices) => api.all(devices.map((device) => api.get(device))))
59        .then((devices) => {
60          const deviceData = devices.map((device) => {
61            const isActive = device.data?.Inserted === true ? true : false;
62            return {
63              id: device.data?.Id,
64              transferProtocolType: device.data?.TransferProtocolType,
65              websocket: device.data?.Oem?.OpenBMC?.WebSocketEndpoint,
66              isActive: isActive,
67            };
68          });
69          const proxyDevices = deviceData
70            .filter((d) => d.transferProtocolType === transferProtocolType.OEM)
71            .map((device) => {
72              return {
73                ...device,
74                file: null,
75              };
76            });
77          const legacyDevices = deviceData
78            .filter((d) => d.transferProtocolType !== transferProtocolType.OEM)
79            .map((device) => {
80              return {
81                ...device,
82                serverUri: '',
83                username: '',
84                password: '',
85                isRW: false,
86              };
87            });
88          commit('setProxyDevicesData', proxyDevices);
89          commit('setLegacyDevicesData', legacyDevices);
90        })
91        .catch((error) => {
92          console.log('Virtual Media:', error);
93        });
94    },
95    async mountImage(_, { id, data }) {
96      return await api
97        .post(
98          `${await this.dispatch('global/getBmcPath')}/VirtualMedia/${id}/Actions/VirtualMedia.InsertMedia`,
99          data,
100        )
101        .catch((error) => {
102          console.log('Mount image:', error);
103          throw new Error();
104        });
105    },
106    async unmountImage(_, id) {
107      return await api
108        .post(
109          `${await this.dispatch('global/getBmcPath')}/VirtualMedia/${id}/Actions/VirtualMedia.EjectMedia`,
110        )
111        .catch((error) => {
112          console.log('Unmount image:', error);
113          throw new Error();
114        });
115    },
116  },
117};
118
119export default VirtualMediaStore;
120