Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign unused subnet automatically #418

Merged
merged 6 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
organization
  • Loading branch information
sinamics committed May 19, 2024
commit 0313f3c09d908f2f5797dd91e62d68852272f5c5
18 changes: 17 additions & 1 deletion src/server/api/routers/organizationRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { throwError } from "~/server/helpers/errorHandler";
import { sendWebhook } from "~/utils/webhook";
import { nameGeneratorConfig } from "../services/networkService";
import rateLimit from "~/utils/rateLimit";
import { RoutesEntity } from "~/types/local/network";

// Create a Zod schema for the HookType enum
const HookTypeEnum = z.enum(Object.values(HookType) as [HookType, ...HookType[]]);
Expand Down Expand Up @@ -353,8 +354,22 @@ export const organizationRouter = createTRPCRouter({
},
});

// get used IPs from the database
const usedCidr = await ctx.prisma.network.findMany({
where: {
organizationId: input.organizationId,
},
select: {
routes: true,
},
});
// Extract the target from the routes
const usedIPs = usedCidr.map((nw) =>
(nw.routes as RoutesEntity[])?.map((r) => r.target),
);

// Generate ipv4 address, cidr, start & end
const ipAssignmentPools = IPv4gen(null);
const ipAssignmentPools = IPv4gen(null, usedIPs);

if (!input?.networkName) {
// Generate adjective and noun word
Expand All @@ -378,6 +393,7 @@ export const organizationRouter = createTRPCRouter({
name: input.networkName,
nwid: newNw.nwid,
description: input.orgName,
routes: ipAssignmentPools.routes,
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions src/server/api/services/networkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export const networkProvisioningFactory = async ({ ctx, input }) => {
}
// get used IPs from the database
const usedCidr = await ctx.prisma.network.findMany({
where: {
authorId: ctx.session.user.id,
},
select: {
routes: true,
},
Expand Down
Loading