From 037f5cfec0302307b9ae823c0d67d578d7afd34b Mon Sep 17 00:00:00 2001 From: Gabriel Rosa Date: Wed, 27 Jul 2022 08:41:32 -0300 Subject: [PATCH] FEAT: new conection test module --- package-lock.json | 14 +-- package.json | 2 +- src/app.module.ts | 3 + .../connection-test.controller.spec.ts | 18 +++ .../connection-test.controller.ts | 108 +++++++++++++++++ .../connection-test/connection-test.module.ts | 10 ++ .../connection-test.service.spec.ts | 18 +++ .../connection-test.service.ts | 42 +++++++ .../connection-test/dto/connection-test.ts | 112 ++++++++++++++++++ src/modules/connection/dtos/connection.ts | 18 +++ .../connection/dtos/create-connection.ts | 6 +- 11 files changed, 340 insertions(+), 11 deletions(-) create mode 100644 src/modules/connection-test/connection-test.controller.spec.ts create mode 100644 src/modules/connection-test/connection-test.controller.ts create mode 100644 src/modules/connection-test/connection-test.module.ts create mode 100644 src/modules/connection-test/connection-test.service.spec.ts create mode 100644 src/modules/connection-test/connection-test.service.ts create mode 100644 src/modules/connection-test/dto/connection-test.ts create mode 100644 src/modules/connection/dtos/connection.ts diff --git a/package-lock.json b/package-lock.json index 3b533d9..e8176d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,7 +38,7 @@ "passport-hubspot-oauth2": "^1.0.3", "passport-mailchimp": "^1.1.0", "protospack": "2.5.2", - "protospack-v2": "3.8.11", + "protospack-v2": "^3.9.0-beta.4", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.5.5", @@ -9018,9 +9018,9 @@ } }, "node_modules/protospack-v2": { - "version": "3.8.11", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.8.11.tgz", - "integrity": "sha512-xInbwBwbRrir7Pmgdt4Qv7cA5gjfzHRLl/LOR24vfeXiGO8kome+iAUvk68pwsg529XL1fIl1rOMTsSakCMY9A==", + "version": "3.9.0-beta.4", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.9.0-beta.4.tgz", + "integrity": "sha512-T1T6g1+qusecCGySLA4FSn5O1u+RyZkhIaO6De1701HrNcJM7jBZjYsN1qHr49vQ7TMeibtKyiVqpqZSoPeBBg==", "dependencies": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", @@ -17247,9 +17247,9 @@ } }, "protospack-v2": { - "version": "3.8.11", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.8.11.tgz", - "integrity": "sha512-xInbwBwbRrir7Pmgdt4Qv7cA5gjfzHRLl/LOR24vfeXiGO8kome+iAUvk68pwsg529XL1fIl1rOMTsSakCMY9A==", + "version": "3.9.0-beta.4", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.9.0-beta.4.tgz", + "integrity": "sha512-T1T6g1+qusecCGySLA4FSn5O1u+RyZkhIaO6De1701HrNcJM7jBZjYsN1qHr49vQ7TMeibtKyiVqpqZSoPeBBg==", "requires": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", diff --git a/package.json b/package.json index 2e69580..ad763f4 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "passport-hubspot-oauth2": "^1.0.3", "passport-mailchimp": "^1.1.0", "protospack": "2.5.2", - "protospack-v2": "3.8.11", + "protospack-v2": "^3.9.0-beta.4", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.5.5", diff --git a/src/app.module.ts b/src/app.module.ts index 575f6d8..d9b13dc 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -43,6 +43,7 @@ import { PermissionsModule } from './modules/permissions/permissions.module'; import { DadosferaLogger } from 'dadosfera-logs'; import { TermsOfUseModule } from './modules/termsOfUse/termsOfUse.module'; import { ConnectionModule } from './modules/connection/connection.module'; +import { ConnectionTestModule } from './modules/connection-test/connection-test.module'; const inputClient = new InputsClientConfiguration(); const outputClient = new OutputsClientConfiguration(); @@ -111,6 +112,8 @@ const transformationClient = new TransformationsClientConfiguration(); ...pipelineClient.config(), }, ]), + + ConnectionTestModule, ], }) export class AppModule {} diff --git a/src/modules/connection-test/connection-test.controller.spec.ts b/src/modules/connection-test/connection-test.controller.spec.ts new file mode 100644 index 0000000..efe3fe0 --- /dev/null +++ b/src/modules/connection-test/connection-test.controller.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { ConnectionTestController } from './connection-test.controller'; + +describe('ConnectionTestController', () => { + let controller: ConnectionTestController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [ConnectionTestController], + }).compile(); + + controller = module.get(ConnectionTestController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/src/modules/connection-test/connection-test.controller.ts b/src/modules/connection-test/connection-test.controller.ts new file mode 100644 index 0000000..4fc1e5c --- /dev/null +++ b/src/modules/connection-test/connection-test.controller.ts @@ -0,0 +1,108 @@ +import { + Body, + Controller, + HttpCode, + HttpStatus, + Inject, + Post, + ValidationPipe, +} from '@nestjs/common'; +import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; +import { User, RequestUser } from 'src/authentication/user.decorator'; +import { ConnectionTestService } from './connection-test.service'; +import { + ConnectionTestPingRes, + ConnectionTestPingReq, + ConnectionTestCredentialsReq, + ConnectionTestCredentialsRes, + ConnectionTestListSchemasReq, + ConnectionTestListSchemasRes, + ConnectionTestListTablesReq, + ConnectionTestListTablesRes, + GetTableMetadataRes, + GetTableMetadataReq, +} from './dto/connection-test'; +import { DadosferaLogger } from 'dadosfera-logs'; + +@ApiTags('Connection Test') +@Controller('connection-test') +export class ConnectionTestController { + logger: any; + constructor( + private connectionTestService: ConnectionTestService, + @Inject(DadosferaLogger) + dadosferaLogger: DadosferaLogger, + ) { + this.logger = dadosferaLogger.logger; + } + + @Post('ping') + @ApiOkResponse({ type: ConnectionTestPingRes }) + @HttpCode(HttpStatus.OK) + async connectionTestPing( + @User() user: RequestUser, + @Body(new ValidationPipe()) body: ConnectionTestPingReq, + ) { + this.logger.info('/connection-test/ping', { + user: user.user_id, + customer: user.customer_name, + }); + await this.connectionTestService.connectionTestPing(body); + } + + @Post('credentials') + @ApiOkResponse({ type: ConnectionTestCredentialsRes }) + @HttpCode(HttpStatus.OK) + async connectionTestCredentials( + @User() user: RequestUser, + @Body(new ValidationPipe()) body: ConnectionTestCredentialsReq, + ) { + this.logger.info('/connection-test/credentials', { + user: user.user_id, + customer: user.customer_name, + }); + await this.connectionTestService.connectionTestCredentials(body); + } + + @Post('schemas') + @ApiOkResponse({ type: ConnectionTestListSchemasRes }) + @HttpCode(HttpStatus.OK) + async connectionTestListSchemas( + @User() user: RequestUser, + @Body(new ValidationPipe()) body: ConnectionTestListSchemasReq, + ) { + this.logger.info('/connection-test/schemas', { + user: user.user_id, + customer: user.customer_name, + }); + await this.connectionTestService.connectionTestListSchemas(body); + } + + @Post('tables') + @ApiOkResponse({ type: ConnectionTestListTablesRes }) + @HttpCode(HttpStatus.OK) + async connectionTestListTables( + @User() user: RequestUser, + @Body(new ValidationPipe()) body: ConnectionTestListTablesReq, + ) { + this.logger.info('/connection-test/tables', { + user: user.user_id, + customer: user.customer_name, + }); + await this.connectionTestService.connectionTestListTables(body); + } + + @Post('table-metadata') + @ApiOkResponse({ type: GetTableMetadataRes }) + @HttpCode(HttpStatus.OK) + async getTableMetadata( + @User() user: RequestUser, + @Body(new ValidationPipe()) body: GetTableMetadataReq, + ) { + this.logger.info('/connection-test/tables', { + user: user.user_id, + customer: user.customer_name, + }); + await this.connectionTestService.getTableMetadata(body); + } +} diff --git a/src/modules/connection-test/connection-test.module.ts b/src/modules/connection-test/connection-test.module.ts new file mode 100644 index 0000000..93cc1bf --- /dev/null +++ b/src/modules/connection-test/connection-test.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { ConnectionTestController } from './connection-test.controller'; +import { ConnectionTestService } from './connection-test.service'; +import { DadosferaLogger } from 'dadosfera-logs'; + +@Module({ + controllers: [ConnectionTestController], + providers: [ConnectionTestService, DadosferaLogger], +}) +export class ConnectionTestModule {} diff --git a/src/modules/connection-test/connection-test.service.spec.ts b/src/modules/connection-test/connection-test.service.spec.ts new file mode 100644 index 0000000..3b44f65 --- /dev/null +++ b/src/modules/connection-test/connection-test.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { ConnectionTestService } from './connection-test.service'; + +describe('ConnectionTestService', () => { + let service: ConnectionTestService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ConnectionTestService], + }).compile(); + + service = module.get(ConnectionTestService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/src/modules/connection-test/connection-test.service.ts b/src/modules/connection-test/connection-test.service.ts new file mode 100644 index 0000000..1475ba3 --- /dev/null +++ b/src/modules/connection-test/connection-test.service.ts @@ -0,0 +1,42 @@ +import { Injectable } from '@nestjs/common'; +import { + ConnectionTestCredentialsReq, + ConnectionTestCredentialsRes, + ConnectionTestListSchemasReq, + ConnectionTestListSchemasRes, + ConnectionTestListTablesReq, + ConnectionTestListTablesRes, + ConnectionTestPingReq, + ConnectionTestPingRes, + GetTableMetadataReq, + GetTableMetadataRes, +} from './dto/connection-test'; + +@Injectable() +export class ConnectionTestService { + async connectionTestPing( + body: ConnectionTestPingReq, + ): Promise { + return { operation_result: true }; + } + async connectionTestCredentials( + body: ConnectionTestCredentialsReq, + ): Promise { + return { operation_result: true, connection_id: '' }; + } + async connectionTestListSchemas( + body: ConnectionTestListSchemasReq, + ): Promise { + return { operation_result: true, schema_list: [] }; + } + async connectionTestListTables( + body: ConnectionTestListTablesReq, + ): Promise { + return { operation_result: true, table_list: [] }; + } + async getTableMetadata( + body: GetTableMetadataReq, + ): Promise { + return { table_metadata: [{ columns: [], table_name: '' }] }; + } +} diff --git a/src/modules/connection-test/dto/connection-test.ts b/src/modules/connection-test/dto/connection-test.ts new file mode 100644 index 0000000..1addb78 --- /dev/null +++ b/src/modules/connection-test/dto/connection-test.ts @@ -0,0 +1,112 @@ +import { ApiProperty, ApiPropertyOptional, OmitType } from '@nestjs/swagger'; +import { IsString, IsObject } from 'class-validator'; +import { DatabaseConnectionPropertiesDto } from 'src/modules/connection/dtos/connection'; +import { CreateConnectionDto } from 'src/modules/connection/dtos/create-connection'; +export class ColumnDto { + @ApiProperty() + name: string; + @ApiProperty() + type: string; +} +export class TableMetadataDto { + @ApiProperty() + table_name: string; + @ApiProperty({ type: [ColumnDto] }) + columns: ColumnDto[]; +} + +export class ConnectionTestPingReq { + @ApiProperty() + @IsString() + plugin: string; + + @ApiProperty() + @IsString() + endpoint: string; + + @ApiProperty() + @IsString() + port: string; + + @ApiPropertyOptional() + @IsString() + network_config_id?: string; +} + +export class ConnectionTestPingRes { + @ApiProperty() + operation_result: boolean; +} + +export class ConnectionTestCredentialsReq extends OmitType( + CreateConnectionDto, + ['properties'], +) { + @ApiProperty() + @IsObject() + properties: DatabaseConnectionPropertiesDto; +} + +export class ConnectionTestCredentialsRes { + @ApiProperty() + operation_result: boolean; + @ApiProperty() + connection_id: string; +} + +export class ConnectionTestListSchemasReq { + @ApiProperty() + @IsString() + plugin: string; + + @ApiProperty() + @IsString() + connection_id: string; +} + +export class ConnectionTestListSchemasRes { + @ApiProperty() + operation_result: boolean; + @ApiProperty() + schema_list: string[]; +} + +export class ConnectionTestListTablesReq { + @ApiProperty() + @IsString() + plugin: string; + + @ApiProperty() + @IsString() + connection_id: string; + + @ApiProperty() + @IsString() + schema_name: string; +} + +export class ConnectionTestListTablesRes { + @ApiProperty() + operation_result: boolean; + @ApiProperty() + table_list: string[]; +} + +export class GetTableMetadataReq { + @ApiProperty() + @IsString() + plugin: string; + + @ApiProperty() + @IsString() + schema: string; + + @ApiProperty() + @IsString() + table_list: string[]; +} + +export class GetTableMetadataRes { + @ApiProperty({ type: [TableMetadataDto] }) + table_metadata: TableMetadataDto[]; +} diff --git a/src/modules/connection/dtos/connection.ts b/src/modules/connection/dtos/connection.ts new file mode 100644 index 0000000..d68872c --- /dev/null +++ b/src/modules/connection/dtos/connection.ts @@ -0,0 +1,18 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class DatabaseConnectionPropertiesDto { + credentials_type: 'basic_auth'; + @ApiProperty() + plugin: string; + @ApiProperty() + host: string; + @ApiProperty() + port: string; + @ApiProperty() + username: string; + @ApiProperty() + @ApiProperty() + password: string; + @ApiProperty() + database: string; +} diff --git a/src/modules/connection/dtos/create-connection.ts b/src/modules/connection/dtos/create-connection.ts index f5db7b2..00e75be 100644 --- a/src/modules/connection/dtos/create-connection.ts +++ b/src/modules/connection/dtos/create-connection.ts @@ -1,4 +1,4 @@ -import { ApiProperty } from '@nestjs/swagger'; +import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; import { IsString, IsObject } from 'class-validator'; export class CreateConnectionDto { @@ -14,9 +14,9 @@ export class CreateConnectionDto { @IsString() description: string; - @ApiProperty() + @ApiPropertyOptional() @IsString() - network_config_id: string; + network_config_id?: string; @ApiProperty() @IsString()