feat: getInstancesByUuid() support daemon id

This commit is contained in:
Unitwk 2024-08-23 17:50:12 +08:00
parent 8b79853c47
commit 98a7097fda
4 changed files with 14 additions and 5 deletions

View File

@ -42,7 +42,7 @@ router.get("/", permission({ level: ROLE.USER, token: false, speedLimit: false }
// Some and only Ajax requests grant access // Some and only Ajax requests grant access
if (isAjax(ctx)) { if (isAjax(ctx)) {
const res = await getInstancesByUuid(uuid, toBoolean(advanced) || false); const res = await getInstancesByUuid(uuid, undefined, toBoolean(advanced) || false);
res.token = getToken(ctx); res.token = getToken(ctx);
ctx.body = res; ctx.body = res;
} }

View File

@ -9,6 +9,7 @@ import {
RequestAction RequestAction
} from "../service/exchange_service"; } from "../service/exchange_service";
import { toText } from "common"; import { toText } from "common";
import { logger } from "../service/log";
const router = new Router({ prefix: "/exchange" }); const router = new Router({ prefix: "/exchange" });
@ -24,6 +25,7 @@ router.post(
try { try {
const requestAction = toText(ctx.request.body.request_action) as RequestAction; const requestAction = toText(ctx.request.body.request_action) as RequestAction;
const params = ctx.request.body.data ?? {}; const params = ctx.request.body.data ?? {};
logger.info("Get exchange request, action: %s, params: %j", requestAction, params);
if (requestAction === RequestAction.PING) { if (requestAction === RequestAction.PING) {
ctx.body = await getNodeStatus(params); ctx.body = await getNodeStatus(params);
return; return;
@ -37,6 +39,7 @@ router.post(
return; return;
} }
} catch (err) { } catch (err) {
logger.error("Exchange request error: " + err);
ctx.body = err; ctx.body = err;
} }
} }

View File

@ -115,7 +115,7 @@ export async function buyOrRenewInstance(
if (request_action === RequestAction.BUY) { if (request_action === RequestAction.BUY) {
payload.endTime = (payload.endTime ? payload.endTime : Date.now()) + hours * 3600 * 1000; payload.endTime = (payload.endTime ? payload.endTime : Date.now()) + hours * 3600 * 1000;
payload.nickname = payload.nickname + "-" + getNanoId(6); payload.nickname = username + "-" + getNanoId(6);
const { instanceUuid: newInstanceId, config: newInstanceConfig } = await remoteRequest.request( const { instanceUuid: newInstanceId, config: newInstanceConfig } = await remoteRequest.request(
"instance/new", "instance/new",
payload payload
@ -189,10 +189,11 @@ export async function queryInstanceByUserId(
params: Record<string, any> params: Record<string, any>
): Promise<InstanceInfoProtocol[]> { ): Promise<InstanceInfoProtocol[]> {
const name = parseUserName(params.username) || ""; const name = parseUserName(params.username) || "";
const targetDaemonId = toText(params.node_id) ?? undefined;
const user = user_service.getUserByUserName(name); const user = user_service.getUserByUserName(name);
if (!user) throw new Error(t("TXT_CODE_903b6c50")); if (!user) throw new Error(t("TXT_CODE_903b6c50"));
const { instances = [] } = await getInstancesByUuid(user.uuid, true); const { instances = [] } = await getInstancesByUuid(user.uuid, targetDaemonId, true);
const newInstancesInfo = instances.map((v) => { const newInstancesInfo = instances.map((v) => {
return formatInstanceData(v); return formatInstanceData(v);
}); });

View File

@ -60,7 +60,11 @@ export function multiOperationForwarding(
} }
} }
export async function getInstancesByUuid(uuid: string, advanced: boolean = false) { export async function getInstancesByUuid(
uuid: string,
targetDaemonId?: string,
advanced: boolean = false
) {
const user = userSystem.getInstance(uuid); const user = userSystem.getInstance(uuid);
if (!user) throw new Error("The UID does not exist"); if (!user) throw new Error("The UID does not exist");
@ -69,9 +73,10 @@ export async function getInstancesByUuid(uuid: string, advanced: boolean = false
if (advanced) { if (advanced) {
const instances = user.instances; const instances = user.instances;
for (const iterator of instances) { for (const iterator of instances) {
if (targetDaemonId && targetDaemonId !== iterator.daemonId) continue;
const remoteService = RemoteServiceSubsystem.getInstance(iterator.daemonId); const remoteService = RemoteServiceSubsystem.getInstance(iterator.daemonId);
// If the remote service doesn't exist at all, load a deleted prompt
if (!remoteService) { if (!remoteService) {
// If the remote service doesn't exist at all, load a deleted prompt
resInstances.push({ resInstances.push({
hostIp: "-- Unknown --", hostIp: "-- Unknown --",
instanceUuid: iterator.instanceUuid, instanceUuid: iterator.instanceUuid,