def batch_delete_resource(self, request, project_id): """批量删除资源""" username = request.user.username slz = BatchResourceSLZ(data=request.data) slz.is_valid(raise_exception=True) data = slz.data['data'] namespace_list = [(ns['cluster_id'], ns.get('namespace')) for ns in data] namespace_list = set(namespace_list) # 检查用户是否有命名空间的使用权限 app_utils.can_use_namespaces(request, project_id, namespace_list) # namespace_dict format: {(cluster_id, ns_name): ns_id} namespace_dict = app_utils.get_ns_id_map( request.user.token.access_token, project_id) success_list = [] failed_list = [] for _d in data: cluster_id = _d.get('cluster_id') name = _d.get('name') namespace = _d.get('namespace') namespace_id = namespace_dict.get((cluster_id, namespace)) # 删除service resp = self.delete_single_resource(request, project_id, cluster_id, namespace, namespace_id, name) # 处理已经删除,但是storage上报数据延迟的问题 message = resp.get('message', '') is_delete_before = True if 'node does not exist' in message or 'not found' in message else False if resp.get("code") == ErrorCode.NoError: success_list.append({ 'name': name, 'desc': self.desc.format(cluster_id=cluster_id, namespace=namespace, resource_name=self.category, name=name), }) else: if is_delete_before: message = _('已经被删除,请手动刷新数据') desc = self.desc.format(cluster_id=cluster_id, namespace=namespace, resource_name=self.category, name=name) failed_list.append({ 'name': name, 'desc': f'{desc}, message: {message}', }) code = 0 message = '' # 添加操作审计 if success_list: name_list = [_s.get('name') for _s in success_list] desc_list = [_s.get('desc') for _s in success_list] message = _("以下{}删除成功:{}").format(self.category, ";".join(desc_list)) activity_client.ContextActivityLogClient( project_id=project_id, user=username, resource_type="instance", resource=';'.join(name_list), resource_id=0, extra=json.dumps({}), description=";".join(desc_list), ).log_modify(activity_status="succeed") if failed_list: name_list = [_s.get('name') for _s in failed_list] desc_list = [_s.get('desc') for _s in failed_list] code = 4004 message = _("以下{}删除失败:{}").format(self.category, ";".join(desc_list)) activity_client.ContextActivityLogClient( project_id=project_id, user=username, resource_type="instance", resource=';'.join(name_list), resource_id=0, extra=json.dumps({}), description=message, ).log_modify(activity_status="failed") return Response({"code": code, "message": message, "data": {}})
def batch_delete(self, request, project_id): """批量删除资源""" username = request.user.username slz = BatchResourceSLZ(data=request.data) slz.is_valid(raise_exception=True) data = slz.data['data'] # 检查用户是否有命名空间的使用权限 namespace_list = set([_d.get('namespace') for _d in data]) namespace_dict = self.check_namespace_use_perm(request, project_id, namespace_list) success_list = [] failed_list = [] for _d in data: cluster_id = _d.get('cluster_id') name = _d.get('name') ns_name = _d.get('namespace') ns_id = namespace_dict.get(ns_name) # 删除 hpa try: utils.delete_hpa(request, project_id, cluster_id, ns_name, ns_id, name) except DeleteResourceError as error: failed_list.append({ 'name': name, 'desc': "{}[命名空间:{}]:{}".format(name, ns_name, error) }) else: success_list.append({ 'name': name, 'desc': "{}[命名空间:{}]".format(name, ns_name) }) # 添加操作审计 message = '--' if success_list: name_list = [_s.get('name') for _s in success_list] desc_list = [_s.get('desc') for _s in success_list] desc_list_msg = ";".join(desc_list) message = "以下HPA删除成功:{}".format(desc_list_msg) utils.activity_log(project_id, username, ';'.join(name_list), message, True) if failed_list: name_list = [_s.get('name') for _s in failed_list] desc_list = [_s.get('desc') for _s in failed_list] desc_list_msg = ";".join(desc_list) message = "以下HPA删除失败:{}".format(desc_list_msg) utils.activity_log(project_id, username, ';'.join(name_list), message, False) # 有一个失败,则返回失败 if failed_list: raise error_codes.APIError(message) return BKAPIResponse({}, message=message)