Skip to content

Commit

Permalink
fix(backend): mongodb单据协议调整 TencentBlueKing#7747
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud committed Dec 26, 2024
1 parent 5315507 commit 9340a1e
Show file tree
Hide file tree
Showing 25 changed files with 184 additions and 100 deletions.
2 changes: 1 addition & 1 deletion dbm-ui/backend/db_dirty/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def host_event_trigger(cls, bk_biz_id, hosts, event, operator="", ticket=None, s
pool = MACHINE_EVENT__POOL_MAP.get(event)
# 如果主机非标准话,则查询cc
if not standard:
hosts = ResourceHandler.standardized_resource_host(hosts, bk_biz_id)
hosts = ResourceHandler.standardized_resource_host(hosts)
# 主机池流转
if pool:
DirtyMachine.hosts_pool_transfer(bk_biz_id, hosts, pool, operator, ticket)
Expand Down
28 changes: 20 additions & 8 deletions dbm-ui/backend/db_dirty/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from backend.db_dirty.constants import PoolType
from backend.db_dirty.models import DirtyMachine, MachineEvent
from backend.db_meta.models import AppCache
from backend.ticket.constants import TicketType
from backend.ticket.models import Ticket


Expand All @@ -38,18 +39,29 @@ def biz_map(self):
return self._biz_map

@property
def ticket_cluster_map(self):
if not hasattr(self, "_ticket_cluster_map"):
def ticket_map(self):
if not hasattr(self, "_ticket_map"):
ticket_ids = [event.ticket.id for event in self.instance if event.ticket]
tickets = Ticket.objects.filter(id__in=ticket_ids)
ticket_cluster_map = {ticket.id: ticket.details.get("clusters", {}).values() for ticket in tickets}
setattr(self, "_ticket_cluster_map", ticket_cluster_map)
return self._ticket_cluster_map
ticket_map = Ticket.objects.in_bulk(ticket_ids, field_name="id")
setattr(self, "_ticket_map", ticket_map)
return self._ticket_map

def to_representation(self, instance):
biz, ticket_data = self.biz_map[instance.bk_biz_id], self.ticket_cluster_map.get(instance.ticket_id, [])
biz, ticket = self.biz_map[instance.bk_biz_id], self.ticket_map.get(instance.ticket_id)
if not ticket:
clusters, ticket_type_display = [], ""
else:
clusters = ticket.details.get("clusters", [])
ticket_type_display = TicketType.get_choice_label(ticket.ticket_type)

instance = super().to_representation(instance)
instance.update(bk_biz_name=biz.bk_biz_name, db_app_abbr=biz.db_app_abbr, clusters=ticket_data)
instance.update(
bk_biz_name=biz.bk_biz_name,
db_app_abbr=biz.db_app_abbr,
clusters=clusters,
ticket_type_display=ticket_type_display,
)

return instance


Expand Down
10 changes: 8 additions & 2 deletions dbm-ui/backend/db_services/dbresource/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.forms import model_to_dict
from django.utils.translation import ugettext as _

from backend.components import CCApi
from backend.components.dbresource.client import DBResourceApi
from backend.components.gse.client import GseApi
from backend.db_meta.enums.spec import SpecClusterType, SpecMachineType
Expand Down Expand Up @@ -466,13 +467,18 @@ def _format_resource_fields(data, _cloud_info, _biz_infos, _tag_infos):
return resource_data

@classmethod
def standardized_resource_host(cls, hosts, bk_biz_id=None):
def standardized_resource_host(cls, hosts):
"""标准化主机信息,将cc字段统一成资源池字段"""
host_ids = [host["bk_host_id"] for host in hosts]
# 获取主机通用信息
hosts = ResourceQueryHelper.search_cc_hosts(role_host_ids=host_ids)
# 获取主机拓扑信息
host_topos = CCApi.find_host_biz_relations({"bk_host_id": host_ids})
host_biz_map = {host["bk_host_id"]: host["bk_biz_id"] for host in host_topos}
# 补充主机信息
for host in hosts:
host.update(
bk_biz_id=bk_biz_id,
bk_biz_id=host_biz_map.get(host["bk_host_id"]),
ip=host.get("bk_host_innerip"),
city=host.get("idc_city_name"),
host_id=host.get("bk_host_id"),
Expand Down
14 changes: 13 additions & 1 deletion dbm-ui/backend/tests/mock_data/components/cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"count": 2,
}
MOCK_FIND_HOST_BIZ_RELATIONS_RETURN = [
{"bk_host_id": 1, "bk_biz_id": constant.BK_BIZ_ID, "bk_module_id": constant.BK_MODULE_ID}
{"bk_host_id": 1, "bk_biz_id": constant.BK_BIZ_ID, "bk_module_id": constant.BK_MODULE_ID},
{"bk_host_id": 2, "bk_biz_id": constant.BK_BIZ_ID, "bk_module_id": constant.BK_MODULE_ID},
{"bk_host_id": 3, "bk_biz_id": constant.BK_BIZ_ID, "bk_module_id": constant.BK_MODULE_ID},
]

MOCK_GET_BIZ_INTERNAL_MODULE_RETURN = {
Expand Down Expand Up @@ -59,6 +61,7 @@
"bk_idc_area": "华东",
"bk_idc_area_id": 5,
"bk_os_name": "Tencent linux release 1.2 (Final)",
"bk_os_type": "linux",
"bk_svr_device_cls_name": "D2-4-50-10",
"idc_city_id": REGISTERED_CITY_ID,
"idc_city_name": "上海",
Expand All @@ -76,6 +79,7 @@
"bk_idc_area": "华东",
"bk_idc_area_id": 5,
"bk_os_name": "Tencent linux release 1.2 (Final)",
"bk_os_type": "linux",
"bk_svr_device_cls_name": "D2-4-50-10",
"idc_city_id": REGISTERED_CITY_ID,
"idc_city_name": "上海",
Expand All @@ -93,6 +97,7 @@
"bk_idc_area": "华东",
"bk_idc_area_id": 5,
"bk_os_name": "Tencent linux release 1.2 (Final)",
"bk_os_type": "linux",
"bk_svr_device_cls_name": "D2-4-50-10",
"idc_city_id": REGISTERED_CITY_ID,
"idc_city_name": "上海",
Expand All @@ -110,6 +115,7 @@
"bk_idc_area": "华东",
"bk_idc_area_id": 5,
"bk_os_name": "Tencent linux release 1.2 (Final)",
"bk_os_type": "linux",
"bk_svr_device_cls_name": "D2-4-50-10",
"idc_city_id": REGISTERED_CITY_ID,
"idc_city_name": "上海",
Expand All @@ -127,6 +133,7 @@
"bk_idc_area": "华东",
"bk_idc_area_id": 5,
"bk_os_name": "Tencent linux release 1.2 (Final)",
"bk_os_type": "linux",
"bk_svr_device_cls_name": "D2-4-50-10",
# 未录入DBM系统的 City ID
"idc_city_id": NOT_REGISTERED_CITY_ID,
Expand Down Expand Up @@ -197,6 +204,11 @@ def search_module(self, *args, **kwargs):
return self.search_module_return

def find_host_biz_relations(self, *args, **kwargs):
if args[0].get("bk_host_id"):
return [
{"bk_host_id": host_id, "bk_biz_id": constant.BK_BIZ_ID, "bk_module_id": constant.BK_MODULE_ID}
for host_id in args[0]["bk_host_id"]
]
return self.find_host_biz_relations_return

def list_service_instance_detail(self, *args, **kwargs):
Expand Down
22 changes: 20 additions & 2 deletions dbm-ui/backend/tests/mock_data/components/nodeman.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,29 @@
specific language governing permissions and limitations under the License.
"""

AGENT_DETAILS_DATA = {
"agent_id": "",
"ip": "xx.xx.xx.xx",
"ipv6": "",
"host_name": "VM",
"os_name": "Linux",
"os_type": "Linux",
"alive": 1,
"cloud_area": {"id": 0, "name": "xxx"},
"biz": {"id": 2, "name": "xxx"},
"bk_agent_id": "",
"bk_agent_alive": 1,
"bk_cloud_id": 0,
}


class NodemanApiMock(object):
"""
gse 的 mock 接口
"""

def ipchooser_host_details(self, *args, **kwargs):
return {}
@classmethod
def ipchooser_host_details(cls, *args, **kwargs):
params = args[0]
host_details = [{"host_id": host["host_id"], **AGENT_DETAILS_DATA} for host in params["host_list"]]
return host_details
16 changes: 8 additions & 8 deletions dbm-ui/backend/tests/mock_data/ticket/doris_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,23 @@
"bk_biz_id": BK_BIZ_ID,
"remark": "测试启用doris集群",
"ticket_type": TicketType.DORIS_ENABLE,
"details": {"cluster_id": CLUSTER_ID + 1},
"details": {"cluster_id": CLUSTER_ID},
}

# doris禁用单据
DORIS_DISABLE_TICKET_DATA = {
"bk_biz_id": BK_BIZ_ID,
"remark": "测试禁用doris集群",
"ticket_type": TicketType.DORIS_DISABLE,
"details": {"cluster_id": CLUSTER_ID},
"details": {"cluster_id": CLUSTER_ID + 1},
}

# doris下架单据
DORIS_DESTROY_TICKET_DATA = {
"bk_biz_id": BK_BIZ_ID,
"remark": "测试下架doris集群",
"ticket_type": TicketType.DORIS_DESTROY,
"details": {"cluster_id": CLUSTER_ID + 1},
"details": {"cluster_id": 1},
}

# doris扩容input单据
Expand All @@ -82,7 +82,7 @@
"remark": "",
"details": {
"cluster_id": CLUSTER_ID,
"nodes": {
"old_nodes": {
"hot": [],
"cold": [],
"observer": [
Expand Down Expand Up @@ -145,9 +145,9 @@
"cluster_id": CLUSTER_ID,
"ip_source": "resource_pool",
"old_nodes": {
"hot": [{"ip": "127.0.0.6", "bk_host_id": 6, "bk_cloud_id": 0}],
"hot": [{"ip": "127.0.0.6", "bk_host_id": 2, "bk_cloud_id": 0}],
"follower": [{"ip": "127.0.0.1", "bk_host_id": 1, "bk_cloud_id": 0}],
"observer": [{"ip": "127.0.0.4", "bk_host_id": 4, "bk_cloud_id": 0}],
"observer": [{"ip": "127.0.0.4", "bk_host_id": 3, "bk_cloud_id": 0}],
},
"resource_spec": {
"follower": {"spec_id": 1, "count": 1},
Expand Down Expand Up @@ -296,7 +296,7 @@
"db_module_id": 0,
"immute_domain": "doris.doris01.dba.db",
"major_version": "2.0.4",
"phase": "online",
"phase": "offline",
"status": "normal",
"bk_cloud_id": 0,
"region": "default",
Expand All @@ -314,7 +314,7 @@
"db_module_id": 0,
"immute_domain": "doris.doris02.dba.db",
"major_version": "2.0.4",
"phase": "offline",
"phase": "online",
"status": "normal",
"bk_cloud_id": 0,
"region": "default",
Expand Down
12 changes: 6 additions & 6 deletions dbm-ui/backend/tests/mock_data/ticket/mongodb_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
{
"cluster_id": CLUSTER_ID,
"role": "mongos",
"reduce_nodes": [{"ip": "127.0.0.1", "bk_cloud_id": 0, "bk_host_id": 3}],
"old_nodes": {"mongos": [{"ip": "127.0.0.1", "bk_cloud_id": 0, "bk_host_id": 3}]},
}
],
},
Expand Down Expand Up @@ -127,9 +127,9 @@
"infos": [
{
"cluster_id": CLUSTER_ID,
"mongos": [{"ip": "1.1.1.3", "spec_id": 3, "bk_cloud_id": 0}],
"mongodb": [{"ip": "1.1.1.4", "spec_id": 3, "bk_cloud_id": 0}],
"mongo_config": [{"ip": "1.1.1.5", "spec_id": 3, "bk_cloud_id": 0}],
"mongos": [{"ip": "1.1.1.3", "spec_id": 3, "bk_cloud_id": 0, "bk_host_id": 1}],
"mongodb": [{"ip": "1.1.1.4", "spec_id": 3, "bk_cloud_id": 0, "bk_host_id": 2}],
"mongo_config": [{"ip": "1.1.1.5", "spec_id": 3, "bk_cloud_id": 0, "bk_host_id": 3}],
}
],
},
Expand Down Expand Up @@ -286,7 +286,7 @@
"name": "",
"time_zone": "+08:00",
"bk_instance_id": 7087,
"machine_id": 130,
"machine_id": 4,
"phase": "online",
},
{
Expand Down Expand Up @@ -344,7 +344,7 @@
"access_layer": "proxy",
"machine_type": "mongos",
"cluster_type": ClusterType.MongoShardedCluster,
"bk_host_id": 130,
"bk_host_id": 4,
"bk_os_name": "linux centos",
"bk_idc_area": "",
"bk_idc_area_id": 0,
Expand Down
10 changes: 6 additions & 4 deletions dbm-ui/backend/tests/mock_data/ticket/sqlserver_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,14 @@
"ticket_type": TicketType.SQLSERVER_RESTORE_SLAVE,
"remark": "xxx",
"details": {
"ip_source": "manual_input",
"ip_source": "resource_pool",
"infos": [
{
"cluster_ids": [CLUSTER_ID + 1],
"old_slave_host": {"ip": "1.1.1.4", "bk_cloud_id": 0, "bk_host_id": 3},
"new_slave_host": {"ip": "1.1.1.5", "bk_cloud_id": 0, "bk_host_id": 4},
"old_nodes": {"old_slave_host": [{"ip": "1.1.1.4", "bk_cloud_id": 0, "bk_host_id": 3}]},
"resource_spec": {
"sqlserver_ha": {"spec_id": 0, "hosts": [{"ip": "1.1.1.5", "bk_cloud_id": 0, "bk_host_id": 4}]}
},
}
],
},
Expand All @@ -379,7 +381,7 @@
{
"resource_spec": {"sqlserver_ha": {"spec_id": 102, "count": 1}},
"cluster_ids": [CLUSTER_ID + 1],
"old_slave_host": {"ip": "1.1.1.4", "bk_cloud_id": 0, "bk_host_id": 3},
"old_nodes": {"old_slave_host": [{"ip": "1.1.1.4", "bk_cloud_id": 0, "bk_host_id": 3}]},
}
],
},
Expand Down
4 changes: 4 additions & 0 deletions dbm-ui/backend/tests/ticket/server_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from backend.tests.mock_data.components.cc import CCApiMock
from backend.tests.mock_data.components.itsm import ItsmApiMock
from backend.tests.mock_data.components.nodeman import NodemanApiMock
from backend.tests.mock_data.iam_app.permission import PermissionMock
from backend.tests.mock_data.ticket.ticket_flow import ROOT_ID, SN
from backend.ticket.constants import FlowType, TicketFlowStatus, TicketStatus
Expand All @@ -27,6 +28,9 @@ class TestFlowBase:
patch.object(TicketViewSet, "get_permissions"),
patch("backend.ticket.flow_manager.itsm.ItsmApi", ItsmApiMock()),
patch("backend.db_services.cmdb.biz.CCApi", CCApiMock()),
patch("backend.db_services.dbresource.handlers.CCApi", CCApiMock()),
patch("backend.db_services.ipchooser.query.resource.CCApi", CCApiMock()),
patch("backend.db_services.ipchooser.query.resource.BKNodeManApi", NodemanApiMock()),
patch("backend.components.cmsi.handler.CmsiHandler.send_msg", lambda msg: "有一条待办事项需要您处理"),
patch("backend.db_services.cmdb.biz.Permission", PermissionMock),
]
Expand Down
11 changes: 7 additions & 4 deletions dbm-ui/backend/ticket/builders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from backend import env
from backend.components.dbresource.client import DBResourceApi
from backend.configuration.constants import DBType, SystemSettingsEnum
from backend.configuration.models import DBAdministrator, SystemSettings
from backend.configuration.models import BizSettings, DBAdministrator, SystemSettings
from backend.db_dirty.constants import MachineEventType, PoolType
from backend.db_dirty.models import DirtyMachine, MachineEvent
from backend.db_meta.models import AppCache, Cluster
Expand Down Expand Up @@ -299,7 +299,6 @@ def format_ticket_data(self):
"os_type": self.ticket_data["recycle_hosts"][0]["os_type"],
"db_type": self.ticket.group,
}
self.add_common_params()

def post_callback(self):
# 转移到故障池,记录机器事件(如果是资源池则资源导入后会记录)
Expand All @@ -325,6 +324,7 @@ def __init__(self, ticket: Ticket):

def format_ticket_data(self):
recycle_hosts = self.ticket_data["recycle_hosts"]
# 我们认为,在资源申请的情况下,不会混用多个集群类型
self.ticket_data = {
"ticket_id": self.ticket.id,
"for_biz": self.ticket_data["ip_recycle"]["for_biz"],
Expand All @@ -334,8 +334,9 @@ def format_ticket_data(self):
"operator": self.ticket.creator,
# 标记为退回
"return_resource": True,
# 要查询主机实际的业务管控
"bk_biz_id": recycle_hosts[0]["bk_host_id"],
}
self.add_common_params()

def pre_callback(self):
# 在run的时候才会生成task id,此时要更新到资源池参数里面
Expand Down Expand Up @@ -589,8 +590,10 @@ def register(cls, ticket_type: str, **kwargs) -> Callable:
@param ticket_type: 单据类型
@param kwargs: 单据注册的额外信息,主要是将单据归为不同的集合中,目前有这几种类型
1. is_apply: bool ---- 表示单据是否是部署类单据(类似集群的部署,扩容,替换等)
2. phase: ClusterPhase ---- 表示单据与集群状态的映射
2. is_recycle: bool ---- 表示单据是否是下架类单据(类似集群的下架,缩容,替换等)
3. phase: ClusterPhase ---- 表示单据与集群状态的映射
4. action: ActionMeta ---- 表示单据与权限动作的映射
5. is_sensitive: bool --- 是否为敏感类单据(有特殊鉴权)
"""

def inner_wrapper(wrapped_class: TicketFlowBuilder) -> TicketFlowBuilder:
Expand Down
6 changes: 2 additions & 4 deletions dbm-ui/backend/ticket/builders/common/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,18 +502,16 @@ def patch_instance_details(self):

def patch_recycle_host_details(self):
"""补充回收主机信息,在回收类单据一定调用此方法"""
bk_biz_id = self.ticket.bk_biz_id
recycle_hosts = fetch_recycle_hosts(self.ticket.details)
if not recycle_hosts:
return
self.ticket.details["recycle_hosts"] = ResourceHandler.standardized_resource_host(recycle_hosts, bk_biz_id)
self.ticket.details["recycle_hosts"] = ResourceHandler.standardized_resource_host(recycle_hosts)

def patch_recycle_cluster_details(self, role=None):
"""补充集群下架后回收主机信息,在下架类单据一定调用此方法"""
bk_biz_id = self.ticket.bk_biz_id
recycle_hosts = Cluster.get_cluster_related_machines(fetch_cluster_ids(self.ticket.details), role)
recycle_hosts = [{"bk_host_id": host.bk_host_id} for host in recycle_hosts]
self.ticket.details["recycle_hosts"] = ResourceHandler.standardized_resource_host(recycle_hosts, bk_biz_id)
self.ticket.details["recycle_hosts"] = ResourceHandler.standardized_resource_host(recycle_hosts)

def patch_ticket_detail(self):
if self.need_patch_cluster_details:
Expand Down
Loading

0 comments on commit 9340a1e

Please sign in to comment.