diff --git a/dbm-ui/backend/ticket/builders/mysql/mysql_db_table_backup.py b/dbm-ui/backend/ticket/builders/mysql/mysql_db_table_backup.py index e83ea01e1e..e048e76814 100644 --- a/dbm-ui/backend/ticket/builders/mysql/mysql_db_table_backup.py +++ b/dbm-ui/backend/ticket/builders/mysql/mysql_db_table_backup.py @@ -72,7 +72,7 @@ def __validate_cluster_id_unique(cluster_ids) -> str: """ 集群 id 不能重复出现 """ - dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids) if cnt > 1] + dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids).items() if cnt > 1] if dup_cluster_ids: return _( "重复输入集群: {}".format( @@ -89,7 +89,7 @@ def __validate_cluster_type(cluster_ids) -> str: cluster_types = [] for cluster_obj in Cluster.objects.filter(pk__in=cluster_ids): if cluster_obj.cluster_type not in [ClusterType.TenDBHA, ClusterType.TenDBSingle]: - bad.append(_("不支持的集群类型 {} {}".format(cluster_obj.immute_domain, cluster_obj.cluster_type))) + bad.append(str(_("不支持的集群类型 {} {}".format(cluster_obj.immute_domain, cluster_obj.cluster_type)))) cluster_types.append(cluster_obj.cluster_type) @@ -107,7 +107,7 @@ def __validate_cluster_exists(cluster_ids) -> str: exists_cluster_ids = list( Cluster.objects.filter( pk__in=cluster_ids, cluster_type__in=[ClusterType.TenDBHA, ClusterType.TenDBSingle] - ).values_list("cluster_id", flat=True) + ).values_list("id", flat=True) ) not_exists_cluster_ids = list(set(cluster_ids) - set(exists_cluster_ids)) if not_exists_cluster_ids: diff --git a/dbm-ui/backend/ticket/builders/mysql/mysql_full_backup.py b/dbm-ui/backend/ticket/builders/mysql/mysql_full_backup.py index 39ccb3edf2..7498572beb 100644 --- a/dbm-ui/backend/ticket/builders/mysql/mysql_full_backup.py +++ b/dbm-ui/backend/ticket/builders/mysql/mysql_full_backup.py @@ -64,7 +64,7 @@ def __validate_cluster_id_unique(cluster_ids) -> str: """ 集群 id 不能重复出现 """ - dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids) if cnt > 1] + dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids).items() if cnt > 1] if dup_cluster_ids: return _( "重复输入集群: {}".format( @@ -81,7 +81,7 @@ def __validate_cluster_type(cluster_ids) -> str: cluster_types = [] for cluster_obj in Cluster.objects.filter(pk__in=cluster_ids): if cluster_obj.cluster_type not in [ClusterType.TenDBHA, ClusterType.TenDBSingle]: - bad.append(_("不支持的集群类型 {} {}".format(cluster_obj.immute_domain, cluster_obj.cluster_type))) + bad.append(str(_("不支持的集群类型 {} {}".format(cluster_obj.immute_domain, cluster_obj.cluster_type)))) cluster_types.append(cluster_obj.cluster_type) @@ -101,15 +101,17 @@ def __validate_backup_local(attrs) -> str: cluster_obj = Cluster.objects.get(pk=cluster_id) if cluster_obj.cluster_type == ClusterType.TenDBSingle and backup_local != InstanceInnerRole.ORPHAN: - bad.append(_("{} 备份位置只能是 {}".format(cluster_obj.immute_domain, InstanceInnerRole.ORPHAN))) + bad.append(str(_("{} 备份位置只能是 {}".format(cluster_obj.immute_domain, InstanceInnerRole.ORPHAN)))) elif cluster_obj.cluster_type == ClusterType.TenDBHA and backup_local not in [ InstanceInnerRole.MASTER, InstanceInnerRole.SLAVE, ]: bad.append( - _( - "{} 备份位置只能是 {}".format( - cluster_obj.immute_domain, [InstanceInnerRole.MASTER, InstanceInnerRole.SLAVE] + str( + _( + "{} 备份位置只能是 {}".format( + cluster_obj.immute_domain, [InstanceInnerRole.MASTER, InstanceInnerRole.SLAVE] + ) ) ) ) @@ -129,10 +131,12 @@ def __validate_cluster_status(attrs) -> str: cluster=cluster_obj, instance_inner_role=backup_local, is_stand_by=True, status=InstanceStatus.RUNNING ).exists(): bad.append( - _( - "{} 没找到正常的 {} 实例".format( - cluster_obj.immute_domain, - backup_local, + str( + _( + "{} 没找到正常的 {} 实例".format( + cluster_obj.immute_domain, + backup_local, + ) ) ) ) diff --git a/dbm-ui/backend/ticket/builders/tendbcluster/db_table_backup.py b/dbm-ui/backend/ticket/builders/tendbcluster/db_table_backup.py index ad25a8f08f..7de23804f0 100644 --- a/dbm-ui/backend/ticket/builders/tendbcluster/db_table_backup.py +++ b/dbm-ui/backend/ticket/builders/tendbcluster/db_table_backup.py @@ -34,7 +34,27 @@ class TenDBClusterDBTableBackupInfoSerializer(serializers.Serializer): infos = serializers.ListSerializer(help_text=_("库表备份信息"), child=TenDBClusterDBTableBackupInfoSerializer()) + @classmethod + def get_backup_local_params(cls, info): + """ + 对备份位置进行提取, + 两种情况:remote/spider_mnt::127.0.0.1 + """ + divider = "::" + if divider not in info["backup_local"]: + return info + + backup_local, spider_mnt_address = info["backup_local"].split(divider) + info["backup_local"] = backup_local + info["spider_mnt_address"] = spider_mnt_address + + return info + def validate(self, attrs): + + for cluster_info in attrs["infos"]: + self.get_backup_local_params(cluster_info) + # 集群不允许重复 cluster_ids = [info["cluster_id"] for info in attrs["infos"]] @@ -69,7 +89,7 @@ def __validate_cluster_id_unique(cluster_ids) -> str: """ 集群 id 不能重复出现 """ - dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids) if cnt > 1] + dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids).items() if cnt > 1] if dup_cluster_ids: return _( "重复输入集群: {}".format( @@ -97,7 +117,7 @@ def __validate_cluster_exists(cluster_ids) -> str: """ exists_cluster_ids = list( Cluster.objects.filter(pk__in=cluster_ids, cluster_type=ClusterType.TenDBCluster).values_list( - "cluster_id", flat=True + "id", flat=True ) ) not_exists_cluster_ids = list(set(cluster_ids) - set(exists_cluster_ids)) @@ -111,10 +131,10 @@ def __validate_backup_local(attrs): for info in attrs["infos"]: backup_local = info["backup_local"] if backup_local not in ["remote", "spider_mnt"]: - bad.append(_("不支持的备份位置 {}".format(backup_local))) + bad.append(str(_("不支持的备份位置 {}".format(backup_local)))) if backup_local == "spider_mnt" and "spider_mnt_address" not in info: - bad.append(_("缺少 spider_mnt_address")) + bad.append(str(_("缺少 spider_mnt_address"))) if bad: return ", ".join(list(set(bad))) diff --git a/dbm-ui/backend/ticket/builders/tendbcluster/full_backup.py b/dbm-ui/backend/ticket/builders/tendbcluster/full_backup.py index 4147f3841e..aefddc41b0 100644 --- a/dbm-ui/backend/ticket/builders/tendbcluster/full_backup.py +++ b/dbm-ui/backend/ticket/builders/tendbcluster/full_backup.py @@ -33,7 +33,27 @@ class TenDBClusterFullBackupInfoSerializer(serializers.Serializer): infos = serializers.ListSerializer(child=TenDBClusterFullBackupInfoSerializer()) + @classmethod + def get_backup_local_params(cls, info): + """ + 对备份位置进行提取, + 两种情况:remote/spider_mnt::127.0.0.1 + """ + divider = "::" + if divider not in info["backup_local"]: + return info + + backup_local, spider_mnt_address = info["backup_local"].split(divider) + info["backup_local"] = backup_local + info["spider_mnt_address"] = spider_mnt_address + + return info + def validate(self, attrs): + + for cluster_info in attrs["infos"]: + self.get_backup_local_params(cluster_info) + cluster_ids = [info["cluster_id"] for info in attrs["infos"]] errors = [] @@ -58,6 +78,9 @@ def validate(self, attrs): if msg: errors.append(msg) + if errors: + raise serializers.ValidationError(errors) + return attrs @staticmethod @@ -65,7 +88,7 @@ def __validate_cluster_id_unique(cluster_ids) -> str: """ 集群 id 不能重复出现 """ - dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids) if cnt > 1] + dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids).items() if cnt > 1] if dup_cluster_ids: return _( "重复输入集群: {}".format( @@ -93,7 +116,7 @@ def __validate_cluster_exists(cluster_ids) -> str: """ exists_cluster_ids = list( Cluster.objects.filter(pk__in=cluster_ids, cluster_type=ClusterType.TenDBCluster).values_list( - "cluster_id", flat=True + "id", flat=True ) ) not_exists_cluster_ids = list(set(cluster_ids) - set(exists_cluster_ids)) @@ -107,10 +130,10 @@ def __validate_backup_local(attrs): for info in attrs["infos"]: backup_local = info["backup_local"] if backup_local not in ["master", "slave", "spider_mnt"]: - bad.append(_("不支持的备份位置 {}".format(backup_local))) + bad.append(str(_("不支持的备份位置 {}".format(backup_local)))) if backup_local == "spider_mnt" and "spider_mnt_address" not in info: - bad.append(_("缺少 spider_mnt_address")) + bad.append(str(_("缺少 spider_mnt_address"))) if bad: return ", ".join(list(set(bad)))