2022-12-16 02:06:14 +00:00
|
|
|
import http, { FractalResponseData } from '@/api/http';
|
|
|
|
import { rawDataToServer, Server } from '@/api/admin/servers/getServers';
|
|
|
|
|
|
|
|
export interface Allocation {
|
|
|
|
id: number;
|
|
|
|
ip: string;
|
|
|
|
port: number;
|
|
|
|
alias: string | null;
|
|
|
|
serverId: number | null;
|
|
|
|
assigned: boolean;
|
|
|
|
|
|
|
|
relations: {
|
|
|
|
server?: Server;
|
2023-01-12 19:31:47 +00:00
|
|
|
};
|
2022-12-16 02:06:14 +00:00
|
|
|
|
2023-01-12 19:31:47 +00:00
|
|
|
getDisplayText(): string;
|
2022-12-16 02:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const rawDataToAllocation = ({ attributes }: FractalResponseData): Allocation => ({
|
|
|
|
id: attributes.id,
|
|
|
|
ip: attributes.ip,
|
|
|
|
port: attributes.port,
|
|
|
|
alias: attributes.alias || null,
|
|
|
|
serverId: attributes.server_id,
|
|
|
|
assigned: attributes.assigned,
|
|
|
|
|
|
|
|
relations: {
|
2023-01-12 19:31:47 +00:00
|
|
|
server:
|
|
|
|
attributes.relationships?.server?.object === 'server'
|
|
|
|
? rawDataToServer(attributes.relationships.server as FractalResponseData)
|
|
|
|
: undefined,
|
2022-12-16 02:06:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// TODO: If IP is an IPv6, wrap IP in [].
|
2023-01-12 19:31:47 +00:00
|
|
|
getDisplayText(): string {
|
2022-12-16 02:06:14 +00:00
|
|
|
if (attributes.alias !== null) {
|
|
|
|
return `${attributes.ip}:${attributes.port} (${attributes.alias})`;
|
|
|
|
}
|
|
|
|
return `${attributes.ip}:${attributes.port}`;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export interface Filters {
|
2023-01-12 19:31:47 +00:00
|
|
|
ip?: string;
|
2022-12-16 02:06:14 +00:00
|
|
|
/* eslint-disable camelcase */
|
|
|
|
server_id?: string;
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
}
|
|
|
|
|
|
|
|
export default (id: string | number, filters: Filters = {}, include: string[] = []): Promise<Allocation[]> => {
|
|
|
|
const params = {};
|
|
|
|
if (filters !== null) {
|
|
|
|
Object.keys(filters).forEach(key => {
|
|
|
|
// @ts-ignore
|
|
|
|
params['filter[' + key + ']'] = filters[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
http.get(`/api/application/nodes/${id}/allocations`, { params: { include: include.join(','), ...params } })
|
|
|
|
.then(({ data }) => resolve((data.data || []).map(rawDataToAllocation)))
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
};
|