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
if (isAjax(ctx)) {
const res = await getInstancesByUuid(uuid, toBoolean(advanced) || false);
const res = await getInstancesByUuid(uuid, undefined, toBoolean(advanced) || false);
res.token = getToken(ctx);
ctx.body = res;
}

View File

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

View File

@ -115,7 +115,7 @@ export async function buyOrRenewInstance(
if (request_action === RequestAction.BUY) {
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(
"instance/new",
payload
@ -189,10 +189,11 @@ export async function queryInstanceByUserId(
params: Record<string, any>
): Promise<InstanceInfoProtocol[]> {
const name = parseUserName(params.username) || "";
const targetDaemonId = toText(params.node_id) ?? undefined;
const user = user_service.getUserByUserName(name);
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) => {
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);
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) {
const instances = user.instances;
for (const iterator of instances) {
if (targetDaemonId && targetDaemonId !== iterator.daemonId) continue;
const remoteService = RemoteServiceSubsystem.getInstance(iterator.daemonId);
// If the remote service doesn't exist at all, load a deleted prompt
if (!remoteService) {
// If the remote service doesn't exist at all, load a deleted prompt
resInstances.push({
hostIp: "-- Unknown --",
instanceUuid: iterator.instanceUuid,