Пример #1
0
def pipeline_node_name_handle(pipeline_tree):
    for value in pipeline_tree.values():
        if isinstance(value, dict):
            for info in value.values():
                if isinstance(info, dict) and 'name' in info:
                    info['name'] = name_handler(info['name'],
                                                TEMPLATE_NODE_NAME_MAX_LENGTH)
            if 'name' in value:
                value['name'] = name_handler(value['name'],
                                             TEMPLATE_NODE_NAME_MAX_LENGTH)
        elif isinstance(value, list):
            for item in value:
                if isinstance(item, dict) and 'name' in item:
                    item['name'] = name_handler(item['name'],
                                                TEMPLATE_NODE_NAME_MAX_LENGTH)
Пример #2
0
 def obj_create(self, bundle, **kwargs):
     biz_cc_id = bundle.data.pop('biz_cc_id', None)
     template_id = bundle.data.pop('template_id', None)
     try:
         template = CommonTemplate.objects.get(pk=template_id)
     except Exception:
         raise BadRequest('common template does not exist')
     business = get_business_for_user(bundle.request.user,
                                      ['view_business'])
     if not business.filter(cc_id=biz_cc_id).exists():
         raise ImmediateHttpResponse(
             HttpResponseForbidden(
                 'you have no permission to make such operation'))
     bundle.data['name'] = name_handler(bundle.data['name'],
                                        TEMPLATE_NODE_NAME_MAX_LENGTH)
     kwargs['unique_id'] = '%s-%s-%s' % (biz_cc_id, template_id,
                                         bundle.data['name'])
     if TemplateScheme.objects.filter(
             unique_id=kwargs['unique_id']).exists():
         raise BadRequest(
             'common template scheme name has existed, please change the name'
         )
     kwargs['template'] = template.pipeline_template
     return super(CommonTemplateSchemeResource,
                  self).obj_create(bundle, **kwargs)
Пример #3
0
    def obj_create(self, bundle, **kwargs):
        try:
            template_id = bundle.data.pop('template_id')
            project_id = bundle.data.pop('project__id')
            json.loads(bundle.data['data'])
        except Exception as e:
            message = 'create scheme params error: %s' % e
            logger.error(message)
            raise BadRequest(message)
        try:
            template = TaskTemplate.objects.get(pk=template_id,
                                                project_id=project_id)
        except TaskTemplate.DoesNotExist:
            message = 'flow template[id={template_id}] in project[id={project_id}] does not exist'.format(
                template_id=template_id, project_id=project_id)
            logger.error(message)
            raise BadRequest(message)

        verify_or_raise_immediate_response(
            principal_type='user',
            principal_id=bundle.request.user.username,
            resource=task_template_resource,
            action_ids=[task_template_resource.actions.edit.id],
            instance=template)

        bundle.data['name'] = name_handler(bundle.data['name'],
                                           TEMPLATE_NODE_NAME_MAX_LENGTH)
        kwargs['unique_id'] = '%s-%s' % (template_id, bundle.data['name'])
        if TemplateScheme.objects.filter(
                unique_id=kwargs['unique_id']).exists():
            raise BadRequest(
                'template scheme name has existed, please change the name')
        kwargs['template'] = template.pipeline_template
        return super(TemplateSchemeResource, self).obj_create(bundle, **kwargs)
Пример #4
0
    def obj_create(self, bundle, **kwargs):
        try:
            template_id = bundle.data.pop('template_id')
            name = bundle.data.pop('name')
            cron = json.loads(bundle.data.pop('cron'))
            pipeline_tree = json.loads(bundle.data.pop('pipeline_tree'))
            business_path = bundle.data['business']
        except (KeyError, ValueError) as e:
            raise BadRequest(e.message)

        # XSS handle
        name = name_handler(name, PERIOD_TASK_NAME_MAX_LENGTH)
        creator = bundle.request.user.username

        # validate pipeline tree
        try:
            validate_web_pipeline_tree(pipeline_tree)
        except PipelineException as e:
            raise BadRequest(e.message)

        try:
            template = TaskTemplate.objects.get(id=template_id)
            kwargs['template_id'] = template.id
        except TaskTemplate.DoesNotExist:
            raise BadRequest('template[id=%s] does not exist' % template_id)

        try:
            replace_template_id(TaskTemplate, pipeline_tree)
        except TaskTemplate.DoesNotExist:
            raise BadRequest(
                'invalid subprocess, check subprocess node please')

        if not isinstance(cron, dict):
            raise BadRequest('cron must be a object json string')

        try:
            business = Business.objects.get(
                cc_id=int(business_path.split('/')[-2]))
        except Exception as e:
            raise BadRequest(e.message)

        try:
            kwargs['task'] = PeriodicTask.objects.create_pipeline_task(
                business=business,
                template=template,
                name=name,
                cron=cron,
                pipeline_tree=pipeline_tree,
                creator=creator)
        except Exception as e:
            logger.warning(traceback.format_exc())
            raise BadRequest(e.message)

        response = super(PeriodicTaskResource,
                         self).obj_create(bundle, **kwargs)
        response.obj.set_enabled(True)

        return response
Пример #5
0
    def obj_create(self, bundle, **kwargs):
        try:
            project_id = bundle.data.pop('project__id')
            template_id = bundle.data.pop('template_id')
            common_template = CommonTemplate.objects.get(pk=template_id)
        except Exception:
            raise BadRequest('common template does not exist')

        verify_or_raise_immediate_response(principal_type='user',
                                           principal_id=bundle.request.user.username,
                                           resource=common_template_resource,
                                           action_ids=[common_template_resource.actions.edit.id],
                                           instance=common_template)

        bundle.data['name'] = name_handler(bundle.data['name'], TEMPLATE_NODE_NAME_MAX_LENGTH)
        kwargs['unique_id'] = '%s-%s-%s' % (project_id, template_id, bundle.data['name'])
        if TemplateScheme.objects.filter(unique_id=kwargs['unique_id']).exists():
            raise BadRequest('common template scheme name has existed, please change the name')
        kwargs['template'] = common_template.pipeline_template
        return super(CommonTemplateSchemeResource, self).obj_create(bundle, **kwargs)
Пример #6
0
    def save_app_maker(self, biz_cc_id, app_params, fake=False):
        """
        @summary:
        @param biz_cc_id: 业务ID
        @param app_params: App maker参数
        @param fake: 为True则不会真正调用API创建轻应用
        @return:
        """
        logger.info('save_app_maker params: %s' % app_params)
        app_id = app_params['id']
        if app_id == '0' or not app_id:
            app_id = None
        template_id = app_params['template_id']
        app_params['name'] = name_handler(app_params['name'], 20)
        app_params['desc'] = name_handler(app_params.get('desc', ''), 30)
        biz = Business.objects.get(cc_id=biz_cc_id)
        try:
            task_template = TaskTemplate.objects.get(pk=template_id,
                                                     business__cc_id=biz_cc_id,
                                                     is_deleted=False)
        except TaskTemplate.DoesNotExist:
            return False, _(u"保存失败,引用的流程模板不存在!")

        # create appmaker
        if not app_id:
            fields = {
                'business': biz,
                'name': app_params['name'],
                'code': '',
                'desc': app_params['desc'],
                'logo_url': '',
                'link': app_params['link_prefix'],
                'creator': app_params['username'],
                'editor': app_params['username'],
                'task_template': task_template,
                # 生成一个删除状态的对象,以便拼接轻应用访问链接
                'is_deleted': True,
            }
            if app_params.get('template_scheme_id'):
                fields['template_scheme_id'] = app_params['template_scheme_id']
            app_maker_obj = AppMaker.objects.create(**fields)

            # update app link
            app_id = app_maker_obj.id
            app_link = '{appmaker_prefix}{app_id}/newtask/{biz_cc_id}/selectnode/?template_id={template_id}'.format(
                appmaker_prefix=app_params['link_prefix'],
                app_id=app_id,
                biz_cc_id=biz_cc_id,
                template_id=template_id)
            app_maker_obj.link = app_link

            if fake:
                app_maker_obj.code = '%s%s' % (settings.APP_CODE,
                                               time_now_str())
                app_maker_obj.is_deleted = False
                app_maker_obj.save()
                return True, app_maker_obj

            # create app on blueking desk
            app_create_result = create_maker_app(
                app_params['username'],
                app_params['name'],
                app_link,
                app_params['username'],
                task_template.category,
                app_params['desc'],
            )
            if not app_create_result['result']:
                return False, _(u"创建轻应用失败:%s") % app_create_result['message']

            app_code = app_create_result['data']['bk_light_app_code']
            app_maker_obj.code = app_code
            app_maker_obj.is_deleted = False

        # edit appmaker
        else:
            try:
                app_maker_obj = AppMaker.objects.get(
                    id=app_id,
                    business__cc_id=biz_cc_id,
                    task_template__id=template_id,
                    is_deleted=False)
            except AppMaker.DoesNotExist:
                return False, _(u"保存失败,当前操作的轻应用不存在或已删除!")

            app_code = app_maker_obj.code
            creator = app_maker_obj.creator
            link = app_maker_obj.link

            if not fake:
                # edit app on blueking
                app_edit_result = edit_maker_app(
                    creator,
                    app_code,
                    app_params['name'],
                    link,
                    creator,
                    task_template.category,
                    app_params['desc'],
                )
                if not app_edit_result['result']:
                    return False, _(u"编辑轻应用失败:%s") % app_edit_result['message']

            app_maker_obj.name = app_params['name']
            app_maker_obj.desc = app_params['desc']
            app_maker_obj.editor = app_params['username']
            if 'template_scheme_id' in app_params:
                app_maker_obj.template_scheme_id = app_params[
                    'template_scheme_id']

        # upload app logo
        if not fake and app_params['logo_content']:
            logo = base64.b64encode(app_params['logo_content'])
            app_logo_result = modify_app_logo(app_maker_obj.creator, app_code,
                                              logo)
            if not app_logo_result['result']:
                logger.warning(u"AppMaker[id=%s] upload logo failed: %s" %
                               (app_maker_obj.id, app_logo_result['message']))

        # update app maker info
        app_maker_obj.logo_url = get_app_logo_url(app_code=app_code)
        app_maker_obj.save()

        return True, app_maker_obj
Пример #7
0
 def handle_template_name_attr(data):
     data['name'] = name_handler(data['name'],
                                 TEMPLATE_NODE_NAME_MAX_LENGTH)
     pipeline_node_name_handle(data['pipeline_tree'])
Пример #8
0
 def handle_task_name_attr(data):
     data['name'] = name_handler(data['name'], TASK_NAME_MAX_LENGTH)
     pipeline_node_name_handle(data['pipeline_tree'])
Пример #9
0
def fast_create_task(request, project_id):
    try:
        params = json.loads(request.body)
    except Exception:
        return JsonResponse({
            'result': False,
            'message': 'invalid json format'
        })

    project = request.project
    logger.info('apigw fast_create_task info, project_id: {project_id}, params: {params}'.format(
        project_id=project.id,
        params=params))

    if not request.is_trust:
        perms_tuples = [(project_resource, [project_resource.actions.fast_create_task.id], project)]
        batch_verify_or_raise_auth_failed(principal_type='user',
                                          principal_id=request.user.username,
                                          perms_tuples=perms_tuples,
                                          status=200)

    try:
        pipeline_tree = params['pipeline_tree']
        pipeline_node_name_handle(pipeline_tree)
        pipeline_tree.setdefault('gateways', {})
        pipeline_tree.setdefault('constants', {})
        pipeline_tree.setdefault('outputs', [])
        draw_pipeline(pipeline_tree)
        validate_web_pipeline_tree(pipeline_tree)
    except Exception as e:
        message = u'invalid param pipeline_tree: %s' % e.message
        logger.exception(message)
        return JsonResponse({
            'result': False,
            'message': message
        })

    try:
        pipeline_instance_kwargs = {
            'name': name_handler(params['name'], TASK_NAME_MAX_LENGTH),
            'creator': request.user.username,
            'pipeline_tree': pipeline_tree,
            'description': params.get('description', '')
        }
    except (KeyError, ValueError) as e:
        return JsonResponse({
            'result': False,
            'message': u'invalid params: %s' % e.message
        })

    try:
        pipeline_instance = TaskFlowInstance.objects.create_pipeline_instance(
            template=None,
            **pipeline_instance_kwargs
        )
    except PipelineException as e:
        message = u'create pipeline instance error: %s' % e.message
        logger.exception(message)
        return JsonResponse({
            'result': False,
            'message': message
        })

    taskflow_kwargs = {
        'project': project,
        'pipeline_instance': pipeline_instance,
        'template_source': ONETIME,
        'create_method': 'api',
    }
    if params.get('category') in [cate[0] for cate in TASK_CATEGORY]:
        taskflow_kwargs['category'] = params['category']
    # 职能化任务,新建后进入职能化认领阶段
    if params.get('flow_type', 'common') == 'common_func':
        taskflow_kwargs['flow_type'] = 'common_func'
        taskflow_kwargs['current_flow'] = 'func_claim'
    # 常规流程,新建后即可执行
    else:
        taskflow_kwargs['flow_type'] = 'common'
        taskflow_kwargs['current_flow'] = 'execute_task'
    task = TaskFlowInstance.objects.create(**taskflow_kwargs)
    return JsonResponse({
        'result': True,
        'data': {
            'task_id': task.id,
            'task_url': task.url,
            'pipeline_tree': task.pipeline_tree
        }})
Пример #10
0
    def save_app_maker(self,
                       biz_cc_id,
                       template_id,
                       app_params,
                       app_id=None,
                       fake=False):
        """
        @summary:
        @param biz_cc_id: 业务ID
        @param template_id: 模板ID
        @param app_params: App maker参数
        @param app_id: 为None则新建,否则编辑
        @param fake: 为True则不会真正调用API创建
        @return:
        """
        logger.info('save_app_maker param: %s' % app_params)
        biz = Business.objects.get(cc_id=biz_cc_id)
        try:
            task_template = TaskTemplate.objects.get(pk=template_id,
                                                     business__cc_id=biz_cc_id,
                                                     is_deleted=False)
        except TaskTemplate.DoesNotExist as e:
            return False, _(u"保存失败,引用的流程模板不存在!")

        # 处理可见范围,获取用户信息
        if not app_id:
            # create app_maker object
            kwargs = {
                'business': biz,
                'name': name_handler(app_params['app_name'], 20),
                'code': '',
                'desc': name_handler(app_params.get('app_desc', ''), 200),
                'logo_url': '',
                'link': app_params['app_link_prefix'],
                'creator': app_params['username'],
                'editor': app_params['username'],
                'task_template': task_template,
                'is_deleted': True,
            }
            if app_params.get('template_schema_id'):
                kwargs['template_schema_id'] = app_params['template_schema_id']
            app_maker_obj = AppMaker.objects.create(**kwargs)

            if fake:
                app_maker_obj.code = '%s%s' % (settings.APP_CODE,
                                               time_now_str())
                app_maker_obj.is_deleted = False
                app_maker_obj.save()
                return True, app_maker_obj.code

            # create app on blueking desk
            app_id = app_maker_obj.id
            app_link = '%s%s/newtask/%s/selectnode/?template_id=%s' % (
                app_params['app_link_prefix'], app_id, biz_cc_id, template_id)
            app_create_result = create_maker_app(
                app_params['username'],
                app_params['app_name'],
                app_link,
                app_params['username'],
                task_template.category,
                app_params.get('app_desc', ''),
            )
            if not app_create_result['result']:
                return None, _(u"创建轻应用失败:%s") % app_create_result['message']

            app_code = app_create_result['data']['app_code']
            app_logo_url = '%s/media/applogo/%s.png?v=%s' % (
                settings.BK_URL, app_code, time.time())
            app_maker_obj.code = app_code
            app_maker_obj.link = app_link
            app_maker_obj.logo_url = app_logo_url
            app_maker_obj.is_deleted = False
            app_maker_obj.save()

            return True, app_code

        else:
            try:
                app_maker_obj = AppMaker.objects.get(
                    id=app_id,
                    business__cc_id=biz_cc_id,
                    task_template__id=template_id,
                    is_deleted=False)
            except AppMaker.DoesNotExist as e:
                return False, _(u"保存失败,当前操作的轻应用不存在或已删除!")

            app_code = app_maker_obj.code
            creator = app_maker_obj.creator

            if not fake:
                # edit app on blueking
                app_edit_result = edit_maker_app(
                    creator,
                    app_code,
                    app_params['app_name'],
                    '',
                    creator,
                    task_template.category,
                    app_params.get('app_desc', ''),
                )
                if not app_edit_result['result']:
                    return None, _(u"编辑轻应用失败:%s") % app_edit_result['message']

            # update app maker info
            app_logo_url = '%s/media/applogo/%s.png?v=%s' % (
                settings.BK_URL, app_code, time.time())
            app_maker_obj.name = app_params['app_name']
            app_maker_obj.desc = app_params.get('app_desc', '')
            app_maker_obj.editor = app_params['username']
            if app_params.get('template_schema_id'):
                app_maker_obj.template_schema_id = app_params.get[
                    'template_schema_id']
            # cannot change bound task_template
            app_maker_obj.logo_url = app_logo_url
            app_maker_obj.save()

            return True, app_code
Пример #11
0
    def save_app_maker(self, project_id, app_params, fake=False):
        """
        @summary:
        @param project_id: 项目 ID
        @param app_params: App maker参数
        @param fake: 为True则不会真正调用API创建轻应用
        @return:
        """
        logger.info('save_app_maker params: %s' % app_params)
        app_id = app_params['id']
        if app_id == '0' or not app_id:
            app_id = None
        template_id = app_params['template_id']
        app_params['name'] = name_handler(app_params['name'], 20)
        app_params['desc'] = name_handler(app_params.get('desc', ''), 30)
        proj = Project.objects.get(id=project_id)
        try:
            task_template = TaskTemplate.objects.get(pk=template_id,
                                                     project_id=project_id,
                                                     is_deleted=False)
        except TaskTemplate.DoesNotExist:
            return False, _(u"保存失败,引用的流程模板不存在!")

        # create appmaker
        from gcloud.contrib.appmaker.permissions import mini_app_resource
        if not app_id:
            verify_or_raise_auth_failed(
                principal_type='user',
                principal_id=app_params['username'],
                resource=task_template_resource,
                action_ids=[task_template_resource.actions.create_mini_app.id],
                instance=task_template)

            fields = {
                'project': proj,
                'name': app_params['name'],
                'code': '',
                'desc': app_params['desc'],
                'logo_url': '',
                'link': app_params['link_prefix'],
                'creator': app_params['username'],
                'editor': app_params['username'],
                'task_template': task_template,
                # 生成一个删除状态的对象,以便拼接轻应用访问链接
                'is_deleted': True,
            }
            if app_params.get('template_scheme_id'):
                fields['template_scheme_id'] = app_params['template_scheme_id']
            app_maker_obj = AppMaker.objects.create(**fields)

            # update app link
            app_id = app_maker_obj.id
            app_link = '{appmaker_prefix}{app_id}/newtask/{project_id}/selectnode/?template_id={template_id}'.format(
                appmaker_prefix=app_params['link_prefix'],
                app_id=app_id,
                project_id=project_id,
                template_id=template_id)
            app_maker_obj.link = app_link

            if fake:
                app_maker_obj.code = '%s%s' % (settings.APP_CODE,
                                               time_now_str())
                app_maker_obj.is_deleted = False
                app_maker_obj.save()
                return True, app_maker_obj

            # create app on blueking desk
            app_create_result = create_maker_app(
                app_params['username'],
                app_params['name'],
                app_link,
                app_params['username'],
                task_template.category,
                app_params['desc'],
            )
            if not app_create_result['result']:
                return False, _(u"创建轻应用失败:%s") % app_create_result['message']

            app_code = app_create_result['data']['bk_light_app_code']
            app_maker_obj.code = app_code
            app_maker_obj.is_deleted = False

        # edit appmaker
        else:
            try:
                app_maker_obj = AppMaker.objects.get(
                    id=app_id,
                    project_id=project_id,
                    task_template__id=template_id,
                    is_deleted=False)
            except AppMaker.DoesNotExist:
                return False, _(u"保存失败,当前操作的轻应用不存在或已删除!")

            verify_or_raise_auth_failed(
                principal_type='user',
                principal_id=app_params['username'],
                resource=mini_app_resource,
                action_ids=[mini_app_resource.actions.edit.id],
                instance=app_maker_obj)

            app_code = app_maker_obj.code
            creator = app_maker_obj.creator
            link = app_maker_obj.link

            if not fake:
                # edit app on blueking
                app_edit_result = edit_maker_app(
                    creator,
                    app_code,
                    app_params['name'],
                    link,
                    creator,
                    task_template.category,
                    app_params['desc'],
                )
                if not app_edit_result['result']:
                    return False, _(u"编辑轻应用失败:%s") % app_edit_result['message']

            app_maker_obj.name = app_params['name']
            app_maker_obj.desc = app_params['desc']
            app_maker_obj.editor = app_params['username']
            if 'template_scheme_id' in app_params:
                app_maker_obj.template_scheme_id = app_params[
                    'template_scheme_id']

        # upload app logo
        if not fake and app_params['logo_content']:
            logo = base64.b64encode(app_params['logo_content'])
            app_logo_result = modify_app_logo(app_maker_obj.creator, app_code,
                                              logo)
            if not app_logo_result['result']:
                logger.warning(u"AppMaker[id=%s] upload logo failed: %s" %
                               (app_maker_obj.id, app_logo_result['message']))
            # update app maker info
            app_maker_obj.logo_url = get_app_logo_url(app_code=app_code)

        app_maker_obj.save()
        return True, app_maker_obj