From 285436ab9e318aa0e6c44cd7bd341146f709a46c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=99=93=E6=AD=A6=5Bfinelin=5D?= Date: Fri, 12 Jul 2024 20:09:19 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=EF=BC=9Acustom=20channel=20in=20CMSI=20w?= =?UTF-8?q?ill=20be=20hidden=20when=20exec=20sync?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/esb/esb/conf/default.py | 4 ++++ .../commands/sync_system_and_channel_data.py | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/esb/esb/conf/default.py b/src/esb/esb/conf/default.py index cae52ac01..cf762420c 100644 --- a/src/esb/esb/conf/default.py +++ b/src/esb/esb/conf/default.py @@ -335,3 +335,7 @@ def _(s): BK_SSM_API_URL = env.str("BK_SSM_API_URL", "") BK_SSM_ACCESS_TOKEN_CACHE_MAXSIZE = env.int("BK_SSM_ACCESS_TOKEN_CACHE_MAXSIZE", 2000) BK_SSM_ACCESS_TOKEN_CACHE_TTL_SECONDS = env.int("BK_SSM_ACCESS_TOKEN_CACHE_TTL_SECONDS", 300) + +# 从环境变量获取同步通道数据时豁免的自定义通道列表,格式为字符串,格式为: +# [{"board": "default", "method": "get", "path": "/cmsi/send_xxxx"}] +EXCLUDE_OFFICIAL_CHANNELS_WHEN_SYNCING = env.str("BK_ESB_EXCLUDE_OFFICIAL_CHANNELS_WHEN_SYNCING", default="") diff --git a/src/esb/esb/esb/management/commands/sync_system_and_channel_data.py b/src/esb/esb/esb/management/commands/sync_system_and_channel_data.py index ac48c2f10..755211bf7 100644 --- a/src/esb/esb/esb/management/commands/sync_system_and_channel_data.py +++ b/src/esb/esb/esb/management/commands/sync_system_and_channel_data.py @@ -18,6 +18,7 @@ # import json import logging +from django.conf import settings from django.core.management.base import BaseCommand @@ -305,4 +306,25 @@ def _get_official_channel_ids(self): return list(ESBChannel.objects.filter(system_id__in=official_system_ids).values_list("id", flat=True)) def _hide_channels(self, channel_ids): + exclude_channels_config = settings.EXCLUDE_OFFICIAL_CHANNELS_WHEN_SYNCING + try: + # 环境变量只能传递字符串,需要先json格式化 + exclude_channels_config = json.loads(exclude_channels_config) + except Exception: + logger.warning("EXCLUDE_OFFICIAL_CHANNELS_WHEN_SYNCING is not a json format!") + exclude_channels_config = [] + if exclude_channels_config: + channel_ids_dict = dict.fromkeys(channel_ids, None) + for channel in exclude_channels_config: + if "board" in channel and "method" in channel and "path" in channel: + try: + exclude_channel_id = ESBChannel.objects.get( + board=channel["board"], method=channel["method"], path=channel["path"] + ).id + channel_ids_dict.pop(exclude_channel_id, None) + except ESBChannel.DoesNotExist: + logger.warning("channel does not exist: board=%s, method=%s, path=%s", + channel["board"], channel["method"], channel["path"]) + continue + channel_ids = channel_ids_dict.keys() ESBChannel.objects.filter(id__in=channel_ids).update(is_public=False)