diff --git a/dbm-ui/backend/components/dbresource/client.py b/dbm-ui/backend/components/dbresource/client.py index 1d306e04d2..e32c268ada 100644 --- a/dbm-ui/backend/components/dbresource/client.py +++ b/dbm-ui/backend/components/dbresource/client.py @@ -109,6 +109,11 @@ def __init__(self): self.resource_label_count = self.generate_data_api( method="POST", url="/resource/groupby/label/count", description=_("按照标签统计资源数量") ) + self.resource_append_labels = self.generate_data_api( + method="POST", + url="/resource/append/labels", + description=_("追加标签"), + ) DBResourceApi = _DBResourceApi() diff --git a/dbm-ui/backend/db_services/dbresource/handlers.py b/dbm-ui/backend/db_services/dbresource/handlers.py index 607b1f6f92..bd8c06bb33 100644 --- a/dbm-ui/backend/db_services/dbresource/handlers.py +++ b/dbm-ui/backend/db_services/dbresource/handlers.py @@ -456,7 +456,7 @@ def _format_resource_fields(data, _cloud_info, _biz_infos, _tag_infos): for_biz_infos = AppCache.batch_get_app_attr(bk_biz_ids=for_biz_ids, attr_name="bk_biz_name") # 获取标签信息 label_ids = itertools.chain(*[data["labels"] for data in resource_data["details"] if data["labels"]]) - label_ids = [int(id) for id in label_ids if id.isdigit()] + label_ids = [int(id) for id in label_ids if isinstance(id, int) or id.isdigit()] tag_infos = {tag.id: tag.value for tag in Tag.objects.filter(id__in=label_ids)} # 格式化资源池字段信息 for data in resource_data.get("details") or []: diff --git a/dbm-ui/backend/db_services/dbresource/serializers.py b/dbm-ui/backend/db_services/dbresource/serializers.py index e0086859e2..eaa0623747 100644 --- a/dbm-ui/backend/db_services/dbresource/serializers.py +++ b/dbm-ui/backend/db_services/dbresource/serializers.py @@ -460,3 +460,8 @@ class UworkIpsSerializer(serializers.Serializer): def validate_ip_list(self, value): return value.split(",") + + +class AppendHostLabelSerializer(serializers.Serializer): + bk_host_ids = serializers.ListField(help_text=_("主机ID列表"), child=serializers.IntegerField()) + labels = serializers.ListField(help_text=_("追加标签列表"), child=serializers.CharField()) diff --git a/dbm-ui/backend/db_services/dbresource/views/resource.py b/dbm-ui/backend/db_services/dbresource/views/resource.py index 172d2c7b9a..b4088495f3 100644 --- a/dbm-ui/backend/db_services/dbresource/views/resource.py +++ b/dbm-ui/backend/db_services/dbresource/views/resource.py @@ -8,7 +8,6 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ -import itertools import time from collections import defaultdict from typing import Dict, List @@ -24,19 +23,20 @@ from backend.bk_web.swagger import common_swagger_auto_schema from backend.components.dbresource.client import DBResourceApi from backend.components.uwork.client import UWORKApi -from backend.db_meta.models.machine import DeviceClass from backend.db_dirty.constants import MachineEventType from backend.db_dirty.models import MachineEvent -from backend.db_meta.models import AppCache, Tag +from backend.db_meta.models import AppCache +from backend.db_meta.models.machine import DeviceClass from backend.db_services.dbresource.constants import ( RESOURCE_IMPORT_EXPIRE_TIME, RESOURCE_IMPORT_TASK_FIELD, SWAGGER_TAG, ) -from backend.db_services.dbresource.filters import DeviceClassFilter from backend.db_services.dbresource.exceptions import ResourceReturnException +from backend.db_services.dbresource.filters import DeviceClassFilter from backend.db_services.dbresource.handlers import ResourceHandler from backend.db_services.dbresource.serializers import ( + AppendHostLabelSerializer, GetDiskTypeResponseSerializer, GetMountPointResponseSerializer, ListCvmDeviceClassSerializer, @@ -470,3 +470,13 @@ def check_uwork_ips(self, request): results = UWORKApi.uwork_list(params={"serverIpList": ip_list}) uwork_list = [result["serverIp"] for result in results] return Response({"results": uwork_list}) + + @common_swagger_auto_schema( + operation_summary=_("追加主机标签"), + request_body=AppendHostLabelSerializer(), + tags=[SWAGGER_TAG], + ) + @action(detail=False, methods=["POST"], serializer_class=AppendHostLabelSerializer) + def append_labels(self, request): + append_params = self.params_validate(self.get_serializer_class()) + return Response(DBResourceApi.resource_append_labels(append_params))