Skip to content

Commit

Permalink
feat(server): implement function query apis (#429)
Browse files Browse the repository at this point in the history
Signed-off-by: maslow <wangfugen@126.com>
  • Loading branch information
maslow authored Nov 24, 2022
1 parent 9759453 commit c0f04e1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 18 deletions.
8 changes: 7 additions & 1 deletion server/src/buckets/buckets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export class BucketsController {
private readonly appService: ApplicationsService,
) {}

/**
* Create a new bucket
* @param dto
* @param req
* @returns
*/
@ApiResponseUtil(Bucket)
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Post()
Expand Down Expand Up @@ -68,7 +74,7 @@ export class BucketsController {
@Get()
async findAll(@Param('appid') appid: string) {
const data = await this.bucketsService.findAll(appid)
return ResponseUtil.ok(data)
return ResponseUtil.ok<BucketList>(data)
}

/**
Expand Down
4 changes: 0 additions & 4 deletions server/src/common/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ export const ApiResponseUtil = <DataDto extends Type<unknown>>(
{
properties: {
data: { $ref: getSchemaPath(dataDto) },
// data: {
// type: 'array',
// items: { $ref: getSchemaPath(dataDto) },
// },
},
},
],
Expand Down
36 changes: 29 additions & 7 deletions server/src/functions/functions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
Delete,
UseGuards,
Req,
HttpException,
HttpStatus,
} from '@nestjs/common'
import { FunctionsService } from './functions.service'
import { CreateFunctionDto } from './dto/create-function.dto'
Expand All @@ -24,33 +26,53 @@ import { IRequest } from 'src/common/types'
export class FunctionsController {
constructor(private readonly functionsService: FunctionsService) {}

/**
* Create a new function
* @param dto
* @param req
* @returns
*/
@ApiResponseUtil(CloudFunction)
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Post()
async create(@Body() dto: CreateFunctionDto, @Req() req: IRequest) {
async create(@Param('appid') appid: string, @Body() dto: CreateFunctionDto) {
const error = dto.validate()
if (error) {
return ResponseUtil.error(error)
}

const appid = req.application.appid
const res = await this.functionsService.create(appid, dto)
if (!res) {
return ResponseUtil.error('create function error')
}
return ResponseUtil.ok(res)
}

/**
* Query function list of an app
* @returns
*/
@ApiResponseUtil(CloudFunctionList)
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get()
findAll() {
return this.functionsService.findAll()
async findAll(@Param('appid') appid: string) {
const data = await this.functionsService.findAll(appid)
return ResponseUtil.ok(data)
}

/**
* Get a function by its name
* @param appid
* @param name
*/
@ApiResponseUtil(CloudFunction)
@Get(':id')
findOne(@Param('id') id: string) {
return this.functionsService.findOne(+id)
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get(':name')
async findOne(@Param('appid') appid: string, @Param('name') name: string) {
const data = await this.functionsService.findOne(appid, name)
if (!data) {
throw new HttpException('function not found', HttpStatus.NOT_FOUND)
}
}

@Patch(':id')
Expand Down
59 changes: 53 additions & 6 deletions server/src/functions/functions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import { GetApplicationNamespaceById } from 'src/common/getter'
import { KubernetesService } from 'src/core/kubernetes.service'
import { CreateFunctionDto } from './dto/create-function.dto'
import { UpdateFunctionDto } from './dto/update-function.dto'
import { CloudFunction } from './entities/function.entity'
import { CloudFunction, CloudFunctionList } from './entities/function.entity'

@Injectable()
export class FunctionsService {
private logger = new Logger(FunctionsService.name)

constructor(private readonly k8sClient: KubernetesService) {}

/**
* Create a new function
* @param appid
* @param dto
* @returns
*/
async create(appid: string, dto: CreateFunctionDto) {
const namespace = GetApplicationNamespaceById(appid)
const func = new CloudFunction(dto.name, namespace)
Expand All @@ -23,17 +29,58 @@ export class FunctionsService {
const res = await this.k8sClient.objectApi.create(func)
return res.body
} catch (error) {
this.logger.error(error, error?.response.body)
this.logger.error(error, error?.response?.body)
return null
}
}

findAll() {
return `This action returns all functions`
/**
* Query functions of a app
* @param appid
* @param labelSelector
* @returns
*/
async findAll(appid: string, labelSelector?: string) {
const namespace = GetApplicationNamespaceById(appid)
const res = await this.k8sClient.customObjectApi.listNamespacedCustomObject(
CloudFunction.Group,
CloudFunction.Version,
namespace,
CloudFunction.PluralName,
undefined,
undefined,
undefined,
labelSelector,
)

return res.body as CloudFunctionList
}

findOne(id: number) {
return `This action returns a #${id} function`
/**
* Find a function by name
* @param appid
* @param name
* @returns
*/
async findOne(appid: string, name: string) {
const namespace = GetApplicationNamespaceById(appid)
try {
const res =
await this.k8sClient.customObjectApi.getNamespacedCustomObject(
CloudFunction.Group,
CloudFunction.Version,
namespace,
CloudFunction.PluralName,
name,
)
return res.body as CloudFunction
} catch (err) {
this.logger.error(err)
if (err?.response?.body?.reason === 'NotFound') {
return null
}
throw err
}
}

update(id: number, updateFunctionDto: UpdateFunctionDto) {
Expand Down

0 comments on commit c0f04e1

Please sign in to comment.