修复dashboard服务器详情页防火墙列表协议字段为空的问题,创建或删除规则后同步防火墙数据

This commit is contained in:
jingrow 2025-08-09 20:01:35 +08:00
parent a46a1f6db8
commit bf658c478b
4 changed files with 44 additions and 42 deletions

View File

@ -108,7 +108,7 @@
<!-- 协议编辑 --> <!-- 协议编辑 -->
<div v-if="rule.editing" class="flex items-center space-x-2"> <div v-if="rule.editing" class="flex items-center space-x-2">
<select <select
v-model="rule.protocol" v-model="rule.rule_protocol"
class="w-20 px-2 py-1 text-sm border border-gray-300 rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500" class="w-20 px-2 py-1 text-sm border border-gray-300 rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
> >
<option value="TCP">TCP</option> <option value="TCP">TCP</option>
@ -119,12 +119,12 @@
<div v-else> <div v-else>
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium" <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium"
:class="{ :class="{
'bg-blue-100 text-blue-800': rule.protocol === 'TCP', 'bg-blue-100 text-blue-800': rule.rule_protocol === 'TCP',
'bg-green-100 text-green-800': rule.protocol === 'UDP', 'bg-green-100 text-green-800': rule.rule_protocol === 'UDP',
'bg-yellow-100 text-yellow-800': rule.protocol === 'ICMP', 'bg-yellow-100 text-yellow-800': rule.rule_protocol === 'ICMP',
'bg-gray-100 text-gray-800': !rule.protocol 'bg-gray-100 text-gray-800': !rule.rule_protocol
}"> }">
{{ rule.protocol || '未知' }} {{ rule.rule_protocol || '未知' }}
</span> </span>
</div> </div>
</td> </td>
@ -143,8 +143,8 @@
</div> </div>
</td> </td>
<td class="px-4 py-4 whitespace-nowrap"> <td class="px-4 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900 max-w-xs truncate" :title="rule.source_ip"> <div class="text-sm text-gray-900 max-w-xs truncate" :title="rule.source_cidr_ip">
{{ rule.source_ip || '0.0.0.0/0' }} {{ rule.source_cidr_ip || '0.0.0.0/0' }}
</div> </div>
</td> </td>
<td class="px-4 py-4 whitespace-nowrap"> <td class="px-4 py-4 whitespace-nowrap">
@ -303,9 +303,9 @@ export default {
const firewallRules = response.data.firewall_rules || []; const firewallRules = response.data.firewall_rules || [];
this.firewallRules = firewallRules.map(rule => ({ this.firewallRules = firewallRules.map(rule => ({
rule_id: rule.rule_id, rule_id: rule.rule_id,
protocol: rule.protocol, rule_protocol: rule.rule_protocol,
port: rule.port, port: rule.port,
source_ip: rule.source_ip, source_cidr_ip: rule.source_cidr_ip,
remark: rule.remark, remark: rule.remark,
editing: false, editing: false,
isNew: false isNew: false
@ -355,9 +355,9 @@ export default {
addNewRow() { addNewRow() {
const newRule = { const newRule = {
rule_id: null, rule_id: null,
protocol: 'TCP', rule_protocol: 'TCP',
port: '', port: '',
source_ip: '0.0.0.0/0', source_cidr_ip: '0.0.0.0/0',
remark: '', remark: '',
editing: true, editing: true,
isNew: true isNew: true
@ -369,7 +369,7 @@ export default {
// //
async saveRule(rule) { async saveRule(rule) {
if (!rule.protocol || !rule.port) { if (!rule.rule_protocol || !rule.port) {
toast.error('协议和端口不能为空'); toast.error('协议和端口不能为空');
return; return;
} }
@ -379,7 +379,7 @@ export default {
url: 'jcloud.api.aliyun_server_light.create_aliyun_firewall_rule', url: 'jcloud.api.aliyun_server_light.create_aliyun_firewall_rule',
params: { params: {
instance_id: this.$jsiteServer.pg.instance_id, instance_id: this.$jsiteServer.pg.instance_id,
rule_protocol: rule.protocol, rule_protocol: rule.rule_protocol,
port: rule.port, port: rule.port,
remark: rule.remark, remark: rule.remark,
region_id: this.$jsiteServer.pg.region region_id: this.$jsiteServer.pg.region
@ -430,7 +430,7 @@ export default {
deleteRule(rule) { deleteRule(rule) {
confirmDialog({ confirmDialog({
title: '删除防火墙规则', title: '删除防火墙规则',
message: `确定要删除这条防火墙规则吗?\n协议: ${rule.protocol}\n端口: ${rule.port}`, message: `确定要删除这条防火墙规则吗?\n协议: ${rule.rule_protocol}\n端口: ${rule.port}`,
primaryAction: { primaryAction: {
label: '确定', label: '确定',
variant: 'solid', variant: 'solid',

View File

@ -1687,6 +1687,10 @@ def create_aliyun_firewall_rule(instance_id, rule_protocol, port, remark=None, r
manager = _get_manager() manager = _get_manager()
result = manager.create_firewall_rule(instance_id, rule_protocol, port, remark, region_id) result = manager.create_firewall_rule(instance_id, rule_protocol, port, remark, region_id)
# 如果创建成功,同步防火墙规则到本地记录
if result and result.get('success'):
sync_firewall_rules(instance_id)
return result return result
except Exception as e: except Exception as e:
@ -1696,13 +1700,7 @@ def create_aliyun_firewall_rule(instance_id, rule_protocol, port, remark=None, r
@jingrow.whitelist() @jingrow.whitelist()
def get_aliyun_firewall_rules(instance_id, region_id='cn-shanghai'): def get_aliyun_firewall_rules(instance_id, region_id='cn-shanghai'):
"""获取阿里云轻量应用服务器防火墙规则列表""" """获取阿里云轻量应用服务器防火墙规则列表"""
try: try:
# 如果没有提供region_id尝试从服务器记录中获取
if not region_id or region_id == 'cn-shanghai':
server = jingrow.get_pg("Jsite Server", {"instance_id": instance_id})
if server and server.region:
region_id = server.region
# 调用管理器获取防火墙规则列表 # 调用管理器获取防火墙规则列表
manager = _get_manager() manager = _get_manager()
result = manager.list_firewall_rules(instance_id, region_id) result = manager.list_firewall_rules(instance_id, region_id)
@ -1721,6 +1719,10 @@ def delete_aliyun_firewall_rules(instance_id, rule_ids, region_id='cn-shanghai')
manager = _get_manager() manager = _get_manager()
result = manager.delete_firewall_rules(instance_id, rule_ids, region_id) result = manager.delete_firewall_rules(instance_id, rule_ids, region_id)
# 如果删除成功,同步防火墙规则到本地记录
if result and result.get('success'):
sync_firewall_rules(instance_id)
return result return result
except Exception as e: except Exception as e:
@ -1754,9 +1756,9 @@ def sync_firewall_rules(instance_id):
for rule in firewall_rules_data: for rule in firewall_rules_data:
converted_rule = { converted_rule = {
"rule_id": rule.get('rule_id', ''), "rule_id": rule.get('rule_id', ''),
"protocol": rule.get('rule_protocol', ''), "rule_protocol": rule.get('rule_protocol', ''),
"port": rule.get('port', ''), "port": rule.get('port', ''),
"source_ip": rule.get('source_cidr_ip', '0.0.0.0/0'), "source_cidr_ip": rule.get('source_cidr_ip', '0.0.0.0/0'),
"remark": rule.get('remark', '') "remark": rule.get('remark', '')
} }
converted_rules.append(converted_rule) converted_rules.append(converted_rule)

View File

@ -6,31 +6,18 @@
"engine": "InnoDB", "engine": "InnoDB",
"field_order": [ "field_order": [
"rule_id", "rule_id",
"protocol", "rule_protocol",
"port", "port",
"source_ip", "source_cidr_ip",
"remark" "remark"
], ],
"fields": [ "fields": [
{
"fieldname": "protocol",
"fieldtype": "Select",
"in_list_view": 1,
"label": "协议",
"options": "TCP\nUDP\nICMP"
},
{ {
"fieldname": "port", "fieldname": "port",
"fieldtype": "Data", "fieldtype": "Data",
"in_list_view": 1, "in_list_view": 1,
"label": "端口范围" "label": "端口范围"
}, },
{
"fieldname": "source_ip",
"fieldtype": "Data",
"in_list_view": 1,
"label": "来源IP"
},
{ {
"fieldname": "remark", "fieldname": "remark",
"fieldtype": "Data", "fieldtype": "Data",
@ -44,13 +31,26 @@
"in_list_view": 1, "in_list_view": 1,
"label": "规则ID", "label": "规则ID",
"read_only": 1 "read_only": 1
},
{
"fieldname": "rule_protocol",
"fieldtype": "Select",
"in_list_view": 1,
"label": "协议",
"options": "TCP\nUDP\nICMP"
},
{
"fieldname": "source_cidr_ip",
"fieldtype": "Data",
"in_list_view": 1,
"label": "来源IP"
} }
], ],
"grid_page_length": 50, "grid_page_length": 50,
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"istable": 1, "istable": 1,
"links": [], "links": [],
"modified": "2025-08-09 18:39:46.276289", "modified": "2025-08-09 19:45:50.104225",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Jcloud", "module": "Jcloud",
"name": "Firewall Rules", "name": "Firewall Rules",

View File

@ -18,9 +18,9 @@ class FirewallRules(Document):
parentfield: DF.Data parentfield: DF.Data
parenttype: DF.Data parenttype: DF.Data
port: DF.Data | None port: DF.Data | None
protocol: DF.Literal["TCP", "UDP", "ICMP"]
remark: DF.Data | None remark: DF.Data | None
rule_id: DF.Data | None rule_id: DF.Data | None
source_ip: DF.Data | None rule_protocol: DF.Literal["TCP", "UDP", "ICMP"]
source_cidr_ip: DF.Data | None
# end: auto-generated types # end: auto-generated types
pass pass