-
-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): implement collection creation api. (#431)
Signed-off-by: maslow <wangfugen@126.com>
- Loading branch information
Showing
14 changed files
with
11,638 additions
and
9,172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { Module } from '@nestjs/common' | ||
import { CollectionsService } from './collections.service' | ||
import { CollectionsController } from './collections.controller' | ||
import { DatabaseService } from './database.service' | ||
import { ApplicationsService } from 'src/applications/applications.service' | ||
|
||
@Module({ | ||
controllers: [CollectionsController], | ||
providers: [CollectionsService], | ||
providers: [CollectionsService, DatabaseService, ApplicationsService], | ||
}) | ||
export class CollectionsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { DatabaseService } from './database.service' | ||
|
||
describe('DatabaseService', () => { | ||
let service: DatabaseService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [DatabaseService], | ||
}).compile() | ||
|
||
service = module.get<DatabaseService>(DatabaseService) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Injectable, Logger } from '@nestjs/common' | ||
import { GetApplicationNamespaceById } from 'src/common/getter' | ||
import { KubernetesService } from 'src/core/kubernetes.service' | ||
import { Database } from './entities/database.entity' | ||
import { MongoClient } from 'mongodb' | ||
import * as assert from 'node:assert' | ||
|
||
@Injectable() | ||
export class DatabaseService { | ||
private readonly logger = new Logger(DatabaseService.name) | ||
|
||
constructor(private readonly k8sService: KubernetesService) {} | ||
|
||
/** | ||
* Find a database and connect to it | ||
* @param appid | ||
* @returns | ||
*/ | ||
async findAndConnect(appid: string) { | ||
const database = await this.findOne(appid) | ||
assert(database, 'Database not found') | ||
|
||
const client = await this.connectDatabase(database) | ||
const db = client.db(database.metadata.name) | ||
return { db, client } | ||
} | ||
|
||
/** | ||
* Get the database of an app | ||
* @param appid | ||
* @returns | ||
*/ | ||
async findOne(appid: string) { | ||
assert(appid, 'appid is required') | ||
const namespace = GetApplicationNamespaceById(appid) | ||
const name = appid | ||
try { | ||
const res = | ||
await this.k8sService.customObjectApi.getNamespacedCustomObject( | ||
Database.GVK.group, | ||
Database.GVK.version, | ||
namespace, | ||
Database.GVK.plural, | ||
name, | ||
) | ||
return res.body as Database | ||
} catch (err) { | ||
this.logger.error(err) | ||
if (err?.response?.body?.reason === 'NotFound') { | ||
return null | ||
} | ||
throw err | ||
} | ||
} | ||
|
||
/** | ||
* Connect to database | ||
*/ | ||
async connectDatabase(db: Database) { | ||
assert(db, 'Database is required') | ||
assert(db.status, 'Database status is required') | ||
assert(db.status.connectionUri, 'Database connection uri is required') | ||
|
||
const uri = db.status?.connectionUri | ||
const client = new MongoClient(uri) | ||
try { | ||
await client.connect() | ||
this.logger.log(`Connected to database ${db.metadata.namespace}`) | ||
return client | ||
} catch { | ||
this.logger.error( | ||
`Failed to connect to database ${db.metadata.namespace}`, | ||
) | ||
await client.close() | ||
return null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,14 @@ | ||
export class CreateCollectionDto {} | ||
import { ApiProperty } from '@nestjs/swagger' | ||
|
||
export class CreateCollectionDto { | ||
@ApiProperty() | ||
name: string | ||
|
||
async validate() { | ||
if (!this.name) { | ||
return 'Collection name is required' | ||
} | ||
|
||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { KubernetesObject } from '@kubernetes/client-node' | ||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger' | ||
import { | ||
Condition, | ||
GroupVersionKind, | ||
ObjectMeta, | ||
} from '../../core/kubernetes.interface' | ||
|
||
export class DatabaseCapacity { | ||
@ApiProperty() | ||
storage: string | ||
} | ||
|
||
export class DatabaseSpec { | ||
@ApiProperty() | ||
provider: string | ||
|
||
@ApiProperty() | ||
region: string | ||
|
||
@ApiProperty() | ||
capacity: DatabaseCapacity | ||
|
||
@ApiProperty() | ||
username: string | ||
|
||
@ApiProperty() | ||
password: string | ||
} | ||
|
||
export class DatabaseStatus { | ||
@ApiProperty() | ||
storeName: string | ||
|
||
@ApiProperty() | ||
storeNamespace: string | ||
|
||
@ApiProperty() | ||
connectionUri: string | ||
|
||
@ApiProperty() | ||
capacity: DatabaseCapacity | ||
|
||
@ApiProperty() | ||
conditions: Condition[] | ||
} | ||
|
||
export class Database implements KubernetesObject { | ||
@ApiProperty() | ||
apiVersion: string | ||
|
||
@ApiProperty() | ||
kind: string | ||
|
||
@ApiProperty() | ||
metadata: ObjectMeta | ||
|
||
@ApiProperty() | ||
spec: DatabaseSpec | ||
|
||
@ApiPropertyOptional() | ||
status?: DatabaseStatus | ||
|
||
static readonly GVK = new GroupVersionKind( | ||
'database.laf.dev', | ||
'v1', | ||
'Database', | ||
'databases', | ||
) | ||
|
||
constructor(name: string, namespace: string) { | ||
this.apiVersion = Database.GVK.apiVersion | ||
this.kind = Database.GVK.kind | ||
this.metadata = new ObjectMeta(name, namespace) | ||
this.spec = new DatabaseSpec() | ||
} | ||
} |