From 8eea9445f36890b695e66ea32c4558567f64bead Mon Sep 17 00:00:00 2001 From: piglei Date: Tue, 15 Oct 2024 14:27:08 +0800 Subject: [PATCH] refactor: remove "diffs" form s-mart /stash/ response" (#1656) --- .../declarative/application/resources.py | 79 +--------------- .../paasng/platform/smart_app/serializers.py | 5 - .../paasng/paasng/platform/smart_app/views.py | 8 +- .../platform/declarative/test_resources.py | 93 ------------------- 4 files changed, 4 insertions(+), 181 deletions(-) delete mode 100644 apiserver/paasng/tests/paasng/platform/declarative/test_resources.py diff --git a/apiserver/paasng/paasng/platform/declarative/application/resources.py b/apiserver/paasng/paasng/platform/declarative/application/resources.py index 11dc0b7946..a310ddc389 100644 --- a/apiserver/paasng/paasng/platform/declarative/application/resources.py +++ b/apiserver/paasng/paasng/platform/declarative/application/resources.py @@ -16,8 +16,7 @@ # to the current version of the project delivered to anyone in the future. import logging -from itertools import chain, product -from typing import Any, Dict, Generic, List, Optional, Set, TypeVar, Union, cast +from typing import Any, Dict, Generic, List, Optional, TypeVar, Union from django.core.files.base import ContentFile from django.utils.functional import Promise @@ -25,7 +24,6 @@ from pydantic import BaseModel, Field from paasng.accessories.publish.market.constant import OpenMode -from paasng.accessories.servicehub.manager import ServiceObj, mixed_service_mgr from paasng.platform.applications.constants import AppLanguage from paasng.platform.applications.models import Application from paasng.platform.declarative.basic import AllowOmittedModel @@ -37,7 +35,6 @@ OmittedType, ) from paasng.platform.declarative.exceptions import DescriptionValidationError -from paasng.platform.modules.models import Module M = TypeVar("M") logger = logging.getLogger(__name__) @@ -130,77 +127,3 @@ def get_plugin(self, plugin_type: AppDescPluginType) -> Optional[Dict]: if plugin["type"] == plugin_type: return plugin return None - - -class ApplicationDescDiffDog: - """deprecated: TODO: 前端重构后未再使用 `diffs` 字段展示差异, 是否可以移除相关实现?""" - - def __init__(self, application: Application, desc: ApplicationDesc): - self.application = application - self.desc = desc - - def diff(self) -> Dict[str, ModuleDiffResult]: - diffs = {} - for module in self.application.modules.all(): - try: - diffs[module.name] = self._diff_module(module) - except ValueError: - logger.warning("Module<%s> of the application<%s> is removed.", module.name, self.application.code) - - # 生成未创建的模块的差异 - for module_name, module_desc in self.desc.modules.items(): - if module_name not in diffs: - diffs[module_name] = ModuleDiffResult( - services=self._diff_services( - current_services=set(), expected_services={item.name for item in module_desc.services} - ) - ) - return diffs - - def _diff_module(self, module: "Module") -> ModuleDiffResult: - """对比模块与模块定义之间的差异 - - :param module: 需要与 ModuleSpec 做对比的模块 - :return: DescDiffResult - :raise ValueError: 如果提供的模块未在 ApplicationDesc 中定义, 那么将抛出 ValueError 异常. - """ - if module.name not in self.desc.modules: - raise ValueError(f"Module<{module.name}> not found!") - - current_services = {service.name for service in mixed_service_mgr.list_binded(module)} # type: ignore - expected_services = {item.name for item in self.desc.modules[module.name].services} - - return ModuleDiffResult(services=self._diff_services(current_services, expected_services)) - - def _diff_services(self, current_services: Set[str], expected_services: Set[str]) -> List[DiffItem]: - """根据支持的增强服务列表, 目前的增强服务列表, 期望的增强服务列表计算差异 - - :param current_services: 目前已绑定的增强服务名称集合 - :param expected_services: 期望绑定的增强服务名称集合 - :return: - """ - supported_services = list(mixed_service_mgr.list_by_region(self.application.region)) - supported_services = cast(List[ServiceObj], supported_services) - not_modified_services = sorted(current_services & expected_services) - added_services = sorted(expected_services - current_services) - deleted_services = sorted(current_services - expected_services) - - def make_service_spec(service: str) -> ServiceSpec: - spec = ServiceSpec(name=service) - try: - spec.display_name = next(item.display_name for item in supported_services if item.name == spec.name) - except StopIteration: - spec.display_name = spec.name - return spec - - return [ - DiffItem( - resource=make_service_spec(service=service), - diff_type=diff_type, - ) - for service, diff_type in chain( - product(deleted_services, [DiffType.DELETED]), - product(not_modified_services, [DiffType.NOT_MODIFIED]), - product(added_services, [DiffType.ADDED]), - ) - ] diff --git a/apiserver/paasng/paasng/platform/smart_app/serializers.py b/apiserver/paasng/paasng/platform/smart_app/serializers.py index cd5e448123..c4b54ad940 100644 --- a/apiserver/paasng/paasng/platform/smart_app/serializers.py +++ b/apiserver/paasng/paasng/platform/smart_app/serializers.py @@ -62,8 +62,3 @@ class DiffItemSLZ(serializers.Serializer): class DescriptionDiffResultSLZ(serializers.Serializer): services = serializers.ListField(child=DiffItemSLZ()) - - -# TODO: 前端重构后未再使用 `diffs` 字段展示差异, 是否可以移除相关实现? -class PackageStashResponseWithDiffSLZ(PackageStashResponseSLZ): - diffs = serializers.DictField(child=DescriptionDiffResultSLZ()) diff --git a/apiserver/paasng/paasng/platform/smart_app/views.py b/apiserver/paasng/paasng/platform/smart_app/views.py index 3ec9469e38..9f27894579 100644 --- a/apiserver/paasng/paasng/platform/smart_app/views.py +++ b/apiserver/paasng/paasng/platform/smart_app/views.py @@ -41,7 +41,7 @@ from paasng.infras.accounts.permissions.application import application_perm_class from paasng.infras.iam.permissions.resources.application import AppAction from paasng.platform.applications.mixins import ApplicationCodeInPathMixin -from paasng.platform.declarative.application.resources import ApplicationDesc, ApplicationDescDiffDog +from paasng.platform.declarative.application.resources import ApplicationDesc from paasng.platform.declarative.constants import AppSpecVersion from paasng.platform.declarative.exceptions import ControllerError, DescriptionValidationError from paasng.platform.declarative.handlers import get_desc_handler @@ -50,7 +50,6 @@ AppDescriptionSLZ, PackageStashRequestSLZ, PackageStashResponseSLZ, - PackageStashResponseWithDiffSLZ, ) from paasng.platform.smart_app.services.app_desc import get_app_description from paasng.platform.smart_app.services.detector import SourcePackageStatReader @@ -240,7 +239,7 @@ def validate_app_desc(app_desc: ApplicationDesc): @swagger_auto_schema( tags=["源码包管理", "S-Mart"], request_body=PackageStashRequestSLZ, - response_serializer=PackageStashResponseWithDiffSLZ, + response_serializer=PackageStashResponseSLZ, parser_classes=[MultiPartParser], ) def stash(self, request, code): @@ -266,11 +265,10 @@ def stash(self, request, code): supported_services = mixed_service_mgr.list_by_region(application.region) return Response( - data=PackageStashResponseWithDiffSLZ( + data=PackageStashResponseSLZ( { "app_description": app_desc, "signature": stat.sha256_signature, - "diffs": ApplicationDescDiffDog(application=application, desc=app_desc).diff(), "supported_services": [service.name for service in supported_services], } ).data diff --git a/apiserver/paasng/tests/paasng/platform/declarative/test_resources.py b/apiserver/paasng/tests/paasng/platform/declarative/test_resources.py deleted file mode 100644 index 8a1740f4f4..0000000000 --- a/apiserver/paasng/tests/paasng/platform/declarative/test_resources.py +++ /dev/null @@ -1,93 +0,0 @@ -# -*- coding: utf-8 -*- -# TencentBlueKing is pleased to support the open source community by making -# 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. -# Copyright (C) 2017 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 -# -# http://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. -# -# We undertake not to change the open source license (MIT license) applicable -# to the current version of the project delivered to anyone in the future. - -from itertools import product -from typing import List -from unittest import mock - -import pytest - -from paasng.platform.declarative.application.resources import ( - ApplicationDesc, - ApplicationDescDiffDog, - DiffItem, - ModuleDiffResult, - ServiceSpec, - mixed_service_mgr, -) -from paasng.platform.declarative.constants import DiffType - -APP_DESC_INFO = dict(name_zh_cn="foo", name_en="foo", code="bar", region="baz") - - -pytestmark = pytest.mark.django_db(databases=["default", "workloads"]) - - -def diff_result(param) -> ModuleDiffResult: - return ModuleDiffResult( - services=[ - DiffItem( - resource=dict(name=service, display_name=service, shared_from=None, specs={}), - diff_type=diff_type, - ) - for services, diff_type in zip(param, [DiffType.DELETED, DiffType.NOT_MODIFIED, DiffType.ADDED]) - for service, diff_type in product(services, [diff_type]) - ] - ) - - -def make_modules(services: List[ServiceSpec], module_name: str = "default"): - return {"modules": {module_name: {"name": module_name, "isDefault": True, "services": services}}} - - -class TestApplicationDescDiffDog: - @pytest.mark.parametrize( - ("service_names", "services", "expected"), - [ - ( - (["a"], ["a"]), - [ServiceSpec(name="a")], - [[], ["a"], []], - ), - ( - (["a"], ["c", "a"]), - [ServiceSpec(name="b"), ServiceSpec(name="c")], - [["a"], ["c"], ["b"]], - ), - ( - (["b"], ["a"]), - [ServiceSpec(name="a"), ServiceSpec(name="b"), ServiceSpec(name="c")], - [[], ["a"], ["b", "c"]], - ), - ( - (["a", "c"], ["b", "c"]), - [ServiceSpec(name="a"), ServiceSpec(name="d")], - [["b", "c"], [], ["a", "d"]], - ), - ], - ) - def test_diff_module(self, bk_app, bk_module, service_names, services, expected): - def fake_list_binded(module): - return [type("", (), dict(name=name)) for name in service_names[1]] - - with mock.patch.object(mixed_service_mgr, "list_binded") as list_binded: - list_binded.side_effect = fake_list_binded - - desc = ApplicationDesc(**APP_DESC_INFO, **make_modules(services)) - assert ApplicationDescDiffDog(application=bk_app, desc=desc).diff() == { - bk_module.name: diff_result(expected) - }