FEAT: network config module using in-factory

This commit is contained in:
Gabriel Rosa
2022-07-28 15:41:21 -03:00
parent 97214280bc
commit 43cec9b6fa
7 changed files with 138 additions and 33 deletions
+7 -7
View File
@@ -38,7 +38,7 @@
"passport-hubspot-oauth2": "^1.0.3",
"passport-mailchimp": "^1.1.0",
"protospack": "2.5.2",
"protospack-v2": "^3.9.0-beta.8",
"protospack-v2": "^3.9.0-beta.11",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.5.5",
@@ -9018,9 +9018,9 @@
}
},
"node_modules/protospack-v2": {
"version": "3.9.0-beta.8",
"resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.9.0-beta.8.tgz",
"integrity": "sha512-C3h62JYRm8Xh5X6mPnKF7ZZA0Ds0qMdu/gFYBu+FEfME5segFgbuiivepkqoVQ3MUoN1gIyV0J5ezmV9kXPPTw==",
"version": "3.9.0-beta.11",
"resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.9.0-beta.11.tgz",
"integrity": "sha512-l2tU5vIlwnr7zgqsX8szMJoOzE9CdBYRJEj48yZc1AK9xVcXZa7+/Ik4n/N/JTAjKsCmQspbxuBezSbKcCpsvA==",
"dependencies": {
"@grpc/grpc-js": "^1.6.7",
"rxjs": "^7.5.5",
@@ -17247,9 +17247,9 @@
}
},
"protospack-v2": {
"version": "3.9.0-beta.8",
"resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.9.0-beta.8.tgz",
"integrity": "sha512-C3h62JYRm8Xh5X6mPnKF7ZZA0Ds0qMdu/gFYBu+FEfME5segFgbuiivepkqoVQ3MUoN1gIyV0J5ezmV9kXPPTw==",
"version": "3.9.0-beta.11",
"resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.9.0-beta.11.tgz",
"integrity": "sha512-l2tU5vIlwnr7zgqsX8szMJoOzE9CdBYRJEj48yZc1AK9xVcXZa7+/Ik4n/N/JTAjKsCmQspbxuBezSbKcCpsvA==",
"requires": {
"@grpc/grpc-js": "^1.6.7",
"rxjs": "^7.5.5",
+1 -1
View File
@@ -53,7 +53,7 @@
"passport-hubspot-oauth2": "^1.0.3",
"passport-mailchimp": "^1.1.0",
"protospack": "2.5.2",
"protospack-v2": "^3.9.0-beta.8",
"protospack-v2": "^3.9.0-beta.11",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.5.5",
@@ -13,7 +13,7 @@ export enum NetworkConfigPluginsEnum {
}
export class NetworkConfigDto {
@ApiProperty()
config_id: string;
id: string;
@ApiProperty()
secret_id: string;
@ApiProperty()
@@ -23,9 +23,9 @@ export class NetworkConfigDto {
@ApiProperty()
description: string;
@ApiProperty({ enum: NetworkConfigTypesEnum })
type: NetworkConfigTypesEnum;
type: string;
@ApiProperty({ enum: NetworkConfigPluginsEnum })
plugin: NetworkConfigPluginsEnum;
plugin: string;
@ApiProperty()
credentials: any;
@ApiProperty()
@@ -68,11 +68,7 @@ export class CreateNetworkConfigReq extends PickType(NetworkConfigDto, [
}
export class CreateNetworkConfigRes {
@ApiProperty()
id: string;
@ApiProperty()
name: string;
@ApiProperty()
description: string;
network_config: NetworkConfigNoSecrets;
}
export class UpdateNetworkConfigReq {
@@ -83,9 +79,10 @@ export class UpdateNetworkConfigReq {
}
export class UpdateNetworkConfigRes {
@ApiProperty()
id: string;
network_config: NetworkConfigNoSecrets;
}
export class DeleteNetworkConfigRes {
@ApiProperty()
name: string;
@ApiProperty()
description: string;
message: string;
}
@@ -0,0 +1,36 @@
import { credentials } from '@grpc/grpc-js';
import {
ClientProviderOptions,
GrpcOptions,
Transport,
} from '@nestjs/microservices';
import { NetworkConfig } from 'protospack-v2';
export class NetworkConfigClientConfiguration {
private config: GrpcOptions = {
transport: Transport.GRPC,
options: {
url: process.env.INFACTORY_URL,
package: [
NetworkConfig.ProtoPackages.ReadPackage,
NetworkConfig.ProtoPackages.WritePackage,
],
credentials:
process.env.ENV === 'local' ? undefined : credentials.createSsl(),
protoPath: [
NetworkConfig.ProtoPaths.ReadFilePath,
NetworkConfig.ProtoPaths.WriteFilePath,
],
loader: {
keepCase: true,
enums: String,
objects: true,
arrays: true,
},
},
};
providerOptions: ClientProviderOptions = {
name: 'NetworkConfigGrpcClient',
...this.config,
};
}
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Delete,
Get,
Inject,
Param,
@@ -14,6 +15,7 @@ import { RequestUser, User } from 'src/authentication/user.decorator';
import {
CreateNetworkConfigReq,
CreateNetworkConfigRes,
DeleteNetworkConfigRes,
FindAllNetworkConfigsRes,
UpdateNetworkConfigReq,
UpdateNetworkConfigRes,
@@ -39,6 +41,19 @@ export class NetworkConfigController {
return this.networkConfigService.findAllNetworkConfigs(user.customer_name);
}
@Get(':id')
@ApiOkResponse({ type: FindAllNetworkConfigsRes })
async findNetworkConfigById(
@User() user: RequestUser,
@Param('id') id: string,
) {
this.logger.info('GET /network-config/:id', {
user: user.user_id,
customer: user.customer_name,
});
return this.networkConfigService.findNetworkConfigById(id);
}
@Post()
@ApiOkResponse({ type: CreateNetworkConfigRes })
async createNetworkConfigs(
@@ -62,7 +77,7 @@ export class NetworkConfigController {
@Param('id') network_config_id: string,
@Body() body: UpdateNetworkConfigReq,
) {
this.logger.info('POST /network-config', {
this.logger.info('PATCH /network-config/:id', {
user: user.user_id,
customer: user.customer_name,
});
@@ -72,16 +87,16 @@ export class NetworkConfigController {
);
}
@Get(':id')
@ApiOkResponse({ type: FindAllNetworkConfigsRes })
async findNetworkConfigById(
@Delete(':id')
@ApiOkResponse({ type: DeleteNetworkConfigRes })
async deleteNetworkConfig(
@User() user: RequestUser,
@Param('id') id: string,
@Param('id') network_config_id: string,
) {
this.logger.info('GET /network-config', {
this.logger.info('DELETE /network-config', {
user: user.user_id,
customer: user.customer_name,
});
return this.networkConfigService.findNetworkConfigById(id);
return this.networkConfigService.deleteNetworkConfig(network_config_id);
}
}
@@ -2,9 +2,13 @@ import { Module } from '@nestjs/common';
import { NetworkConfigController } from './network-config.controller';
import { NetworkConfigService } from './network-config.service';
import { DadosferaLogger } from 'dadosfera-logs';
import { ClientsModule } from '@nestjs/microservices';
import { NetworkConfigClientConfiguration } from './network-config-client.config';
const client = new NetworkConfigClientConfiguration();
@Module({
controllers: [NetworkConfigController],
providers: [NetworkConfigService, DadosferaLogger],
imports: [ClientsModule.register([client.providerOptions])],
})
export class NetworkConfigModule {}
@@ -1,7 +1,11 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices';
import { NetworkConfig } from 'protospack-v2';
import { lastValueFrom } from 'rxjs';
import {
CreateNetworkConfigReq,
CreateNetworkConfigRes,
DeleteNetworkConfigRes,
FindAllNetworkConfigsRes,
FindNetworkConfigByidRes,
UpdateNetworkConfigReq,
@@ -10,22 +14,54 @@ import {
@Injectable()
export class NetworkConfigService {
private networkConfigReadClient: NetworkConfig.ReadService.NetworkConfigReadService;
private networkConfigWriteClient: NetworkConfig.WriteService.NetworkConfigWriteService;
constructor(
@Inject('NetworkConfigGrpcClient') private readonly grpcClient: ClientGrpc,
) {
this.networkConfigReadClient =
grpcClient.getService<NetworkConfig.ReadService.NetworkConfigReadService>(
NetworkConfig.ProtoServices.NetworkConfigReadService,
);
this.networkConfigWriteClient =
grpcClient.getService<NetworkConfig.WriteService.NetworkConfigWriteService>(
NetworkConfig.ProtoServices.NetworkConfigWriteService,
);
}
async findAllNetworkConfigs(
customer_name: string,
): Promise<FindAllNetworkConfigsRes> {
return { network_configs: [] };
return lastValueFrom(
this.networkConfigReadClient.NetworkConfigFindByCustomer({
customer_name,
}),
);
}
async findNetworkConfigById(id: string): Promise<FindNetworkConfigByidRes> {
return { network_config: undefined };
const { network_config } = await lastValueFrom(
this.networkConfigReadClient.NetworkConfigFindOneById({
id,
}),
);
return { network_config };
}
async createNetworkConfig(
body: CreateNetworkConfigReq,
customer_name: string,
): Promise<CreateNetworkConfigRes> {
const { description, name } = body;
return { description, id: '', name };
const { description, name, properties, type } = body;
const { network_config } = await lastValueFrom(
this.networkConfigWriteClient.NetworkConfigCreate({
customer_name,
description,
name,
properties: JSON.stringify(properties),
type,
}),
);
return { network_config };
}
async updateNetworkConfig(
@@ -33,6 +69,23 @@ export class NetworkConfigService {
network_config_id: string,
): Promise<UpdateNetworkConfigRes> {
const { description, name } = body;
return { description, id: network_config_id, name };
const { network_config } = await lastValueFrom(
this.networkConfigWriteClient.NetworkConfigUpdate({
id: network_config_id,
description,
name,
}),
);
return { network_config };
}
async deleteNetworkConfig(
network_config_id: string,
): Promise<DeleteNetworkConfigRes> {
return lastValueFrom(
this.networkConfigWriteClient.NetworkConfigDelete({
id: network_config_id,
}),
);
}
}