mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-07 03:59:09 +00:00
FEAT: Connection Manager
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { credentials } from '@grpc/grpc-js';
|
||||
import { ClientOptions, Transport } from '@nestjs/microservices';
|
||||
import { ConnectionManager } from 'protospack-v2';
|
||||
|
||||
export class ConnectionClientConfiguration {
|
||||
config(): ClientOptions {
|
||||
return {
|
||||
transport: Transport.GRPC,
|
||||
options: {
|
||||
url: process.env.INFACTORY_URL,
|
||||
package: [
|
||||
ConnectionManager.ProtoPackages.WritePackage,
|
||||
ConnectionManager.ProtoPackages.ReadPackage,
|
||||
],
|
||||
// credentials: undefined,
|
||||
credentials: process.env.LOCAL_ENV
|
||||
? undefined
|
||||
: credentials.createSsl(),
|
||||
protoPath: [
|
||||
ConnectionManager.ProtoPaths.WriteFilePath,
|
||||
ConnectionManager.ProtoPaths.ReadFilePath,
|
||||
],
|
||||
loader: {
|
||||
keepCase: true,
|
||||
enums: String,
|
||||
objects: true,
|
||||
arrays: true,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { Metadata } from '@grpc/grpc-js';
|
||||
import {
|
||||
OnModuleInit,
|
||||
Inject,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { ClientGrpc } from '@nestjs/microservices';
|
||||
import { ConnectionManager } from 'protospack-v2';
|
||||
import { lastValueFrom } from 'rxjs';
|
||||
|
||||
export class ConnectionClientService implements OnModuleInit {
|
||||
private connectionServiceRead: ConnectionManager.ReadService.ConnectionManagerReadServices;
|
||||
private connectionServiceWrite: ConnectionManager.WriteService.ConnectionManagerWriteServices;
|
||||
constructor(
|
||||
@Inject('CONNECTION_PACKAGE') private readonly grpcClient: ClientGrpc,
|
||||
) {}
|
||||
|
||||
onModuleInit() {
|
||||
this.connectionServiceWrite =
|
||||
this.grpcClient.getService<ConnectionManager.WriteService.ConnectionManagerWriteServices>(
|
||||
ConnectionManager.ProtoServices.ConnectionManagerWriteServices,
|
||||
);
|
||||
|
||||
this.connectionServiceRead =
|
||||
this.grpcClient.getService<ConnectionManager.ReadService.ConnectionManagerReadServices>(
|
||||
ConnectionManager.ProtoServices.ConnectionManagerReadServices,
|
||||
);
|
||||
}
|
||||
|
||||
async createConnection({ body, info }) {
|
||||
const meta = new Metadata();
|
||||
meta.add('customer_id', info.customer_id);
|
||||
meta.add('customer_name', info.customer_name);
|
||||
meta.add('user_id', info.user_id);
|
||||
|
||||
const {
|
||||
connector_id,
|
||||
description,
|
||||
image_url,
|
||||
name,
|
||||
network_config_id,
|
||||
properties,
|
||||
type,
|
||||
} = body;
|
||||
|
||||
return lastValueFrom(
|
||||
this.connectionServiceWrite.CreateConnection(
|
||||
{
|
||||
connector_id,
|
||||
description,
|
||||
image_url,
|
||||
name,
|
||||
network_config_id,
|
||||
properties: JSON.stringify(properties),
|
||||
type,
|
||||
},
|
||||
meta,
|
||||
),
|
||||
).catch((err) => {
|
||||
throw new HttpException(
|
||||
err.details,
|
||||
err.code === 6 ? HttpStatus.CONFLICT : 400,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Body, Controller, Post, Inject } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { ConnectionClientService } from './client.service';
|
||||
import { CreateConnectionDto } from './dtos/create-connection';
|
||||
import { DadosferaLogger } from 'dadosfera-logs';
|
||||
import {
|
||||
Authenticated,
|
||||
RequireAllPermissions,
|
||||
} from 'src/authentication/authentication.decorator';
|
||||
import { Permissions } from 'src/authentication/permissions.enum';
|
||||
import { User } from 'src/authentication/user.decorator';
|
||||
import { ValidationPipe } from '../../pipes/object-validation.pipe';
|
||||
|
||||
@ApiTags('connections')
|
||||
@ApiBearerAuth()
|
||||
@Authenticated()
|
||||
@Controller('connections')
|
||||
export class ConnectionController {
|
||||
logger: any;
|
||||
constructor(
|
||||
@Inject(DadosferaLogger)
|
||||
dadosferaLogger: DadosferaLogger,
|
||||
private clientService: ConnectionClientService,
|
||||
) {
|
||||
this.logger = dadosferaLogger.logger;
|
||||
}
|
||||
|
||||
@Post()
|
||||
// @RequireAllPermissions(Permissions.CONNECTIONS.CREATE)
|
||||
async createConnection(
|
||||
@User() user,
|
||||
@Body(new ValidationPipe()) body: CreateConnectionDto,
|
||||
) {
|
||||
this.logger.info('/connections - Create Connection');
|
||||
|
||||
const { user_id, customer_id, customer_name } = user;
|
||||
|
||||
const response = await this.clientService.createConnection({
|
||||
body,
|
||||
info: { user_id, customer_id, customer_name },
|
||||
});
|
||||
|
||||
delete response.connection.properties;
|
||||
delete response.connection.user_id;
|
||||
delete response.connection.customer_id;
|
||||
delete response.connection.customer_name;
|
||||
delete response.connection.updated_at;
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ClientsModule } from '@nestjs/microservices';
|
||||
import { ConnectionClientService } from './client.service';
|
||||
import { ConnectionClientConfiguration } from './client.config';
|
||||
import { ConnectionController } from './connection.controller';
|
||||
import { DadosferaLogger } from 'dadosfera-logs';
|
||||
|
||||
const client = new ConnectionClientConfiguration();
|
||||
|
||||
@Module({
|
||||
controllers: [ConnectionController],
|
||||
providers: [ConnectionClientService, DadosferaLogger],
|
||||
imports: [
|
||||
ClientsModule.register([
|
||||
{
|
||||
name: 'CONNECTION_PACKAGE',
|
||||
...client.config(),
|
||||
},
|
||||
]),
|
||||
],
|
||||
})
|
||||
export class ConnectionModule {}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsString, IsObject } from 'class-validator';
|
||||
|
||||
export class CreateConnectionDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
connector_id: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
name: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
description: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
network_config_id: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
type: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
image_url: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsObject()
|
||||
properties: any;
|
||||
}
|
||||
@@ -12,7 +12,6 @@ export class ConnectorClientConfiguration {
|
||||
ConnectorManager.ProtoPackages.WritePackage,
|
||||
ConnectorManager.ProtoPackages.ReadPackage,
|
||||
],
|
||||
// credentials: undefined,
|
||||
credentials: process.env.LOCAL_ENV
|
||||
? undefined
|
||||
: credentials.createSsl(),
|
||||
@@ -21,6 +20,7 @@ export class ConnectorClientConfiguration {
|
||||
ConnectorManager.ProtoPaths.ReadFilePath,
|
||||
],
|
||||
loader: {
|
||||
keepCase: true,
|
||||
enums: String,
|
||||
objects: true,
|
||||
arrays: true,
|
||||
|
||||
Reference in New Issue
Block a user