From 153eb72d274a6c02a0d0d7ba760d7085dc01c6aa Mon Sep 17 00:00:00 2001 From: iSecloud <869820505@qq.com> Date: Wed, 27 Nov 2024 14:56:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20=E4=BF=AE=E5=A4=8Dmysql=20proxy?= =?UTF-8?q?=E7=9A=84=E8=81=9A=E5=90=88=E9=80=BB=E8=BE=91=20#8223?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ticket/builders/mysql/mysql_proxy_add.py | 20 ++++++++++++++++--- .../builders/mysql/mysql_proxy_switch.py | 20 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/dbm-ui/backend/ticket/builders/mysql/mysql_proxy_add.py b/dbm-ui/backend/ticket/builders/mysql/mysql_proxy_add.py index 55be8aa48a..fee1ddfc60 100644 --- a/dbm-ui/backend/ticket/builders/mysql/mysql_proxy_add.py +++ b/dbm-ui/backend/ticket/builders/mysql/mysql_proxy_add.py @@ -52,12 +52,25 @@ def validate(self, attrs): class MysqlProxyAddParamBuilder(builders.FlowParamBuilder): controller = MySQLController.mysql_proxy_add_scene + @classmethod + def merge_same_proxy_clusters(cls, infos): + """聚合增加相同的proxy的集群""" + add_proxy_cluster_map = {} + for info in infos: + proxy = info["proxy_ip"] + if proxy["bk_host_id"] not in add_proxy_cluster_map: + add_proxy_cluster_map[proxy["bk_host_id"]] = {**info, "cluster_ids": []} + add_proxy_cluster_map[proxy["bk_host_id"]]["cluster_ids"].extend(info["cluster_ids"]) + return list(add_proxy_cluster_map.values()) + def format_ticket_data(self): if self.ticket_data["ip_source"] == IpSource.RESOURCE_POOL: return - for info in self.ticket_data["infos"]: info["proxy_ip"] = info["new_proxy"] + # 聚合集群 + infos = self.merge_same_proxy_clusters(self.ticket_data["infos"]) + self.ticket_data["infos"] = infos class MysqlProxyAddResourceParamBuilder(BaseOperateResourceParamBuilder): @@ -67,7 +80,9 @@ def post_callback(self): for info in ticket_data["infos"]: info["new_proxy"] = info.pop("new_proxy")[0] info["proxy_ip"] = info["new_proxy"] - + # 聚合集群 + infos = MysqlProxyAddParamBuilder.merge_same_proxy_clusters(ticket_data["infos"]) + next_flow.details["ticket_data"]["infos"] = infos next_flow.save(update_fields=["details"]) @@ -75,5 +90,4 @@ def post_callback(self): class MysqlProxyAddFlowBuilder(BaseMySQLHATicketFlowBuilder): serializer = MysqlProxyAddDetailSerializer inner_flow_builder = MysqlProxyAddParamBuilder - inner_flow_name = _("添加PROXY执行") resource_batch_apply_builder = MysqlProxyAddResourceParamBuilder diff --git a/dbm-ui/backend/ticket/builders/mysql/mysql_proxy_switch.py b/dbm-ui/backend/ticket/builders/mysql/mysql_proxy_switch.py index 48dd6053f9..b090d54196 100644 --- a/dbm-ui/backend/ticket/builders/mysql/mysql_proxy_switch.py +++ b/dbm-ui/backend/ticket/builders/mysql/mysql_proxy_switch.py @@ -72,11 +72,26 @@ def validate(self, attrs): class MysqlProxySwitchParamBuilder(builders.FlowParamBuilder): controller = MySQLController.mysql_proxy_switch_scene + @classmethod + def merge_same_proxy_clusters(cls, infos): + """聚合替换相同的proxy的集群""" + switch_proxy_cluster_map = {} + for info in infos: + switch_key = f"{info['origin_proxy_ip']['bk_host_id']}--{info['target_proxy_ip']['bk_host_id']}" + if switch_key not in switch_proxy_cluster_map: + switch_proxy_cluster_map[switch_key] = {**info, "cluster_ids": []} + switch_proxy_cluster_map[switch_key]["cluster_ids"].extend(info["cluster_ids"]) + return list(switch_proxy_cluster_map.values()) + def format_ticket_data(self): for info in self.ticket_data["infos"]: info["origin_proxy_ip"] = info["origin_proxy"] if self.ticket_data["ip_source"] == IpSource.MANUAL_INPUT: info["target_proxy_ip"] = info["target_proxy"] + # 聚合集群 + if self.ticket_data["ip_source"] == IpSource.MANUAL_INPUT: + infos = self.merge_same_proxy_clusters(self.ticket_data["infos"]) + self.ticket_data["infos"] = infos class MysqlProxySwitchResourceParamBuilder(BaseOperateResourceParamBuilder): @@ -86,7 +101,9 @@ def post_callback(self): for info in ticket_data["infos"]: info["target_proxy"] = info.pop("target_proxy")[0] info["target_proxy_ip"] = info["target_proxy"] - + # 聚合集群 + infos = MysqlProxySwitchParamBuilder.merge_same_proxy_clusters(ticket_data["infos"]) + next_flow.details["ticket_data"]["infos"] = infos next_flow.save(update_fields=["details"]) @@ -94,7 +111,6 @@ def post_callback(self): class MysqlProxySwitchFlowBuilder(BaseMySQLHATicketFlowBuilder): serializer = MysqlProxySwitchDetailSerializer inner_flow_builder = MysqlProxySwitchParamBuilder - inner_flow_name = _("替换PROXY执行") resource_batch_apply_builder = MysqlProxySwitchResourceParamBuilder retry_type = FlowRetryType.MANUAL_RETRY pause_node_builder = MySQLBasePauseParamBuilder