一、后台代码
# -*- coding: utf-8 -*-
import logging
from django.utils.translation import ugettext_lazy as _
from pipeline.core.flow.activity import Service
from pipeline.component_framework.component import Component
from gcloud.conf import settings
logger = logging.getLogger('celery')
get_client_by_user = settings.ESB_GET_CLIENT_BY_USER
# 标准插件所属分类(一般是对应 API 的系统简称,如配置平台(CMDB)
__group_name__ = _(u"自定义插件(CUSTOM)")
# 标准插件后台执行逻辑
class TestCustomService(Service):
__need_schedule__ = False ## 是否是异步标准插件(包括异步轮询和异步回调),默认为 False | interval:异步标准插件的轮询策略
# 标准插件执行前预处理逻辑,可进行插件输入数据预处理和校验,返回True/False,代表预处理结果,为False则不会调用execute函数
# 可以是任何 python 代码,对插件数据进行校验和预处理,返回预处理结果。可以不实现,默认返回True
def execute_pre_process(self, data, parent_data):
test_input = data.inputs.test_input
if not test_input.startswith("test_"):
message = "test_input should start with 'test_'"
data.set_outputs('ex_data', message)
return False
return True
# 标准插件执行逻辑,包含前端参数获取、API 参数组装、结果解析、结果输出
# 可以是任何 python 代码,如果需要调用蓝鲸API网关接口,一般分为参数组装、API 调用、结果解析
# 是任务的公共参数,包括 executor(执行者),operator(操作员),biz_cc_id(所属业务 ID)等。详细信息请查看gcloud/taskflow3/utils.py
def execute(self, data, parent_data):
# 前端参数获取
executor = parent_data.inputs.executor
biz_cc_id = parent_data.inputs.biz_cc_id
client = get_client_by_user(executor)
test_input = data.inputs.test_input
test_textarea = data.inputs.test_textarea
test_radio = data.inputs.test_radio
# API 参数组装
api_kwargs = {
'biz_biz_id': biz_cc_id,
'executor': executor,
'test_input': test_input,
'test_textarea': test_textarea,
'test_radio': test_radio,
}
# 结果解析
api_result = client.test_api.test1(api_kwargs)
logger.info('test_api result: {result}, api_kwargs: {kwargs}'.format(result=api_result, kwargs=api_kwargs))
# 结果输出
if api_result['result']:
data.set_outputs('data1', api_result['data']['data1'])
return True
else:
data.set_outputs('ex_data', api_result['message'])
return False
# def schedule:异步标准插件的轮询或者回调逻辑,同步标准插件不需要定义该方法
# 输出参数定义 | def inputs_format:输入参数定义
def outputs_format(self):
return [
self.OutputItem(name=_(u'结果数据1'), key='data1', type='string')
]
"""
schedule 函数详解:
由 TestCustomService 类的 interval 属性控制调度策略,如 pipeline.core.flow.activity.StaticIntervalGenerator(每隔
多少秒轮询一次)、SquareIntervalGenerator(每次轮询间隔时间是当前已调度次数的平方)。
使用 self.finish_schedule 结束轮询,返回 True 表示标准插件执行成功,False 表示执行失败
"""
# 标准插件定义,前后端服务绑定
class TestCustomComponent(Component):
# 标准插件名称
name = _(u"自定义插件测试")
# 标准插件唯一编码,请保持code和version全局联合唯一
code = 'test_custom'
# 绑定后台服务 TestCustomService
bound_service = TestCustomService
# 前端表单文件路径,请加上 settings.STATIC_URL 前缀
form = '%scustom_plugins/plugin.js' % settings.STATIC_URL
# 插件版本号字符串,用于对 code 相同的插件进行版本管理
version = '1.1.0'
"""
分组命名规则是“系统名(系统英文缩写)”,如“作业平台(JOB)”。
标准插件编码(code)使用下划线方式,规则是“系统名_接口名”,如 job_execute_task。
后台类名使用驼峰式,规则是“标准插件编码+继承类名”,如 JobExecuteTaskService。
前端 JS 文件目录保持和系统名缩写一致,JS 文件名保持和标准插件编码一致。
参数 tag_code 命名规则是“系统名_参数名”,这样可以保证全局唯一;长度不要超过 20 个字符。
后台和前端中的中文都要使用翻译函数,以便可以国际化
"""
二、前台代码
```javascript
(function(){
// 注册准插件前端配置 test_custom:标准插件后台定义的code
$.atoms.test_custom = [
{
// 参数code,请保持全局唯一,命名规范为“系统名_参数名”
tag_code: "test_input",
// 前端表单类型,可选 input、textarea、radio、checkbox、select、datetime、datatable、upload、combine等
type: "input",
// 对应type的属性设置,如 name、validation等
attrs: {
name: gettext("参数1"),
placeholder: gettext("请输入字符串"),
hookable: true,
validation: [
{
type: "required"
}
]
}
},
{
tag_code: "test_textarea",
type: "textarea",
attrs: {
name: gettext("参数2"),
placeholder: gettext("多个用换行分隔"),
hookable: true,
validation: [
{
type: "required"
}
]
}
},
{
tag_code: "test_radio",
type: "radio",
attrs: {
name: gettext("参数3"),
items: [
{value: "1", name: gettext("选项1")},
{value: "2", name: gettext("选项2")},
{value: "3", name: gettext("选项3")}
],
default: "1",
hookable: true,
validation: [
{
type: "required"
}
]
}
}
]
})();