feat(integration-connector): 新增集成连接器管理后台,支持关键字段拆列配置
This commit is contained in:
1 parent
bf30cddc4a
commit
1269e04116
28 files changed
+2909
-155
No files matched your search
@@ -43,6 +43,7 @@ def _serialize_client(client) -> dict:
|
||||
return {
|
||||
'id': client.id, 'code': client.code, 'name': client.name,
|
||||
'connector_type': client.connector_type, 'environment': client.environment,
|
||||
'endpoint': client.endpoint, 'timeout_seconds': client.timeout_seconds,
|
||||
'config_json': client.config_json or {}, 'credential_ref_code': client.credential_ref_code,
|
||||
'enabled': client.enabled,
|
||||
}
|
||||
@@ -52,6 +53,7 @@ def _serialize_interface(iface) -> dict:
|
||||
return {
|
||||
'id': iface.id, 'code': iface.code, 'name': iface.name,
|
||||
'client_instance_code': iface.client_instance_code, 'connector_type': iface.connector_type,
|
||||
'http_method': iface.http_method, 'request_path': iface.request_path,
|
||||
'request_spec_json': iface.request_spec_json or {}, 'input_schema_json': iface.input_schema_json or {},
|
||||
'response_mapping_json': iface.response_mapping_json or {}, 'output_schema_json': iface.output_schema_json or {},
|
||||
'version': iface.version, 'status': iface.status, 'enabled': iface.enabled,
|
||||
@@ -62,6 +64,7 @@ def _serialize_binding(binding) -> dict:
|
||||
return {
|
||||
'id': binding.id, 'capability': binding.capability,
|
||||
'context_selector_json': binding.context_selector_json or {},
|
||||
'context_source_code': binding.context_source_code,
|
||||
'client_instance_code': binding.client_instance_code,
|
||||
'interface_definition_code': binding.interface_definition_code,
|
||||
'mapping_version': binding.mapping_version, 'priority': binding.priority,
|
||||
@@ -146,6 +149,8 @@ def create_client_instance():
|
||||
client = IntegrationClientInstance(
|
||||
code=str(body['code']).strip(), name=str(body['name']).strip(),
|
||||
connector_type=connector_type, environment=str(body.get('environment') or 'prod').strip(),
|
||||
endpoint=(str(body.get('endpoint') or '').strip() or None),
|
||||
timeout_seconds=(int(body['timeout_seconds']) if body.get('timeout_seconds') is not None else None),
|
||||
config_json=body.get('config_json') or {},
|
||||
credential_ref_code=body.get('credential_ref_code'),
|
||||
enabled=bool(body.get('enabled', True)),
|
||||
@@ -177,6 +182,39 @@ def delete_client_instance(code):
|
||||
return ok({'deleted': True})
|
||||
|
||||
|
||||
@integration_connector_bp.route('/client-instances/<code>', methods=['PUT'])
|
||||
@require_admin
|
||||
def update_client_instance(code):
|
||||
"""更新客户端实例(code 不变;可改名称/连接配置/凭据引用/启停)。"""
|
||||
client = IntegrationClientInstance.query.filter_by(code=code).first()
|
||||
if client is None:
|
||||
raise NotFoundError('client instance not found', code='INTEGRATION.CLIENT_INSTANCE_NOT_FOUND')
|
||||
body = request.get_json(silent=True) or {}
|
||||
connector_type = str(body.get('connector_type') or client.connector_type)
|
||||
if connector_type not in VALID_CONNECTOR_TYPES:
|
||||
raise ValidationError('invalid connector_type', details={'allowed': list(VALID_CONNECTOR_TYPES)})
|
||||
if body.get('name') is not None:
|
||||
_require(body.get('name'), 'name')
|
||||
client.name = str(body['name']).strip()
|
||||
if body.get('connector_type') is not None:
|
||||
client.connector_type = connector_type
|
||||
if body.get('environment') is not None:
|
||||
client.environment = str(body['environment']).strip()
|
||||
if 'endpoint' in body:
|
||||
client.endpoint = str(body.get('endpoint') or '').strip() or None
|
||||
if 'timeout_seconds' in body:
|
||||
client.timeout_seconds = int(body['timeout_seconds']) if body.get('timeout_seconds') is not None else None
|
||||
if 'config_json' in body:
|
||||
client.config_json = body.get('config_json') or {}
|
||||
if 'credential_ref_code' in body:
|
||||
client.credential_ref_code = body.get('credential_ref_code')
|
||||
if 'enabled' in body:
|
||||
client.enabled = bool(body.get('enabled', True))
|
||||
from app import db
|
||||
db.session.commit()
|
||||
return ok(_serialize_client(client))
|
||||
|
||||
|
||||
# ---- 接口定义 ----
|
||||
|
||||
@integration_connector_bp.route('/interface-definitions', methods=['GET'])
|
||||
@@ -202,6 +240,8 @@ def create_interface_definition():
|
||||
iface = IntegrationInterfaceDefinition(
|
||||
code=code, name=str(body.get('name') or code).strip(),
|
||||
client_instance_code=body['client_instance_code'], connector_type=connector_type,
|
||||
http_method=(str(body.get('http_method') or '').strip().upper() or None),
|
||||
request_path=(str(body.get('request_path') or '').strip() or None),
|
||||
request_spec_json=body.get('request_spec_json') or {},
|
||||
input_schema_json=body.get('input_schema_json') or {},
|
||||
response_mapping_json=body.get('response_mapping_json') or {},
|
||||
@@ -215,6 +255,46 @@ def create_interface_definition():
|
||||
return ok(_serialize_interface(iface), status=201)
|
||||
|
||||
|
||||
@integration_connector_bp.route('/interface-definitions/<code>', methods=['PUT'])
|
||||
@require_admin
|
||||
def update_interface_definition(code):
|
||||
"""更新接口定义(code 不变;可改模板/入参/回传/出参 Schema、状态、启停)。"""
|
||||
iface = IntegrationInterfaceDefinition.query.filter_by(code=code).first()
|
||||
if iface is None:
|
||||
raise NotFoundError('interface definition not found', code='INTEGRATION.INTERFACE_NOT_FOUND')
|
||||
body = request.get_json(silent=True) or {}
|
||||
if body.get('name') is not None:
|
||||
_require(body.get('name'), 'name')
|
||||
iface.name = str(body['name']).strip()
|
||||
if body.get('client_instance_code') is not None:
|
||||
_require(body.get('client_instance_code'), 'client_instance_code')
|
||||
iface.client_instance_code = body['client_instance_code']
|
||||
if body.get('connector_type') is not None:
|
||||
connector_type = str(body['connector_type'])
|
||||
if connector_type not in VALID_CONNECTOR_TYPES:
|
||||
raise ValidationError('invalid connector_type', details={'allowed': list(VALID_CONNECTOR_TYPES)})
|
||||
iface.connector_type = connector_type
|
||||
if 'http_method' in body:
|
||||
iface.http_method = str(body.get('http_method') or '').strip().upper() or None
|
||||
if 'request_path' in body:
|
||||
iface.request_path = str(body.get('request_path') or '').strip() or None
|
||||
for json_field in ('request_spec_json', 'input_schema_json', 'response_mapping_json', 'output_schema_json'):
|
||||
if json_field in body:
|
||||
setattr(iface, json_field, body.get(json_field) or {})
|
||||
if body.get('version') is not None:
|
||||
iface.version = int(body.get('version') or 1)
|
||||
if body.get('status') is not None:
|
||||
status = str(body['status'])
|
||||
if status not in VALID_STATUSES:
|
||||
raise ValidationError('invalid status', details={'allowed': list(VALID_STATUSES)})
|
||||
iface.status = status
|
||||
if 'enabled' in body:
|
||||
iface.enabled = bool(body.get('enabled', True))
|
||||
from app import db
|
||||
db.session.commit()
|
||||
return ok(_serialize_interface(iface))
|
||||
|
||||
|
||||
# ---- Binding ----
|
||||
|
||||
@integration_connector_bp.route('/bindings', methods=['GET'])
|
||||
@@ -237,6 +317,7 @@ def create_binding():
|
||||
binding = IntegrationBinding(
|
||||
capability=body['capability'],
|
||||
context_selector_json=body.get('context_selector_json') or {},
|
||||
context_source_code=(str(body.get('context_source_code') or '').strip() or None),
|
||||
client_instance_code=body['client_instance_code'],
|
||||
interface_definition_code=body['interface_definition_code'],
|
||||
mapping_version=int(body.get('mapping_version') or 1),
|
||||
@@ -250,6 +331,48 @@ def create_binding():
|
||||
return ok(_serialize_binding(binding), status=201)
|
||||
|
||||
|
||||
@integration_connector_bp.route('/bindings/<int:binding_id>', methods=['PUT'])
|
||||
@require_admin
|
||||
def update_binding(binding_id):
|
||||
"""更新绑定(capability 不变;可改 context 选择器、客户端/接口、映射版本、优先级、环境、生效区间、状态)。"""
|
||||
from app import db
|
||||
binding = db.session.get(IntegrationBinding, binding_id)
|
||||
if binding is None:
|
||||
raise NotFoundError('binding not found', code='INTEGRATION.BINDING_NOT_FOUND')
|
||||
body = request.get_json(silent=True) or {}
|
||||
if body.get('capability') is not None:
|
||||
_require(body.get('capability'), 'capability')
|
||||
binding.capability = body['capability']
|
||||
if 'context_selector_json' in body:
|
||||
binding.context_selector_json = body.get('context_selector_json') or {}
|
||||
if 'context_source_code' in body:
|
||||
binding.context_source_code = str(body.get('context_source_code') or '').strip() or None
|
||||
if body.get('client_instance_code') is not None:
|
||||
_require(body.get('client_instance_code'), 'client_instance_code')
|
||||
binding.client_instance_code = body['client_instance_code']
|
||||
if body.get('interface_definition_code') is not None:
|
||||
_require(body.get('interface_definition_code'), 'interface_definition_code')
|
||||
binding.interface_definition_code = body['interface_definition_code']
|
||||
if body.get('mapping_version') is not None:
|
||||
binding.mapping_version = int(body.get('mapping_version') or 1)
|
||||
if body.get('priority') is not None:
|
||||
binding.priority = int(body.get('priority') or 0)
|
||||
if body.get('environment') is not None:
|
||||
binding.environment = str(body['environment']).strip()
|
||||
if body.get('effective_from') is not None:
|
||||
binding.effective_from = body['effective_from'] or None
|
||||
if body.get('effective_to') is not None:
|
||||
binding.effective_to = body['effective_to'] or None
|
||||
if body.get('status') is not None:
|
||||
status = str(body['status'])
|
||||
if status not in VALID_STATUSES:
|
||||
raise ValidationError('invalid status', details={'allowed': list(VALID_STATUSES)})
|
||||
binding.status = status
|
||||
from app import db
|
||||
db.session.commit()
|
||||
return ok(_serialize_binding(binding))
|
||||
|
||||
|
||||
# ---- 凭据引用 ----
|
||||
|
||||
@integration_connector_bp.route('/credential-references', methods=['GET'])
|
||||
@@ -280,6 +403,28 @@ def create_credential_reference():
|
||||
return ok(_serialize_credential(cred), status=201)
|
||||
|
||||
|
||||
@integration_connector_bp.route('/credential-references/<code>', methods=['PUT'])
|
||||
@require_admin
|
||||
def update_credential_reference(code):
|
||||
"""更新凭据引用(code 不变;可改 secret_ref/provider/type/status)。只存引用,永不回显明文。"""
|
||||
cred = IntegrationCredentialReference.query.filter_by(code=code).first()
|
||||
if cred is None:
|
||||
raise NotFoundError('credential reference not found', code='INTEGRATION.CREDENTIAL_NOT_FOUND')
|
||||
body = request.get_json(silent=True) or {}
|
||||
if body.get('secret_ref') is not None:
|
||||
_require(body.get('secret_ref'), 'secret_ref')
|
||||
cred.secret_ref = str(body['secret_ref']).strip()
|
||||
if body.get('secret_provider') is not None:
|
||||
cred.secret_provider = str(body['secret_provider']).strip()
|
||||
if body.get('credential_type') is not None:
|
||||
cred.credential_type = str(body['credential_type']).strip()
|
||||
if body.get('status') is not None:
|
||||
cred.status = str(body['status']).strip()
|
||||
from app import db
|
||||
db.session.commit()
|
||||
return ok(_serialize_credential(cred))
|
||||
|
||||
|
||||
# ---- 连接测试 ----
|
||||
|
||||
@integration_connector_bp.route('/client-instances/<code>/test', methods=['POST'])
|
||||
|
||||
@@ -84,6 +84,10 @@ class IntegrationClientInstance(Base):
|
||||
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,
|
||||
@@ -119,6 +123,9 @@ class IntegrationInterfaceDefinition(Base):
|
||||
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)
|
||||
@@ -153,6 +160,8 @@ class IntegrationBinding(Base):
|
||||
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,
|
||||
)
|
||||
|
||||
@@ -89,6 +89,25 @@ def resolve_binding(capability: str, context: dict, environment: str, *, now: da
|
||||
iface = IntegrationInterfaceDefinition.query.filter_by(code=row.interface_definition_code).first()
|
||||
if client is None or iface is None:
|
||||
raise BindingNotFoundError(capability=capability)
|
||||
|
||||
# 兼容层:把独立列拼回 dict,供 engine/mapping/transport 无感消费。
|
||||
# 优先列值,缺失回退 JSON(config_json / request_spec_json / context_selector_json)。
|
||||
config = dict(client.config_json or {})
|
||||
if client.endpoint is not None:
|
||||
config['endpoint'] = client.endpoint
|
||||
if client.timeout_seconds is not None:
|
||||
config['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 {})
|
||||
if row.context_source_code is not None and 'source_code' not in selector:
|
||||
selector['source_code'] = row.context_source_code
|
||||
|
||||
return BindingSnapshot(
|
||||
capability=row.capability,
|
||||
context_selector=selector,
|
||||
@@ -98,8 +117,8 @@ def resolve_binding(capability: str, context: dict, environment: str, *, now: da
|
||||
interface_version=iface.version,
|
||||
mapping_version=row.mapping_version,
|
||||
credential_ref_code=client.credential_ref_code,
|
||||
config=client.config_json or {},
|
||||
request_spec=iface.request_spec_json or {},
|
||||
config=config,
|
||||
request_spec=request_spec,
|
||||
response_mapping=iface.response_mapping_json or {},
|
||||
output_schema=iface.output_schema_json or {},
|
||||
)
|
||||
@@ -19,10 +19,11 @@ class SoapTransport:
|
||||
|
||||
def call(self, *, client: dict, interface: dict, request: dict) -> dict[str, Any]:
|
||||
config = client.get('config_json') or {}
|
||||
endpoint = str(config.get('endpoint') or '').strip()
|
||||
wsdl = str(config.get('wsdl') or '').strip()
|
||||
if not endpoint or not wsdl:
|
||||
raise TransportNotFrozenError('SOAP contract not frozen (missing endpoint/wsdl)')
|
||||
# SOAP 只需一个连接地址(WSDL):WSDL 已含服务端点,不需单独的 REST 式 endpoint。
|
||||
# 该地址存于 config['endpoint'](语义即连接地址);timeout 与 REST 同语义。
|
||||
wsdl = str(config.get('endpoint') or '').strip()
|
||||
if not wsdl:
|
||||
raise TransportNotFrozenError('SOAP contract not frozen (missing wsdl/endpoint)')
|
||||
|
||||
# 真实 SOAP 调用(zeep.Client(action=...,构造请求体、解析响应))待 WSDL 冻结后落地。
|
||||
# 真实 SOAP 调用(zeep.Client(timeout=int(config.get('timeout_seconds') or 60), ...)) 待 WSDL 冻结后落地。
|
||||
raise NotImplementedError('SOAP implementation pending frozen WSDL')
|
||||
Reference in new issue
Block a user