bk-cmdb 对象模型 API 实战指南:创建、查询、更新、删除与拓扑图管理
bk-cmdb 对象模型 API 实战指南:创建、查询、更新、删除与拓扑图管理
导读
对象模型(Object Model)是蓝鲸智云配置平台(bk-cmdb)中描述"业务实体"的核心概念——例如主机、交换机、进程等,都以对象模型为载体建模,再通过实例(Instance)承载具体数据。本文基于仓库中归档的开放接口文档 docs/apidoc/archived/topo/object_model.md,系统讲解 bk-cmdb 提供的 7 个对象模型相关 API:创建、删除、更新、查询对象模型,以及查询普通模型拓扑结构、查询与更新拓扑图(Topo Graphics)。读完本文,你将掌握每个接口的请求路径、入参字段、返回结构、注意事项,并能结合 拓扑服务实现 等源码理解其底层调用链与校验逻辑,具备直接调用这些接口完成模型生命周期管理的实战能力。
一、对象模型与接口总览
在 bk-cmdb 中,对象模型通过 bk_obj_id(模型 ID,仅限英文字母序列)、bk_obj_name(模型展示名,可使用任意人类可读语言)与 bk_classification_id(模型分类 ID)来标识与归类。仓库中的元数据结构定义于 src/common/metadata/object.go#L63-L83:
type Object struct {
ID int64 `field:"id" json:"id" ...`
ObjCls string `field:"bk_classification_id" json:"bk_classification_id" ...`
ObjIcon string `field:"bk_obj_icon" json:"bk_obj_icon" ...`
ObjectID string `field:"bk_obj_id" json:"bk_obj_id" ...`
ObjectName string `field:"bk_obj_name" json:"bk_obj_name" ...`
IsPre bool `field:"ispre" json:"ispre" ...`
IsPaused bool `field:"bk_ispaused" json:"bk_ispaused" ...`
Position string `field:"position" json:"position" ...`
OwnerID string `field:"bk_supplier_account" json:"bk_supplier_account" ...`
Creator string `field:"creator" json:"creator" ...`
Modifier string `field:"modifier" json:"modifier" ...`
CreateTime *Time `field:"create_time" json:"create_time" ...`
LastTime *Time `field:"last_time" json:"last_time" ...`
}
对象模型相关的 7 个接口如下表,全部属于拓扑服务(topo_server)的开放 API:
| 序号 | 方法与路径 | API 名称 | 功能 |
|---|---|---|---|
| 1 | POST /api/v3/create/object |
create_object | 创建模型 |
| 2 | DELETE /api/v3/delete/object/{id} |
delete_object | 删除模型 |
| 3 | PUT /api/v3/update/object/{id} |
update_object | 更新模型定义 |
| 4 | POST /api/v3/find/object |
search_objects | 查询模型 |
| 5 | POST /api/v3/find/objecttopology |
search_object_topo | 查询普通模型拓扑 |
| 6 | POST /api/v3/find/objecttopo/scope_type/{scope_type}/scope_id/{scope_id} |
search_object_topo_graphics | 查询拓扑图 |
| 7 | POST /api/v3/update/objecttopo/scope_type/{scope_type}/scope_id/{scope_id} |
update_object_topo_graphics | 更新拓扑图形 |
这些路径的注册可见于拓扑服务的路由初始化文件 src/scene_server/topo_server/service/service_business_initfunc.go#L35-L39:
utility.AddHandler(rest.Action{Verb: http.MethodPost, Path: "/create/object", Handler: s.CreateObject})
utility.AddHandler(rest.Action{Verb: http.MethodPost, Path: "/find/object", Handler: s.SearchObject})
utility.AddHandler(rest.Action{Verb: http.MethodPut, Path: "/update/object/{id}", Handler: s.UpdateObject})
utility.AddHandler(rest.Action{Verb: http.MethodDelete, Path: "/delete/object/{id}", Handler: s.DeleteObject})
utility.AddHandler(rest.Action{Verb: http.MethodPost, Path: "/find/objecttopology", Handler: s.SearchObjectTopo})
拓扑图相关接口注册于同一文件的 L138-L140,对应的处理函数定义在 src/scene_server/topo_server/service/graphics.go。客户端侧的 API 封装(Go SDK)可参考 src/apimachinery/toposerver/object/obj.go,其中 CreateObject、SelectObjectWithParams、UpdateObject、DeleteObject、SelectObjectTopo 分别对应上述接口。
二、创建对象模型(create_object)
2.1 请求说明
- API:
POST /api/v3/create/object - API 名称:create_object
- 功能说明:创建模型(英文:create a object)
2.2 请求示例
{
"creator": "admin",
"bk_classification_id": "cc_test",
"bk_obj_name": "cc_test_inst",
"bk_supplier_account": "0",
"bk_obj_icon": "icon-cc-business",
"bk_obj_id": "cc_test_inst"
}
注:以上 JSON 数据中各字段的取值仅为示例数据。
2.3 入参字段说明
| 字段 | 类型 | 必填 | 默认值 | 说明 | Description |
|---|---|---|---|---|---|
| creator | string | 否 | 无 | 本条数据创建者 | creator |
| bk_classification_id | string | 是 | 无 | 对象模型的分类 ID,只能用英文字母序列命名 | the classification identifier |
| bk_obj_id | string | 是 | 无 | 对象模型的 ID,只能用英文字母序列命名 | the object identifier |
| bk_obj_name | string | 是 | 无 | 对象模型的名字,用于展示,可以使用人类可以阅读的任何语言 | the object name, it will be used to shown |
| bk_supplier_account | string | 是 | 无 | 开发商账号 | supplier account code |
| bk_obj_icon | string | 否 | 无 | 对象模型的 ICON 信息,用于前端显示 | the icon of the object |
关于 bk_obj_icon 的可选取值,原文档指向一份 modleIcon.json 图标清单文件,但该文件并未收录在当前仓库中,实际使用时请以部署环境内提供的图标枚举或前端页面展示的图标集为准。
2.4 返回示例
{
"result": true,
"bk_error_code": 0,
"bk_error_msg": null,
"data": {
"id": 1038
}
}
注:以上 JSON 数据中各字段的取值仅为示例数据。
2.5 返回字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| result | bool | true:成功,false:失败 | true: success, false: failure |
| bk_error_code | int | 错误编码。0 表示 success,>0 表示失败错误 | error code. 0 represent success, >0 represent failure code |
| bk_error_msg | string | 请求失败返回的错误信息 | error message from failed request |
| data | object | 操作结果 | the result |
data 字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| id | int | 新增的数据记录的 ID | the data record identifier |
2.6 底层实现原理
创建模型的完整调用链为:POST /create/object → Service.CreateObject → Logics.ObjectOperation().CreateObject(定义于 src/scene_server/topo_server/logics/model/object.go#L130-L204)。从源码可以看出创建过程包含以下关键环节:
- 先建表后建模型:服务层在创建模型前会先调用
createObjectTable建立对应的 MongoDB 集合,避免模型创建后查询数据时出现SnapshotUnavailable ... pending collection catalog changes的 MongoDB 快照错误(源码中对该原因有明确注释)。 - 参数校验:
isValid(src/scene_server/topo_server/logics/model/object.go#L631-L666)会校验bk_obj_id、bk_obj_name的合法性与必填性,且创建时bk_classification_id必须提供;同时调用metadata.IsCommon判断自定义模型的bk_obj_id不得与内置模型 ID 冲突。 - 分类存在性检查:
isClassificationExist会查询cc_ObjectClassification表确认bk_classification_id真实存在。 - 自动补全基础结构:创建模型后会自动创建默认属性分组(名为
Default、GroupIndex=-1)、创建默认属性(createDefaultAttrs,如创建时间、创建人、更新时间等系统内置字段),并为模型创建唯一索引(Unique)。 - 事务与审计:整个创建过程包裹在
AutoRunTxn事务中,若开启鉴权(auth.EnableAuthorize())还会将模型同步注册到 IAM;同时生成并保存对象审计日志(audit log)。
三、删除对象模型(delete_object)
3.1 请求说明
- API:
DELETE /api/v3/delete/object/{id} - API 名称:delete_object
- 功能说明:删除模型(英文:delete a object)
3.2 请求示例
{
}
3.3 入参字段说明
| 字段 | 类型 | 必填 | 默认值 | 说明 | Description |
|---|---|---|---|---|---|
| id | int | 否 | 无 | 被删除的数据记录的 ID | the id of the target data record |
id为路径参数,即待删除模型的数据记录 ID(创建接口返回的data.id)。
3.4 返回示例
{
"result": true,
"bk_error_code": 0,
"bk_error_msg": null,
"data": "success"
}
注:以上 JSON 数据中各字段的取值仅为示例数据。
3.5 返回字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| result | bool | true:成功,false:失败 | true: success, false: failure |
| bk_error_code | int | 错误编码。0 表示 success,>0 表示失败错误 | error code. 0 represent success, >0 represent failure code |
| bk_error_msg | string | 请求失败返回的错误信息 | error message from failed request |
| data | string | 结果信息 | the result |
3.6 底层实现原理
删除逻辑位于 src/scene_server/topo_server/logics/model/object.go#L241-L299 的 DeleteObject 方法。其执行流程为:
- 按
id条件查询模型,若命中 0 条记录则按历史兼容逻辑忽略(返回空);若命中多条则报CCErrCommGetMultipleObject错误。 - 执行
canDelete检查(服务层传入needCheckInst=true),确认模型当前状态下允许删除。 - 生成删除前的审计日志。
- 调用
DeleteModelCascade级联删除模型、模型属性、属性分组与唯一校验,随后保存审计日志。因此删除模型是一个影响面较大的级联操作,调用前务必确认模型已无在用实例与依赖。
四、更新对象模型(update_object)
4.1 请求说明
- API:
PUT /api/v3/update/object/{id} - API 名称:update_object
- 功能说明:更新模型定义(英文:update a object)
4.2 请求示例
{
"modifier": "admin",
"bk_classification_id": "cc_test",
"bk_obj_name": "cc2_test_inst",
"bk_supplier_account": "0",
"bk_obj_icon": "icon-cc-business",
"position": "{\"ff\":{\"x\":-863,\"y\":1}}"
}
注:以上 JSON 数据中各字段的取值仅为示例数据。
4.3 入参字段说明
| 字段 | 类型 | 必填 | 默认值 | 说明 | Description |
|---|---|---|---|---|---|
| id | int | 否 | 无 | 目标数据的记录 ID,作为更新操作的条件 | the record id, as the update condition |
| modifier | string | 否 | 无 | 本条数据的最后修改人员 | the modifier |
| bk_classification_id | string | 是 | 无 | 对象模型的分类 ID,只能用英文字母序列命名 | the classification identifier |
| bk_obj_name | string | 否 | 无 | 对象模型的名字 | the name of the object |
| bk_supplier_account | string | 是 | 无 | 开发商账号 | supplier account code |
| bk_obj_icon | string | 否 | 无 | 对象模型的 ICON 信息,用于前端显示 | the icon of the object |
| position | json object string | 否 | 无 | 用于前端展示的坐标 | the position to display |
id为路径参数;position为 JSON 对象序列化后的字符串,例如{"ff":{"x":-863,"y":1}},表示该模型节点在拓扑图中的画布坐标。
4.4 返回示例
{
"result": true,
"bk_error_code": 0,
"bk_error_msg": null,
"data": "success"
}
注:以上 JSON 数据中各字段的取值仅为示例数据。
4.5 返回字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| result | bool | true:成功,false:失败 | true: success, false: failure |
| bk_error_code | int | 错误编码。0 表示 success,>0 表示失败错误 | error code. 0 represent success, >0 represent failure code |
| bk_error_msg | string | 请求失败返回的错误信息 | error message from failed request |
| data | string | 结果数据 | the result |
4.6 底层实现原理
更新接口在服务层 src/scene_server/topo_server/service/object.go#L222-L283 的实现中,会先解析路径参数 id 并校验其必须为整数,随后在事务内调用 ObjectOperation().UpdateObject。更新时 isValid(isUpdate=true)仅对请求中实际出现的 bk_obj_id、bk_obj_name 字段做合法性校验。此外,若开启了 IAM 鉴权且本次更新包含 bk_obj_name 字段,服务层还会将该模型的名称变更同步到 IAM 的 Viewer 视图,保证权限视图中的模型名称实时一致。
五、查询对象模型(search_objects)
5.1 请求说明
- API:
POST /api/v3/find/object - API 名称:search_objects
- 功能说明:查询模型(英文:search a object)
5.2 请求示例
{
"bk_obj_id": "biz",
"bk_supplier_account": "0"
}
注:以上 JSON 数据中各字段的取值仅为示例数据。实际使用中可以按照查询的需求填写多个字段。
5.3 入参字段说明
| 字段 | 类型 | 必填 | 默认值 | 说明 | Description |
|---|---|---|---|---|---|
| creator | string | 否 | 无 | 本条数据创建者 | creator |
| modifier | string | 否 | 无 | 本条数据的最后修改人员 | modifier |
| bk_classification_id | string | 否 | 无 | 对象模型的分类 ID,只能用英文字母序列命名 | the classification identifier |
| bk_obj_id | string | 否 | 无 | 对象模型的 ID,只能用英文字母序列命名 | the object identifier |
| bk_obj_name | string | 否 | 无 | 对象模型的名字,用于展示,可以使用人类可以阅读的任何语言 | the name of the object, it will be used to shown |
| bk_supplier_account | string | 否 | 无 | 开发商账号 | supplier account code |
查询条件为组合条件,可同时填写多个字段进行过滤;不填写任何条件则查询全部模型。
5.4 返回示例
{
"result": true,
"bk_error_code": 0,
"bk_error_msg": null,
"data": [
{
"bk_classification_id": "bk_organization",
"create_time": "2018-03-08T11:30:28.005+08:00",
"creator": "cc_system",
"description": "",
"id": 4,
"bk_ispaused": false,
"ispre": true,
"last_time": null,
"modifier": "",
"bk_obj_icon": "icon-XXX",
"bk_obj_id": "XX",
"bk_obj_name": "XXX",
"position": "{\"test_obj\":{\"x\":-253,\"y\":137}}",
"bk_supplier_account": "0"
}
]
}
注:以上 JSON 数据中各字段的取值仅为示例数据。
5.5 返回字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| result | bool | true:成功,false:失败 | true: success, false: failure |
| bk_error_code | int | 错误编码。0 表示 success,>0 表示失败错误 | error code. 0 represent success, >0 represent failure code |
| bk_error_msg | string | 请求失败返回的错误信息 | error message from failed request |
| data | object | 结果数据 | the result |
data 字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| id | int | 数据记录的 ID | the record identifier |
| creator | string | 本条数据创建者 | creator |
| modifier | string | 本条数据的最后修改人员 | modifier |
| bk_classification_id | string | 对象模型的分类 ID,只能用英文字母序列命名 | the classification identifier |
| bk_obj_id | string | 对象模型的 ID,只能用英文字母序列命名 | the object identifier |
| bk_obj_name | string | 对象模型的名字,用于展示 | the name of the object, it will be used to shown |
| bk_supplier_account | string | 开发商账号 | supplier account code |
| bk_ispaused | bool | 是否停用,true or false | is not in use status |
| ispre | bool | 是否预定义,true or false | is pre definition |
| bk_obj_icon | string | 对象模型的 ICON 信息,用于前端显示 | the icon of the object |
| position | json object string | 用于前端展示的坐标 | the position to display |
ispre=true表示该模型为系统预定义模型(如业务 biz、主机 host 等),自定义模型的ispre为 false。查询接口在服务层通过CoreService().Model().ReadModel读取模型列表(src/scene_server/topo_server/service/object.go#L173-L204),并对命中的模型逐一做查询鉴权。
六、查询普通对象模型的拓扑结构(search_object_topo)
6.1 请求说明
- API:
POST /api/v3/find/objecttopology - API 名称:search_object_topo
- 功能说明:查询普通模型拓扑(英文:search a object topo)
6.2 请求示例
{
"bk_classification_id": "bk_host_manage"
}
注:以上 JSON 数据中各字段的取值仅为示例数据。实际使用中可以按照查询的需求填写多个字段。
6.3 入参字段说明
| 字段 | 类型 | 必填 | 默认值 | 说明 | Description |
|---|---|---|---|---|---|
| bk_classification_id | string | 是 | 无 | 对象模型的分类 ID,只能用英文字母序列命名 | the classification identifier |
6.4 返回示例
{
"result": true,
"bk_error_code": 0,
"bk_error_msg": null,
"data": [
{
"arrows": "to",
"from": {
"bk_classification_id": "bk_host_manage",
"bk_obj_id": "host",
"bk_obj_name": "主机",
"position": "{\"bk_host_manage\":{\"x\":-357,\"y\":-344},\"lhmtest\":{\"x\":163,\"y\":75}}",
"bk_supplier_account": "0"
},
"label": "switch_to_host",
"label_name": "",
"label_type": "",
"to": {
"bk_classification_id": "bk_network",
"bk_obj_id": "bk_switch",
"bk_obj_name": "交换机",
"position": "{\"bk_network\":{\"x\":-172,\"y\":-160}}",
"bk_supplier_account": "0"
}
}
]
}
注:以上 JSON 数据中各字段的取值仅为示例数据。
6.5 返回字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| result | bool | true:成功,false:失败 | true: success, false: failure |
| bk_error_code | int | 错误编码。0 表示 success,>0 表示失败错误 | error code. 0 represent success, >0 represent failure code |
| bk_error_msg | string | 请求失败返回的错误信息 | error message from failed request |
| data | array | 结果数据 | the result |
data 字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| arrows | string | 取值 to(单向)或 to,from(双向) | direction |
| label_name | string | 关联关系的名字 | the associated name |
| label | string | 表明 from 通过哪个字段关联到 to | the associated attribute |
| from | string | 对象模型的英文 id,拓扑关系的发起方 | the starting point of the association |
| to | string | 对象模型的英文 ID,拓扑关系的终止方 | the associated end point |
该接口返回的是"模型之间的关联关系"(Association),即某一分类下模型通过关联字段形成的拓扑关系集合,例如示例中"主机(host)→ 交换机(bk_switch)"的单向关联。服务端实现位于 src/scene_server/topo_server/service/object.go#L206-L220,最终由
ObjectOperation().FindObjectTopo完成关联关系组装。
七、查询拓扑图(search_object_topo_graphics)
7.1 请求说明
- API:
POST /api/v3/find/objecttopo/scope_type/{scope_type}/scope_id/{scope_id} - API 名称:search_object_topo_graphics
- 功能说明:查询拓扑图(英文:search a topo graphics)
7.2 请求示例
{
}
注:以上 JSON 数据中各字段的取值仅为示例数据。实际使用中可以按照查询的需求填写多个字段。
7.3 入参字段说明
| 字段 | 类型 | 必填 | 默认值 | 说明 | Description |
|---|---|---|---|---|---|
| scope_type | string | 是 | 无 | 图形范围类型,可选 global、biz、cls(当前只有 global) | the graphical scope type, could be global, biz, cls |
| scope_id | string | 是 | 无 | 图形范围类型下的 ID,如果为 global,则填 0 | the id under the graphical scope, should be 0 when scope type is global |
scope_type与scope_id作为路径参数共同定位"唯一一张图";当前实现仅支持global范围,此时scope_id传0。
7.4 返回示例
{
"result": true,
"bk_error_code": 0,
"bk_error_msg": null,
"data": [
{
"node_type": "obj",
"bk_obj_id": "switch",
"bk_inst_id": 0,
"node_name": "switch",
"position": {
"x": 100,
"y": 100
},
"ext": {},
"bk_obj_icon": "icon-cc-switch2",
"scope_type": "global",
"scope_id": "",
"bk_biz_id": 1,
"bk_supplier_account": "0",
"assts": [
{
"bk_asst_type": "singleasst",
"node_type": "obj",
"bk_obj_id": "host",
"bk_inst_id": 0,
"bk_object_att_id": "host_id",
"lable": {}
}
]
}
]
}
注:以上 JSON 数据中各字段的取值仅为示例数据。
7.5 返回字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| result | bool | true:成功,false:失败 | true: success, false: failure |
| bk_error_code | int | 错误编码。0 表示 success,>0 表示失败错误 | error code. 0 represent success, >0 represent failure code |
| bk_error_msg | string | 请求失败返回的错误信息 | error message from failed request |
| data | array | 结果数据 | the result |
data 字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| node_type | string | 节点类型,可选 obj、inst | node type, could be obj, inst |
| bk_obj_id | string | 对象模型的 ID | the object identifier |
| bk_inst_id | int | 实例 ID | the inst identifier |
| node_name | string | 节点名,当 node_type 为 obj 时是模型名称,当 node_type 为 inst 时是实例名称 | the node name |
| position | string | 节点在图中的位置 | the node position in the graphics |
| ext | object | 前端扩展字段 | the extension field for frontend |
| bk_obj_icon | string | 对象模型的图标 | the object icon |
| scope_type | string | 图形范围类型,可选 global、biz、cls(当前只有 global) | the graphical scope type, could be global, biz, cls |
| scope_id | string | 图形范围类型下的 ID,如果为 global,则填 0 | the id under the graphical scope, should be 0 when scope type is global |
| bk_biz_id | int | 业务 id | business id |
| bk_supplier_account | string | 开发商账号 | supplier account code |
| assts | array | 关联节点 | the associated end point |
assts 字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| bk_asst_type | string | 关联类型 | association type |
| node_type | string | 节点类型,可选 obj、inst | node type, could be obj, inst |
| bk_obj_id | string | 对象模型的 ID | the object identifier |
| bk_inst_id | int | 实例 ID | the inst identifier |
| bk_object_att_id | string | 关联的属性 | the associated attribute |
| lable | object | 标签,扩展字段,未启用 | the association label |
7.6 底层实现原理
拓扑图查询的服务端实现为 Service.SelectObjectTopoGraphics,其业务逻辑在 logics/operation/graphics.go#L55-L122 中。从源码可见:
- 当前实现仅处理
scopeType == "global",其余范围直接返回空节点数组。 - 查询流程为:先按
scope_type + scope_id读取已保存的图形节点,再读取全部模型关联(ReadModelAssociation)与全部模型(ReadModel),随后按模型 ID 聚合、生成每个模型的拓扑节点,并把该模型作为起点(from)的关联关系组装为assts关联节点数组。 - 节点/关联的数据结构对应 src/common/metadata/graphic.go#L27-L51 中的
GraphAsst与TopoGraphics类型,字段与返回示例一一对应(如NodeType、ObjID、InstID、Position、Ext、Icon、Assts等)。
八、更新拓扑图(update_object_topo_graphics)
8.1 请求说明
- API:
POST /api/v3/update/objecttopo/scope_type/{scope_type}/scope_id/{scope_id} - API 名称:update_object_topo_graphics
- 功能说明:更新拓扑图形(英文:update a topo graphics)
8.2 请求示例
{
"origin": [
{
"node_type": "obj",
"bk_obj_id": "switch",
"bk_inst_id": 0,
"position": {
"x": 100,
"y": 100
},
"ext": {
},
"bk_obj_icon": "icon-cc-switch2"
}
]
}
注:以上 JSON 数据中各字段的取值仅为示例数据。实际使用中可以按照查询的需求填写多个字段。
8.3 入参字段说明
| 字段 | 类型 | 必填 | 默认值 | 说明 | Description |
|---|---|---|---|---|---|
| action | string | 是 | 无 | 更新方法,可选 update、override(update 表示仅更新指定节点,override 表示用指定节点覆盖整张图) | modify action, could be update — only update the specified node, override — override the graphics with the specified node |
| scope_type | string | 是 | 无 | 图形范围类型,可选 global、biz、cls(当前只有 global) | the graphical scope type, could be global, biz, cls |
| scope_id | string | 是 | 无 | 图形范围类型下的 ID,如果为 global,则填 0 | the id under the graphical scope, should be 0 when scope type is global |
| node_type | string | 是 | 无 | 节点类型,可选 obj、inst | node type, could be obj, inst |
| bk_obj_id | string | 是 | 无 | 对象模型的 ID | the object identifier |
| bk_inst_id | int | 是 | 无 | 实例 ID | the inst identifier |
| position | string | 否 | 无 | 节点在图中的位置 | the node position in the graphics |
| ext | object | 否 | 无 | 前端扩展字段 | the extension field for frontend |
| bk_obj_icon | string | 否 | 无 | 对象模型的图标 | the object icon |
定位规则:
scope_type、scope_id唯一确定一张图;node_type、bk_obj_id、bk_inst_id三者唯一确定每张图的一个节点,故为必填。
8.4 返回示例
{
"result": true,
"bk_error_code": 0,
"bk_error_msg": null,
"data": "success"
}
注:以上 JSON 数据中各字段的取值仅为示例数据。
8.5 返回字段说明
| 字段 | 类型 | 说明 | Description |
|---|---|---|---|
| result | bool | true:成功,false:失败 | true: success, false: failure |
| bk_error_code | int | 错误编码。0 表示 success,>0 表示失败错误 | error code. 0 represent success, >0 represent failure code |
| bk_error_msg | string | 请求失败返回的错误信息 | error message from failed request |
| data | string | 结果数据 | the result |
8.6 底层实现原理
更新拓扑图由 Service.UpdateObjectTopoGraphicsNew 处理,请求体解码为 metadata.UpdateTopoGraphicsInput(其结构定义见 src/common/metadata/graphic.go#L53-L56,仅包含 Origin []TopoGraphics 字段),即示例中的 origin 数组对应待写入的图形节点集合。从源码结构看,当前版本的实现以 origin 节点列表为准进行图形数据写入,并在事务内调用 GraphicsOperation().UpdateObjectTopoGraphics 完成落库。原文档描述的 action 参数(update / override)属于归档文档对更新语义的约定说明,实际以部署版本接口行为为准;在调用时建议仅按上述示例携带 origin 节点列表,并严格遵守"node_type + bk_obj_id + bk_inst_id 唯一确定节点"的定位规则,避免重复节点覆盖。
九、接口调用通用约定与注意事项
- 鉴权要求:上述接口均受 bk-cmdb 权限体系约束。从 src/scene_server/topo_server/service/object.go 与 src/scene_server/topo_server/service/graphics.go 可以看出,创建、更新、删除、查询接口在服务层均执行了
Authorize/HasFindModelAuthUseID等 IAM 鉴权检查,未授权请求将返回RespNoAuth结果。调用前需要为调用账号在配置平台权限中心(IAM)中授予相应模型操作权限。 - 命名约束:
bk_obj_id与bk_classification_id只能使用英文字母序列命名;bk_obj_name可使用任意语言,仅作展示用途。 - 内置模型保护:
ispre=true的模型为系统预定义模型,创建自定义模型时bk_obj_id不能与内置模型 ID 冲突(源码通过metadata.IsCommon校验);删除接口同样会经过canDelete检查。 - 级联影响:删除模型会级联删除其属性、属性分组与唯一校验(
DeleteModelCascade),务必在业务低峰期谨慎执行。 - 事务一致性:创建、更新、删除模型均在
AutoRunTxn事务中执行,任一步失败会整体回滚;开启鉴权时模型数据还会同步至 IAM,保证权限视图一致。 - 返回结构统一:所有接口均返回
result(bool)、bk_error_code(int,0 表示成功)、bk_error_msg(string,失败时的错误信息)与data(具体数据)四段结构,便于调用方统一解析。
十、相关文档与源码导航
- 本文主体来源:docs/apidoc/archived/topo/object_model.md
- 同目录下可继续阅读:模型分类 object_model_classify.md、模型属性 object_model_property.md、模型字段分组 object_model_field_group.md、模型关联 object_association.md、通用模型实例 object_common_inst.md、主线模型 object_main_line_model.md
- 服务端路由注册:src/scene_server/topo_server/service/service_business_initfunc.go
- 对象模型服务实现:src/scene_server/topo_server/service/object.go
- 对象模型业务逻辑:src/scene_server/topo_server/logics/model/object.go
- 拓扑图服务与逻辑:src/scene_server/topo_server/service/graphics.go、src/scene_server/topo_server/logics/operation/graphics.go
- 元数据结构定义:src/common/metadata/object.go、src/common/metadata/graphic.go
- Go 客户端封装:src/apimachinery/toposerver/object/obj.go