-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
416 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
dbm-ui/backend/db_meta/migrations/0043_auto_20241015_2128.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Generated by Django 3.2.25 on 2024-10-15 13:28 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("db_meta", "0042_auto_20240903_1138"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name="bksubzone", | ||
options={"verbose_name": "蓝鲸园区表(BKSubzone)", "verbose_name_plural": "蓝鲸园区表(BKSubzone)"}, | ||
), | ||
migrations.AddField( | ||
model_name="cluster", | ||
name="tags", | ||
field=models.ManyToManyField(blank=True, help_text="标签(外键)", to="db_meta.Tag"), | ||
), | ||
migrations.AddField( | ||
model_name="tag", | ||
name="key", | ||
field=models.CharField(default="", help_text="标签键", max_length=64), | ||
), | ||
migrations.AddField( | ||
model_name="tag", | ||
name="value", | ||
field=models.CharField(default="", help_text="标签值", max_length=255), | ||
), | ||
migrations.AlterField( | ||
model_name="spec", | ||
name="cpu", | ||
field=models.JSONField(help_text='cpu规格描述:{"min":1,"max":10}', null=True), | ||
), | ||
migrations.AlterField( | ||
model_name="spec", | ||
name="device_class", | ||
field=models.JSONField(help_text='实际机器机型: ["class1","class2"]', null=True), | ||
), | ||
migrations.AlterField( | ||
model_name="spec", | ||
name="mem", | ||
field=models.JSONField(help_text='mem规格描述:{"min":100,"max":1000}', null=True), | ||
), | ||
migrations.AlterField( | ||
model_name="spec", | ||
name="qps", | ||
field=models.JSONField(default=dict, help_text='qps规格描述:{"min": 1, "max": 100}'), | ||
), | ||
migrations.AlterField( | ||
model_name="spec", | ||
name="storage_spec", | ||
field=models.JSONField(help_text='存储磁盘需求配置:[{"mount_point":"/data","size":500,"type":"ssd"}]', null=True), | ||
), | ||
migrations.AlterField( | ||
model_name="tag", | ||
name="bk_biz_id", | ||
field=models.IntegerField(default=0, help_text="业务 ID"), | ||
), | ||
migrations.AlterField( | ||
model_name="tag", | ||
name="type", | ||
field=models.CharField( | ||
choices=[("custom", "自定义标签"), ("system", "系统标签"), ("builtin", "内置标签")], | ||
default="custom", | ||
help_text="tag类型", | ||
max_length=64, | ||
), | ||
), | ||
migrations.AlterUniqueTogether( | ||
name="tag", | ||
unique_together={("bk_biz_id", "key", "value")}, | ||
), | ||
migrations.RemoveField( | ||
model_name="tag", | ||
name="cluster", | ||
), | ||
migrations.RemoveField( | ||
model_name="tag", | ||
name="name", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. | ||
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
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. | ||
""" | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from blue_krill.data_types.enum import EnumField, StructuredEnum | ||
|
||
|
||
class TagResourceType(str, StructuredEnum): | ||
DB_RESOURCE = EnumField("db_resource", _("资源池")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. | ||
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
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. | ||
""" | ||
from typing import Dict, List | ||
|
||
from django.db.models import ManyToManyRel | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from backend.db_meta.models import Tag | ||
from backend.db_services.tag.constants import TagResourceType | ||
from backend.exceptions import ValidationError | ||
|
||
|
||
class TagHandler: | ||
"""标签的操作类""" | ||
|
||
def batch_set_tags(self, tag_ids: List[int]): | ||
""" | ||
给资源批量设置标签 | ||
""" | ||
# 1. 判断标签中 key 是否允许多值 | ||
|
||
# 2. 批量设置标签 | ||
pass | ||
|
||
@classmethod | ||
def delete_tags(cls, bk_biz_id: int, ids: List[int]): | ||
""" | ||
删除标签 | ||
""" | ||
# 1. 检查标签是否被引用 | ||
related_resources = cls.query_related_resources(ids) | ||
for related_resource in related_resources: | ||
if related_resource["count"] > 0: | ||
raise ValidationError(_("标签被引用,无法删除")) | ||
|
||
# 2. 批量删除标签 | ||
Tag.objects.filter(bk_biz_id=bk_biz_id, id__in=ids).delete() | ||
|
||
@classmethod | ||
def query_related_resources(cls, ids: List[int], resource_type: str = None): | ||
""" | ||
查询关联资源 | ||
""" | ||
# 1. 查询外键关联资源 | ||
data = [] | ||
for tag_id in ids: | ||
info = {"id": tag_id, "related_resources": []} | ||
for field in Tag._meta.get_fields(): | ||
if isinstance(field, ManyToManyRel) and (field.name == resource_type or resource_type is None): | ||
related_objs = field.related_model.objects.prefetch_related("tags").filter(tags__id=tag_id) | ||
info["related_resources"].append( | ||
{ | ||
"resource_type": field.name, | ||
"count": related_objs.count(), | ||
} | ||
) | ||
|
||
# 2. 查询第三方服务关联资源(如资源池、后续可能扩展的别的服务) | ||
if resource_type == TagResourceType.DB_RESOURCE.value or resource_type is None: | ||
info["related_resources"].append( | ||
{ | ||
"resource_type": TagResourceType.DB_RESOURCE.value, | ||
# TODO 请求资源池接口得到统计数量 | ||
"count": 0, | ||
} | ||
) | ||
data.append(info) | ||
return data | ||
|
||
@classmethod | ||
def batch_create(cls, bk_biz_id: int, tags: List[Dict[str, str]], creator: str = ""): | ||
""" | ||
批量创建标签 | ||
""" | ||
duplicate_tags = cls.verify_duplicated(bk_biz_id, tags) | ||
if duplicate_tags: | ||
raise ValidationError(_("检查到重复的标签"), data=duplicate_tags) | ||
|
||
tag_models = [Tag(bk_biz_id=bk_biz_id, key=tag["key"], value=tag["value"], creator=creator) for tag in tags] | ||
Tag.objects.bulk_create(tag_models) | ||
|
||
@classmethod | ||
def verify_duplicated(cls, bk_biz_id: int, tags: List[Dict[str, str]]) -> List[Dict[str, str]]: | ||
""" | ||
检查标签是否重复 | ||
""" | ||
biz_tags = [f"{tag.key}:{tag.value}" for tag in Tag.objects.filter(bk_biz_id=bk_biz_id)] | ||
duplicate_tags = [] | ||
for tag in tags: | ||
if f'{tag["key"]}:{tag["value"]}' in biz_tags: | ||
duplicate_tags.append(tag) | ||
return duplicate_tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. | ||
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
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. | ||
""" | ||
|
||
from django.utils.translation import gettext_lazy as _ | ||
from rest_framework import serializers | ||
|
||
from backend.bk_web.serializers import AuditedSerializer | ||
from backend.db_meta.models import Tag | ||
|
||
|
||
class TagSerializer(AuditedSerializer, serializers.ModelSerializer): | ||
""" | ||
标签序列化器 | ||
""" | ||
|
||
class Meta: | ||
model = Tag | ||
fields = "__all__" | ||
|
||
|
||
class BatchCreateTagsSerializer(serializers.Serializer): | ||
class CreateTagSerializer(serializers.Serializer): | ||
key = serializers.CharField(help_text=_("标签key")) | ||
value = serializers.CharField(help_text=_("标签value")) | ||
|
||
bk_biz_id = serializers.IntegerField(help_text=_("业务ID")) | ||
tags = serializers.ListField(child=CreateTagSerializer()) | ||
|
||
|
||
class UpdateTagSerializer(serializers.Serializer): | ||
bk_biz_id = serializers.IntegerField(help_text=_("业务ID")) | ||
id = serializers.IntegerField(help_text=_("标签 ID")) | ||
value = serializers.CharField(help_text=_("标签value")) | ||
|
||
|
||
class DeleteTagsSerializer(serializers.Serializer): | ||
bk_biz_id = serializers.IntegerField(help_text=_("业务ID")) | ||
ids = serializers.ListSerializer(child=serializers.IntegerField(help_text=_("标签 ID")), help_text=_("标签 ID 列表")) | ||
|
||
|
||
class QueryRelatedResourceSerializer(serializers.Serializer): | ||
ids = serializers.ListSerializer(child=serializers.IntegerField(help_text=_("标签 ID")), help_text=_("标签 ID 列表")) | ||
resource_type = serializers.CharField(help_text=_("资源类型"), required=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. | ||
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
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. | ||
""" | ||
|
||
from rest_framework.routers import DefaultRouter | ||
|
||
from . import views | ||
|
||
routers = DefaultRouter(trailing_slash=True) | ||
routers.register("", views.TagViewSet, basename="tag") | ||
|
||
urlpatterns = routers.urls |
Oops, something went wrong.