feat(integration-connector): database 传输类型、两步绑定编排与调用日志捕获查询
- 新增 database 传输:直连 MySQL/Oracle/SQLServer/PostgreSQL 执行绑定 SQL(新增 pymysql/pymssql 驱动依赖) - 绑定支持 steps_json 多步编排:每步独立传输调用与审计,无 steps 的既有绑定走单步兼容路径 - 客户端实例校验 connector_type ∈ rest/soap/database,database 强制 db_kind 白名单,非 database 置空 - 三个传输层捕获线级事实(最终 SQL/渲染后 XML/原始响应)随调用记录落库,引擎可选透传 business_ref 业务锚点 - 新增调用日志查询 API(列表/详情/同 operation 步骤时间线,仅管理员)与管理端「调用日志」tab:筛选、双栏对照、外部工具复现命令 - 迁移 tt7a/tta9/tt5b:连接器类型枚举、绑定 steps_json、放开 calls 操作唯一约束 - 说明:wire_json 列迁移挂链尾(依赖域表迁移),随后续批次单独落地
This commit is contained in:
1 parent
8a814e7152
commit
e750fa8602
47 files changed
+7441
-353
No files matched your search
@@ -1,11 +1,13 @@
|
||||
"""services/integration_connector/binding.py
|
||||
|
||||
用途:Binding 解析与版本冻结——按 (capability, context, environment) 解析到
|
||||
(客户端 + 接口 + 映射版本),并冻结调用期配置快照。
|
||||
依赖:IntegrationBinding/IntegrationClientInstance/IntegrationInterfaceDefinition、errors.py。
|
||||
有序 BindingStep 快照(一对多配方),并冻结调用期配置。
|
||||
依赖:IntegrationBinding/IntegrationClientInstance/IntegrationInterfaceDefinition、
|
||||
errors.py、transports.base.merge_client_config。
|
||||
逻辑:只命中 published + environment 匹配 + 生效区间 + context_selector 是 context 子集;
|
||||
且接口定义须为 published、客户端实例须 enabled(管理端「发布/退役」「启停」的总开关);
|
||||
同 context 下 priority 高者优先;无命中抛 BindingNotFoundError(fail-closed)。
|
||||
每步接口须 published、每步客户端须 enabled(fail-closed);同 context 下 priority
|
||||
高者优先;无命中或任一步门禁失败抛 BindingNotFoundError。
|
||||
存量空 steps_json 时由旧列合成单步(迁移兜底);读能力=单步,形状与多步统一。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -22,24 +24,35 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class BindingSnapshot:
|
||||
"""一次调用冻结的绑定快照:调用期配置 + 版本号(非活表引用)。"""
|
||||
class BindingStep:
|
||||
"""绑定中的单步冻结快照:接口 + 归属客户端配置 + 该步参数映射。"""
|
||||
|
||||
capability: str
|
||||
context_selector: dict
|
||||
client_code: str
|
||||
interface_code: str
|
||||
connector_type: str
|
||||
interface_version: int
|
||||
mapping_version: int
|
||||
credential_ref_code: str | None
|
||||
client_code: str
|
||||
config: dict
|
||||
request_spec: dict
|
||||
response_mapping: dict
|
||||
output_schema: dict
|
||||
# 域参数→接口参数映射(空 = 同名直通)与接口入参 Schema(映射结果校验)。
|
||||
param_mapping: dict
|
||||
input_schema: dict
|
||||
param_mapping: dict
|
||||
output_schema: dict
|
||||
interface_version: int
|
||||
credential_ref_code: str | None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class BindingSnapshot:
|
||||
"""一次调用冻结的绑定快照:有序步骤 + 映射版本(非活表引用)。
|
||||
|
||||
读能力为单元素 steps;写能力可为多步。credential_ref_code 兼容字段取自首步
|
||||
(真实凭据在逐步 client 上,顺序引擎按步解析)。
|
||||
"""
|
||||
|
||||
capability: str
|
||||
context_selector: dict
|
||||
steps: tuple[BindingStep, ...]
|
||||
mapping_version: int
|
||||
credential_ref_code: str | None
|
||||
|
||||
|
||||
def _to_naive(dt: datetime | None) -> datetime | None:
|
||||
@@ -65,19 +78,140 @@ def _is_subset(selector: dict, context: dict) -> bool:
|
||||
return all(context.get(k) == v for k, v in selector.items())
|
||||
|
||||
|
||||
def _raw_steps_from_binding(row: IntegrationBinding) -> list[dict]:
|
||||
"""取出绑定步骤原料:优先 steps_json;空则由旧单步列合成(存量迁移兜底)。"""
|
||||
raw = list(row.steps_json or [])
|
||||
if raw:
|
||||
return raw
|
||||
# 兼容窗:迁移前/未回填行 → 单元素数组
|
||||
if row.interface_definition_code:
|
||||
return [{
|
||||
'interface_code': row.interface_definition_code,
|
||||
'param_mapping_json': row.param_mapping_json or {},
|
||||
}]
|
||||
return []
|
||||
|
||||
|
||||
def _freeze_step(
|
||||
*,
|
||||
capability: str,
|
||||
binding_id: int,
|
||||
step_index: int,
|
||||
step_raw: dict,
|
||||
) -> BindingStep:
|
||||
"""将一步配置冻结为 BindingStep;门禁失败抛 BindingNotFoundError。
|
||||
|
||||
Args:
|
||||
capability: 能力名(日志/异常)。
|
||||
binding_id: 绑定主键。
|
||||
step_index: 步骤下标(0-based,日志区分哪一步)。
|
||||
step_raw: {interface_code, param_mapping_json|param_mapping}。
|
||||
|
||||
Returns:
|
||||
BindingStep。
|
||||
|
||||
Raises:
|
||||
BindingNotFoundError: 接口/客户端缺失、未发布或停用。
|
||||
"""
|
||||
if not isinstance(step_raw, dict):
|
||||
logger.warning(
|
||||
"connector binding step invalid: capability=%s binding_id=%s step=%s reason=not_dict",
|
||||
capability, binding_id, step_index,
|
||||
)
|
||||
raise BindingNotFoundError(capability=capability)
|
||||
|
||||
iface_code = str(
|
||||
step_raw.get('interface_code') or step_raw.get('interface_definition_code') or ''
|
||||
).strip()
|
||||
if not iface_code:
|
||||
logger.warning(
|
||||
"connector binding step invalid: capability=%s binding_id=%s step=%s reason=missing_interface_code",
|
||||
capability, binding_id, step_index,
|
||||
)
|
||||
raise BindingNotFoundError(capability=capability)
|
||||
|
||||
mapping = step_raw.get('param_mapping_json', step_raw.get('param_mapping', {}))
|
||||
if mapping is None:
|
||||
mapping = {}
|
||||
if not isinstance(mapping, dict):
|
||||
logger.warning(
|
||||
"connector binding step invalid: capability=%s binding_id=%s step=%s reason=param_mapping_not_dict",
|
||||
capability, binding_id, step_index,
|
||||
)
|
||||
raise BindingNotFoundError(capability=capability)
|
||||
|
||||
iface = IntegrationInterfaceDefinition.query.filter_by(code=iface_code).first()
|
||||
if iface is None:
|
||||
logger.warning(
|
||||
"connector binding step target missing: capability=%s binding_id=%s step=%s iface=%s",
|
||||
capability, binding_id, step_index, iface_code,
|
||||
)
|
||||
raise BindingNotFoundError(capability=capability)
|
||||
|
||||
client = IntegrationClientInstance.query.filter_by(code=iface.client_instance_code).first()
|
||||
if client is None:
|
||||
logger.warning(
|
||||
"connector binding step client missing: capability=%s binding_id=%s step=%s client=%s iface=%s",
|
||||
capability, binding_id, step_index, iface.client_instance_code, iface_code,
|
||||
)
|
||||
raise BindingNotFoundError(capability=capability)
|
||||
|
||||
# fail-closed:该步接口未发布或客户端停用 → 视同无可用绑定(日志标明步号与原因)。
|
||||
if iface.status != 'published':
|
||||
logger.warning(
|
||||
"connector binding gated: capability=%s binding_id=%s step=%s iface=%s reason=iface_status=%s",
|
||||
capability, binding_id, step_index, iface_code, iface.status,
|
||||
)
|
||||
raise BindingNotFoundError(capability=capability)
|
||||
if not client.enabled:
|
||||
logger.warning(
|
||||
"connector binding gated: capability=%s binding_id=%s step=%s client=%s reason=client_disabled",
|
||||
capability, binding_id, step_index, client.code,
|
||||
)
|
||||
raise BindingNotFoundError(capability=capability)
|
||||
|
||||
config = merge_client_config({
|
||||
'config_json': client.config_json,
|
||||
'endpoint': client.endpoint,
|
||||
'timeout_seconds': client.timeout_seconds,
|
||||
'db_kind': client.db_kind,
|
||||
})
|
||||
request_spec = dict(iface.request_spec_json or {})
|
||||
if iface.http_method is not None:
|
||||
request_spec['method'] = iface.http_method
|
||||
if iface.request_path is not None:
|
||||
request_spec['path'] = iface.request_path
|
||||
|
||||
return BindingStep(
|
||||
interface_code=iface.code,
|
||||
connector_type=client.connector_type,
|
||||
client_code=client.code,
|
||||
config=config,
|
||||
request_spec=request_spec,
|
||||
response_mapping=iface.response_mapping_json or {},
|
||||
input_schema=iface.input_schema_json or {},
|
||||
param_mapping=dict(mapping),
|
||||
output_schema=iface.output_schema_json or {},
|
||||
interface_version=int(iface.version or 1),
|
||||
credential_ref_code=client.credential_ref_code,
|
||||
)
|
||||
|
||||
|
||||
def resolve_binding(capability: str, context: dict, environment: str, *, now: datetime | None = None) -> BindingSnapshot:
|
||||
"""解析最佳绑定并冻结调用期快照。
|
||||
"""解析最佳绑定并冻结有序步骤快照。
|
||||
|
||||
Args:
|
||||
capability: 能力名(如 in_transit_transfer.query_inventory)。
|
||||
context: 路由上下文(来源/组织/租户等),如 {"source_code": "DBS-AWSBI"}。
|
||||
environment: 环境(prod/test/dev)。
|
||||
now: 可选时钟(测试注入);缺省用 utcnow。
|
||||
|
||||
Returns:
|
||||
BindingSnapshot。
|
||||
BindingSnapshot(steps 至少一元素;读能力通常单步)。
|
||||
|
||||
Raises:
|
||||
BindingNotFoundError: 无可用绑定(fail-closed);接口未发布或客户端已停用同样按
|
||||
无可用绑定处理(与列表「发布/退役」「启停」按钮语义闭环,且不向调用方泄露配置细节)。
|
||||
BindingNotFoundError: 无可用绑定,或任一步接口未发布/客户端停用/目标缺失
|
||||
(与列表「发布/退役」「启停」语义闭环;不向调用方泄露配置细节)。
|
||||
"""
|
||||
base_now = now or datetime.utcnow()
|
||||
rows = IntegrationBinding.query.filter_by(capability=capability, environment=environment, status='published').all()
|
||||
@@ -90,63 +224,37 @@ def resolve_binding(capability: str, context: dict, environment: str, *, now: da
|
||||
continue
|
||||
candidates.append((row, selector))
|
||||
if not candidates:
|
||||
# fail-closed 必须可排障:区分「一条 published 都没有」与「有但路由选择器/生效窗口不命中」,
|
||||
# 否则调用方只看到 no binding for capability(2026-09 排障实测痛点)。
|
||||
# fail-closed 必须可排障:区分「一条 published 都没有」与「有但路由选择器/生效窗口不命中」。
|
||||
logger.warning(
|
||||
"connector binding miss: capability=%s env=%s published_rows=%s context_keys=%s",
|
||||
capability, environment, len(rows), sorted((context or {}).keys()),
|
||||
)
|
||||
raise BindingNotFoundError(capability=capability)
|
||||
|
||||
candidates.sort(key=lambda item: (item[0].priority, len(item[1])), reverse=True)
|
||||
row, selector = candidates[0]
|
||||
|
||||
client = IntegrationClientInstance.query.filter_by(code=row.client_instance_code).first()
|
||||
iface = IntegrationInterfaceDefinition.query.filter_by(code=row.interface_definition_code).first()
|
||||
if client is None or iface is None:
|
||||
raw_steps = _raw_steps_from_binding(row)
|
||||
if not raw_steps:
|
||||
logger.warning(
|
||||
"connector binding target missing: capability=%s binding_id=%s client=%s iface=%s",
|
||||
capability, row.id, row.client_instance_code, row.interface_definition_code,
|
||||
)
|
||||
raise BindingNotFoundError(capability=capability)
|
||||
# fail-closed 门禁:接口未发布(draft/retired)或客户端已停用视同无可用绑定——
|
||||
# 管理端「发布/退役」「启停」按钮因此具备真实的调用期语义;错误类型与无命中一致。
|
||||
if iface.status != 'published' or not client.enabled:
|
||||
logger.warning(
|
||||
"connector binding gated: capability=%s binding_id=%s iface_status=%s client_enabled=%s",
|
||||
capability, row.id, iface.status, client.enabled,
|
||||
"connector binding empty steps: capability=%s binding_id=%s",
|
||||
capability, row.id,
|
||||
)
|
||||
raise BindingNotFoundError(capability=capability)
|
||||
|
||||
# 兼容层:把独立列拼回 dict,供 engine/mapping/transport 无感消费
|
||||
# (与 transports.base.merge_client_config 同一实现)。
|
||||
# 优先列值,缺失回退 JSON(config_json / request_spec_json / context_selector_json)。
|
||||
config = merge_client_config({
|
||||
'config_json': client.config_json,
|
||||
'endpoint': client.endpoint,
|
||||
'timeout_seconds': client.timeout_seconds,
|
||||
})
|
||||
|
||||
request_spec = dict(iface.request_spec_json or {})
|
||||
if iface.http_method is not None:
|
||||
request_spec['method'] = iface.http_method
|
||||
if iface.request_path is not None:
|
||||
request_spec['path'] = iface.request_path
|
||||
|
||||
selector = dict(row.context_selector_json or {})
|
||||
steps: list[BindingStep] = []
|
||||
for idx, step_raw in enumerate(raw_steps):
|
||||
steps.append(_freeze_step(
|
||||
capability=capability,
|
||||
binding_id=row.id,
|
||||
step_index=idx,
|
||||
step_raw=step_raw,
|
||||
))
|
||||
|
||||
return BindingSnapshot(
|
||||
capability=row.capability,
|
||||
context_selector=selector,
|
||||
client_code=client.code,
|
||||
interface_code=iface.code,
|
||||
connector_type=client.connector_type,
|
||||
interface_version=iface.version,
|
||||
context_selector=dict(row.context_selector_json or {}),
|
||||
steps=tuple(steps),
|
||||
mapping_version=row.mapping_version,
|
||||
credential_ref_code=client.credential_ref_code,
|
||||
config=config,
|
||||
request_spec=request_spec,
|
||||
response_mapping=iface.response_mapping_json or {},
|
||||
output_schema=iface.output_schema_json or {},
|
||||
param_mapping=row.param_mapping_json or {},
|
||||
input_schema=iface.input_schema_json or {},
|
||||
credential_ref_code=steps[0].credential_ref_code,
|
||||
)
|
||||
Reference in new issue
Block a user