Files
NetworkDeviceManagementSystem/backend/modules/monitor_config/controller.py
QuangMinh_123 2683cdb882 Device
2026-05-27 13:50:27 +07:00

150 lines
4.7 KiB
Python

# pyrefly: ignore [missing-import]
from flask import request
from common.response.api_response import success_response
from modules.monitor_config.service import (
get_monitor_config_service,
update_monitor_config_service,
test_connection_service
)
from modules.monitor_config.schemas import (
UpdateMonitorConfigSchema,
TestConnectionSchema
)
# ============================================
# CONTROLLER: Nhận request → Validate → Gọi Service → Trả response
# ============================================
# Luồng đi tổng thể:
#
# Client (Frontend/Postman)
# │
# ├── GET /api/devices/{device_id}/monitor-config → get_monitor_config()
# ├── PUT /api/devices/{device_id}/monitor-config → update_monitor_config()
# └── POST /api/devices/{device_id}/monitor-config/test → test_connection()
# │
# ▼
# Controller (file này)
# │ 1. Lấy dữ liệu từ request (params, body)
# │ 2. Validate bằng Marshmallow Schema
# │ 3. Gọi Service xử lý logic
# │
# ▼
# Service → Repository → Database
#
# Controller KHÔNG chứa business logic (check tồn tại, tính toán...)
# Controller CHỈ là cầu nối giữa HTTP request và Service layer
# ============================================
def get_monitor_config(device_id):
"""
GET /api/devices/<device_id>/monitor-config
Lấy cấu hình giám sát hiện tại của thiết bị.
Luồng đi:
1. Nhận device_id từ URL path
2. Gọi service → service check thiết bị tồn tại → trả config
3. Trả response thành công kèm data
"""
config = get_monitor_config_service(device_id)
return success_response(
data=config,
message="Monitor config retrieved successfully"
)
def update_monitor_config(device_id):
"""
PUT /api/devices/<device_id>/monitor-config
Cập nhật cấu hình giám sát của thiết bị.
Luồng đi:
1. Nhận device_id từ URL path + JSON body từ request
2. Validate body bằng UpdateMonitorConfigSchema
→ Marshmallow kiểm tra: kiểu dữ liệu, range, enum...
→ Nếu sai → ném ValidationError → Global handler trả 400
→ Nếu đúng → loại bỏ trường thừa, áp dụng defaults
3. Gọi service → service check tồn tại → cập nhật DB → reschedule job
4. Trả response thành công kèm config đã cập nhật
Ví dụ request body:
{
"enable_ping": true,
"ping_interval": 30,
"ping_count": 5,
"enable_snmp": true,
"snmp_community": "public",
"snmp_version": "v2c",
"snmp_port": 161
}
"""
body = request.get_json()
# Validate bằng schema — loại bỏ trường không hợp lệ, check range/enum
schema = UpdateMonitorConfigSchema()
data = schema.load(body)
# Gọi service để xử lý logic nghiệp vụ
updated_config = update_monitor_config_service(device_id, data)
return success_response(
data=updated_config,
message="Monitor config updated successfully"
)
def test_connection(device_id):
"""
POST /api/devices/<device_id>/monitor-config/test
Kiểm tra kết nối tới thiết bị (Ping và/hoặc SNMP).
Luồng đi:
1. Nhận device_id từ URL path + JSON body chứa tham số test
2. Validate body bằng TestConnectionSchema
3. Gọi service → service lấy IP thiết bị → chạy Ping/SNMP test
4. Trả kết quả kiểm tra (Up/Down, RTT, chi tiết SNMP)
Ví dụ request body (test cả Ping và SNMP):
{
"test_ping": true,
"ping_count": 3,
"ping_timeout": 5,
"test_snmp": true,
"snmp_community": "public",
"snmp_version": "v2c",
"snmp_port": 161,
"snmp_timeout": 5
}
Ví dụ response:
{
"success": true,
"data": {
"ping_result": {
"status": "up",
"method": "icmplib",
"avg_rtt_ms": 12.5,
"packet_loss": 0.0
},
"snmp_result": {
"status": "up",
"method": "snmp",
"snmp_data": {"1.3.6.1.2.1.1.1.0": "Cisco IOS XR Software..."}
}
}
}
"""
body = request.get_json() or {}
# Validate tham số test
schema = TestConnectionSchema()
test_params = schema.load(body)
# Gọi service chạy test kết nối
results = test_connection_service(device_id, test_params)
return success_response(
data=results,
message="Connection test completed"
)