Skip to content

Commit

Permalink
feat(sys-server): impl im/export apis;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 9, 2021
1 parent 5ec26ef commit 10d651b
Show file tree
Hide file tree
Showing 11 changed files with 408 additions and 35 deletions.
2 changes: 2 additions & 0 deletions packages/system-server/init/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

> @Deprecated 此目录中的脚本原为初始化应用,现在多租户版中已废弃,待清理。
51 changes: 29 additions & 22 deletions packages/system-server/src/api/function.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-09-06 18:16:25
* @LastEditTime: 2021-09-09 17:59:42
* @Description:
*/

Expand All @@ -14,38 +14,45 @@ import * as assert from 'assert'
import { logger } from "../lib/logger"
import { ApplicationStruct, getApplicationDbAccessor } from "./application"

const db = DatabaseAgent.sys_db
export enum FunctionStatus {
DISABLED = 0,
ENABLED = 1
}

/**
* Extended function struct
*/
export interface FunctionStruct extends CloudFunctionStruct {
description: string
tags: string[]
label: string
triggers: any[]
status: FunctionStatus
version: number
hash: string
created_at: number
updated_at: number
created_by: any
appid: string
debugParams: string
}


/**
* Load function data by its name
* @param func_name
* @returns
*/
export async function getFunctionByName(appid: string, func_name: string) {
const db = DatabaseAgent.sys_db
const r = await db.collection(Constants.cn.functions)
.where({ name: func_name, appid })
.getOne<CloudFunctionStruct>()

assert.ok(r.ok, `getCloudFunction() failed to get function [${func_name}]: ${r.error.toString()}`)
return r.data
}
.getOne<FunctionStruct>()

/**
* Load function data by its id
* @param func_name
* @returns
*/
export async function getFunctionById(appid: string, func_id: string) {
const r = await db.collection(Constants.cn.functions)
.where({ _id: func_id, appid })
.getOne()

assert.ok(r.ok, `getCloudFunctionById() failed to get function [${func_id}]: ${r.error.toString()}`)
return r.data
}



/**
* Publish functions
* Means that copying sys db functions to app db
Expand Down Expand Up @@ -93,7 +100,7 @@ function compileFunction(func: any) {
/**
* Deploy functions which pushed from remote environment
*/
export async function deployFunctions(functions: CloudFunctionStruct[]) {
export async function deployFunctions(functions: FunctionStruct[]) {
assert.ok(functions)
assert.ok(functions instanceof Array)

Expand Down Expand Up @@ -121,7 +128,7 @@ export async function deployFunctions(functions: CloudFunctionStruct[]) {
* @see deployFunctions()
* @returns
*/
async function _deployOneFunction(func: CloudFunctionStruct, session: ClientSession) {
async function _deployOneFunction(func: FunctionStruct, session: ClientSession) {

await _processFunctionWithSameNameButNotId(func, session)

Expand Down Expand Up @@ -155,7 +162,7 @@ async function _deployOneFunction(func: CloudFunctionStruct, session: ClientSess
* @param func the function to be processing
* @param session the mongodb session for transaction operations
*/
async function _processFunctionWithSameNameButNotId(func: CloudFunctionStruct, session: ClientSession) {
async function _processFunctionWithSameNameButNotId(func: FunctionStruct, session: ClientSession) {
const db = DatabaseAgent.sys_accessor.db

// remove functions
Expand Down
38 changes: 34 additions & 4 deletions packages/system-server/src/api/policy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-09-02 16:34:22
* @LastEditTime: 2021-09-09 17:59:47
* @Description:
*/

Expand All @@ -11,6 +11,36 @@ import { DatabaseAgent } from "../lib/db-agent"
import { ClientSession, ObjectId } from 'mongodb'
import { ApplicationStruct, getApplicationDbAccessor } from './application'

export enum PolicyStatus {
DISABLED = 0,
ENABLED = 1
}
export interface PolicyStruct {
_id: string
name: string
description: string
status: PolicyStatus
rules: any
injector: string
hash: string
created_at: number
updated_at: number
created_by: string
appid: string
}
/**
* Load policy by its name
* @param func_name
* @returns
*/
export async function getPolicyByName(appid: string, policy_name: string) {
const db = DatabaseAgent.sys_db
const r = await db.collection(Constants.cn.policies)
.where({ name: policy_name, appid })
.getOne<PolicyStruct>()

return r.data
}

/**
* Publish access policies
Expand Down Expand Up @@ -45,7 +75,7 @@ export async function publishAccessPolicy(app: ApplicationStruct) {
/**
* Deploy policies which pushed from remote environment
*/
export async function deployPolicies(policies: any[]) {
export async function deployPolicies(policies: PolicyStruct[]) {
assert.ok(policies)
assert.ok(policies instanceof Array)

Expand Down Expand Up @@ -73,7 +103,7 @@ export async function deployPolicies(policies: any[]) {
* @private
* @returns
*/
async function _deployOnePolicy(policy: any, session: ClientSession) {
async function _deployOnePolicy(policy: PolicyStruct, session: ClientSession) {

await _deletePolicyWithSameNameButNotId(policy, session)

Expand Down Expand Up @@ -109,7 +139,7 @@ async function _deployOnePolicy(policy: any, session: ClientSession) {
* @see _deployOnePolicy()
* @private
*/
async function _deletePolicyWithSameNameButNotId(policy: any, session: ClientSession) {
async function _deletePolicyWithSameNameButNotId(policy: PolicyStruct, session: ClientSession) {
const db = DatabaseAgent.sys_accessor.db
await db.collection(Constants.cn.policies).findOneAndDelete({
_id: {
Expand Down
85 changes: 85 additions & 0 deletions packages/system-server/src/lib/exporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { ApplicationStruct } from "../api/application"
import { FunctionStruct } from "../api/function"
import { Constants } from "../constants"
import { DatabaseAgent } from "./db-agent"


/**
* Export application definition to json object:
* - cloud functions
* - policies
*/
export class ApplicationExporter {
readonly app: ApplicationStruct

constructor(app: ApplicationStruct) {
this.app = app
}

async build() {
const meta = this.buildMeta()
const functions = await this.buildFunctions()
const policies = await this.buildPolicies()

return {
meta,
functions,
policies
}
}

private buildMeta() {
return {
name: this.app.name,
created_at: Date.now()
}
}

private async buildFunctions() {
const db = DatabaseAgent.sys_db
const r = await db.collection(Constants.cn.functions)
.where({ appid: this.app.appid })
.limit(999)
.get<FunctionStruct>()

const functions = r.data
const ret = functions.map(func => {
return {
name: func.name,
label: func.label,
code: func.code,
hash: func.hash,
tags: func.tags,
description: func.description,
enableHTTP: func.enableHTTP,
status: func.status,
triggers: func.triggers,
debugParams: func.debugParams,
version: func.version
}
})

return ret
}

private async buildPolicies() {
const db = DatabaseAgent.sys_db
const r = await db.collection(Constants.cn.policies)
.where({ appid: this.app.appid })
.get()

const policies = r.data
const ret = policies.map(po => {
return {
name: po.name,
description: po.description,
status: po.status,
rules: po.rules,
injector: po.injector ?? null,
hash: po.hash,
}
})

return ret
}
}
Loading

0 comments on commit 10d651b

Please sign in to comment.