Пример #1
0
    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)
Пример #2
0
    def batch_delete_services(self, request, project_id):
        """批量删除service"""
        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)

        # check perm
        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)

        project_kind = request.project.kind
        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_service(request, project_id,
                                              project_kind, 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':
                    _('{}[命名空间:{}]').format(name, namespace),
                })
            else:
                if is_delete_before:
                    message = _('已经被删除,请手动刷新数据')
                failed_list.append({
                    'name':
                    name,
                    'desc':
                    _('{}][命名空间:{}]:{}').format(name, namespace, 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 = _("以下service删除成功:{}").format(";".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 = _("以下service删除失败:{}").format(";".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": {}})
Пример #3
0
    def batch_delete_services(self, request, project_id):
        """批量删除service
        """
        username = request.user.username
        slz = BatchResourceSLZ(data=request.data)
        slz.is_valid(raise_exception=True)
        data = slz.data['data']

        # 检查用户是否有命名空间的使用权限
        namespace_list = [_d.get('namespace') for _d in data]
        namespace_list = set(namespace_list)
        namespace_dict = self.check_namespace_use_perm(request, project_id, namespace_list)

        flag, project_kind = self.get_project_kind(request, project_id)
        if not flag:
            return project_kind

        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(namespace)
            # 删除service
            resp = self.delete_single_service(request, project_id, project_kind,
                                              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': u'%s[命名空间:%s]' % (name, namespace),
                })
            else:
                if is_delete_before:
                    message = u'已经被删除,请手动刷新数据'
                failed_list.append({
                    'name': name,
                    'desc': u'%s[命名空间:%s]:%s' % (name, namespace, 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 = u"以下service删除成功:%s" % ";".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 = u"以下service删除失败:%s" % ";".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": {}
        })
Пример #4
0
    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)