def delete(self, request, project_id, cluster_id, ns_name, name): username = request.user.username namespace_dict = self.check_namespace_use_perm(request, project_id, [ns_name]) namespace_id = namespace_dict.get(ns_name) try: utils.delete_hpa(request, project_id, cluster_id, ns_name, namespace_id, name) except utils.DeleteHPAError as error: message = f'删除HPA:{name}失败[命名空间:{ns_name}], {error}' utils.activity_log(project_id, username, name, message, False) raise error_codes.APIError(message) message = f'删除HPA:{name}成功[命名空间:{ns_name}]' utils.activity_log(project_id, username, name, message, True) return Response({})
def batch_delete(self, request, project_id): """批量删除资源 """ username = request.user.username if request.project.kind != ProjectKind.K8S.value: raise error_codes.NotOpen("HPA暂时只支持K8S集群") 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 result, message = utils.delete_hpa(request, project_id, cluster_id, ns_name, ns_id, name) if result is False: failed_list.append({ 'name': name, 'desc': f'{name}[命名空间:{ns_name}]:{message}' }) else: success_list.append({ 'name': name, 'desc': f'{name}[命名空间:{ns_name}]' }) # 添加操作审计 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 = f"以下{self.category}删除成功:{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 = f"以下{self.category}删除失败:{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)
def delete(self, request, project_id, cluster_id, ns_name, name): username = request.user.username namespace_dict = self.check_namespace_use_perm(request, project_id, [ns_name]) namespace_id = namespace_dict.get(ns_name) try: utils.delete_hpa(request, project_id, cluster_id, ns_name, namespace_id, name) except hpa_exceptions.DeleteHPAError as error: message = "删除HPA:{}失败, [命名空间:{}], {}".format(name, ns_name, error) utils.activity_log(project_id, username, name, message, False) raise error_codes.APIError(message) message = "删除HPA:{}成功, [命名空间:{}]".format(name, ns_name) utils.activity_log(project_id, username, name, message, True) return Response({})
def delete(self, request, project_id, cluster_id, ns_name, name): # 检查用户是否有命名空间的使用权限 if request.project.kind != ProjectKind.K8S.value: raise error_codes.NotOpen("HPA暂时只支持K8S集群") username = request.user.username namespace_dict = self.check_namespace_use_perm(request, project_id, [ns_name]) namespace_id = namespace_dict.get(ns_name) result, message = utils.delete_hpa(request, project_id, cluster_id, ns_name, namespace_id, name) if result is False: message = f'删除HPA:{name}失败[命名空间:{ns_name}], {message}' utils.activity_log(project_id, username, name, message, result) raise error_codes.APIError(message) message = f'删除HPA:{name}成功[命名空间:{ns_name}]' utils.activity_log(project_id, username, name, message, result) return Response({})
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 utils.DeleteHPAError as error: failed_list.append({ 'name': name, 'desc': "{}[命名空间:{}]:{}".format(name, ns_name, error) }) else: success_list.append({ 'name': name, 'desc': "{}[命名空间:{}]".format(name, ns_name) }) # 添加操作审计 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)