Exemplo n.º 1
0
    def put(self, request, project_id, pk):
        """复制模板
        """
        self.request = request
        # 验证用户是否使用权限
        template = validate_template_id(project_id, pk, is_return_tempalte=True)
        perm = bcs_perm.Templates(request, project_id, pk, template.name)
        perm.can_edit(raise_exception=True)

        self.project_id = project_id
        self.pk = pk
        data = request.data
        data['project_id'] = project_id
        self.slz = TemplateCreateSLZ(data=data)
        self.slz.is_valid(raise_exception=True)
        new_template_name = self.slz.data['name']
        # 验证模板名是否已经存在
        is_exist = Template.default_objects.filter(
            name=new_template_name, project_id=project_id).exists()
        if is_exist:
            detail = {
                'field': ['{prefix_msg}[{tmpl_name}]{suffix_msg}'.format(
                    prefix_msg=_("模板集名称"),
                    tmpl_name=new_template_name,
                    suffix_msg=_("已经存在")
                )]
            }
            raise ValidationError(detail=detail)
        # 验证 old模板集id 是否正确
        old_tems = self.get_queryset()
        if not old_tems.exists():
            detail = {
                'field': [_("要复制的模板集不存在")]
            }
            raise ValidationError(detail=detail)
        old_tem = old_tems.first()

        username = request.user.username
        template_id, version_id, show_version_id = old_tem.copy_tempalte(
            project_id, new_template_name, username)
        # 注册资源到权限中心
        perm.register(template_id, new_template_name)
        # 记录操作日志
        client.ContextActivityLogClient(
            project_id=project_id,
            user=request.user.username,
            resource_type="template",
            resource=new_template_name,
            resource_id=template_id,
            description=_("复制模板集")
        ).log_add()
        return Response({
            "code": 0,
            "message": "OK",
            "data": {
                "template_id": template_id,
                "version_id": version_id,
                "show_version_id": show_version_id,
            }
        })
Exemplo n.º 2
0
    def post(self, request, project_id, pk):
        self.request = request
        self.project_id = project_id
        self.pk = pk
        template = validate_template_id(project_id, pk, is_return_tempalte=True)

        # 验证用户是否有编辑权限
        perm = bcs_perm.Templates(request, project_id, pk, template.name)
        perm.can_edit(raise_exception=True)
        self.perm = perm

        # 检查模板集是否可操作(即未被加锁)
        check_tempalte_available(template, request.user.username)

        # 验证模板名是否已经存在
        new_template_name = request.data.get('name')
        is_exist = Template.default_objects.exclude(id=pk).filter(
            name=new_template_name, project_id=project_id).exists()
        if is_exist:
            detail = {
                'field': [u"模板集名称[%s]已经存在" % new_template_name]
            }
            raise ValidationError(detail=detail)

        self.slz = TemplateUpdateSLZ(data=request.data)
        self.slz.is_valid(raise_exception=True)
        return super(SingleTempalteView, self).update(self.slz)
Exemplo n.º 3
0
class SingleTempalteView(generics.RetrieveUpdateDestroyAPIView):
    serializer_class = serializers_new.TemplateSLZ
    renderer_classes = (BKAPIRenderer, BrowsableAPIRenderer)

    def get_queryset(self):
        return Template.objects.filter(project_id=self.project_id, id=self.pk)

    def get_serializer_context(self):
        context = super(SingleTempalteView, self).get_serializer_context()
        context.update({'request': self.request})
        return context

    def perform_update(self, serializer):
        instance = serializer.save(updator=self.request.user.username,
                                   project_id=self.project_id)
        # 记录操作日志
        client.ContextActivityLogClient(project_id=self.project_id,
                                        user=self.request.user.username,
                                        resource_type="template",
                                        resource=instance.name,
                                        resource_id=instance.id,
                                        extra=json.dumps(serializer.data),
                                        description=_("更新模板集")).log_modify()
        # 同步模板集名称到权限中心
        self.perm.update_name(instance.name)

    def post(self, request, project_id, pk):
        self.request = request
        self.project_id = project_id
        self.pk = pk
        template = validate_template_id(project_id,
                                        pk,
                                        is_return_tempalte=True)

        # 验证用户是否有编辑权限
        perm = bcs_perm.Templates(request, project_id, pk, template.name)
        perm.can_edit(raise_exception=True)
        self.perm = perm

        # 检查模板集是否可操作(即未被加锁)
        check_tempalte_available(template, request.user.username)

        # 验证模板名是否已经存在
        new_template_name = request.data.get('name')
        is_exist = Template.default_objects.exclude(id=pk).filter(
            name=new_template_name, project_id=project_id).exists()
        if is_exist:
            detail = {'field': [_('模板集名称[{}]已经存在').format(new_template_name)]}
            raise ValidationError(detail=detail)

        self.slz = serializers_new.UpdateTemplateSLZ(data=request.data)
        self.slz.is_valid(raise_exception=True)
        return super(SingleTempalteView, self).update(self.slz)

    def get(self, request, project_id, pk):
        self.request = request
        self.project_id = project_id
        self.pk = pk
        template = validate_template_id(project_id,
                                        pk,
                                        is_return_tempalte=True)

        # 获取项目类型
        kind = request.project.kind

        tems = self.get_queryset()
        if tems:
            tem = tems.first()
            data = get_tempate_info(tem, kind)
        else:
            data = {}
        perm = bcs_perm.Templates(request, project_id, pk, template.name)
        data_list = perm.hook_perms([data])
        return Response({"code": 0, "message": "OK", "data": data_list[0]})

    def delete(self, request, project_id, pk):
        self.request = request
        self.project_id = project_id
        self.pk = pk
        # 验证用户是否删除权限
        template = validate_template_id(project_id,
                                        pk,
                                        is_return_tempalte=True)
        perm = bcs_perm.Templates(request, project_id, pk, template.name)
        perm.can_delete(raise_exception=True)

        # 检查模板集是否可操作(即未被加锁)
        check_tempalte_available(template, request.user.username)

        # 已经实例化过的版本,不能被删除
        exist_version = is_tempalte_instance(pk)
        if exist_version:
            return Response({
                "code": 400,
                "message": _("模板集已经被实例化过,不能被删除"),
                "data": {}
            })
        instance = self.get_queryset().first()
        with client.ContextActivityLogClient(
                project_id=project_id,
                user=request.user.username,
                resource_type="template",
                resource=instance.name,
                resource_id=instance.id,
                description=_("删除模板集")).log_delete():
            # 删除后名称添加 [deleted]前缀
            _del_prefix = '[deleted_%s]' % int(time.time())
            del_tem_name = "%s%s" % (_del_prefix, instance.name)
            self.get_queryset().update(name=del_tem_name,
                                       is_deleted=True,
                                       deleted_time=timezone.now())
            # 直接调用delete删除权限中心的资源
            perm.delete()

        return Response({"code": 0, "message": "OK", "data": {"id": pk}})
        # return super(SingleTempalteView, self).delete(self, request)

    @transaction.atomic
    def put(self, request, project_id, pk):
        """复制模板
        """
        self.request = request
        # 验证用户是否使用权限
        template = validate_template_id(project_id,
                                        pk,
                                        is_return_tempalte=True)
        perm = bcs_perm.Templates(request, project_id, pk, template.name)
        perm.can_edit(raise_exception=True)

        self.project_id = project_id
        self.pk = pk
        data = request.data
        data['project_id'] = project_id
        self.slz = TemplateCreateSLZ(data=data)
        self.slz.is_valid(raise_exception=True)
        new_template_name = self.slz.data['name']
        # 验证模板名是否已经存在
        is_exist = Template.default_objects.filter(
            name=new_template_name, project_id=project_id).exists()
        if is_exist:
            detail = {'field': [_('模板集名称[{}]已经存在').format(new_template_name)]}
            raise ValidationError(detail=detail)
        # 验证 old模板集id 是否正确
        old_tems = self.get_queryset()
        if not old_tems.exists():
            detail = {'field': [_("要复制的模板集不存在")]}
            raise ValidationError(detail=detail)
        old_tem = old_tems.first()

        username = request.user.username
        template_id, version_id, show_version_id = old_tem.copy_tempalte(
            project_id, new_template_name, username)
        # 注册资源到权限中心
        perm.register(template_id, new_template_name)
        # 记录操作日志
        client.ContextActivityLogClient(project_id=project_id,
                                        user=request.user.username,
                                        resource_type="template",
                                        resource=new_template_name,
                                        resource_id=template_id,
                                        description=_("复制模板集")).log_add()
        return Response({
            "code": 0,
            "message": "OK",
            "data": {
                "template_id": template_id,
                "version_id": version_id,
                "show_version_id": show_version_id,
            }
        })