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

View File

@ -1687,6 +1687,10 @@ def create_aliyun_firewall_rule(instance_id, rule_protocol, port, remark=None, r
manager = _get_manager()
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
except Exception as e:
@ -1697,12 +1701,6 @@ def create_aliyun_firewall_rule(instance_id, rule_protocol, port, remark=None, r
def get_aliyun_firewall_rules(instance_id, region_id='cn-shanghai'):
"""获取阿里云轻量应用服务器防火墙规则列表"""
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()
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()
result = manager.delete_firewall_rules(instance_id, rule_ids, region_id)
# 如果删除成功,同步防火墙规则到本地记录
if result and result.get('success'):
sync_firewall_rules(instance_id)
return result
except Exception as e:
@ -1754,9 +1756,9 @@ def sync_firewall_rules(instance_id):
for rule in firewall_rules_data:
converted_rule = {
"rule_id": rule.get('rule_id', ''),
"protocol": rule.get('rule_protocol', ''),
"rule_protocol": rule.get('rule_protocol', ''),
"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', '')
}
converted_rules.append(converted_rule)

View File

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

View File

@ -18,9 +18,9 @@ class FirewallRules(Document):
parentfield: DF.Data
parenttype: DF.Data
port: DF.Data | None
protocol: DF.Literal["TCP", "UDP", "ICMP"]
remark: 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
pass