Skip to content

Commit

Permalink
Update cmdb search host (#835)
Browse files Browse the repository at this point in the history
* feat: 更改查询业务主机信息接口

* feat: 更改查询业务主机信息接口

* feat: 更改查询业务主机信息接口
  • Loading branch information
abner authored Mar 16, 2021
1 parent c254a5a commit 0d0c8cd
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 53 deletions.
157 changes: 104 additions & 53 deletions bcs-app/backend/components/cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"add_host_lock": "/v2/cc/add_host_lock",
"delete_host_lock": "/v2/cc/delete_host_lock/",
"search_host_lock": "/v2/cc/search_host_lock/",
"list_biz_hosts": "/v2/cc/list_biz_hosts/",
}


Expand All @@ -52,8 +53,7 @@ def get_application(username, bk_supplier_account=None):


def get_app_by_user_role(username, bk_supplier_account=None):
"""获取运维和产品角色中包含username的业务
"""
"""获取运维和产品角色中包含username的业务"""
username_regex_info = "^{username},|,{username},|,{username}$|^{username}$".format(username=username)
regex_map = {"$regex": username_regex_info}
maintainers_resp = get_all_application(
Expand All @@ -67,10 +67,7 @@ def get_app_by_user_role(username, bk_supplier_account=None):
return []
data = maintainers_resp.get("data") or []
data.extend(productor_resp.get("data") or [])
return [
{"id": item["bk_biz_id"], "name": item["bk_biz_name"]}
for item in data
]
return [{"id": item["bk_biz_id"], "name": item["bk_biz_name"]} for item in data]


def get_application_name(username, bk_biz_id, bk_supplier_account=None):
Expand All @@ -89,8 +86,7 @@ def get_application_name(username, bk_biz_id, bk_supplier_account=None):


def get_all_application(username, bk_supplier_account=None, condition={}):
"""获取用户有权限的所有业务
"""
"""获取用户有权限的所有业务"""
resp_data = {"data": [], "message": "", "code": ErrorCode.NoError}
# 设置初始值
start, limit = 0, 200
Expand All @@ -113,15 +109,14 @@ def get_all_application(username, bk_supplier_account=None, condition={}):


def get_app_hosts(username, bk_biz_id, bk_supplier_account=None):
resp = search_host(username, bk_biz_id, bk_supplier_account=bk_supplier_account)
resp = list_biz_hosts(username, bk_biz_id, bk_supplier_account=bk_supplier_account)
if not resp.get("result"):
return resp
data = resp.get("data") or []
if not data:
return resp
ret_data = []
for info in data:
host = info.get("host", {})
for host in data:
if not host:
continue
host["InnerIP"] = host["bk_host_innerip"]
Expand All @@ -131,8 +126,7 @@ def get_app_hosts(username, bk_biz_id, bk_supplier_account=None):


def get_application_staff(username, bk_biz_id, fields=None):
"""获取业务的成员列表
"""
"""获取业务的成员列表"""
if not fields:
fields = ["bk_biz_developer", "bk_biz_maintainer", "bk_biz_tester", "bk_biz_productor"]
resp = get_application_with_page(username, fields=fields, condition={"bk_biz_id": bk_biz_id})
Expand All @@ -146,15 +140,13 @@ def get_application_staff(username, bk_biz_id, fields=None):


def get_host_by_operator(bk_biz_id, username, bk_supplier_account=None):
"""获取业务下主备负责人为username的机器
"""
resp = search_host(username, bk_biz_id, bk_supplier_account=bk_supplier_account)
"""获取业务下主备负责人为username的机器"""
resp = list_biz_hosts(username, bk_biz_id, bk_supplier_account=bk_supplier_account)
if resp.get("code") != ErrorCode.NoError:
return resp
data = resp.get("data") or []
host_list = []
for ip_info in data:
host = ip_info.get("host") or {}
for host in data:
if not host:
continue
operator = host.get("operator", "")
Expand All @@ -168,8 +160,7 @@ def get_host_by_operator(bk_biz_id, username, bk_supplier_account=None):


def get_app_maintainers(username, bk_biz_id, bk_supplier_account=None):
"""获取业务下的所有运维
"""
"""获取业务下的所有运维"""
resp = get_application_with_page(
username,
bk_supplier_account=bk_supplier_account,
Expand All @@ -186,17 +177,15 @@ def get_app_maintainers(username, bk_biz_id, bk_supplier_account=None):


def get_cc_hosts(bk_biz_id, username):
"""查询业务下有权限的主机
"""
"""查询业务下有权限的主机"""
all_maintainers = get_app_maintainers(username, bk_biz_id)
if username in all_maintainers:
return get_app_hosts(username, bk_biz_id)
return get_host_by_operator(bk_biz_id, username)


def check_ips(bk_biz_id, username, req_ip_list):
"""检查用户是都有权限使用请求的IP
"""
"""检查用户是都有权限使用请求的IP"""
msg_suffix = "请联系管理员在【配置中心】添加为业务的运维人员角色"
all_ip_info = get_cc_hosts(bk_biz_id, username)
if not all_ip_info.get("result"):
Expand All @@ -220,14 +209,18 @@ def get_application_host(username, bk_biz_id, inner_ip, bk_supplier_account=None
"""获取服务器信息
注意: 其中信息包含了先前get_host_base_info获取到的信息
"""
resp = search_host(
resp = list_biz_hosts(
username,
bk_biz_id,
bk_supplier_account=bk_supplier_account,
condition=[
{"bk_obj_id": "host", "condition": [{"field": "bk_host_innerip", "operator": "$eq", "value": inner_ip}]}
],
host_property_filter={
"condition": "OR",
"rules": [
{"field": "bk_bak_operator", "operator": "equal", "value": username},
{"filed": "operator", "operator": "equal", "value": "username"},
],
},
)

if resp.get("code") != ErrorCode.NoError:
return {}
data = resp.get("data")
Expand Down Expand Up @@ -256,8 +249,7 @@ def get_host_base_info(username, bk_biz_id, inner_ip):


def host_lock(username, ip_list, bk_cloud_id=0):
"""主机加锁
"""
"""主机加锁"""
url = "{host}{prefix_path}{path}".format(
host=CC_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["add_host_lock"]
)
Expand All @@ -268,8 +260,7 @@ def host_lock(username, ip_list, bk_cloud_id=0):


def remove_host_lock(username, ip_list, bk_cloud_id=0):
"""主机解锁
"""
"""主机解锁"""
url = "{host}{prefix_path}{path}".format(
host=CC_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["delete_host_lock"]
)
Expand All @@ -280,8 +271,7 @@ def remove_host_lock(username, ip_list, bk_cloud_id=0):


def get_host_lock_status(username, ip_list, bk_cloud_id=0):
"""查询主机锁状态
"""
"""查询主机锁状态"""
url = "{host}{prefix_path}{path}".format(
host=CC_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["search_host_lock"]
)
Expand All @@ -292,8 +282,7 @@ def get_host_lock_status(username, ip_list, bk_cloud_id=0):


def cc_set_instance(username, bk_biz_id, bk_set_name, bk_supplier_account=None):
"""实例化set
"""
"""实例化set"""
url = "{host}{prefix_path}{path}".format(
host=CC_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["create_set"]
)
Expand All @@ -303,8 +292,7 @@ def cc_set_instance(username, bk_biz_id, bk_set_name, bk_supplier_account=None):


def get_set_id(username, bk_biz_id, bk_set_name, bk_supplier_account=None):
"""查询set
"""
"""查询set"""
url = "{host}{prefix_path}{path}".format(host=CC_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["get_set"])
data = {"bk_biz_id": bk_biz_id, "condition": {"bk_set_name": bk_set_name}, "fields": ["bk_set_id"]}
resp = cmdb_base_request(url, username, data, bk_supplier_account=bk_supplier_account)
Expand All @@ -317,8 +305,7 @@ def get_set_id(username, bk_biz_id, bk_set_name, bk_supplier_account=None):


def delete_set(username, bk_biz_id, bk_set_id, bk_supplier_account=None):
"""删除set
"""
"""删除set"""
url = "{host}{prefix_path}{path}".format(
host=CC_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["delete_set"]
)
Expand All @@ -328,8 +315,7 @@ def delete_set(username, bk_biz_id, bk_set_id, bk_supplier_account=None):


def cc_module_instance(username, bk_biz_id, bk_set_id, bk_module_name, bk_supplier_account=None):
"""实例化模块
"""
"""实例化模块"""
url = "{host}{prefix_path}{path}".format(
host=CC_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["create_module"]
)
Expand All @@ -342,8 +328,7 @@ def cc_module_instance(username, bk_biz_id, bk_set_id, bk_module_name, bk_suppli


def search_set_module(username, bk_biz_id, bk_set_id, bk_module_name=None, bk_supplier_account=None):
"""查询set下module
"""
"""查询set下module"""
url = "{host}{prefix_path}{path}".format(
host=CC_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["get_module"]
)
Expand All @@ -357,8 +342,7 @@ def search_set_module(username, bk_biz_id, bk_set_id, bk_module_name=None, bk_su


def get_application_with_page(username, bk_supplier_account=None, condition={}, fields=[], start=0, limit=200):
"""分页查询业务
"""
"""分页查询业务"""
url = "{host}{prefix_path}{path}".format(
host=CC_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["get_application"]
)
Expand All @@ -367,8 +351,7 @@ def get_application_with_page(username, bk_supplier_account=None, condition={},


def search_host(username, bk_biz_id, bk_supplier_account=None, ip=None, condition=None):
"""查询所有主机
"""
"""查询所有主机"""
resp_data = {"data": [], "message": "", "code": ErrorCode.NoError, "result": True}
# 设置初始值
start, limit = 0, 200
Expand Down Expand Up @@ -398,8 +381,7 @@ def search_host(username, bk_biz_id, bk_supplier_account=None, ip=None, conditio


def search_host_with_page(username, bk_biz_id, bk_supplier_account=None, ip=None, condition=None, start=0, limit=200):
"""获取业务下的主机
"""
"""获取业务下的主机"""
url = "{host}{prefix_path}{path}".format(
host=CC_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["search_host"]
)
Expand All @@ -412,9 +394,78 @@ def search_host_with_page(username, bk_biz_id, bk_supplier_account=None, ip=None
return cmdb_base_request(url, username, data, bk_supplier_account=bk_supplier_account)


def list_biz_hosts(
username, bk_biz_id, host_property_filter=None, bk_module_ids=None, start=0, limit=200, bk_supplier_account=None
):
"""获取业务下所有主机信息"""
resp_data = {"data": [], "message": "", "code": ErrorCode.NoError, "result": True}
while True:
resp = list_hosts_by_pagination(
username,
bk_biz_id,
host_property_filter=host_property_filter,
bk_module_ids=bk_module_ids,
start=start,
limit=limit,
bk_supplier_account=bk_supplier_account,
)
if resp.get("code") != ErrorCode.NoError:
resp_data.update({"code": resp.get("code"), "message": resp.get("message"), "result": resp.get("result")})
break
data = resp.get("data") or {}
biz_info = data.get("info") or []
resp_data["data"].extend(biz_info)
# 对比机器数量,满足条件时终止请求
start = start + limit
if start >= data.get("count", 0) or not biz_info:
break

return resp_data


def list_hosts_by_pagination(
username, bk_biz_id, host_property_filter=None, bk_module_ids=None, start=0, limit=200, bk_supplier_account=None
):
"""根据分页参数,获取业务下主机信息"""
data = {"bk_biz_id": bk_biz_id, "page": {"start": start, "limit": limit}}
# host_property_filter 主机组合属性查询条件
data["host_property_filter"] = host_property_filter
# bk_module_ids 模块ID列表
data["bk_module_ids"] = bk_module_ids
# cmdb要求添加fields字段
data["fields"] = [
"bk_bak_operator",
"classify_level_name",
"svr_device_class",
"bk_svr_type_id",
"svr_type_name",
"hard_memo",
"bk_host_id",
"bk_host_name",
"idc_name",
"bk_idc_area",
"bk_idc_area_id",
"idc_id",
"idc_unit_name",
"idc_unit_id",
"bk_host_innerip",
"bk_comment",
"module_name",
"operator",
"bk_os_name",
"bk_os_version",
"bk_host_outerip",
"rack",
"bk_cloud_id",
]
url = "{host}{prefix_path}{path}".format(
host=CC_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["list_biz_hosts"]
)
return cmdb_base_request(url, username, data, bk_supplier_account=bk_supplier_account)


def cmdb_base_request(url, username, data, bk_supplier_account=None):
"""请求
"""
"""请求"""
data.update({"bk_app_code": BK_APP_CODE, "bk_app_secret": BK_APP_SECRET, "bk_username": username})
if bk_supplier_account:
data["bk_supplier_account"] = bk_supplier_account
Expand Down
10 changes: 10 additions & 0 deletions docs/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
#### 优化
- [#793](https://github.com/Tencent/bk-bcs-saas/pull/793) HPA: k8s hpa client 替换为 dynamic client


## release-1.3.23

#### 修复
- [#834](https://github.com/Tencent/bk-bcs-saas/pull/834)修复前端paste事件没有过滤\r问题

#### 更新
- 更新查询CMDB业务下主机信息接口


## release-1.3.22

#### 修复
Expand Down

0 comments on commit 0d0c8cd

Please sign in to comment.