Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): implements cloud function template & templates marketplace #1259

Merged
merged 9 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/charts/laf-server/templates/metering.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,4 @@ spec:
securityContext:
runAsNonRoot: true
serviceAccountName: resources-controller-manager
terminationGracePeriodSeconds: 10
terminationGracePeriodSeconds: 10
2 changes: 2 additions & 0 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as path from 'path'
import { AcceptLanguageResolver, I18nModule, QueryResolver } from 'nestjs-i18n'
import { BillingModule } from './billing/billing.module'
import { AuthenticationModule } from './authentication/authentication.module'
import { FunctionTemplateModule } from './function-template/function-template.module'

@Module({
imports: [
Expand Down Expand Up @@ -62,6 +63,7 @@ import { AuthenticationModule } from './authentication/authentication.module'
),
}),
BillingModule,
FunctionTemplateModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
7 changes: 6 additions & 1 deletion server/src/application/application.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import { ResourceService } from 'src/billing/resource.service'
BundleService,
ResourceService,
],
exports: [ApplicationService, BundleService],
exports: [
ApplicationService,
ApplicationConfigurationService,
EnvironmentVariableService,
BundleService,
],
})
export class ApplicationModule {}
1 change: 1 addition & 0 deletions server/src/dependency/dependency.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import { DependencyService } from './dependency.service'
imports: [ApplicationModule],
controllers: [DependencyController],
providers: [DependencyService],
exports: [DependencyService],
})
export class DependencyModule {}
6 changes: 3 additions & 3 deletions server/src/dependency/dependency.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class DependencyService {
* @param appid
* @returns
*/
private async getExtras(appid: string) {
async getExtras(appid: string) {
const conf = await this.db
.collection<ApplicationConfiguration>('ApplicationConfiguration')
.findOne({ appid })
Expand All @@ -138,12 +138,12 @@ export class DependencyService {
* Get the built-in dependencies in string array
* @returns
*/
private getBuiltins() {
getBuiltins() {
const obj = RUNTIME_BUILTIN_DEPENDENCIES
return Object.keys(obj).map((key) => `${key}@${obj[key]}`)
}

private validate(dto: CreateDependencyDto) {
validate(dto: CreateDependencyDto) {
try {
npa.resolve(dto.name, dto.spec)
return true
Expand Down
78 changes: 78 additions & 0 deletions server/src/function-template/dto/create-function-template.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { CreateEnvironmentDto } from '../../application/dto/create-env.dto'
import { CreateDependencyDto } from 'src/dependency/dto/create-dependency.dto'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import {
IsBoolean,
IsIn,
IsNotEmpty,
IsString,
Matches,
MaxLength,
ValidateNested,
} from 'class-validator'
import { Type } from 'class-transformer'
import { HTTP_METHODS } from '../../constants'
import { HttpMethod } from '../../function/entities/cloud-function'
export class FunctionTemplateItemDto {
@ApiProperty({
description: 'FunctionTemplate item name',
})
@IsNotEmpty()
@Matches(/^[a-zA-Z0-9_.\-\/]{1,256}$/)
name: string

@ApiPropertyOptional()
@MaxLength(256)
description?: string

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

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

validate() {
return null
}
}

export class CreateFunctionTemplateDto {
@ApiProperty({ description: 'function template name' })
@IsNotEmpty()
@IsString()
@MaxLength(64)
name: string

@ApiProperty({ description: 'Dependencies', type: [CreateDependencyDto] })
@IsNotEmpty()
@ValidateNested({ each: true })
@Type(() => CreateDependencyDto)
dependencies: CreateDependencyDto[]

@ApiProperty({ description: 'environments', type: [CreateEnvironmentDto] })
@ValidateNested({ each: true })
@Type(() => CreateEnvironmentDto)
environments: CreateEnvironmentDto[]

@ApiProperty({ description: 'Private flag' })
@IsBoolean()
private: boolean

@ApiPropertyOptional({ description: 'function template description' })
@IsString()
@MaxLength(256)
description?: string

@ApiProperty({
description: 'items of the function template',
type: [FunctionTemplateItemDto],
})
@IsNotEmpty()
@ValidateNested({ each: true })
@Type(() => FunctionTemplateItemDto)
items: FunctionTemplateItemDto[]
}
56 changes: 56 additions & 0 deletions server/src/function-template/dto/update-function-template.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ObjectId } from 'mongodb'
import { CreateEnvironmentDto } from '../../application/dto/create-env.dto'
import { CreateDependencyDto } from 'src/dependency/dto/create-dependency.dto'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import {
IsBoolean,
IsNotEmpty,
IsString,
Length,
MaxLength,
ValidateNested,
} from 'class-validator'
import { Type } from 'class-transformer'
import { FunctionTemplateItemDto } from './create-function-template.dto'

export class UpdateFunctionTemplateDto {
@ApiProperty({ description: 'Function template id' })
@IsNotEmpty()
@Length(24, 24)
functionTemplateId: ObjectId

@ApiProperty({ description: 'Template name' })
@IsNotEmpty()
@MaxLength(64)
@IsString()
name: string

@ApiProperty({ description: 'Dependencies', type: [CreateDependencyDto] })
@IsNotEmpty()
@ValidateNested({ each: true })
@Type(() => CreateDependencyDto)
dependencies: CreateDependencyDto[]

@ApiProperty({ description: 'Environments', type: [CreateEnvironmentDto] })
@ValidateNested({ each: true })
@Type(() => CreateEnvironmentDto)
environments: CreateEnvironmentDto[]

@ApiProperty({ description: 'Private flag' })
@IsBoolean()
private: boolean

@ApiPropertyOptional({ description: 'function template description' })
@IsString()
@MaxLength(256)
description?: string

@ApiPropertyOptional({
description: 'items of the function template',
type: [FunctionTemplateItemDto],
})
@IsNotEmpty()
@ValidateNested({ each: true })
@Type(() => FunctionTemplateItemDto)
items: FunctionTemplateItemDto[]
}
17 changes: 17 additions & 0 deletions server/src/function-template/dto/use-function-template.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsNotEmpty, Length } from 'class-validator'
import { ObjectId } from 'mongodb'

export class UseFunctionTemplateDto {
@ApiProperty({
description: 'The ObjectId of function template',
type: 'string',
})
@IsNotEmpty()
@Length(24, 24)
functionTemplateId: ObjectId

@IsNotEmpty()
@ApiProperty()
appid: string
}
107 changes: 107 additions & 0 deletions server/src/function-template/entities/function-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import {
CloudFunctionSource,
HttpMethod,
} from 'src/function/entities/cloud-function'
import { EnvironmentVariable } from 'src/application/entities/application-configuration'
import { ObjectId } from 'mongodb'

export enum RelationState {
Enabled = 'Active',
Disabled = 'Inactive',
}

export class FunctionTemplate {
@ApiProperty({ type: String })
_id?: ObjectId

@ApiProperty({ type: String })
uid: ObjectId

@ApiProperty()
name: string

@ApiProperty()
dependencies: string[]

@ApiProperty({ isArray: true, type: EnvironmentVariable })
environments: EnvironmentVariable[]

@ApiProperty()
private: boolean

@ApiProperty()
isRecommended: boolean

@ApiProperty()
description: string

@ApiProperty()
star: number

@ApiProperty()
createdAt: Date

@ApiProperty()
updatedAt: Date

@ApiPropertyOptional()
tags?: string[]

@ApiPropertyOptional()
category?: string
}

export class FunctionTemplateItem {
@ApiProperty({ type: String })
_id?: ObjectId

@ApiProperty({ type: String })
templateId: ObjectId

@ApiProperty()
name: string

@ApiProperty()
desc: string

@ApiProperty()
source: Partial<CloudFunctionSource>

@ApiProperty({ type: [String], enum: HttpMethod })
methods: HttpMethod[]

createdAt: Date

updatedAt: Date
}

export class FunctionTemplateStarRelation {
@ApiProperty({ type: String })
_id?: ObjectId
@ApiProperty({ type: String })
uid: ObjectId
@ApiProperty({ type: String })
templateId: ObjectId
@ApiProperty()
createdAt: Date
@ApiProperty()
updatedAt: Date
@ApiProperty({ type: String })
state?: RelationState
}

export class FunctionTemplateUseRelation {
@ApiProperty({ type: String })
_id?: ObjectId
@ApiProperty({ type: String })
uid: ObjectId
@ApiProperty({ type: String })
templateId: ObjectId
@ApiProperty()
createdAt: Date
@ApiProperty()
updatedAt: Date
@ApiProperty({ type: String })
state?: RelationState
}
Loading