ui(admin): add "working" React admin ui
This commit is contained in:
parent
d1c7494933
commit
5402584508
199 changed files with 13387 additions and 151 deletions
80
resources/scripts/api/admin/servers/createServer.ts
Normal file
80
resources/scripts/api/admin/servers/createServer.ts
Normal file
|
@ -0,0 +1,80 @@
|
|||
import http from '@/api/http';
|
||||
import { Server, rawDataToServer } from '@/api/admin/servers/getServers';
|
||||
|
||||
export interface CreateServerRequest {
|
||||
externalId: string;
|
||||
name: string;
|
||||
description: string | null;
|
||||
ownerId: number;
|
||||
nodeId: number;
|
||||
|
||||
limits: {
|
||||
memory: number;
|
||||
swap: number;
|
||||
disk: number;
|
||||
io: number;
|
||||
cpu: number;
|
||||
threads: string;
|
||||
oomDisabled: boolean;
|
||||
}
|
||||
|
||||
featureLimits: {
|
||||
allocations: number;
|
||||
backups: number;
|
||||
databases: number;
|
||||
};
|
||||
|
||||
allocation: {
|
||||
default: number;
|
||||
additional: number[];
|
||||
};
|
||||
|
||||
startup: string;
|
||||
environment: Record<string, any>;
|
||||
eggId: number;
|
||||
image: string;
|
||||
skipScripts: boolean;
|
||||
startOnCompletion: boolean;
|
||||
}
|
||||
|
||||
export default (r: CreateServerRequest, include: string[] = []): Promise<Server> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.post('/api/application/servers', {
|
||||
externalId: r.externalId,
|
||||
name: r.name,
|
||||
description: r.description,
|
||||
owner_id: r.ownerId,
|
||||
node_id: r.nodeId,
|
||||
|
||||
limits: {
|
||||
cpu: r.limits.cpu,
|
||||
disk: r.limits.disk,
|
||||
io: r.limits.io,
|
||||
memory: r.limits.memory,
|
||||
swap: r.limits.swap,
|
||||
threads: r.limits.threads,
|
||||
oom_killer: r.limits.oomDisabled,
|
||||
},
|
||||
|
||||
feature_limits: {
|
||||
allocations: r.featureLimits.allocations,
|
||||
backups: r.featureLimits.backups,
|
||||
databases: r.featureLimits.databases,
|
||||
},
|
||||
|
||||
allocation: {
|
||||
default: r.allocation.default,
|
||||
additional: r.allocation.additional,
|
||||
},
|
||||
|
||||
startup: r.startup,
|
||||
environment: r.environment,
|
||||
egg_id: r.eggId,
|
||||
image: r.image,
|
||||
skip_scripts: r.skipScripts,
|
||||
start_on_completion: r.startOnCompletion,
|
||||
}, { params: { include: include.join(',') } })
|
||||
.then(({ data }) => resolve(rawDataToServer(data)))
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
9
resources/scripts/api/admin/servers/deleteServer.ts
Normal file
9
resources/scripts/api/admin/servers/deleteServer.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import http from '@/api/http';
|
||||
|
||||
export default (id: number): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.delete(`/api/application/servers/${id}`)
|
||||
.then(() => resolve())
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
10
resources/scripts/api/admin/servers/getServer.ts
Normal file
10
resources/scripts/api/admin/servers/getServer.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import http from '@/api/http';
|
||||
import { Server, rawDataToServer } from '@/api/admin/servers/getServers';
|
||||
|
||||
export default (id: number, include: string[]): Promise<Server> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.get(`/api/application/servers/${id}`, { params: { include: include.join(',') } })
|
||||
.then(({ data }) => resolve(rawDataToServer(data)))
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
177
resources/scripts/api/admin/servers/getServers.ts
Normal file
177
resources/scripts/api/admin/servers/getServers.ts
Normal file
|
@ -0,0 +1,177 @@
|
|||
import { Allocation, rawDataToAllocation } from '@/api/admin/nodes/getAllocations';
|
||||
import { useContext } from 'react';
|
||||
import useSWR from 'swr';
|
||||
import { createContext } from '@/api/admin';
|
||||
import http, { FractalResponseData, FractalResponseList, getPaginationSet, PaginatedResult } from '@/api/http';
|
||||
import { Egg, rawDataToEgg } from '@/api/admin/eggs/getEgg';
|
||||
import { Node, rawDataToNode } from '@/api/admin/nodes/getNodes';
|
||||
import { Transformers, User } from '@definitions/admin';
|
||||
|
||||
export interface ServerVariable {
|
||||
id: number;
|
||||
eggId: number;
|
||||
name: string;
|
||||
description: string;
|
||||
envVariable: string;
|
||||
defaultValue: string;
|
||||
userViewable: boolean;
|
||||
userEditable: boolean;
|
||||
rules: string;
|
||||
required: boolean;
|
||||
serverValue: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export const rawDataToServerVariable = ({ attributes }: FractalResponseData): ServerVariable => ({
|
||||
id: attributes.id,
|
||||
eggId: attributes.egg_id,
|
||||
name: attributes.name,
|
||||
description: attributes.description,
|
||||
envVariable: attributes.env_variable,
|
||||
defaultValue: attributes.default_value,
|
||||
userViewable: attributes.user_viewable,
|
||||
userEditable: attributes.user_editable,
|
||||
rules: attributes.rules,
|
||||
required: attributes.required,
|
||||
serverValue: attributes.server_value,
|
||||
createdAt: new Date(attributes.created_at),
|
||||
updatedAt: new Date(attributes.updated_at),
|
||||
});
|
||||
|
||||
export interface Server {
|
||||
id: number;
|
||||
externalId: string | null
|
||||
uuid: string;
|
||||
identifier: string;
|
||||
name: string;
|
||||
description: string;
|
||||
status: string;
|
||||
|
||||
limits: {
|
||||
memory: number;
|
||||
swap: number;
|
||||
disk: number;
|
||||
io: number;
|
||||
cpu: number;
|
||||
threads: string | null;
|
||||
oomDisabled: boolean;
|
||||
}
|
||||
|
||||
featureLimits: {
|
||||
databases: number;
|
||||
allocations: number;
|
||||
backups: number;
|
||||
}
|
||||
|
||||
ownerId: number;
|
||||
nodeId: number;
|
||||
allocationId: number;
|
||||
nestId: number;
|
||||
eggId: number;
|
||||
|
||||
container: {
|
||||
startup: string;
|
||||
image: string;
|
||||
environment: Map<string, string>;
|
||||
}
|
||||
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
|
||||
relations: {
|
||||
allocations?: Allocation[];
|
||||
egg?: Egg;
|
||||
node?: Node;
|
||||
user?: User;
|
||||
variables: ServerVariable[];
|
||||
}
|
||||
}
|
||||
|
||||
export const rawDataToServer = ({ attributes }: FractalResponseData): Server => ({
|
||||
id: attributes.id,
|
||||
externalId: attributes.external_id,
|
||||
uuid: attributes.uuid,
|
||||
identifier: attributes.identifier,
|
||||
name: attributes.name,
|
||||
description: attributes.description,
|
||||
status: attributes.status,
|
||||
|
||||
limits: {
|
||||
memory: attributes.limits.memory,
|
||||
swap: attributes.limits.swap,
|
||||
disk: attributes.limits.disk,
|
||||
io: attributes.limits.io,
|
||||
cpu: attributes.limits.cpu,
|
||||
threads: attributes.limits.threads,
|
||||
oomDisabled: attributes.limits.oom_disabled,
|
||||
},
|
||||
|
||||
featureLimits: {
|
||||
databases: attributes.feature_limits.databases,
|
||||
allocations: attributes.feature_limits.allocations,
|
||||
backups: attributes.feature_limits.backups,
|
||||
},
|
||||
|
||||
ownerId: attributes.owner_id,
|
||||
nodeId: attributes.node_id,
|
||||
allocationId: attributes.allocation_id,
|
||||
nestId: attributes.nest_id,
|
||||
eggId: attributes.egg_id,
|
||||
|
||||
container: {
|
||||
startup: attributes.container.startup,
|
||||
image: attributes.container.image,
|
||||
environment: attributes.container.environment,
|
||||
},
|
||||
|
||||
createdAt: new Date(attributes.created_at),
|
||||
updatedAt: new Date(attributes.updated_at),
|
||||
|
||||
relations: {
|
||||
allocations: ((attributes.relationships?.allocations as FractalResponseList | undefined)?.data || []).map(rawDataToAllocation),
|
||||
egg: attributes.relationships?.egg?.object === 'egg' ? rawDataToEgg(attributes.relationships.egg as FractalResponseData) : undefined,
|
||||
node: attributes.relationships?.node?.object === 'node' ? rawDataToNode(attributes.relationships.node as FractalResponseData) : undefined,
|
||||
user: attributes.relationships?.user?.object === 'user' ? Transformers.toUser(attributes.relationships.user as FractalResponseData) : undefined,
|
||||
variables: ((attributes.relationships?.variables as FractalResponseList | undefined)?.data || []).map(rawDataToServerVariable),
|
||||
},
|
||||
}) as Server;
|
||||
|
||||
export interface Filters {
|
||||
id?: string;
|
||||
uuid?: string;
|
||||
name?: string;
|
||||
/* eslint-disable camelcase */
|
||||
owner_id?: string;
|
||||
node_id?: string;
|
||||
external_id?: string;
|
||||
/* eslint-enable camelcase */
|
||||
}
|
||||
|
||||
export const Context = createContext<Filters>();
|
||||
|
||||
export default (include: string[] = []) => {
|
||||
const { page, filters, sort, sortDirection } = useContext(Context);
|
||||
|
||||
const params = {};
|
||||
if (filters !== null) {
|
||||
Object.keys(filters).forEach(key => {
|
||||
// @ts-ignore
|
||||
params['filter[' + key + ']'] = filters[key];
|
||||
});
|
||||
}
|
||||
|
||||
if (sort !== null) {
|
||||
// @ts-ignore
|
||||
params.sort = (sortDirection ? '-' : '') + sort;
|
||||
}
|
||||
|
||||
return useSWR<PaginatedResult<Server>>([ 'servers', page, filters, sort, sortDirection ], async () => {
|
||||
const { data } = await http.get('/api/application/servers', { params: { include: include.join(','), page, ...params } });
|
||||
|
||||
return ({
|
||||
items: (data.data || []).map(rawDataToServer),
|
||||
pagination: getPaginationSet(data.meta.pagination),
|
||||
});
|
||||
});
|
||||
};
|
64
resources/scripts/api/admin/servers/updateServer.ts
Normal file
64
resources/scripts/api/admin/servers/updateServer.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import http from '@/api/http';
|
||||
import { Server, rawDataToServer } from '@/api/admin/servers/getServers';
|
||||
|
||||
export interface Values {
|
||||
externalId: string;
|
||||
name: string;
|
||||
ownerId: number;
|
||||
|
||||
limits: {
|
||||
memory: number;
|
||||
swap: number;
|
||||
disk: number;
|
||||
io: number;
|
||||
cpu: number;
|
||||
threads: string;
|
||||
oomDisabled: boolean;
|
||||
}
|
||||
|
||||
featureLimits: {
|
||||
allocations: number;
|
||||
backups: number;
|
||||
databases: number;
|
||||
}
|
||||
|
||||
allocationId: number;
|
||||
addAllocations: number[];
|
||||
removeAllocations: number[];
|
||||
}
|
||||
|
||||
export default (id: number, server: Partial<Values>, include: string[] = []): Promise<Server> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.patch(
|
||||
`/api/application/servers/${id}`,
|
||||
{
|
||||
external_id: server.externalId,
|
||||
name: server.name,
|
||||
owner_id: server.ownerId,
|
||||
|
||||
limits: {
|
||||
memory: server.limits?.memory,
|
||||
swap: server.limits?.swap,
|
||||
disk: server.limits?.disk,
|
||||
io: server.limits?.io,
|
||||
cpu: server.limits?.cpu,
|
||||
threads: server.limits?.threads,
|
||||
oom_killer: server.limits?.oomDisabled,
|
||||
},
|
||||
|
||||
feature_limits: {
|
||||
allocations: server.featureLimits?.allocations,
|
||||
backups: server.featureLimits?.backups,
|
||||
databases: server.featureLimits?.databases,
|
||||
},
|
||||
|
||||
allocation_id: server.allocationId,
|
||||
add_allocations: server.addAllocations,
|
||||
remove_allocations: server.removeAllocations,
|
||||
},
|
||||
{ params: { include: include.join(',') } }
|
||||
)
|
||||
.then(({ data }) => resolve(rawDataToServer(data)))
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
28
resources/scripts/api/admin/servers/updateServerStartup.ts
Normal file
28
resources/scripts/api/admin/servers/updateServerStartup.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import http from '@/api/http';
|
||||
import { Server, rawDataToServer } from '@/api/admin/servers/getServers';
|
||||
|
||||
export interface Values {
|
||||
startup: string;
|
||||
environment: Record<string, any>;
|
||||
eggId: number;
|
||||
image: string;
|
||||
skipScripts: boolean;
|
||||
}
|
||||
|
||||
export default (id: number, values: Partial<Values>, include: string[] = []): Promise<Server> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.patch(
|
||||
`/api/application/servers/${id}/startup`,
|
||||
{
|
||||
startup: values.startup !== '' ? values.startup : null,
|
||||
environment: values.environment,
|
||||
egg_id: values.eggId,
|
||||
image: values.image,
|
||||
skip_scripts: values.skipScripts,
|
||||
},
|
||||
{ params: { include: include.join(',') } }
|
||||
)
|
||||
.then(({ data }) => resolve(rawDataToServer(data)))
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue