Skip to content

Commit

Permalink
feat(doc): refact entity & interfaces to support swagger struct (#426)
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 b4df288 commit 4d56e64
Show file tree
Hide file tree
Showing 18 changed files with 442 additions and 126 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"Referer",
"rolebinding",
"runc",
"runtimes",
"runtimev",
"sealctl",
"sealos",
Expand Down
8 changes: 7 additions & 1 deletion server/src/applications/applications.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import {
HttpException,
HttpStatus,
} from '@nestjs/common'
import { ApiBody, ApiExtraModels, ApiResponse, ApiTags } from '@nestjs/swagger'
import { IRequest } from 'src/common/types'
import { JwtAuthGuard } from '../auth/jwt-auth.guard'
import { ResponseUtil } from '../common/response'
import { ApiResponseUtil, ResponseUtil } from '../common/response'
import { ApplicationAuthGuard } from './application.auth.guard'
import { ApplicationsService } from './applications.service'
import { CreateApplicationDto } from './dto/create-application.dto'
import { UpdateApplicationDto } from './dto/update-application.dto'
import { Application, ApplicationList } from './entities/application.entity'

@ApiTags('Applications')
@Controller('applications')
export class ApplicationsController {
constructor(private readonly appService: ApplicationsService) {}
Expand All @@ -27,6 +30,8 @@ export class ApplicationsController {
* Create application
* @returns
*/
// @ApiResponse({ type: ResponseUtil<CreateApplicationDto> })
@ApiResponseUtil(Application)
@UseGuards(JwtAuthGuard)
@Post()
async create(@Body() dto: CreateApplicationDto, @Req() req: IRequest) {
Expand All @@ -51,6 +56,7 @@ export class ApplicationsController {
return ResponseUtil.ok(app)
}

@ApiResponseUtil(ApplicationList)
@UseGuards(JwtAuthGuard)
@Get()
async findAll(@Req() req: IRequest) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/applications/applications.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe.skip('AppService find app by appid', () => {

it('should find app by appid', async () => {
appid = '1i43zq'
const res = await service.findOne(userid, appid)
const res = await service.findOne(appid)
expect(res).not.toBeNull()
expect(res.kind).toBe('Application')
expect(res.metadata.name).toBe(appid)
Expand Down
28 changes: 15 additions & 13 deletions server/src/applications/applications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Injectable, Logger } from '@nestjs/common'
import { KubernetesService } from '../core/kubernetes.service'
import {
Application,
IApplication,
IApplicationList,
ApplicationList,
ApplicationSpec,
} from './entities/application.entity'
import * as k8s from '@kubernetes/client-node'
import * as nanoid from 'nanoid'
import { CreateApplicationDto } from './dto/create-application.dto'
import { UpdateApplicationDto } from './dto/update-application.dto'
import { ResourceLabels } from '../constants'
import { GetApplicationNamespaceById } from 'src/common/getter'
import { GetApplicationNamespaceById } from '../common/getter'

@Injectable()
export class ApplicationsService {
Expand Down Expand Up @@ -46,31 +46,33 @@ export class ApplicationsService {

async create(userid: string, appid: string, dto: CreateApplicationDto) {
// create app resources
const app = Application.create(dto.name, appid)
const app = new Application(dto.name, appid)
app.metadata.name = dto.name
app.metadata.namespace = appid
app.metadata.labels = {
[ResourceLabels.APP_ID]: appid,
[ResourceLabels.USER_ID]: userid,
}
app.spec = {
app.spec = new ApplicationSpec({
appid,
state: dto.state,
region: dto.region,
bundleName: dto.bundleName,
runtimeName: dto.runtimeName,
}
})

console.log(app)

try {
const res = await this.k8sClient.objectApi.create(app)
return res.body
} catch (error) {
this.logger.error(error)
this.logger.error(error, error?.response.body)
return null
}
}

async findAll(labelSelector?: string): Promise<IApplicationList> {
async findAll(labelSelector?: string): Promise<ApplicationList> {
const res = await this.k8sClient.customObjectApi.listClusterCustomObject(
Application.Group,
Application.Version,
Expand All @@ -81,15 +83,15 @@ export class ApplicationsService {
undefined,
labelSelector,
)
return res.body as IApplicationList
return res.body as ApplicationList
}

async findAllByUser(userid: string): Promise<IApplicationList> {
async findAllByUser(userid: string): Promise<ApplicationList> {
const apps = await this.findAll(`${ResourceLabels.USER_ID}=${userid}`)
return apps
}

async findOne(appid: string): Promise<IApplication> {
async findOne(appid: string): Promise<Application> {
const namespace = GetApplicationNamespaceById(appid)
const name = appid

Expand All @@ -103,7 +105,7 @@ export class ApplicationsService {
Application.PluralName,
name,
)
return appRes.body as IApplication
return appRes.body as Application
} catch (err) {
this.logger.error(err)
if (err?.response?.body?.reason === 'NotFound') {
Expand All @@ -113,7 +115,7 @@ export class ApplicationsService {
}
}

async findOneByUser(userid: string, appid: string): Promise<IApplication> {
async findOneByUser(userid: string, appid: string): Promise<Application> {
const app = await this.findOne(appid)
if (app?.metadata?.labels?.[ResourceLabels.USER_ID] === userid) {
return app
Expand Down
14 changes: 14 additions & 0 deletions server/src/applications/dto/create-application.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { ApiProperty } from '@nestjs/swagger'
import { ApplicationState } from '../entities/application.entity'

export class CreateApplicationDto {
@ApiProperty({ required: true })
name: string

@ApiProperty({
default: ApplicationState.ApplicationStateRunning,
required: false,
enum: ApplicationState,
})
state: ApplicationState

@ApiProperty()
region: string

@ApiProperty()
bundleName: string

@ApiProperty()
runtimeName: string

validate(): string | null {
Expand Down
4 changes: 2 additions & 2 deletions server/src/applications/dto/update-application.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreateApplicationDto } from './create-application.dto';
import { PartialType } from '@nestjs/swagger'
import { CreateApplicationDto } from './create-application.dto'

export class UpdateApplicationDto extends PartialType(CreateApplicationDto) {}
126 changes: 88 additions & 38 deletions server/src/applications/entities/application.entity.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
import { KubernetesObject, V1ObjectMeta } from '@kubernetes/client-node'
import { Condition } from '../../core/kubernetes.interface'
import { IBundleSpec } from './bundle.entity'
import { IRuntimeSpec } from './runtime.entity'

export class Application {
static readonly Group = 'application.laf.dev'
static readonly Version = 'v1'
static readonly PluralName = 'applications'
static readonly Kind = 'Application'
static get GroupVersion() {
return `${Application.Group}/${Application.Version}`
}

static create(name: string, namespace: string, spec?: IApplicationSpec) {
const data: IApplication = {
apiVersion: Application.GroupVersion,
kind: Application.Kind,
metadata: new V1ObjectMeta(),
spec: spec,
}
data.metadata.name = name
data.metadata.namespace = namespace
return data
}
}
import { KubernetesObject } from '@kubernetes/client-node'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { Condition, ObjectMeta } from '../../core/kubernetes.interface'
import { BundleSpec } from './bundle.entity'
import { RuntimeSpec } from './runtime.entity'

export enum ApplicationState {
ApplicationStateInitializing = 'Initializing',
Expand All @@ -33,30 +12,101 @@ export enum ApplicationState {
ApplicationStateStopping = 'Stopping',
ApplicationStateStopped = 'Stopped',
}

export interface IApplication extends KubernetesObject {
spec: IApplicationSpec
status?: IApplicationStatus
}

export interface IApplicationSpec {
export class ApplicationSpec {
@ApiProperty()
appid: string

@ApiProperty()
state: ApplicationState

@ApiProperty()
region: string

@ApiProperty()
bundleName: string

@ApiProperty()
runtimeName: string

constructor({
appid = '',
state = ApplicationState.ApplicationStateInitializing,
region = '',
bundleName = '',
runtimeName = '',
} = {}) {
this.appid = appid
this.state = state
this.region = region
this.bundleName = bundleName
this.runtimeName = runtimeName
}
}

export interface IApplicationStatus {
export class ApplicationStatus {
@ApiPropertyOptional()
bundleName: string
bundleSpec: IBundleSpec

@ApiPropertyOptional()
bundleSpec: BundleSpec

@ApiPropertyOptional()
runtimeName: string
runtimeSpec: IRuntimeSpec

@ApiPropertyOptional()
runtimeSpec: RuntimeSpec

@ApiPropertyOptional({
enum: ApplicationState,
})
phase: ApplicationState

@ApiPropertyOptional({
type: [Condition],
})
conditions: Condition[]
}

export interface IApplicationList {
export class Application implements KubernetesObject {
@ApiProperty()
apiVersion: string
items: IApplication[]

@ApiProperty()
kind: string

@ApiProperty()
metadata: ObjectMeta

@ApiProperty()
spec: ApplicationSpec

@ApiPropertyOptional()
status?: ApplicationStatus

static readonly Group = 'application.laf.dev'
static readonly Version = 'v1'
static readonly PluralName = 'applications'
static readonly Kind = 'Application'
static get GroupVersion() {
return `${this.Group}/${this.Version}`
}

constructor(name: string, namespace: string) {
this.apiVersion = Application.GroupVersion
this.kind = Application.Kind
this.metadata = {
name,
namespace,
}
this.spec = new ApplicationSpec()
}
}
export class ApplicationList {
@ApiProperty()
apiVersion: string

@ApiProperty({
type: [Application],
})
items: Application[]
}
Loading

0 comments on commit 4d56e64

Please sign in to comment.