Skip to content

Commit

Permalink
fix(devbox):fix multi-region
Browse files Browse the repository at this point in the history
  • Loading branch information
xudaotutou committed Jan 6, 2025
1 parent 226ddef commit aeb6360
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getTemplateRepository, listOfficialTemplateRepository } from '@/api/template'
import useDriver from '@/hooks/useDriver'
import { TemplateRepositoryKind } from '@/prisma/generated/client'
import { useDevboxStore } from '@/stores/devbox'
import { DevboxEditTypeV2 } from '@/types/devbox'
Expand All @@ -11,7 +12,6 @@ import { useFormContext } from 'react-hook-form'
import Label from '../../Label'
import TemplateRepositoryListNav from '../TemplateRepositoryListNav'
import TemplateRepositoryItem from './TemplateReposistoryItem'
import useDriver from '@/hooks/useDriver'

interface TemplateRepositorySelectorProps {
isEdit: boolean
Expand Down Expand Up @@ -139,8 +139,8 @@ export default function TemplateRepositorySelector({ isEdit }: TemplateRepositor
])
return (
<VStack alignItems={'center'} mb={7} gap={'24px'}>
<Box className="guide-runtimes">
<Flex w="full" justify={'space-between'} mb={'24px'}>
<Flex className="guide-runtimes" gap={'24px'} flexDir={'column'} w={'full'}>
<Flex w="full" justify={'space-between'} >
<Label w={100} alignSelf={'flex-start'}>
{t('runtime_environment')}
</Label>
Expand Down Expand Up @@ -171,7 +171,7 @@ export default function TemplateRepositorySelector({ isEdit }: TemplateRepositor
))}
</Flex>
</Flex>
</Box>
</Flex>
<Flex gap={'10px'} px={'14px'} width={'full'}>
{/* Framework */}
{categorizedData['FRAMEWORK'].length !== 0 && <Box width={'85px'}>{t('framework')}</Box>}
Expand Down
5 changes: 3 additions & 2 deletions frontend/providers/devbox/app/api/auth/init/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { authSessionWithDesktopJWT, generateDevboxToken } from "@/services/backend/auth"
import { jsonRes } from "@/services/backend/response"
import { devboxDB } from "@/services/db/init"
import { getRegionUid } from "@/utils/env"
import { makeOrganizationName } from "@/utils/user"

import { NextRequest } from "next/server"
Expand Down Expand Up @@ -109,9 +110,9 @@ const findOrCreateUser = async (regionUid: string, namespaceId: string) => {
})
}
export async function POST(req: NextRequest) {
const regionUid = process.env.REGION_UID
const regionUid = getRegionUid()
if (!regionUid) {
console.log("REGIONUID is not set")
console.log("REGION_UID is not set")
return jsonRes({
code: 500,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { authSessionWithJWT } from '@/services/backend/auth'
import { jsonRes } from '@/services/backend/response'
import { devboxDB } from '@/services/db/init'
import { getRegionUid } from '@/utils/env'
import { NextRequest } from 'next/server'
import { z } from 'zod'

Expand All @@ -23,11 +24,13 @@ export async function DELETE(req: NextRequest) {
const {payload} = await authSessionWithJWT(headerList)

const deletedAt = new Date()
const regionUid = getRegionUid()
await devboxDB.templateRepository.update({
where: {
uid,
organizationUid: payload.organizationUid,
isDeleted: false,
regionUid
},
data: {
deletedAt,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { authSessionWithJWT } from '@/services/backend/auth'
import { jsonRes } from '@/services/backend/response'
import { devboxDB } from '@/services/db/init'
import { getRegionUid } from '@/utils/env'
import { NextRequest } from 'next/server'
import { z } from 'zod'

Expand All @@ -19,10 +20,12 @@ export async function GET(req: NextRequest) {
})
}
const uid = uidResult.data
const regionUid = getRegionUid()
const templateRepository = await devboxDB.templateRepository.findUnique({
where: {
uid,
isDeleted: false
isDeleted: false,
regionUid
},
select: {
templates: {
Expand Down
60 changes: 26 additions & 34 deletions frontend/providers/devbox/app/api/templateRepository/list/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Prisma } from '@/prisma/generated/client'
import { jsonRes } from '@/services/backend/response'
import { devboxDB } from '@/services/db/init'
import { getRegionUid } from '@/utils/env'
import { NextRequest } from 'next/server'
import { z } from 'zod'
export const dynamic = 'force-dynamic'
Expand All @@ -11,45 +12,36 @@ export async function GET(req: NextRequest) {
const search = searchParams.get('search') || ''
const page = z.number().int().positive().safeParse(Number(searchParams.get('page'))).data || 1
const pageSize = z.number().int().min(1).safeParse(Number(searchParams.get('pageSize'))).data || 30
const dbquery: Prisma.TemplateRepositoryWhereInput = {

...(tags && tags.length > 0
? {
AND: tags.map((tag) => ({
templateRepositoryTags: {
some: {
tagUid: tag
}
}
}))
}
: {}),
...(search && search.length > 0
? {
name: {
contains: search
}
}
: {})
}
const [templateRepositoryList, totalItems] = await devboxDB.$transaction(async tx => {
const validRepoIds = await tx.template.findMany({
where: {
isDeleted: false,
},
select: {
templateRepositoryUid: true
},
distinct: ['templateRepositoryUid']
})

const regionUid = getRegionUid()
const where: Prisma.TemplateRepositoryWhereInput = {
uid: {
in: validRepoIds.map(r => r.templateRepositoryUid)
templates: {
some: {
isDeleted: false
}
},
isPublic: true,
isDeleted: false,
...dbquery
regionUid,
...(tags && tags.length > 0
? {
AND: tags.map((tag) => ({
templateRepositoryTags: {
some: {
tagUid: tag
}
}
}))
}
: {}),
...(search && search.length > 0
? {
name: {
contains: search
}
}
: {})
}
const [templateRepositoryList, totalItems] = await Promise.all([
tx.templateRepository.findMany({
Expand Down Expand Up @@ -88,7 +80,7 @@ export async function GET(req: NextRequest) {
]
}),
tx.templateRepository.count({
where: dbquery,
where,
})
])
return [templateRepositoryList, totalItems]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { jsonRes } from '@/services/backend/response'
import { devboxDB } from '@/services/db/init'
import { getRegionUid } from '@/utils/env'
import { NextRequest } from 'next/server'
export const dynamic = 'force-dynamic'

Expand All @@ -10,12 +11,14 @@ export const GET = async function GET(req: NextRequest) {
id: 'labring'
}
})
if(!organization) throw Error('organization not found')
if (!organization) throw Error('organization not found')
const regionUid = getRegionUid()
const templateRepositoryList = await devboxDB.templateRepository.findMany({
where: {
isPublic: true,
isDeleted: false,
organizationUid: organization.uid
organizationUid: organization.uid,
regionUid,
},
select: {
kind: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { authSessionWithJWT } from '@/services/backend/auth'
import { jsonRes } from '@/services/backend/response'
import { devboxDB } from '@/services/db/init'
import { getRegionUid } from '@/utils/env'
import { NextRequest } from 'next/server'

export const dynamic = 'force-dynamic'
Expand Down Expand Up @@ -32,16 +33,19 @@ export async function GET(req: NextRequest) {
isDeleted: true
}
},
regionUid: true,
isDeleted: true,
isPublic: true,
}
}
}
})
const regionUid = getRegionUid()
if (!template ||
!(template.templateRepository.organization.uid === payload.organizationUid
|| template.templateRepository.isPublic === true
)
) ||
template.templateRepository.regionUid !== regionUid
) {
return jsonRes({
code: 404,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { authSessionWithJWT } from '@/services/backend/auth'
import { jsonRes } from '@/services/backend/response'
import { devboxDB } from '@/services/db/init'
import { getRegionUid } from '@/utils/env'
import { NextRequest } from 'next/server'

export const dynamic = 'force-dynamic'
Expand All @@ -18,12 +19,13 @@ export async function GET(req: NextRequest) {
const templateRepository = await devboxDB.templateRepository.findUnique({
where: {
uid,
isDeleted: false
isDeleted: false,
regionUid: getRegionUid()
},
select: {
name: true,
uid: true,
organization:{
organization: {
select: {
uid: true,
isDeleted: true
Expand All @@ -45,18 +47,26 @@ export async function GET(req: NextRequest) {
}
}
})
if (templateRepository &&
!(
(templateRepository.organization.isDeleted === false
&& templateRepository.organization.uid === payload.organizationUid)
|| templateRepository.isPublic === true
)) {

if (!templateRepository) {
return jsonRes({
data: {
templateList: []
}
})
}
const templateList = templateRepository.templates
if (!(
(templateRepository.organization.isDeleted === false
&& templateRepository.organization.uid === payload.organizationUid)
|| templateRepository.isPublic === true
)) {
return jsonRes({
code: 404,
error: 'Template is not found'
data: {
templateList
}
})
}
const templateList = templateRepository?.templates || []
return jsonRes({
data: {
templateList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TagType } from "@/prisma/generated/client"
import { authSessionWithJWT } from "@/services/backend/auth"
import { jsonRes } from "@/services/backend/response"
import { devboxDB } from "@/services/db/init"
import { getRegionUid } from "@/utils/env"
import { updateTemplateRepositorySchema } from "@/utils/vaildate"
import { NextRequest } from "next/server"
import { z } from "zod"
Expand Down Expand Up @@ -66,7 +67,8 @@ export async function POST(req: NextRequest) {
)
const isExist = await devboxDB.templateRepository.findUnique({
where: {
isDeleted_name: {
isDeleted_regionUid_name: {
regionUid: getRegionUid(),
name: query.templateRepositoryName,
isDeleted: false,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { devboxDB } from "@/services/db/init"
import { ERROR_ENUM } from "@/services/error"
import { retagSvcClient } from "@/services/retag"
import { KBDevboxReleaseType, KBDevboxTypeV2 } from "@/types/k8s"
import { getRegionUid } from "@/utils/env"
import { createTemplateRepositorySchema } from "@/utils/vaildate"
import { NextRequest } from "next/server"
import { z } from "zod"
Expand Down Expand Up @@ -58,7 +59,8 @@ export async function POST(req: NextRequest) {
}
const isExist = await devboxDB.templateRepository.findUnique({
where: {
isDeleted_name: {
isDeleted_regionUid_name: {
regionUid: getRegionUid(),
name: query.templateRepositoryName,
isDeleted: false
},
Expand Down Expand Up @@ -139,6 +141,7 @@ export async function POST(req: NextRequest) {
parentUid: devboxBody.spec.templateID,
}
},
regionUid: getRegionUid(),
organizationUid: payload.organizationUid,
iconId: origionalTemplate?.templateRepository.iconId,
kind: TemplateRepositoryKind.CUSTOM,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { devboxDB } from '@/services/db/init'
import { ERROR_ENUM } from '@/services/error'
import { retagSvcClient } from '@/services/retag'
import { KBDevboxReleaseType, KBDevboxTypeV2 } from '@/types/k8s'
import { getRegionUid } from '@/utils/env'
import { updateTemplateSchema } from '@/utils/vaildate'
import { NextRequest } from 'next/server'
import { z } from 'zod'
Expand Down Expand Up @@ -75,6 +76,7 @@ export async function POST(req: NextRequest) {
where: {
uid: query.templateRepositoryUid,
organizationUid: payload.organizationUid,
regionUid: getRegionUid()
},
select: {
uid: true,
Expand All @@ -83,6 +85,7 @@ export async function POST(req: NextRequest) {
select: {
tagUid: true
}

},
templates: true
}
Expand Down Expand Up @@ -130,6 +133,7 @@ export async function POST(req: NextRequest) {
await tx.templateRepository.update({
where: {
organizationUid: organization.uid,
regionUid: getRegionUid(),
uid: templateRepository.uid,
},
data: {
Expand Down
Loading

0 comments on commit aeb6360

Please sign in to comment.