25
25
from backend .components import BKBaseApi
26
26
from backend .configuration .constants import DBType
27
27
from backend .db_meta .enums import ClusterType , InstanceRole
28
- from backend .db_meta .models import Cluster , DBModule , ProxyInstance , StorageInstance
28
+ from backend .db_meta .models import Cluster , DBModule , ProxyInstance , StorageInstance , Tag
29
29
from backend .db_services .dbbase .cluster .handlers import ClusterServiceHandler
30
30
from backend .db_services .dbbase .cluster .serializers import (
31
31
BatchCheckClusterDbsSerializer ,
38
38
from backend .db_services .dbbase .resources .query import ListRetrieveResource , ResourceList
39
39
from backend .db_services .dbbase .resources .serializers import ClusterSLZ
40
40
from backend .db_services .dbbase .serializers import (
41
+ AddClusterTagKeysSerializer ,
41
42
ClusterDbTypeSerializer ,
42
43
ClusterEntryFilterSerializer ,
43
44
ClusterFilterSerializer ,
53
54
QueryClusterCapResponseSerializer ,
54
55
QueryClusterCapSerializer ,
55
56
QueryClusterInstanceCountSerializer ,
57
+ RemoveClusterTagKeysSerializer ,
56
58
ResourceAdministrationSerializer ,
57
59
UpdateClusterAliasSerializer ,
60
+ UpdateClusterTagsSerializer ,
58
61
WebConsoleResponseSerializer ,
59
62
WebConsoleSerializer ,
60
63
)
63
66
from backend .db_services .mysql .remote_service .handlers import RemoteServiceHandler
64
67
from backend .db_services .redis .toolbox .handlers import ToolboxHandler
65
68
from backend .iam_app .handlers .drf_perm .base import DBManagePermission
66
- from backend .iam_app .handlers .drf_perm .cluster import ClusterDBConsolePermission , ClusterWebconsolePermission
69
+ from backend .iam_app .handlers .drf_perm .cluster import (
70
+ ClusterDBConsolePermission ,
71
+ ClusterEditPermission ,
72
+ ClusterWebconsolePermission ,
73
+ )
67
74
68
75
SWAGGER_TAG = _ ("集群通用接口" )
69
76
@@ -83,9 +90,16 @@ class DBBaseViewSet(viewsets.SystemViewSet):
83
90
(
84
91
"simple_query_cluster" ,
85
92
"common_query_cluster" ,
93
+ "filter_clusters" ,
86
94
): [DBManagePermission ()],
87
95
("webconsole" ,): [ClusterWebconsolePermission ()],
88
96
("dbconsole" ,): [ClusterDBConsolePermission ()],
97
+ (
98
+ "update_cluster_alias" ,
99
+ "update_cluster_tag" ,
100
+ "remove_cluster_tag_keys" ,
101
+ "add_cluster_tag_keys" ,
102
+ ): [ClusterEditPermission ()],
89
103
}
90
104
default_permission_class = [DBManagePermission ()]
91
105
@@ -479,5 +493,54 @@ def update_cluster_alias(self, request):
479
493
cluster = Cluster .objects .get (bk_biz_id = validated_data ["bk_biz_id" ], id = validated_data ["cluster_id" ])
480
494
cluster .alias = validated_data ["new_alias" ]
481
495
cluster .save (update_fields = ["alias" ])
482
- serializer = ClusterSLZ (cluster )
483
- return Response (serializer .data )
496
+ return Response (ClusterSLZ (cluster ).data )
497
+
498
+ @common_swagger_auto_schema (
499
+ operation_summary = _ ("更新集群标签" ),
500
+ request_body = UpdateClusterTagsSerializer (),
501
+ tags = [SWAGGER_TAG ],
502
+ )
503
+ @action (methods = ["POST" ], detail = False , serializer_class = UpdateClusterTagsSerializer )
504
+ def update_cluster_tag (self , request ):
505
+ """更新集群标签"""
506
+ data = self .params_validate (self .get_serializer_class ())
507
+ cluster = Cluster .objects .get (bk_biz_id = data ["bk_biz_id" ], id = data ["cluster_id" ])
508
+ tags = Tag .objects .filter (id__in = data ["tags" ])
509
+ # 清空旧标签,添加新标签
510
+ cluster .tags .clear ()
511
+ cluster .tags .add (* tags )
512
+ return Response (ClusterSLZ (cluster ).data )
513
+
514
+ @common_swagger_auto_schema (
515
+ operation_summary = _ ("批量移除标签键" ),
516
+ request_body = RemoveClusterTagKeysSerializer (),
517
+ tags = [SWAGGER_TAG ],
518
+ )
519
+ @action (methods = ["POST" ], detail = False , serializer_class = RemoveClusterTagKeysSerializer )
520
+ def remove_cluster_tag_keys (self , request ):
521
+ """批量移除标签键"""
522
+ data = self .params_validate (self .get_serializer_class ())
523
+ tag_ids = list (Tag .objects .filter (key__in = data ["keys" ]).values_list ("id" , flat = True ))
524
+ Cluster .tags .through .objects .filter (cluster_id__in = data ["cluster_ids" ], tag_id__in = tag_ids ).delete ()
525
+ return Response ()
526
+
527
+ @common_swagger_auto_schema (
528
+ operation_summary = _ ("批量增加标签键" ),
529
+ request_body = AddClusterTagKeysSerializer (),
530
+ tags = [SWAGGER_TAG ],
531
+ )
532
+ @action (methods = ["POST" ], detail = False , serializer_class = AddClusterTagKeysSerializer )
533
+ def add_cluster_tag_keys (self , request ):
534
+ """批量增加标签键"""
535
+ data = self .params_validate (self .get_serializer_class ())
536
+
537
+ tags = Tag .objects .filter (id__in = data ["tags" ])
538
+ through = Cluster .tags .through
539
+
540
+ # 这里需要先获取到所有标签键,然后排除已经存在该key的集群,考虑到这里标签一次性不会太多,暂用for循环处理
541
+ for tag in tags :
542
+ add_clusters = Cluster .objects .filter (id__in = data ["cluster_ids" ]).exclude (tags__key = tag .key )
543
+ add_tags = [through (cluster_id = cluster .id , tag_id = tag .id ) for cluster in add_clusters ]
544
+ through .objects .bulk_create (add_tags )
545
+
546
+ return Response ()
0 commit comments