新增get_jingrow_firewall_rules端点,前端防火墙列表改为使用get_jingrow_firewall_rules获取本地数据

This commit is contained in:
jingrow 2025-08-09 21:12:31 +08:00
parent bf658c478b
commit 45036ff400
2 changed files with 78 additions and 17 deletions

View File

@ -291,10 +291,11 @@ export default {
resources: { resources: {
firewallRules() { firewallRules() {
return { return {
url: 'jcloud.api.aliyun_server_light.get_aliyun_firewall_rules', url: 'jcloud.api.aliyun_server_light.get_jingrow_firewall_rules',
params: { params: {
instance_id: this.$jsiteServer.pg?.instance_id, instance_id: this.$jsiteServer.pg?.instance_id,
region_id: this.$jsiteServer.pg?.region limit: this.pagination.limit,
pageno: this.pagination.pageno
}, },
auto: true, auto: true,
onSuccess: (response) => { onSuccess: (response) => {
@ -311,22 +312,13 @@ export default {
isNew: false isNew: false
})); }));
// // 使API
const total = this.firewallRules.length;
const limit = this.pagination.limit;
const pageno = this.pagination.pageno;
const start = (pageno - 1) * limit;
const end = start + limit;
this.pagination = { this.pagination = {
pageno: pageno, pageno: response.data.pageno,
limit: limit, limit: response.data.limit,
total: total, total: response.data.total,
pagecount: Math.ceil(total / limit) pagecount: response.data.pagecount
}; };
//
this.firewallRules = this.firewallRules.slice(start, end);
} else { } else {
this.error = response.message || '获取防火墙规则失败'; this.error = response.message || '获取防火墙规则失败';
} }

View File

@ -1782,3 +1782,72 @@ def sync_firewall_rules(instance_id):
except Exception as e: except Exception as e:
jingrow.log_error("同步防火墙规则失败", f"同步实例 {instance_id} 防火墙规则时发生错误: {str(e)}") jingrow.log_error("同步防火墙规则失败", f"同步实例 {instance_id} 防火墙规则时发生错误: {str(e)}")
return {"success": False, "error": str(e), "message": "同步防火墙规则失败"} return {"success": False, "error": str(e), "message": "同步防火墙规则失败"}
@jingrow.whitelist()
def get_jingrow_firewall_rules(**data):
"""获取防火墙规则(从本地数据库获取,支持分页)"""
try:
instance_id = data.get('instance_id')
limit = data.get('limit', 20)
pageno = data.get('pageno', 1)
if not instance_id:
return {"success": False, "message": "缺少实例ID参数"}
# 查找对应的Jsite Server记录
server_records = jingrow.get_all(
"Jsite Server",
{"instance_id": instance_id},
["name"]
)
if not server_records:
return {"success": False, "message": "未找到指定的服务器记录"}
server_record = server_records[0]
# 获取该服务器的所有防火墙规则
firewall_rules = jingrow.get_all(
"Firewall Rules",
{"parent": server_record.name},
["name", "rule_id", "rule_protocol", "port", "source_cidr_ip", "remark"]
)
# 转换为API格式的数据结构
all_items = []
for rule in firewall_rules:
item = {
"rule_id": rule.rule_id or rule.name,
"rule_protocol": rule.rule_protocol or "",
"port": rule.port or "",
"source_cidr_ip": rule.source_cidr_ip or "0.0.0.0/0",
"remark": rule.remark or ""
}
all_items.append(item)
# 对所有记录按协议排序
sorted_items = sorted(all_items, key=lambda x: x.get('rule_protocol', ''))
# 计算分页
total = len(sorted_items)
pagecount = (total + limit - 1) // limit
start_index = (pageno - 1) * limit
end_index = start_index + limit
# 返回当前页的记录
current_page_items = sorted_items[start_index:end_index]
# 返回格式化的防火墙规则信息
return {
"success": True,
"data": {
"pageno": pageno,
"limit": limit,
"total": total,
"pagecount": pagecount,
"firewall_rules": current_page_items
}
}
except Exception as e:
return {"success": False, "message": f"防火墙规则查询失败: {str(e)}"}