Skip to content

Commit

Permalink
feat(server): impl cloud function update & deletetion apis (#451)
Browse files Browse the repository at this point in the history
Signed-off-by: maslow <wangfugen@126.com>

Signed-off-by: maslow <wangfugen@126.com>
  • Loading branch information
maslow authored Nov 29, 2022
1 parent 894c57b commit f297002
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 36 deletions.
4 changes: 1 addition & 3 deletions server/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ export class AppController {
* @returns
*/
@ApiOperation({ summary: 'Get user token by auth code' })
@ApiResponse({
type: ResponseUtil,
})
@ApiResponse({ type: ResponseUtil })
@Get('code2token')
async code2token(@Query('code') code: string) {
const token = await this.authService.code2token(code)
Expand Down
10 changes: 8 additions & 2 deletions server/src/applications/applications.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import {
HttpException,
HttpStatus,
} from '@nestjs/common'
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
import {
ApiBearerAuth,
ApiOperation,
ApiResponse,
ApiTags,
} from '@nestjs/swagger'
import { IRequest } from 'src/common/types'
import { JwtAuthGuard } from '../auth/jwt-auth.guard'
import { ApiResponseUtil, ResponseUtil } from '../common/response'
Expand Down Expand Up @@ -125,9 +130,10 @@ export class ApplicationsController {
* Delete an application
* @returns
*/
@ApiResponse({ type: ResponseUtil })
@ApiOperation({ summary: 'Delete an application' })
@Delete(':appid')
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@ApiOperation({ summary: 'Delete an application' })
async remove(@Param('appid') _appid: string, @Req() req: IRequest) {
const app = req.application
const res = await this.appService.remove(app)
Expand Down
9 changes: 7 additions & 2 deletions server/src/buckets/buckets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
HttpException,
HttpStatus,
} from '@nestjs/common'
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
import {
ApiBearerAuth,
ApiOperation,
ApiResponse,
ApiTags,
} from '@nestjs/swagger'
import { ApplicationAuthGuard } from 'src/applications/application.auth.guard'
import { IRequest } from 'src/common/types'
import { JwtAuthGuard } from '../auth/jwt-auth.guard'
Expand Down Expand Up @@ -135,7 +140,7 @@ export class BucketsController {
* @param name bucket name
* @returns
*/
@ApiResponseUtil(Bucket)
@ApiResponse({ type: ResponseUtil })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@ApiOperation({ summary: 'Delete a bucket' })
@Delete(':name')
Expand Down
20 changes: 10 additions & 10 deletions server/src/functions/dto/create-function.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { IsIn, IsNotEmpty, IsString, Length, MaxLength } from 'class-validator'
import {
IsBoolean,
IsIn,
IsNotEmpty,
IsString,
Length,
MaxLength,
} from 'class-validator'
import { HTTP_METHODS } from 'src/constants'

export class CreateFunctionDto {
Expand All @@ -15,10 +22,11 @@ export class CreateFunctionDto {
description: string

@ApiProperty()
@IsBoolean()
websocket: boolean

@ApiProperty({ type: [String], enum: HTTP_METHODS })
@IsIn(HTTP_METHODS)
@IsIn(HTTP_METHODS, { each: true })
methods: string[] = []

@ApiProperty({ description: 'The source code of the function' })
Expand All @@ -28,14 +36,6 @@ export class CreateFunctionDto {
codes: string

validate() {
if (!this.methods) {
this.methods = []
}
const valid = this.methods.every((method) => HTTP_METHODS.includes(method))
if (!valid) {
return 'methods is invalid'
}

return null
}
}
35 changes: 32 additions & 3 deletions server/src/functions/dto/update-function.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
import { PartialType } from '@nestjs/mapped-types'
import { CreateFunctionDto } from './create-function.dto'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import {
IsBoolean,
IsIn,
IsNotEmpty,
IsString,
MaxLength,
} from 'class-validator'
import { HTTP_METHODS } from 'src/constants'

export class UpdateFunctionDto extends PartialType(CreateFunctionDto) {}
export class UpdateFunctionDto {
@ApiPropertyOptional()
@MaxLength(256)
description: string

@ApiProperty()
@IsBoolean()
websocket: boolean

@ApiProperty({ type: [String], enum: HTTP_METHODS })
@IsIn(HTTP_METHODS, { each: true })
methods: string[] = []

@ApiProperty({ description: 'The source code of the function' })
@IsNotEmpty()
@IsString()
@MaxLength(1024 * 512)
codes: string

validate() {
return null
}
}
69 changes: 58 additions & 11 deletions server/src/functions/functions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import { CreateFunctionDto } from './dto/create-function.dto'
import { UpdateFunctionDto } from './dto/update-function.dto'
import { ApiResponseUtil, ResponseUtil } from 'src/common/response'
import { CloudFunction, CloudFunctionList } from './entities/function.entity'
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
import {
ApiBearerAuth,
ApiOperation,
ApiResponse,
ApiTags,
} from '@nestjs/swagger'
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard'
import { ApplicationAuthGuard } from 'src/applications/application.auth.guard'

Expand All @@ -40,6 +45,12 @@ export class FunctionsController {
return ResponseUtil.error(error)
}

// check name is unique
const found = await this.functionsService.findOne(appid, dto.name)
if (found) {
return ResponseUtil.error('function name is already existed')
}

const res = await this.functionsService.create(appid, dto)
if (!res) {
return ResponseUtil.error('create function error')
Expand Down Expand Up @@ -77,18 +88,54 @@ export class FunctionsController {
return ResponseUtil.ok(data)
}

@Patch(':id')
@ApiOperation({ summary: 'TODO - ⌛️' })
update(
@Param('id') id: string,
@Body() updateFunctionDto: UpdateFunctionDto,
/**
* Update a function
* @param appid
* @param name
* @param dto
* @returns
*/
@ApiResponseUtil(CloudFunction)
@ApiOperation({ summary: 'Update a function' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Patch(':name')
async update(
@Param('appid') appid: string,
@Param('name') name: string,
@Body() dto: UpdateFunctionDto,
) {
return this.functionsService.update(+id, updateFunctionDto)
const func = await this.functionsService.findOne(appid, name)
if (!func) {
throw new HttpException('function not found', HttpStatus.NOT_FOUND)
}

const res = await this.functionsService.update(func, dto)
if (!res) {
return ResponseUtil.error('update function error')
}
return ResponseUtil.ok(res)
}

@ApiOperation({ summary: 'TODO - ⌛️' })
@Delete(':id')
remove(@Param('id') id: string) {
return this.functionsService.remove(+id)
/**
* Delete a function
* @param appid
* @param name
* @returns
*/
@ApiResponse({ type: ResponseUtil })
@ApiOperation({ summary: 'Delete a function' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Delete(':name')
async remove(@Param('appid') appid: string, @Param('name') name: string) {
const func = await this.functionsService.findOne(appid, name)
if (!func) {
throw new HttpException('function not found', HttpStatus.NOT_FOUND)
}

const res = await this.functionsService.remove(func)
if (!res) {
return ResponseUtil.error('delete function error')
}
return ResponseUtil.ok(res)
}
}
35 changes: 30 additions & 5 deletions server/src/functions/functions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class FunctionsService {

try {
const res = await this.k8sClient.objectApi.create(func)
return res.body
return CloudFunction.fromObject(res.body)
} catch (error) {
this.logger.error(error, error?.response?.body)
return null
Expand Down Expand Up @@ -83,11 +83,36 @@ export class FunctionsService {
}
}

update(id: number, updateFunctionDto: UpdateFunctionDto) {
return `This action updates a #${id} function`
async update(func: CloudFunction, dto: UpdateFunctionDto) {
if (dto.description) {
func.spec.description = dto.description
}
if (dto.methods) {
func.spec.methods = dto.methods
}
if (dto.codes) {
func.spec.source.codes = dto.codes
}
if (dto.websocket) {
func.spec.websocket = dto.websocket
}

try {
const res = await this.k8sClient.patchCustomObject(func)
return CloudFunction.fromObject(res)
} catch (error) {
this.logger.error(error, error?.response?.body)
return null
}
}

remove(id: number) {
return `This action removes a #${id} function`
async remove(func: CloudFunction) {
try {
const res = await this.k8sClient.deleteCustomObject(func)
return CloudFunction.fromObject(res)
} catch (error) {
this.logger.error(error, error?.response?.body)
return null
}
}
}

0 comments on commit f297002

Please sign in to comment.