Files
maestro/src/modules/network-config/network-config.service.ts
T

159 lines
4.5 KiB
TypeScript

import { Metadata } from '@grpc/grpc-js';
import { Inject, Injectable } from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices';
import { NetworkConfig } from '@dadosfera/protospack-v2';
import { lastValueFrom } from 'rxjs';
import {
CreateNetworkConfigReq,
CreateNetworkConfigRes,
DeleteNetworkConfigRes,
FindAllNetworkConfigsRes,
FindNetworkConfigByidDetailsRes,
FindNetworkConfigByidRes,
UpdateNetworkConfigReq,
UpdateNetworkConfigRes,
} from './dto/network-config';
import { NetworkConfigGrpcClient } from './network-config-client.config';
@Injectable()
export class NetworkConfigService {
private networkConfigReadClient: NetworkConfig.ReadService.NetworkConfigReadService;
private networkConfigWriteClient: NetworkConfig.WriteService.NetworkConfigWriteService;
constructor(
@Inject(NetworkConfigGrpcClient.name)
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({
body,
info,
}): Promise<FindAllNetworkConfigsRes> {
const { search, page, size, filters } = body;
const { language, customer_id, customer_name, user_id } = info;
const meta = new Metadata();
meta.add('language', language);
meta.add('customer_id', customer_id);
meta.add('customer_name', customer_name);
meta.add('user_id', user_id);
return lastValueFrom(
this.networkConfigReadClient.NetworkConfigFindAll(
{ search, filters: JSON.stringify(filters), page, size },
meta,
),
);
}
async findNetworkConfigById(id: string): Promise<FindNetworkConfigByidRes> {
const { network_config } = await lastValueFrom(
this.networkConfigReadClient.NetworkConfigFindOneById({
id,
}),
);
return { network_config };
}
async findNetworkConfigByIdDetails(
id: string,
info?,
): Promise<FindNetworkConfigByidDetailsRes> {
const { customer_name, customer_id, language } = info;
const meta = new Metadata();
meta.add('customer_name', customer_name);
meta.add('customer_id', customer_id);
meta.add('language', language);
const { network_config } = await lastValueFrom(
this.networkConfigReadClient.NetworkConfigFindOneDetailsById(
{ id },
meta,
),
);
Object.assign(network_config, {
properties: JSON.parse(network_config.properties),
});
return { network_config };
}
async createNetworkConfig(
body: CreateNetworkConfigReq,
file: Express.Multer.File,
{ customer_name, customer_id, user_id },
): Promise<CreateNetworkConfigRes> {
const { description, name, network_type_id, network_type_name } = body;
const type = 'vpn';
const plugin = 'openvpn';
const OVPN_FILE_STRING = Buffer.from(file.buffer).toString();
const meta = new Metadata();
meta.add('customer_id', customer_id);
meta.add('customer_name', customer_name);
meta.add('user_id', user_id);
const { network_config } = await lastValueFrom(
this.networkConfigWriteClient.NetworkConfigCreate(
{
description,
name,
properties: JSON.stringify({
OVPN_FILE: OVPN_FILE_STRING,
plugin,
}),
type,
network_type_id,
network_type_name,
plugin,
},
meta,
),
);
return { network_config };
}
async updateNetworkConfig(
body: UpdateNetworkConfigReq,
network_config_id: string,
): Promise<UpdateNetworkConfigRes> {
const { description, name } = body;
const { network_config } = await lastValueFrom(
this.networkConfigWriteClient.NetworkConfigUpdate({
id: network_config_id,
description,
name,
}),
);
return { network_config };
}
async deleteNetworkConfig(
network_config_id: string,
{ customer_name, customer_id },
): Promise<DeleteNetworkConfigRes> {
const meta = new Metadata();
meta.add('customer_id', customer_id);
meta.add('customer_name', customer_name);
return lastValueFrom(
this.networkConfigWriteClient.NetworkConfigDelete(
{ id: network_config_id },
meta,
),
);
}
}