Skip to content

Commit

Permalink
fix(backend): 追加标签功能 #7747
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud committed Dec 9, 2024
1 parent fbd1f38 commit fa2f4b3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions dbm-ui/backend/components/dbresource/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion dbm-ui/backend/db_services/dbresource/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []:
Expand Down
5 changes: 5 additions & 0 deletions dbm-ui/backend/db_services/dbresource/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
18 changes: 14 additions & 4 deletions dbm-ui/backend/db_services/dbresource/views/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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))

0 comments on commit fa2f4b3

Please sign in to comment.