| 1 | import { randomUUID } from "crypto"; |
| 2 | import { and, count |
| 3 | import { z } |
| 4 | import { ORPCError } |
| 5 | import { db } |
| 6 | import { nodeAllocations } |
| 7 | import { recordActivity } |
| 8 | import { adminProcedure } |
| 9 | |
| 10 | export const allocationsRouter = |
| 11 | listByNode: adminProcedure |
| 12 | .input(z.object |
| 13 | .handler(async ( |
| 14 | const pageSize = |
| 15 | const offset = |
| 16 | const where = |
| 17 | |
| 18 | const [allocations, |
| 19 | db.query.nodeAllocations |
| 20 | where, |
| 21 | limit: pageSize, |
| 22 | offset, |
| 23 | orderBy: (a, |
| 24 | }), |
| 25 | db.select({ |
| 26 | ]); |
| 27 | |
| 28 | return { allocations, |
| 29 | }), |
| 30 | |
| 31 | search: adminProcedure |
| 32 | .input(z.object |
| 33 | .handler(async ( |
| 34 | const q = |
| 35 | return db.query |
| 36 | where: and( |
| 37 | eq(nodeAllocations.nodeId |
| 38 | isNull(nodeAllocations.serverId |
| 39 | q ? or( |
| 40 | ), |
| 41 | limit: input.limit |
| 42 | orderBy: (a, |
| 43 | }); |
| 44 | }), |
| 45 | |
| 46 | create: adminProcedure |
| 47 | .input( |
| 48 | z.object({ |
| 49 | nodeId: z.string |
| 50 | ip: z.string |
| 51 | ipAlias: z.string |
| 52 | ports: z.string |
| 53 | }), |
| 54 | ) |
| 55 | .handler(async ( |
| 56 | const ports = |
| 57 | if (ports.length |
| 58 | throw new ORPCError( |
| 59 | } |
| 60 | |
| 61 | const rows = |
| 62 | id: randomUUID(), |
| 63 | nodeId: input.nodeId |
| 64 | ip: input.ip |
| 65 | ipAlias: input.ipAlias |
| 66 | port, |
| 67 | })); |
| 68 | |
| 69 | await db.insert |
| 70 | recordActivity({ eventType: |
| 71 | return { created: |
| 72 | }), |
| 73 | |
| 74 | delete: adminProcedure |
| 75 | .input(z.object |
| 76 | .handler(async ( |
| 77 | const allocation = |
| 78 | where: eq(nodeAllocations |
| 79 | }); |
| 80 | if (!allocation) |
| 81 | if (allocation.serverId |
| 82 | throw new ORPCError( |
| 83 | message: "Cannot delete an allocation assigned to a server", |
| 84 | }); |
| 85 | } |
| 86 | await db.delete |
| 87 | recordActivity({ eventType: |
| 88 | }), |
| 89 | }; |
| 90 | |
| 91 | function expandPortRange(ports: |
| 92 | const result: |
| 93 | for (const part |
| 94 | const trimmed = |
| 95 | const rangeParts = |
| 96 | if (rangeParts.length |
| 97 | const start = |
| 98 | const end = |
| 99 | if (!isNaN(start |
| 100 | for (let p |
| 101 | result.push(p |
| 102 | } |
| 103 | } |
| 104 | } else { |
| 105 | const port = |
| 106 | if (!isNaN(port |
| 107 | result.push(port |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return result; |
| 112 | } |