2024-12-26 12:19:48 +01:00
|
|
|
|
|
|
|
|
import { fetcher } from 'pankow';
|
2025-03-03 11:22:56 +01:00
|
|
|
import { API_ORIGIN } from '../constants.js';
|
2024-12-26 12:19:48 +01:00
|
|
|
|
2025-05-03 10:30:49 +02:00
|
|
|
const mountTypes = [
|
|
|
|
|
{ name: 'CIFS', value: 'cifs' },
|
|
|
|
|
{ name: 'EXT4', value: 'ext4' },
|
|
|
|
|
{ name: 'Filesystem', value: 'filesystem' },
|
|
|
|
|
{ name: 'Filesystem (Mountpoint)', value: 'mountpoint' },
|
|
|
|
|
{ name: 'NFS', value: 'nfs' },
|
|
|
|
|
{ name: 'SSHFS', value: 'sshfs' },
|
|
|
|
|
{ name: 'XFS', value: 'xfs' },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function filterConfigForMountType(mountType, config) {
|
|
|
|
|
let props = [];
|
|
|
|
|
switch (mountType) {
|
|
|
|
|
case 'filesystem':
|
|
|
|
|
props = ['hostPath'];
|
|
|
|
|
break;
|
|
|
|
|
case 'mountpoint':
|
|
|
|
|
props = ['hostPath'];
|
|
|
|
|
break;
|
|
|
|
|
case 'ext4':
|
|
|
|
|
props = ['diskPath'];
|
|
|
|
|
break;
|
|
|
|
|
case 'xfs':
|
|
|
|
|
props = ['diskPath'];
|
|
|
|
|
break;
|
|
|
|
|
case 'cifs':
|
|
|
|
|
props = ['host', 'seal', 'remoteDir', 'username', 'password'];
|
|
|
|
|
break;
|
|
|
|
|
case 'nfs':
|
|
|
|
|
props = ['host', 'remoteDir'];
|
|
|
|
|
break;
|
|
|
|
|
case 'sshfs':
|
|
|
|
|
props = ['host', 'port', 'remoteDir', 'user', 'privateKey'];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ret = {};
|
|
|
|
|
for (const p of props) ret[p] = config[p];
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-31 21:02:48 +01:00
|
|
|
function create() {
|
|
|
|
|
const accessToken = localStorage.token;
|
|
|
|
|
|
2024-12-26 12:19:48 +01:00
|
|
|
return {
|
|
|
|
|
async list() {
|
2025-02-17 16:38:30 +01:00
|
|
|
let result;
|
2024-12-26 12:19:48 +01:00
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.get(`${API_ORIGIN}/api/v1/volumes`, { access_token: accessToken });
|
2024-12-26 12:19:48 +01:00
|
|
|
} catch (e) {
|
2025-02-17 16:38:30 +01:00
|
|
|
return [e];
|
2024-12-26 12:19:48 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-17 16:38:30 +01:00
|
|
|
if (result.status !== 200) return [result];
|
2024-12-26 12:19:48 +01:00
|
|
|
|
2025-02-17 16:38:30 +01:00
|
|
|
return [null, result.body.volumes];
|
2024-12-26 12:19:48 +01:00
|
|
|
},
|
|
|
|
|
async getStatus(id) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.get(`${API_ORIGIN}/api/v1/volumes/${id}/status`, { access_token: accessToken });
|
2024-12-26 12:19:48 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) {
|
2024-12-27 22:25:00 +01:00
|
|
|
console.error('Failed to get volume status.', error, result);
|
2024-12-26 12:19:48 +01:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.body;
|
2024-12-27 22:25:00 +01:00
|
|
|
},
|
|
|
|
|
async remount(id) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/volumes/${id}/remount`, {}, { access_token: accessToken });
|
2024-12-27 22:25:00 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 202) console.error('Failed to remount volume.', error, result);
|
|
|
|
|
},
|
|
|
|
|
async add(name, mountType, mountOptions) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-05-03 10:30:49 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/volumes`, { name, mountType, mountOptions: filterConfigForMountType(mountType, mountOptions) }, { access_token: accessToken });
|
2024-12-27 22:25:00 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 201) throw error || result;
|
|
|
|
|
},
|
2025-05-03 10:30:49 +02:00
|
|
|
async update(id, mountType, mountOptions) {
|
2024-12-27 22:25:00 +01:00
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-05-03 10:30:49 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/volumes/${id}`, { mountOptions: filterConfigForMountType(mountType, mountOptions) }, { access_token: accessToken });
|
2024-12-27 22:25:00 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 204) throw error || result;
|
|
|
|
|
},
|
|
|
|
|
async remove(id) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-03-14 18:48:13 +01:00
|
|
|
result = await fetcher.del(`${API_ORIGIN}/api/v1/volumes/${id}`, null, { access_token: accessToken });
|
2024-12-27 22:25:00 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 204) throw error || result;
|
2025-01-03 00:24:36 +01:00
|
|
|
},
|
|
|
|
|
async getBlockDevices() {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.get(`${API_ORIGIN}/api/v1/system/block_devices`, { access_token: accessToken });
|
2025-01-03 00:24:36 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) {
|
|
|
|
|
console.error('Failed to get block devices.', error, result);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.body.devices;
|
|
|
|
|
},
|
2024-12-26 12:19:48 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
2025-05-03 10:30:49 +02:00
|
|
|
create,
|
|
|
|
|
mountTypes,
|
2024-12-26 12:19:48 +01:00
|
|
|
};
|