跳到主要内容

API 开发者指南

本文档面向需要通过 API 集成或扩展华杨 CMDB 系统的开发者,介绍华杨 CMDB REST API 的设计规范、认证方式、请求/响应格式和各模块 API 概览。

基础约定

URL 结构

所有 API 遵循统一的 URL 模式:

{base_url}/api/{tenantId}/cmdb/{module}/{resource}
部分说明示例
base_urlAPI 服务器地址http://localhost:8102/itom
tenantId租户 ID(路径参数)100000001
module模块名称class-modeltql-querydatain
resource资源路径definitionsdefinitions/{name}

HTTP 方法语义

方法语义幂等性
GET查询资源
POST创建资源或执行操作
PUT更新资源(全量替换)
DELETE删除资源

请求头

Content-Type: application/json
Accept: application/json
Authorization: Bearer {access_token}

认证

系统使用 Keycloak 作为身份认证服务。调用受保护的 API 需要先获取访问令牌。

获取令牌

POST /api/{tenantId}/platform/authenticate

请求体:

{
"username": "admin",
"password": "admin"
}

成功响应(200):

{
"success": true,
"code": 200,
"timestamp": "2026-04-15T10:30:00.000Z",
"data": {
"access_token": "eyJhbGciOiJSUzI1NiIs...",
"refresh_token": "eyJhbGciOiJSUzI1NiIs...",
"token_type": "Bearer",
"expires_in": 300,
"refresh_expires_in": 1800
}
}

认证失败(401):

{
"success": false,
"code": 401,
"timestamp": "2026-04-15T10:30:00.000Z",
"error": {
"errorcode": "15010401",
"message": "Invalid username or password",
"type": "AUTHENTICATION_ERROR",
"details": {}
}
}

使用令牌

在后续请求中通过 Authorization 头携带令牌:

Authorization: Bearer eyJhbGciOiJSUzI1NiIs...

统一响应格式

所有 API 响应遵循统一的信封格式,包含 successcodetimestamp 三个必填字段。

成功响应

{
"success": true,
"code": 200,
"timestamp": "2026-04-10T07:28:33.695Z",
"data": { ... }
}

错误响应

{
"success": false,
"code": 400,
"timestamp": "2026-04-10T07:28:33.695Z",
"error": {
"errorcode": "15010400",
"message": "Validation failed",
"type": "VALIDATION_ERROR",
"details": { ... }
}
}

错误码结构

错误码为 8 位数字,由三部分组成:

{serviceCode}{moduleCode}{errorCode}
组成说明示例
服务码(2 位)标识来源服务15 = cmdb-api, 20 = class-model
模块码(2 位)标识模块01 = classModel, 02 = tqlQuery, 03 = datain
错误码(4 位)具体错误0400 = 验证失败, 0404 = 未找到

错误类型

typeHTTP 状态码说明
VALIDATION_ERROR400请求参数验证失败,details 中包含具体字段错误
AUTHENTICATION_ERROR401未认证或令牌无效
AUTHORIZATION_ERROR403权限不足
NOT_FOUND_ERROR404请求的资源不存在
CONFLICT_ERROR409资源冲突(如重复创建)
RATE_LIMIT_ERROR429请求频率超限
INTERNAL_SERVER_ERROR500服务器内部错误

查询参数

列表类接口支持统一的查询参数:

参数类型默认值说明
limitinteger20每页记录数(1-1000)
offsetinteger0偏移量
sortarray-排序条件 [{field, direction}]
fieldsarray-返回字段白名单
filtersarray-过滤条件(见下文)

API 模块概览

Class Model — 配置项类型管理

管理配置项类型(Class Definition)的 CRUD 操作和继承层次。

方法路径说明
GET/class-model/definitions获取所有类定义
POST/class-model/definitions创建类定义
GET/class-model/definitions/{name}获取指定类定义
PUT/class-model/definitions/{name}更新类定义
DELETE/class-model/definitions/{name}删除类定义
GET/class-model/definitions/{name}/parents获取父类列表
GET/class-model/definitions/{name}/children获取子类列表
GET/class-model/definitions/{name}/hierarchy获取完整层次结构

类定义核心字段

{
"name": "ci_server",
"display_name": "服务器",
"parent_name": "ci_host",
"description": "服务器配置项类型",
"attributes": {
"ip_address": {
"name": "ip_address",
"type": "string",
"required": false,
"display_name": "IP 地址"
}
},
"identification_rule": {
"attributes": ["serial_number"]
}
}

查询参数

  • includeAttributes (boolean) — 是否在响应中包含属性定义
  • includeMetadata (boolean) — 是否包含元数据
  • parentDepth / childDepth (integer) — 层次查询的深度

Datain — 数据写入

统一的数据写入入口,支持在单次请求中同时操作配置项和关系。

POST /datain/topology

操作类型(operation 字段):

  • add — 新增节点和关系
  • update — 更新已有节点
  • delete — 删除节点或关系
  • addOrUpdate — 存在则更新,不存在则新增

请求示例:

{
"operation": "add",
"nodes": [
{
"id": "server-001",
"type": "ci_server",
"properties": {
"name": "web-server-01",
"ip_address": "192.168.1.10"
}
}
],
"links": [
{
"id": "link-001",
"type": "r_runs_on",
"from_id": "server-001",
"to_id": "app-001"
}
]
}

nodes 中的 id 为客户端临时标识,系统完成处理后返回实际 ID。links 通过 from_id/to_id 引用节点的临时 ID 建立关系。

TQL Query — 拓扑查询

执行 TQL(Topology Query Language)查询,跨类型查询配置项及其关系数据。

POST /tql-query/execute

请求结构核心概念:

{
"tqlQuery": {
"type": "normal",
"rootNode": "ci_computer",
"limit": 20,
"nodes": [
{
"type": "ci_computer",
"queryIdentifier": "computer",
"visible": true,
"typeConfig": {
"includeSubtypes": true
},
"layoutConfig": {
"type": "SIMPLE",
"items": ["name", "ip_address"]
},
"attributeConditions": [
{
"attribute": "name",
"operator": "contains",
"value": "server"
}
],
"linkConditions": [
{
"linkIdentifier": "runs_on",
"conditionType": "exists"
}
]
}
],
"relations": [
{
"type": "r_runs_on",
"queryIdentifier": "runs_on",
"from": "computer",
"to": "app",
"linkType": "normal"
}
],
"orderBy": [{ "field": "id", "direction": "ASC" }]
}
}

顶层字段

字段说明
type查询类型,目前固定为 normal
rootNode查询的根配置项类型名称
nodes查询涉及的配置项类型节点列表
relations节点之间的关系定义
orderBy排序配置,格式为 [{field, direction}],direction 可选 ASC / DESC
limit返回数量限制

nodes[] 节点字段

字段说明
type配置项类型名称(如 ci_computer
queryIdentifier节点标识符,在查询内唯一,供 relations 引用
visible是否在结果中返回,默认 true
typeConfig类型配置(是否包含子类型等)
layoutConfig返回字段配置
attributeConditions属性过滤条件数组
linkConditions关系过滤条件数组
idCondition按 ID 过滤(用于指定/排除特定实例)

nodes[].typeConfig — 类型配置

字段说明
includeSubtypes是否包含子类型。true = 包含自身及所有子类型(递归),false = 仅自身
complexTypeConditions各类型单独配置,优先级高于全局 includeSubtypes

complexTypeConditions 每项结构:

字段说明
type类型名称
includeSubtypes该类型是否包含其子类型
exclude是否排除该类型本身,默认 false

nodes[].layoutConfig — 返回字段配置

type 值说明
SIMPLE自定义字段列表,items 为字符串数组,如 ["name", "ip_address"]
ALL返回所有字段,items 省略
COMPLEX按子类型分别配置字段

COMPLEX 模式下 items 每项结构:

字段说明
type子类型名称
includedAttributes包含的字段列表(与 excludedAttributes 二选一)
excludedAttributes排除的字段列表(与 includedAttributes 二选一)

nodes[].attributeConditions — 属性过滤条件

简单条件:

{
"attribute": "name",
"operator": "contains",
"value": "server"
}

条件组(支持嵌套,conditions 内可混入条件组实现不超过5层的深度):

{
"logicalOperator": "and",
"not": false,
"conditions": [
{ "attribute": "name", "operator": "contains", "value": "server" },
{
"logicalOperator": "or",
"conditions": [
{ "attribute": "status", "operator": "equals", "value": "active" }
]
}
]
}
字段说明
attribute属性名称
operator操作符(见下方 TQL 操作符表)
value比较值,类型为 string / number / boolean / 数组
logicalOperator条件组的逻辑运算符:and / or
not是否对条件组取反,默认 false
conditions条件组的子条件数组(可嵌套条件组)

TQL 操作符

操作符说明
equals等于
notEquals不等于
greaterThan大于
greaterThanOrEqual大于等于
lessThan小于
lessThanOrEqual小于等于
in在列表中
notIn不在列表中
isNull为空(value: true = IS NULL,value: false = IS NOT NULL)
like模糊匹配
contains包含
startsWith开始于
endsWith结束于
containsAny包含任意一个(数组)
containsAll包含全部(数组)
containsIgnoreCase包含(不区分大小写)

nodes[].linkConditions — 关系过滤条件

简单条件:

{
"linkIdentifier": "runs_on",
"conditionType": "exists"
}

条件组(支持嵌套,结构与 attributeConditions 一致):

{
"logicalOperator": "and",
"not": false,
"conditions": [
{ "linkIdentifier": "runs_on", "conditionType": "exists" },
{ "linkIdentifier": "depends_on", "conditionType": "optional" }
]
}
字段说明
linkIdentifier引用的 relation 的 queryIdentifier
conditionType关系约束类型(见下表)
logicalOperator条件组逻辑运算符:and / or
not是否对条件组取反
conditions条件组的子条件数组(可嵌套)
conditionType说明
exists关系必须存在(至少一条)
not_exists关系必须不存在
optional关系可有可无

分页:当响应中 hasMoretrue 时,使用 cursor 值继续查询获取后续数据。

Query Definition — 查询定义管理

管理可复用的 TQL 查询定义。

方法路径说明
GET/query-definition/definitions获取所有查询定义
POST/query-definition/definitions创建查询定义
GET/query-definition/definitions/{name}获取指定查询定义
PUT/query-definition/definitions/{name}更新查询定义
DELETE/query-definition/definitions/{name}删除查询定义
GET/query-definition/query-groups获取查询分组列表
POST/query-definition/query-groups创建查询分组
PUT/query-definition/query-groups/{id}更新查询分组
DELETE/query-definition/query-groups/{id}删除查询分组
GET/query-definition/query-groups/{groupId}/definitions获取分组下的查询定义

查询参数:includeDefinition (boolean) — 列表接口默认不返回查询定义内容,设为 true 可包含。

CI History — 配置项变更历史

查询配置项的变更记录。

方法路径说明
GET/ci-history查询变更历史列表
GET/ci-history/entity/{entityId}/{entityType}查询指定配置项的变更历史
GET/ci-history/transaction/{transactionId}查询指定事务的变更详情

Relation History — 关系变更历史

查询配置项关系的变更记录。

方法路径说明
GET/relation-history查询关系变更历史列表
GET/relation-history/from/{fromId}/to/{toId}查询两个配置项之间的变更历史

CI Export — 配置项导出

异步导出配置项数据。

方法路径说明
POST/ci-export/list提交导出任务
GET/ci-export/{jobId}/status查询导出任务状态

导出为异步操作。提交后返回 jobId,通过轮询状态接口获取进度和下载链接。

Dashboard — 仪表盘管理

管理仪表盘及其分组。

仪表盘 API 支持 CRUD 操作、分组管理、导入导出等功能。系统预置的仪表盘带有保护标记,不可编辑或删除。

Swagger 文档

系统内置了 Swagger UI,部署后可在导航菜单中找到 API 文档入口,直接在浏览器中查看完整的 API 规范、请求示例和在线调试。