Skip to content

Commit

Permalink
fix(backend): mongodb单据协议调整 #7747
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud committed Dec 27, 2024
1 parent 5315507 commit 6ae3604
Show file tree
Hide file tree
Showing 26 changed files with 198 additions and 103 deletions.
17 changes: 14 additions & 3 deletions dbm-ui/backend/db_dirty/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from django_filters.filters import BaseInFilter, NumberFilter

from backend.db_dirty.models import DirtyMachine, MachineEvent
from backend.db_meta.models import Cluster


class NumberInFilter(BaseInFilter, NumberFilter):
Expand All @@ -23,15 +24,25 @@ class NumberInFilter(BaseInFilter, NumberFilter):
class MachineEventFilter(filters.FilterSet):
operator = filters.CharFilter(field_name="creator", lookup_expr="icontains", label=_("操作者"))
bk_biz_id = filters.NumberFilter(field_name="bk_biz_id", label=_("业务"))
event = filters.CharFilter(field_name="event", lookup_expr="exact", label=_("事件类型"))
ips = filters.CharFilter(field_name="ip", method="filter_ips", label=_("过滤IP"))
create_at__lte = filters.DateTimeFilter(field_name="create_at", lookup_expr="lte", label=_("创建时间早于"))
create_at__gte = filters.DateTimeFilter(field_name="create_at", lookup_expr="gte", label=_("创建时间晚于"))
events = filters.CharFilter(field_name="events", method="filter_events", label=_("过滤事件"))
ips = filters.CharFilter(field_name="ips", method="filter_ips", label=_("过滤IP"))
domain = filters.CharFilter(field_name="domain", method="filter_domain", label=_("过滤集群"))

def filter_ips(self, queryset, name, value):
return queryset.filter(ip__in=value.split(","))

def filter_events(self, queryset, name, value):
return queryset.filter(event__in=value.split(","))

def filter_domain(self, queryset, name, value):
cluster_ids = Cluster.objects.filter(immute_domain__icontains=value).values_list("id", flat=True)
return queryset.filter(ticket__clusteroperaterecord__cluster_id__in=cluster_ids)

class Meta:
model = MachineEvent
fields = ["operator", "bk_biz_id", "event", "ips"]
fields = ["operator", "bk_biz_id", "events", "ips", "create_at__lte", "create_at__gte", "domain"]


class DirtyMachinePoolFilter(filters.FilterSet):
Expand Down
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
Loading

0 comments on commit 6ae3604

Please sign in to comment.