forked from TencentBlueKing/blueking-dbm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): 单据状态细化 TencentBlueKing#6755
- Loading branch information
Showing
33 changed files
with
942 additions
and
421 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
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
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.
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,75 @@ | ||
# -*- 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. | ||
""" | ||
import copy | ||
import logging | ||
from unittest.mock import PropertyMock, patch | ||
|
||
import pytest | ||
from django.conf import settings | ||
from rest_framework.permissions import AllowAny | ||
from rest_framework.test import APIClient | ||
|
||
from backend.constants import DEFAULT_SYSTEM_USER | ||
from backend.tests.mock_data.components.cc import CCApiMock | ||
from backend.tests.mock_data.components.itsm import ItsmApiMock | ||
from backend.tests.mock_data.iam_app.permission import PermissionMock | ||
from backend.tests.mock_data.ticket.ticket_flow import MYSQL_FULL_BACKUP_TICKET_DATA, SN | ||
from backend.ticket.builders.mysql.mysql_ha_full_backup import MySQLHaFullBackupDetailSerializer | ||
from backend.ticket.constants import TicketStatus, TodoStatus, TodoType | ||
from backend.ticket.flow_manager.inner import InnerFlow | ||
from backend.ticket.handler import TicketHandler | ||
from backend.ticket.models import Flow, Ticket | ||
from backend.ticket.views import TicketViewSet | ||
|
||
logger = logging.getLogger("test") | ||
pytestmark = pytest.mark.django_db | ||
client = APIClient() | ||
|
||
|
||
@pytest.fixture(autouse=True) # autouse=True 会自动应用这个fixture到所有的测试中 | ||
def set_empty_middleware(): | ||
with patch.object(settings, "MIDDLEWARE", []): | ||
yield | ||
|
||
|
||
class TestTicketRevoke: | ||
""" | ||
测试单据终止 | ||
""" | ||
|
||
@patch.object(TicketViewSet, "permission_classes") | ||
@patch.object(MySQLHaFullBackupDetailSerializer, "validate") | ||
@patch.object(InnerFlow, "status", new_callable=PropertyMock) | ||
@patch.object(TicketViewSet, "get_permissions", lambda x: []) | ||
@patch("backend.ticket.flow_manager.itsm.ItsmApi", ItsmApiMock()) | ||
@patch("backend.db_services.cmdb.biz.CCApi", CCApiMock()) | ||
@patch("backend.db_services.cmdb.biz.Permission", PermissionMock) | ||
def test_ticket_revoke( | ||
self, mocked_status, mocked_validate, mocked_permission_classes, query_fixture, db, init_app | ||
): | ||
# 以全库备份为例,测试流程:start --> itsm --> inner --> end | ||
mocked_status.return_value = TicketStatus.SUCCEEDED | ||
mocked_permission_classes.return_value = [AllowAny] | ||
mocked_validate.return_value = MYSQL_FULL_BACKUP_TICKET_DATA | ||
|
||
client.login(username="admin") | ||
# 创建单据 | ||
sql_import_data = copy.deepcopy(MYSQL_FULL_BACKUP_TICKET_DATA) | ||
ticket = client.post("/apis/tickets/", data=sql_import_data).data | ||
|
||
# 在todo流程终止 | ||
current_flow = Flow.objects.filter(flow_obj_id=SN).first() | ||
client.post(f"/apis/tickets/{current_flow.ticket_id}/callback/") | ||
TicketHandler.revoke_ticket(ticket_ids=[ticket["id"]], operator=DEFAULT_SYSTEM_USER) | ||
# 验证单据和todo已经终止 | ||
revoke_ticket = Ticket.objects.get(id=ticket["id"]) | ||
assert revoke_ticket.status == TicketStatus.TERMINATED | ||
assert revoke_ticket.todo_of_ticket.filter(type=TodoType.APPROVE)[0].status == TodoStatus.DONE_FAILED |
67 changes: 67 additions & 0 deletions
67
dbm-ui/backend/ticket/builders/mysql/mysql_ha_full_backup.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,67 @@ | ||
# -*- 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 ugettext_lazy as _ | ||
from rest_framework import serializers | ||
|
||
from backend.db_meta.enums import ClusterDBHAStatusFlags, InstanceInnerRole | ||
from backend.db_meta.models import Cluster | ||
from backend.flow.consts import MySQLBackupFileTagEnum, MySQLBackupTypeEnum | ||
from backend.flow.engine.controller.mysql import MySQLController | ||
from backend.ticket import builders | ||
from backend.ticket.builders.common.base import fetch_cluster_ids | ||
from backend.ticket.builders.mysql.base import BaseMySQLHATicketFlowBuilder, MySQLBaseOperateDetailSerializer | ||
from backend.ticket.constants import FlowRetryType, TicketType | ||
|
||
|
||
class MySQLHaFullBackupDetailSerializer(MySQLBaseOperateDetailSerializer): | ||
class FullBackupDataInfoSerializer(serializers.Serializer): | ||
class ClusterDetailSerializer(serializers.Serializer): | ||
cluster_id = serializers.IntegerField(help_text=_("集群ID")) | ||
backup_local = serializers.ChoiceField( | ||
help_text=_("备份位置"), choices=InstanceInnerRole.get_choices(), default=InstanceInnerRole.SLAVE.value | ||
) | ||
|
||
# 废弃online,暂时不需要传递 | ||
# online = serializers.BooleanField(help_text=_("是否在线备份"), required=False) | ||
backup_type = serializers.ChoiceField(help_text=_("备份类型"), choices=MySQLBackupTypeEnum.get_choices()) | ||
file_tag = serializers.ChoiceField(help_text=_("备份文件tag"), choices=MySQLBackupFileTagEnum.get_choices()) | ||
clusters = serializers.ListSerializer(help_text=_("集群信息"), child=ClusterDetailSerializer()) | ||
|
||
infos = FullBackupDataInfoSerializer() | ||
|
||
def validate(self, attrs): | ||
try: | ||
self.validate_cluster_can_access(attrs) | ||
except serializers.ValidationError as e: | ||
clusters = Cluster.objects.filter(id__in=fetch_cluster_ids(details=attrs)) | ||
id__cluster = {cluster.id: cluster for cluster in clusters} | ||
# 如果备份位置选的是master,但是slave异常,则认为是可以的 | ||
for info in attrs["infos"]["clusters"]: | ||
if info["backup_local"] != InstanceInnerRole.MASTER: | ||
raise serializers.ValidationError(e) | ||
if id__cluster[info["cluster_id"]].status_flag & ClusterDBHAStatusFlags.BackendMasterUnavailable: | ||
raise serializers.ValidationError(e) | ||
|
||
return attrs | ||
|
||
|
||
class MySQLHaFullBackupFlowParamBuilder(builders.FlowParamBuilder): | ||
"""MySQL HA 备份执行单据参数""" | ||
|
||
controller = MySQLController.mysql_full_backup_scene | ||
|
||
|
||
@builders.BuilderFactory.register(TicketType.MYSQL_HA_FULL_BACKUP) | ||
class MySQLHaFullBackupFlowBuilder(BaseMySQLHATicketFlowBuilder): | ||
serializer = MySQLHaFullBackupDetailSerializer | ||
inner_flow_builder = MySQLHaFullBackupFlowParamBuilder | ||
retry_type = FlowRetryType.AUTO_RETRY |
Oops, something went wrong.