mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-11 22:45:18 +00:00
Merge pull request #3800 from Dokploy/3765-missing-mounts-endpoint-for-managed-database-services
feat: enhance mount API to support service type filtering and organiz…
This commit is contained in:
@@ -1,22 +1,70 @@
|
|||||||
import {
|
import {
|
||||||
|
checkServiceAccess,
|
||||||
createMount,
|
createMount,
|
||||||
deleteMount,
|
deleteMount,
|
||||||
findApplicationById,
|
findApplicationById,
|
||||||
|
findComposeById,
|
||||||
|
findMariadbById,
|
||||||
|
findMongoById,
|
||||||
findMountById,
|
findMountById,
|
||||||
findMountOrganizationId,
|
findMountOrganizationId,
|
||||||
|
findMountsByApplicationId,
|
||||||
|
findMySqlById,
|
||||||
|
findPostgresById,
|
||||||
|
findRedisById,
|
||||||
getServiceContainer,
|
getServiceContainer,
|
||||||
updateMount,
|
updateMount,
|
||||||
} from "@dokploy/server";
|
} from "@dokploy/server";
|
||||||
|
import type { ServiceType } from "@dokploy/server/db/schema/mount";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import {
|
import {
|
||||||
apiCreateMount,
|
apiCreateMount,
|
||||||
|
apiFindMountByApplicationId,
|
||||||
apiFindOneMount,
|
apiFindOneMount,
|
||||||
apiRemoveMount,
|
apiRemoveMount,
|
||||||
apiUpdateMount,
|
apiUpdateMount,
|
||||||
} from "@/server/db/schema";
|
} from "@/server/db/schema";
|
||||||
import { createTRPCRouter, protectedProcedure } from "../trpc";
|
import { createTRPCRouter, protectedProcedure } from "../trpc";
|
||||||
|
|
||||||
|
async function getServiceOrganizationId(
|
||||||
|
serviceId: string,
|
||||||
|
serviceType: ServiceType,
|
||||||
|
): Promise<string | null> {
|
||||||
|
switch (serviceType) {
|
||||||
|
case "application": {
|
||||||
|
const app = await findApplicationById(serviceId);
|
||||||
|
return app?.environment?.project?.organizationId ?? null;
|
||||||
|
}
|
||||||
|
case "postgres": {
|
||||||
|
const postgres = await findPostgresById(serviceId);
|
||||||
|
return postgres?.environment?.project?.organizationId ?? null;
|
||||||
|
}
|
||||||
|
case "mariadb": {
|
||||||
|
const mariadb = await findMariadbById(serviceId);
|
||||||
|
return mariadb?.environment?.project?.organizationId ?? null;
|
||||||
|
}
|
||||||
|
case "mongo": {
|
||||||
|
const mongo = await findMongoById(serviceId);
|
||||||
|
return mongo?.environment?.project?.organizationId ?? null;
|
||||||
|
}
|
||||||
|
case "mysql": {
|
||||||
|
const mysql = await findMySqlById(serviceId);
|
||||||
|
return mysql?.environment?.project?.organizationId ?? null;
|
||||||
|
}
|
||||||
|
case "redis": {
|
||||||
|
const redis = await findRedisById(serviceId);
|
||||||
|
return redis?.environment?.project?.organizationId ?? null;
|
||||||
|
}
|
||||||
|
case "compose": {
|
||||||
|
const compose = await findComposeById(serviceId);
|
||||||
|
return compose?.environment?.project?.organizationId ?? null;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const mountRouter = createTRPCRouter({
|
export const mountRouter = createTRPCRouter({
|
||||||
create: protectedProcedure
|
create: protectedProcedure
|
||||||
.input(apiCreateMount)
|
.input(apiCreateMount)
|
||||||
@@ -71,4 +119,35 @@ export const mountRouter = createTRPCRouter({
|
|||||||
);
|
);
|
||||||
return mounts;
|
return mounts;
|
||||||
}),
|
}),
|
||||||
|
listByServiceId: protectedProcedure
|
||||||
|
.input(apiFindMountByApplicationId)
|
||||||
|
.query(async ({ input, ctx }) => {
|
||||||
|
console.log("input", input);
|
||||||
|
if (ctx.user.role === "member") {
|
||||||
|
await checkServiceAccess(
|
||||||
|
ctx.user.id,
|
||||||
|
input.serviceId,
|
||||||
|
ctx.session.activeOrganizationId,
|
||||||
|
"access",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const organizationId = await getServiceOrganizationId(
|
||||||
|
input.serviceId,
|
||||||
|
input.serviceType,
|
||||||
|
);
|
||||||
|
if (
|
||||||
|
organizationId === null ||
|
||||||
|
organizationId !== ctx.session.activeOrganizationId
|
||||||
|
) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message:
|
||||||
|
"You are not authorized to access this service or it does not exist",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return await findMountsByApplicationId(
|
||||||
|
input.serviceId,
|
||||||
|
input.serviceType,
|
||||||
|
);
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -146,12 +146,20 @@ export const apiRemoveMount = createSchema
|
|||||||
export const apiFindMountByApplicationId = createSchema
|
export const apiFindMountByApplicationId = createSchema
|
||||||
.extend({
|
.extend({
|
||||||
serviceId: z.string().min(1),
|
serviceId: z.string().min(1),
|
||||||
|
serviceType: z.enum([
|
||||||
|
"application",
|
||||||
|
"postgres",
|
||||||
|
"mysql",
|
||||||
|
"mariadb",
|
||||||
|
"mongo",
|
||||||
|
"redis",
|
||||||
|
"compose",
|
||||||
|
]),
|
||||||
})
|
})
|
||||||
.pick({
|
.pick({
|
||||||
serviceId: true,
|
serviceId: true,
|
||||||
serviceType: true,
|
serviceType: true,
|
||||||
})
|
});
|
||||||
.required();
|
|
||||||
|
|
||||||
export const apiUpdateMount = createSchema.partial().extend({
|
export const apiUpdateMount = createSchema.partial().extend({
|
||||||
mountId: z.string().min(1),
|
mountId: z.string().min(1),
|
||||||
|
|||||||
@@ -263,6 +263,9 @@ export const findMountsByApplicationId = async (
|
|||||||
case "redis":
|
case "redis":
|
||||||
sqlChunks.push(eq(mounts.redisId, serviceId));
|
sqlChunks.push(eq(mounts.redisId, serviceId));
|
||||||
break;
|
break;
|
||||||
|
case "compose":
|
||||||
|
sqlChunks.push(eq(mounts.composeId, serviceId));
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unknown service type: ${serviceType}`);
|
throw new Error(`Unknown service type: ${serviceType}`);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user