203 lines
11 KiB
Python
203 lines
11 KiB
Python
"""models/integration_connector.py
|
||
|
||
用途:Integration Connector(外部系统连接器框架)P1 配置与运行时模型:
|
||
CredentialReference、ClientInstance、InterfaceDefinition、Binding、Call。
|
||
依赖:models.base.Base、SQLAlchemy 2.0 ORM。
|
||
逻辑:配置层实体(CredentialReference/ClientInstance/InterfaceDefinition/Binding)
|
||
维护版本与状态;IntegrationCall 记录一次技术调用事实(冻结绑定版本 + 脱敏摘要)。
|
||
连接器类型(rest/soap)与状态均用 CheckConstraint 兜底,fail-closed 语义在服务层。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime
|
||
from typing import Any, Optional
|
||
|
||
from sqlalchemy import (
|
||
Boolean,
|
||
CheckConstraint,
|
||
DateTime,
|
||
ForeignKey,
|
||
Index,
|
||
Integer,
|
||
JSON,
|
||
String,
|
||
UniqueConstraint,
|
||
text,
|
||
)
|
||
from sqlalchemy.orm import Mapped, mapped_column
|
||
from sqlalchemy.dialects.postgresql import JSONB
|
||
|
||
from app.models.base import Base
|
||
|
||
# P1 仅落地 REST/SOAP;后续扩 RPA/SQL/FILE 时放宽约束并新建迁移。
|
||
CONNECTOR_TYPES = ('rest', 'soap')
|
||
CONFIG_STATUSES = ('draft', 'published', 'retired')
|
||
CREDENTIAL_STATUSES = ('active', 'rotating', 'retired')
|
||
|
||
# context_selector_json 需要参与唯一约束/索引:PostgreSQL 的 JSON 无默认 btree 操作符表,
|
||
# 无法建唯一索引;映射为 JSONB 即可。SQLite/测试仍用 JSON(TEXT)。
|
||
CONTEXT_SELECTOR_JSON = JSON().with_variant(JSONB(), 'postgresql')
|
||
|
||
|
||
class IntegrationCredentialReference(Base):
|
||
"""凭据引用:只存 secret_ref 键 + 元数据,真实凭据由 Secret 服务注入,绝不落库明文。"""
|
||
|
||
__tablename__ = 'integration_credential_references'
|
||
__table_args__ = (
|
||
UniqueConstraint('code', name='uq_integration_credential_references_code'),
|
||
CheckConstraint(
|
||
f"status IN ({', '.join(repr(s) for s in CREDENTIAL_STATUSES)})",
|
||
name='ck_integration_credential_references_status',
|
||
),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
code: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
secret_ref: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
secret_provider: Mapped[str] = mapped_column(String(32), nullable=False, default='env', server_default='env')
|
||
credential_type: Mapped[str] = mapped_column(String(32), nullable=False, default='password', server_default='password')
|
||
version: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default=text('1'))
|
||
status: Mapped[str] = mapped_column(String(16), nullable=False, default='active', server_default='active')
|
||
last_verified_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
updated_at: Mapped[datetime] = mapped_column(
|
||
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False,
|
||
)
|
||
|
||
|
||
class IntegrationClientInstance(Base):
|
||
"""客户端实例:一份到外部系统的连接配置(连到哪 + 凭据引用 + 超时/TLS/默认 org)。"""
|
||
|
||
__tablename__ = 'integration_client_instances'
|
||
__table_args__ = (
|
||
UniqueConstraint('code', name='uq_integration_client_instances_code'),
|
||
CheckConstraint(
|
||
f"connector_type IN ({', '.join(repr(s) for s in CONNECTOR_TYPES)})",
|
||
name='ck_integration_client_instances_connector_type',
|
||
),
|
||
Index('ix_integration_client_instances_type_env', 'connector_type', 'environment'),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
code: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
name: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
connector_type: Mapped[str] = mapped_column(String(16), nullable=False)
|
||
environment: Mapped[str] = mapped_column(String(16), nullable=False, default='prod', server_default='prod')
|
||
# 关键强类型字段独立列(JSON 保留作兜底;兼容层 binding.py 拼回 dict)。
|
||
# endpoint = 连接地址:REST 为接口基地址,SOAP 为 WSDL 地址(WSDL 内含服务端点)。
|
||
endpoint: Mapped[Optional[str]] = mapped_column(String(512), nullable=True)
|
||
timeout_seconds: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
||
config_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
credential_ref_code: Mapped[Optional[str]] = mapped_column(
|
||
ForeignKey('integration_credential_references.code'), nullable=True,
|
||
)
|
||
enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=text('true'))
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
updated_at: Mapped[datetime] = mapped_column(
|
||
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False,
|
||
)
|
||
|
||
|
||
class IntegrationInterfaceDefinition(Base):
|
||
"""接口定义:一个可调操作(请求模板 + 入参 Schema + 返回值映射 + 出参 Schema)。"""
|
||
|
||
__tablename__ = 'integration_interface_definitions'
|
||
__table_args__ = (
|
||
UniqueConstraint('code', name='uq_integration_interface_definitions_code'),
|
||
CheckConstraint(
|
||
f"connector_type IN ({', '.join(repr(s) for s in CONNECTOR_TYPES)})",
|
||
name='ck_integration_interface_definitions_connector_type',
|
||
),
|
||
CheckConstraint(
|
||
f"status IN ({', '.join(repr(s) for s in CONFIG_STATUSES)})",
|
||
name='ck_integration_interface_definitions_status',
|
||
),
|
||
Index('ix_integration_interface_definitions_client', 'client_instance_code'),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
code: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
name: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
client_instance_code: Mapped[str] = mapped_column(
|
||
ForeignKey('integration_client_instances.code'), nullable=False,
|
||
)
|
||
connector_type: Mapped[str] = mapped_column(String(16), nullable=False)
|
||
# 关键强类型字段独立列(JSON 保留作兜底)。
|
||
http_method: Mapped[Optional[str]] = mapped_column(String(16), nullable=True)
|
||
request_path: Mapped[Optional[str]] = mapped_column(String(512), nullable=True)
|
||
request_spec_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
input_schema_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
response_mapping_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
output_schema_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
version: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default=text('1'))
|
||
status: Mapped[str] = mapped_column(String(16), nullable=False, default='draft', server_default='draft')
|
||
enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=text('true'))
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
updated_at: Mapped[datetime] = mapped_column(
|
||
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False,
|
||
)
|
||
|
||
|
||
class IntegrationBinding(Base):
|
||
"""绑定:(Capability, context_selector) → 客户端 + 接口 + 映射版本;版本化、可发布、可退役。"""
|
||
|
||
__tablename__ = 'integration_bindings'
|
||
__table_args__ = (
|
||
UniqueConstraint(
|
||
'capability', 'environment', 'context_selector_json', name='uq_integration_bindings_cap_env_ctx',
|
||
),
|
||
CheckConstraint(
|
||
f"status IN ({', '.join(repr(s) for s in CONFIG_STATUSES)})",
|
||
name='ck_integration_bindings_status',
|
||
),
|
||
CheckConstraint(
|
||
"priority >= 0", name='ck_integration_bindings_priority',
|
||
),
|
||
Index('ix_integration_bindings_cap_env_status', 'capability', 'environment', 'status'),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
capability: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
context_selector_json: Mapped[dict[str, Any]] = mapped_column(CONTEXT_SELECTOR_JSON, nullable=False, default=dict)
|
||
# 最常用的 context 匹配键独立列(JSON 保留作兜底)。
|
||
context_source_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
client_instance_code: Mapped[str] = mapped_column(
|
||
ForeignKey('integration_client_instances.code'), nullable=False,
|
||
)
|
||
interface_definition_code: Mapped[str] = mapped_column(
|
||
ForeignKey('integration_interface_definitions.code'), nullable=False,
|
||
)
|
||
mapping_version: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default=text('1'))
|
||
priority: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text('0'))
|
||
environment: Mapped[str] = mapped_column(String(16), nullable=False, default='prod', server_default='prod')
|
||
effective_from: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
effective_to: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
status: Mapped[str] = mapped_column(String(16), nullable=False, default='draft', server_default='draft')
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
updated_at: Mapped[datetime] = mapped_column(
|
||
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False,
|
||
)
|
||
|
||
|
||
class IntegrationCall(Base):
|
||
"""一次技术调用事实:冻结绑定版本 + 脱敏请求/响应摘要,供审计与追溯。"""
|
||
|
||
__tablename__ = 'integration_calls'
|
||
__table_args__ = (
|
||
UniqueConstraint('operation_id', name='uq_integration_calls_operation_id'),
|
||
Index('ix_integration_calls_capability_created', 'capability', 'created_at'),
|
||
Index('ix_integration_calls_trace_id', 'trace_id'),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
operation_id: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
capability: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
binding_snapshot_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
request_summary_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
response_summary_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||
elapsed_ms: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text('0'))
|
||
trace_id: Mapped[str] = mapped_column(String(64), nullable=False, default='', server_default='')
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|