Skip to content

Commit

Permalink
feat(server): impl bucket creation api
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Nov 23, 2022
1 parent 36971fd commit a9790a7
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 15 deletions.
48 changes: 41 additions & 7 deletions server/src/buckets/buckets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,62 @@ import {
Patch,
Param,
Delete,
UseGuards,
Req,
Logger,
} from '@nestjs/common'
import { ApplicationAuthGuard } from 'src/applications/application.auth.guard'
import { IRequest } from 'src/common/types'
import { ApplicationsService } from '../applications/applications.service'
import { JwtAuthGuard } from '../auth/jwt-auth.guard'
import { ResponseUtil } from '../common/response'
import { BucketsService } from './buckets.service'
import { CreateBucketDto } from './dto/create-bucket.dto'
import { UpdateBucketDto } from './dto/update-bucket.dto'

@Controller('buckets')
@Controller('apps/:appid/buckets')
export class BucketsController {
constructor(private readonly bucketsService: BucketsService) {}
logger = new Logger(BucketsController.name)
constructor(
private readonly bucketsService: BucketsService,
private readonly appService: ApplicationsService,
) {}

@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Post()
create(@Body() createBucketDto: CreateBucketDto) {
return this.bucketsService.create(createBucketDto)
async create(@Body() dto: CreateBucketDto, @Req() req: IRequest) {
// check if the bucket name is unique
const found = await this.bucketsService.findOne(dto.appid, dto.name)
if (found) {
return ResponseUtil.error('bucket name is already existed')
}

// TODO: check the storage capacity of the app
const app = req.application
this.logger.warn(
'TODO: check the storage capacity of the app: ',
app.metadata.name,
)

// create bucket
const bucket = await this.bucketsService.create(dto)
if (!bucket) {
return ResponseUtil.error('create bucket failed')
}

return ResponseUtil.ok(bucket)
}

@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get()
findAll() {
return this.bucketsService.findAll()
}

@Get(':id')
findOne(@Param('id') id: string) {
return this.bucketsService.findOne(+id)
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get(':name')
findOne(@Param('appid') appid: string, @Param('name') name: string) {
return this.bucketsService.findOne(appid, name)
}

@Patch(':id')
Expand Down
3 changes: 2 additions & 1 deletion server/src/buckets/buckets.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common'
import { BucketsService } from './buckets.service'
import { BucketsController } from './buckets.controller'
import { ApplicationsService } from 'src/applications/applications.service'

@Module({
controllers: [BucketsController],
providers: [BucketsService],
providers: [BucketsService, ApplicationsService],
})
export class BucketsModule {}
28 changes: 23 additions & 5 deletions server/src/buckets/buckets.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import { Injectable } from '@nestjs/common'
import { Injectable, Logger } from '@nestjs/common'
import { KubernetesService } from 'src/core/kubernetes.service'
import { CreateBucketDto } from './dto/create-bucket.dto'
import { UpdateBucketDto } from './dto/update-bucket.dto'
import { Bucket, IBucket } from './entities/bucket.entity'

@Injectable()
export class BucketsService {
create(createBucketDto: CreateBucketDto) {
return 'This action adds a new bucket'
logger: Logger = new Logger(BucketsService.name)
constructor(private readonly k8sClient: KubernetesService) {}

async create(dto: CreateBucketDto) {
const namespace = dto.appid
const bucket = Bucket.create(dto.name, namespace)
bucket.spec = {
policy: dto.policy,
storage: dto.storage,
}

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

findAll() {
return `This action returns all buckets`
}

findOne(id: number) {
return `This action returns a #${id} bucket`
async findOne(appid: string, name: string): Promise<IBucket> {
return null
}

update(id: number, updateBucketDto: UpdateBucketDto) {
Expand Down
13 changes: 12 additions & 1 deletion server/src/buckets/dto/create-bucket.dto.ts
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
export class CreateBucketDto {}
import { BucketPolicy } from '../entities/bucket.entity'

export class CreateBucketDto {
shortName: string
appid: string
policy: BucketPolicy
storage: string

get name() {
return `${this.appid}-${this.shortName}`
}
}
62 changes: 61 additions & 1 deletion server/src/buckets/entities/bucket.entity.ts
Original file line number Diff line number Diff line change
@@ -1 +1,61 @@
export class Bucket {}
import { KubernetesObject, V1ObjectMeta } from '@kubernetes/client-node'
import { Condition } from '../../core/kubernetes.interface'

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

static create(name: string, namespace: string, spec?: IBucketSpec) {
const data: IBucket = {
apiVersion: Bucket.GroupVersion,
kind: Bucket.Kind,
metadata: new V1ObjectMeta(),
spec: spec,
}
data.metadata.name = name
data.metadata.namespace = namespace
return data
}
}
export enum BucketPolicy {
Private = 'private',
ReadOnly = 'readonly',
Public = 'readwrite',
}

export interface IBucket extends KubernetesObject {
spec: IBucketSpec
status?: IBucketStatus
}

export interface IBucketSpec {
policy: BucketPolicy
storage: string
}

export interface IBucketStatus {
// username of bucket in oss
user: string
policy: BucketPolicy
versioning: boolean
capacity: IBucketCapacity
// type: Ready
conditions: Condition[]
}

export interface IBucketCapacity {
maxStorage: string
// used storage
storage: string
objectCount: number
}

export interface IBucketList {
apiVersion: string
items: IBucket[]
}

0 comments on commit a9790a7

Please sign in to comment.