Skip to content

Commit

Permalink
fix(backend): 支持批量修改访问入口 #1771
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhw8 committed Nov 14, 2023
1 parent 988f9fc commit 15456de
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 30 deletions.
5 changes: 4 additions & 1 deletion dbm-ui/backend/db_monitor/views/duty_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.db.models import Count
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext_lazy as _
from rest_framework import filters
from rest_framework.decorators import action
from rest_framework.response import Response

Expand Down Expand Up @@ -60,10 +61,12 @@ class MonitorDutyRuleViewSet(viewsets.AuditedModelViewSet):
轮值规则视图
"""

queryset = DutyRule.objects.all()
queryset = DutyRule.objects.all().order_by("-update_at")
serializer_class = DutyRuleSerializer
pagination_class = AuditedLimitOffsetPagination
filter_backends = [filters.SearchFilter]
filter_fields = {"db_type": ["exact"], "name": ["exact"]}
search_fields = ["name"]

def _get_custom_permissions(self):
return [GlobalManageIAMPermission()]
Expand Down
3 changes: 1 addition & 2 deletions dbm-ui/backend/db_monitor/views/notice_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from rest_framework.decorators import action
from rest_framework.response import Response

from backend import env
from backend.bk_web import viewsets
from backend.bk_web.pagination import AuditedLimitOffsetPagination
from backend.bk_web.swagger import common_swagger_auto_schema
Expand Down Expand Up @@ -88,7 +87,7 @@ class MonitorNoticeGroupViewSet(viewsets.AuditedModelViewSet):
监控告警组视图
"""

queryset = NoticeGroup.objects.all()
queryset = NoticeGroup.objects.all().order_by("-update_at")
serializer_class = NoticeGroupSerializer
pagination_class = AuditedLimitOffsetPagination
filter_class = MonitorGroupListFilter
Expand Down
24 changes: 16 additions & 8 deletions dbm-ui/backend/db_services/cluster_entry/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@


class ModifyClusterEntrySerializer(serializers.Serializer):
cluster_id = serializers.IntegerField(help_text=_("集群 ID"))
cluster_entry_type = serializers.ChoiceField(help_text=_("集群 ID"), choices=ClusterEntryType.get_choices())
class ClusterEntryDetailsSerializer(serializers.Serializer):
cluster_entry_type = serializers.ChoiceField(help_text=_("访问入口类型"), choices=ClusterEntryType.get_choices())
# DNS 域名修改
domain_name = serializers.CharField(help_text=_("域名"), required=False)
target_instances = serializers.ListSerializer(
child=serializers.CharField(help_text=_("目标实例,格式为 ip#port ")), help_text=_("目标实例列表"), required=False
)

domain_name = serializers.CharField(help_text=_("域名"), required=False)
target_instances = serializers.ListSerializer(
child=serializers.CharField(help_text=_("目标实例,格式为 ip#port ")), help_text=_("目标实例列表"), required=False
cluster_id = serializers.IntegerField(help_text=_("集群 ID"))
cluster_entry_details = serializers.ListSerializer(
help_text=_("访问入口详情"), child=ClusterEntryDetailsSerializer(), allow_empty=False
)

def validate(self, attrs):
if attrs["cluster_entry_type"] == ClusterEntryType.DNS:
if not all(key in attrs for key in ("domain_name", "target_instances")):
raise serializers.ValidationError(_("修改 DNS 访问入口需要传入 domain_name 和 target_instances"))
for detail in attrs["cluster_entry_details"]:
if detail["cluster_entry_type"] == ClusterEntryType.DNS:
if not all(key in detail for key in ("domain_name", "target_instances")):
raise serializers.ValidationError(_("修改 DNS 访问入口需要传入 domain_name 和 target_instances"))
if not detail["target_instances"]:
raise serializers.ValidationError(_("修改 DNS,目标实例列表不能为空"))
return attrs
9 changes: 5 additions & 4 deletions dbm-ui/backend/db_services/cluster_entry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class ClusterEntryViewSet(viewsets.SystemViewSet):
def refresh_cluster_domain(self, request, *args, **kwargs):
data = self.params_validate(self.get_serializer_class())
cluster = Cluster.objects.get(id=data["cluster_id"])
if data["cluster_entry_type"] == ClusterEntryType.DNS:
DnsManage(cluster.bk_biz_id, cluster.bk_cloud_id).refresh_cluster_domain(
data["domain_name"], data["target_instances"]
)
for detail in data["cluster_entry_details"]:
if detail["cluster_entry_type"] == ClusterEntryType.DNS:
DnsManage(cluster.bk_biz_id, cluster.bk_cloud_id).refresh_cluster_domain(
detail["domain_name"], detail["target_instances"]
)
return Response({})
22 changes: 16 additions & 6 deletions dbm-ui/backend/flow/utils/dns_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def __init__(self, bk_biz_id: int, bk_cloud_id: int):
self.bk_biz_id = bk_biz_id
self.bk_cloud_id = bk_cloud_id

@staticmethod
def format_domain(domain_name: str) -> str:
"""
保证域名的格式为,保留最后一个英文句号 "."
"""
if domain_name.endswith("."):
return domain_name
else:
return f"{domain_name}."

def create_domain(self, instance_list: list, add_domain_name: str) -> bool:
"""
创建域名
Expand All @@ -41,7 +51,7 @@ def create_domain(self, instance_list: list, add_domain_name: str) -> bool:
"""
create_domain_payload = [
{
"domain_name": f"{add_domain_name}.",
"domain_name": self.format_domain(add_domain_name),
"instances": instance_list,
}
]
Expand All @@ -66,7 +76,7 @@ def delete_domain(self, cluster_id: int, is_only_delete_slave_domain=False) -> b
else:
dns_info = ClusterEntry.objects.filter(cluster=cluster, cluster_entry_type=ClusterEntryType.DNS).all()
for d in dns_info:
delete_domain_payload = [{"domain_name": f"{d.entry}."}]
delete_domain_payload = [{"domain_name": self.format_domain(d.entry)}]
logger.info(d.entry)
res = DnsApi.delete_domain(
{"app": str(self.bk_biz_id), "domains": delete_domain_payload, "bk_cloud_id": self.bk_cloud_id}
Expand Down Expand Up @@ -100,7 +110,7 @@ def update_domain(self, old_instance: str, new_instance: str, update_domain_name
DnsApi.update_domain(
{
"app": str(self.bk_biz_id),
"domain_name": f"{update_domain_name}.",
"domain_name": self.format_domain(update_domain_name),
"instance": f"{old_instance}",
"set": {"instance": f"{new_instance}"},
"bk_cloud_id": self.bk_cloud_id,
Expand All @@ -115,14 +125,14 @@ def get_domain(self, domain_name: str) -> list:
res = DnsApi.get_domain(
{
"app": str(self.bk_biz_id),
"domain_name": f"{domain_name}.",
"domain_name": self.format_domain(domain_name),
"bk_cloud_id": self.bk_cloud_id,
}
)
return res["detail"]

def remove_domain_ip(self, domain: str, del_instance_list: list) -> bool:
delete_domain_payload = [{"domain_name": f"{domain}.", "instances": del_instance_list}]
delete_domain_payload = [{"domain_name": self.format_domain(domain), "instances": del_instance_list}]
res = DnsApi.delete_domain(
{"app": str(self.bk_biz_id), "domains": delete_domain_payload, "bk_cloud_id": self.bk_cloud_id}
)
Expand Down Expand Up @@ -154,7 +164,7 @@ def batch_update_domain(
DnsApi.batch_update_domain(
{
"app": str(self.bk_biz_id),
"domain_name": f"{update_domain_name}.",
"domain_name": self.format_domain(update_domain_name),
"set": sets,
"bk_cloud_id": self.bk_cloud_id,
}
Expand Down
12 changes: 6 additions & 6 deletions helm-charts/bk-dbm/Chart.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ dependencies:
version: 1.13.0
- name: mysql
repository: https://charts.bitnami.com/bitnami
version: 9.14.2
version: 9.14.3
- name: redis
repository: https://charts.bitnami.com/bitnami
version: 16.13.2
- name: etcd
repository: https://charts.bitnami.com/bitnami
version: 9.6.0
version: 9.6.2
- name: reloader
repository: https://stakater.github.io/stakater-charts
version: 1.0.50
version: 1.0.51
- name: grafana
repository: file://./charts/grafana
version: 7.9.8
Expand All @@ -22,7 +22,7 @@ dependencies:
version: 0.1.28
- name: dbconfig
repository: file://charts/dbconfig
version: 0.1.8
version: 0.1.9
- name: dbpriv
repository: file://charts/dbpriv
version: 0.1.31
Expand Down Expand Up @@ -56,5 +56,5 @@ dependencies:
- name: backup-consumer
repository: file://charts/backup-consumer
version: 0.0.3
digest: sha256:6a298ec313dd81bf8304ff4851852594333711addfc65f44aff1aeda34c7ed24
generated: "2023-11-06T21:45:38.599672+08:00"
digest: sha256:3de5f96c055c450856010568192660ef672f1216eb7d3b9d9bf81faa255df2f9
generated: "2023-11-14T10:53:10.905277+08:00"
4 changes: 2 additions & 2 deletions helm-charts/bk-dbm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ dependencies:
description: A Helm chart for bkdbm
name: bk-dbm
type: application
version: 1.2.0-alpha.81
appVersion: 1.2.0-alpha.81
version: 1.2.0-alpha.82
appVersion: 1.2.0-alpha.82
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/dbm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
appVersion: 1.2.0-alpha.458
appVersion: 1.2.0-alpha.462
description: A Helm chart for dbm
name: dbm
type: application
Expand Down

0 comments on commit 15456de

Please sign in to comment.