mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-21 22:49:08 +00:00
FEAT: network config module
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import {
|
||||
ApiProperty,
|
||||
ApiPropertyOptional,
|
||||
OmitType,
|
||||
PickType,
|
||||
} from '@nestjs/swagger';
|
||||
|
||||
export enum NetworkConfigTypesEnum {
|
||||
vpn = 'vpn',
|
||||
}
|
||||
export enum NetworkConfigPluginsEnum {
|
||||
openvpn = 'openvpn',
|
||||
}
|
||||
export class NetworkConfigDto {
|
||||
@ApiProperty()
|
||||
config_id: string;
|
||||
@ApiProperty()
|
||||
secret_id: string;
|
||||
@ApiProperty()
|
||||
name: string;
|
||||
@ApiProperty()
|
||||
customer_name: string;
|
||||
@ApiProperty()
|
||||
description: string;
|
||||
@ApiProperty({ enum: NetworkConfigTypesEnum })
|
||||
type: NetworkConfigTypesEnum;
|
||||
@ApiProperty({ enum: NetworkConfigPluginsEnum })
|
||||
plugin: NetworkConfigPluginsEnum;
|
||||
@ApiProperty()
|
||||
credentials: any;
|
||||
@ApiProperty()
|
||||
created_at: string;
|
||||
@ApiProperty()
|
||||
updated_at: string;
|
||||
}
|
||||
export class NetworkConfigNoSecrets extends OmitType(NetworkConfigDto, [
|
||||
'secret_id',
|
||||
'credentials',
|
||||
]) {}
|
||||
export class NetworkConfigOpenVpnProperties {
|
||||
@ApiProperty({ enum: NetworkConfigPluginsEnum })
|
||||
plugin: NetworkConfigPluginsEnum;
|
||||
@ApiProperty()
|
||||
OVPN_FILE: string;
|
||||
@ApiPropertyOptional()
|
||||
CERTIFICATE_FILE?: string;
|
||||
@ApiPropertyOptional()
|
||||
OVPN_CREDENTIALS?: string;
|
||||
@ApiPropertyOptional()
|
||||
TLS_AUTH_FILE?: string;
|
||||
}
|
||||
export class FindAllNetworkConfigsRes {
|
||||
@ApiProperty({ type: [NetworkConfigNoSecrets] })
|
||||
network_configs: NetworkConfigNoSecrets[];
|
||||
}
|
||||
|
||||
export class FindNetworkConfigByidRes {
|
||||
@ApiProperty()
|
||||
network_config: NetworkConfigNoSecrets;
|
||||
}
|
||||
export class CreateNetworkConfigReq extends PickType(NetworkConfigDto, [
|
||||
'name',
|
||||
'description',
|
||||
'type',
|
||||
]) {
|
||||
@ApiProperty()
|
||||
properties: NetworkConfigOpenVpnProperties;
|
||||
}
|
||||
export class CreateNetworkConfigRes {
|
||||
@ApiProperty()
|
||||
id: string;
|
||||
@ApiProperty()
|
||||
name: string;
|
||||
@ApiProperty()
|
||||
description: string;
|
||||
}
|
||||
|
||||
export class UpdateNetworkConfigReq {
|
||||
@ApiProperty()
|
||||
name: string;
|
||||
@ApiProperty()
|
||||
description: string;
|
||||
}
|
||||
export class UpdateNetworkConfigRes {
|
||||
@ApiProperty()
|
||||
id: string;
|
||||
@ApiProperty()
|
||||
name: string;
|
||||
@ApiProperty()
|
||||
description: string;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { NetworkConfigController } from './network-config.controller';
|
||||
|
||||
describe('NetworkConfigController', () => {
|
||||
let controller: NetworkConfigController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [NetworkConfigController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<NetworkConfigController>(NetworkConfigController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,87 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Inject,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
} from '@nestjs/common';
|
||||
import { NetworkConfigService } from './network-config.service';
|
||||
import { DadosferaLogger } from 'dadosfera-logs';
|
||||
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { RequestUser, User } from 'src/authentication/user.decorator';
|
||||
import {
|
||||
CreateNetworkConfigReq,
|
||||
CreateNetworkConfigRes,
|
||||
FindAllNetworkConfigsRes,
|
||||
UpdateNetworkConfigReq,
|
||||
UpdateNetworkConfigRes,
|
||||
} from './dto/network-config';
|
||||
|
||||
@ApiTags('Network Config')
|
||||
@Controller('network-config')
|
||||
export class NetworkConfigController {
|
||||
logger: any;
|
||||
constructor(
|
||||
private networkConfigService: NetworkConfigService,
|
||||
@Inject(DadosferaLogger) dadosferaLogger: DadosferaLogger,
|
||||
) {
|
||||
this.logger = dadosferaLogger.logger;
|
||||
}
|
||||
@Get()
|
||||
@ApiOkResponse({ type: FindAllNetworkConfigsRes })
|
||||
async findAllNetworkConfigs(@User() user: RequestUser) {
|
||||
this.logger.info('GET /network-config', {
|
||||
user: user.user_id,
|
||||
customer: user.customer_name,
|
||||
});
|
||||
return this.networkConfigService.findAllNetworkConfigs(user.customer_name);
|
||||
}
|
||||
|
||||
@Post()
|
||||
@ApiOkResponse({ type: CreateNetworkConfigRes })
|
||||
async createNetworkConfigs(
|
||||
@User() user: RequestUser,
|
||||
@Body() body: CreateNetworkConfigReq,
|
||||
) {
|
||||
this.logger.info('POST /network-config', {
|
||||
user: user.user_id,
|
||||
customer: user.customer_name,
|
||||
});
|
||||
return this.networkConfigService.createNetworkConfig(
|
||||
body,
|
||||
user.customer_name,
|
||||
);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@ApiOkResponse({ type: UpdateNetworkConfigRes })
|
||||
async updateNetworkConfig(
|
||||
@User() user: RequestUser,
|
||||
@Param('id') network_config_id: string,
|
||||
@Body() body: UpdateNetworkConfigReq,
|
||||
) {
|
||||
this.logger.info('POST /network-config', {
|
||||
user: user.user_id,
|
||||
customer: user.customer_name,
|
||||
});
|
||||
return this.networkConfigService.updateNetworkConfig(
|
||||
body,
|
||||
network_config_id,
|
||||
);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOkResponse({ type: FindAllNetworkConfigsRes })
|
||||
async findNetworkConfigById(
|
||||
@User() user: RequestUser,
|
||||
@Param('id') id: string,
|
||||
) {
|
||||
this.logger.info('GET /network-config', {
|
||||
user: user.user_id,
|
||||
customer: user.customer_name,
|
||||
});
|
||||
return this.networkConfigService.findNetworkConfigById(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { NetworkConfigController } from './network-config.controller';
|
||||
import { NetworkConfigService } from './network-config.service';
|
||||
import { DadosferaLogger } from 'dadosfera-logs';
|
||||
|
||||
@Module({
|
||||
controllers: [NetworkConfigController],
|
||||
providers: [NetworkConfigService, DadosferaLogger],
|
||||
})
|
||||
export class NetworkConfigModule {}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { NetworkConfigService } from './network-config.service';
|
||||
|
||||
describe('NetworkConfigService', () => {
|
||||
let service: NetworkConfigService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [NetworkConfigService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<NetworkConfigService>(NetworkConfigService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
CreateNetworkConfigReq,
|
||||
CreateNetworkConfigRes,
|
||||
FindAllNetworkConfigsRes,
|
||||
FindNetworkConfigByidRes,
|
||||
UpdateNetworkConfigReq,
|
||||
UpdateNetworkConfigRes,
|
||||
} from './dto/network-config';
|
||||
|
||||
@Injectable()
|
||||
export class NetworkConfigService {
|
||||
async findAllNetworkConfigs(
|
||||
customer_name: string,
|
||||
): Promise<FindAllNetworkConfigsRes> {
|
||||
return { network_configs: [] };
|
||||
}
|
||||
|
||||
async findNetworkConfigById(id: string): Promise<FindNetworkConfigByidRes> {
|
||||
return { network_config: undefined };
|
||||
}
|
||||
|
||||
async createNetworkConfig(
|
||||
body: CreateNetworkConfigReq,
|
||||
customer_name: string,
|
||||
): Promise<CreateNetworkConfigRes> {
|
||||
const { description, name } = body;
|
||||
return { description, id: '', name };
|
||||
}
|
||||
|
||||
async updateNetworkConfig(
|
||||
body: UpdateNetworkConfigReq,
|
||||
network_config_id: string,
|
||||
): Promise<UpdateNetworkConfigRes> {
|
||||
const { description, name } = body;
|
||||
return { description, id: network_config_id, name };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user