Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions apps/nginx-strangler/conf.d/routing.conf
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ server {
proxy_set_header X-Forwarded-Proto $scheme;
}

# Les routes /api/v2 n'existent que sur server-nestjs
location /api/v2/ {
proxy_pass http://server-nestjs;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

# ── Routes par défaut (pour l'instant dans le legacy) ─────────────────────────
location /api/ {
proxy_pass http://server-legacy;
Expand Down
2 changes: 2 additions & 0 deletions apps/server-nestjs/src/main.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common'
import { ScheduleModule } from '@nestjs/schedule'
import { DeploymentModule } from './modules/deployment/deployment.module'
import { EnvironmentModule } from './modules/environment/environment.module'
import { HealthzModule } from './modules/healthz/healthz.module'
import { KeycloakModule } from './modules/keycloak/keycloak.module'
import { LogModule } from './modules/log/log.module'
Expand Down Expand Up @@ -31,6 +32,7 @@ import { VersionModule } from './modules/version/version.module'
ProjectRolesModule,
LogModule,
DeploymentModule,
EnvironmentModule,
VersionModule,
],
controllers: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import type { TestingModule } from '@nestjs/testing'
import type { DeepMockProxy } from 'vitest-mock-extended'
import { Test } from '@nestjs/testing'
import { beforeEach, describe, expect, it } from 'vitest'
import { mockDeep } from 'vitest-mock-extended'
import { PrismaService } from '../infrastructure/database/prisma.service'
import { EnvironmentDatastoreService } from './environment-datastore.service'
import { makeCluster, makeEnvironment, makeStage } from './environment-testing.utils'

describe('environmentDatastoreService', () => {
let module: TestingModule
let service: EnvironmentDatastoreService
let prisma: DeepMockProxy<PrismaService>

beforeEach(async () => {
prisma = mockDeep<PrismaService>()

module = await Test.createTestingModule({
providers: [
EnvironmentDatastoreService,
{ provide: PrismaService, useValue: prisma },
],
}).compile()

service = module.get(EnvironmentDatastoreService)
})

describe('getEnvironmentById', () => {
it('should return an environment with its cluster', async () => {
const environment = makeEnvironment({ id: 'env1' })
prisma.environment.findUnique.mockResolvedValue(environment)

const result = await service.getEnvironmentById('env1')

expect(prisma.environment.findUnique).toHaveBeenCalledWith({
where: { id: 'env1' },
include: { cluster: true },
})
expect(result).toEqual(environment)
})
})

describe('getEnvironmentsByProjectId', () => {
it('should return environments for a project with their stage', async () => {
const environments = [makeEnvironment({ projectId: 'project1' })]
prisma.environment.findMany.mockResolvedValue(environments)

const result = await service.getEnvironmentsByProjectId('project1')

expect(prisma.environment.findMany).toHaveBeenCalledWith({
where: { projectId: 'project1' },
include: { stage: true },
})
expect(result).toEqual(environments)
})
})

describe('getEnvironmentByName', () => {
it('should look the environment up by its project unique name', async () => {
prisma.environment.findUnique.mockResolvedValue(null)

const result = await service.getEnvironmentByName('project1', 'dev')

expect(prisma.environment.findUnique).toHaveBeenCalledWith({
where: { projectId_name: { projectId: 'project1', name: 'dev' } },
})
expect(result).toBeNull()
})
})

describe('getStageById', () => {
it('should return the stage', async () => {
const stage = makeStage({ id: 'stage1' })
prisma.stage.findUnique.mockResolvedValue(stage)

const result = await service.getStageById('stage1')

expect(prisma.stage.findUnique).toHaveBeenCalledWith({ where: { id: 'stage1' } })
expect(result).toEqual(stage)
})
})

describe('getProdStageIds', () => {
it('should return the ids of the prod stages', async () => {
prisma.stage.findMany.mockResolvedValue([makeStage({ id: 'stage1', name: 'prod' })])

await service.getProdStageIds()

expect(prisma.stage.findMany).toHaveBeenCalledWith({
select: { id: true },
where: { name: 'prod' },
})
})
})

describe('getAvailableCluster', () => {
it('should only match public clusters or dedicated clusters of the project', async () => {
const cluster = makeCluster({ id: 'cluster1' })
prisma.cluster.findFirst.mockResolvedValue(cluster)

const result = await service.getAvailableCluster('cluster1', 'project1')

expect(prisma.cluster.findFirst).toHaveBeenCalledWith({
where: {
OR: [{
id: 'cluster1',
privacy: 'public',
}, {
id: 'cluster1',
privacy: 'dedicated',
projects: { some: { id: 'project1' } },
}],
},
})
expect(result).toEqual(cluster)
})
})

describe('sumEnvironmentResources', () => {
it('should aggregate cpu, gpu and memory', async () => {
await service.sumEnvironmentResources({ clusterId: 'cluster1' })

expect(prisma.environment.aggregate).toHaveBeenCalledWith({
_sum: {
cpu: true,
gpu: true,
memory: true,
},
where: { clusterId: 'cluster1' },
})
})
})

describe('createEnvironment', () => {
it('should create an environment', async () => {
const environment = makeEnvironment()
prisma.environment.create.mockResolvedValue(environment)

const result = await service.createEnvironment({
projectId: environment.projectId,
name: environment.name,
clusterId: environment.clusterId,
stageId: environment.stageId,
cpu: environment.cpu,
gpu: environment.gpu,
memory: environment.memory,
autosync: environment.autosync,
})

expect(prisma.environment.create).toHaveBeenCalled()
expect(result).toEqual(environment)
})
})

describe('updateEnvironment', () => {
it('should update the environment resources', async () => {
const environment = makeEnvironment({ id: 'env1' })
prisma.environment.update.mockResolvedValue(environment)

const result = await service.updateEnvironment('env1', { cpu: 2, gpu: 0, memory: 4, autosync: false })

expect(prisma.environment.update).toHaveBeenCalledWith({
where: { id: 'env1' },
data: { cpu: 2, gpu: 0, memory: 4, autosync: false },
})
expect(result).toEqual(environment)
})
})

describe('deleteEnvironment', () => {
it('should delete the environment', async () => {
const environment = makeEnvironment({ id: 'env1' })
prisma.environment.delete.mockResolvedValue(environment)

const result = await service.deleteEnvironment('env1')

expect(prisma.environment.delete).toHaveBeenCalledWith({ where: { id: 'env1' } })
expect(result).toEqual(environment)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import type { Cluster, Environment, Prisma, Project, Stage } from '@prisma/client'
import { Inject, Injectable } from '@nestjs/common'
import { PrismaService } from '../infrastructure/database/prisma.service'
import { PROD_STAGE_NAME } from './environment.constants'

export type EnvironmentWithCluster = Environment & { cluster: Cluster }
export type EnvironmentWithStage = Environment & { stage: Stage }

export interface EnvironmentResourcesSum {
_sum: {
cpu: number | null
gpu: number | null
memory: number | null
}
}

@Injectable()
export class EnvironmentDatastoreService {
constructor(@Inject(PrismaService) private readonly prisma: PrismaService) {}

getEnvironmentById(environmentId: string): Promise<EnvironmentWithCluster | null> {
return this.prisma.environment.findUnique({
where: { id: environmentId },
include: { cluster: true },
})
}

getEnvironmentsByProjectId(projectId: string): Promise<EnvironmentWithStage[]> {
return this.prisma.environment.findMany({
where: { projectId },
include: { stage: true },
})
}

getEnvironmentByName(projectId: string, name: string): Promise<Environment | null> {
return this.prisma.environment.findUnique({
where: { projectId_name: { projectId, name } },
})
}

getStageById(stageId: string): Promise<Stage | null> {
return this.prisma.stage.findUnique({ where: { id: stageId } })
}

getProdStageIds(): Promise<{ id: string }[]> {
return this.prisma.stage.findMany({
select: { id: true },
where: { name: PROD_STAGE_NAME },
})
}

/** Only public clusters or dedicated clusters attached to the project are usable. */
getAvailableCluster(clusterId: string, projectId: string): Promise<Cluster | null> {
return this.prisma.cluster.findFirst({
where: {
OR: [{
id: clusterId,
privacy: 'public',
}, {
id: clusterId,
privacy: 'dedicated',
projects: { some: { id: projectId } },
}],
},
})
}

getProjectById(projectId: string): Promise<Project> {
return this.prisma.project.findUniqueOrThrow({ where: { id: projectId } })
}

sumEnvironmentResources(where: Prisma.EnvironmentWhereInput): Promise<EnvironmentResourcesSum> {
return this.prisma.environment.aggregate({
_sum: {
cpu: true,
gpu: true,
memory: true,
},
where,
})
}

createEnvironment(data: Prisma.EnvironmentUncheckedCreateInput): Promise<Environment> {
return this.prisma.environment.create({ data })
}

updateEnvironment(environmentId: string, data: Pick<Prisma.EnvironmentUncheckedUpdateInput, 'cpu' | 'gpu' | 'memory' | 'autosync'>): Promise<Environment> {
return this.prisma.environment.update({
where: { id: environmentId },
data,
})
}

deleteEnvironment(environmentId: string): Promise<Environment> {
return this.prisma.environment.delete({
where: { id: environmentId },
})
}
}
Loading