diff --git a/apps/applications/api/application.py b/apps/applications/api/application.py index 49fc71664..b2e73c1e5 100644 --- a/apps/applications/api/application.py +++ b/apps/applications/api/application.py @@ -41,16 +41,17 @@ class ApplicationViewSet(OrgBulkModelViewSet): if self.action in [ 'create', 'update', 'partial_update', 'bulk_update', 'partial_bulk_update' ] and not app_type: - # action: create / update ... + # action: create / update raise JMSException( 'The `{}` action must take the `type` query parameter'.format(self.action) ) if app_type: + # action: create / update / list / retrieve / metadata attrs_cls = models.Category.get_type_serializer_cls(app_type) elif app_category: # action: list / retrieve / metadata attrs_cls = models.Category.get_category_serializer_cls(app_category) else: - attrs_cls = serializers.CommonCategorySerializer + attrs_cls = models.Category.get_no_password_serializer_cls() return type('ApplicationDynamicSerializer', (serializer_class,), {'attrs': attrs_cls()}) diff --git a/apps/applications/api/mixin.py b/apps/applications/api/mixin.py index eb4fc9486..98ec99092 100644 --- a/apps/applications/api/mixin.py +++ b/apps/applications/api/mixin.py @@ -39,11 +39,11 @@ class SerializeApplicationToTreeNodeMixin: 'meta': {'type': 'k8s_app'} } - def dispatch_serialize(self, application): + def _serialize(self, application): method_name = f'_serialize_{application.category}' data = getattr(self, method_name)(application) return data def serialize_applications(self, applications): - data = [self.dispatch_serialize(application) for application in applications] + data = [self._serialize(application) for application in applications] return data diff --git a/apps/applications/api/remote_app.py b/apps/applications/api/remote_app.py index 45e5c097c..9b983449b 100644 --- a/apps/applications/api/remote_app.py +++ b/apps/applications/api/remote_app.py @@ -27,8 +27,14 @@ class RemoteAppConnectionInfoApi(generics.RetrieveAPIView): permission_classes = (IsAppUser, ) serializer_class = RemoteAppConnectionInfoSerializer + @staticmethod + def check_category_allowed(obj): + if not obj.category_is_remote_app: + raise JMSException( + 'The request instance(`{}`) is not of category `remote_app`'.format(obj.category) + ) + def get_object(self): obj = super().get_object() - if not models.Category.is_remote_app(obj.category): - raise JMSException('The request instance is not of category `remote_app`') + self.check_category_allowed(obj) return obj diff --git a/apps/applications/models/application.py b/apps/applications/models/application.py index 43f6e9a99..fb0812858 100644 --- a/apps/applications/models/application.py +++ b/apps/applications/models/application.py @@ -102,9 +102,9 @@ class Category(ChoiceSet): def get_category_serializer_mapper(cls): from ..serializers import remote_app, database_app, k8s_app return { - cls.db: database_app.DatabaseCategorySerializer, - cls.remote_app: remote_app.RemmoteAppCategorySerializer, - cls.cloud: k8s_app.CloudCategorySerializer, + cls.db: database_app.DBAttrsSerializer, + cls.remote_app: remote_app.RemoteAppAttrsSerializer, + cls.cloud: k8s_app.CloudAttrsSerializer, } @classmethod @@ -113,8 +113,9 @@ class Category(ChoiceSet): return mapper.get(cg, None) @classmethod - def is_remote_app(cls, cg): - return cg == cls.remote_app + def get_no_password_serializer_cls(cls): + from ..serializers import common + return common.NoPasswordSerializer class Application(CommonModelMixin, OrgModelMixin): @@ -136,3 +137,6 @@ class Application(CommonModelMixin, OrgModelMixin): category_display = self.get_category_display() type_display = self.get_type_display() return f'{self.name}({type_display})[{category_display}]' + + def category_is_remote_app(self): + return self.category == Category.remote_app diff --git a/apps/applications/serializers/common.py b/apps/applications/serializers/common.py index 07a67551d..31c0ba2ac 100644 --- a/apps/applications/serializers/common.py +++ b/apps/applications/serializers/common.py @@ -1,7 +1,7 @@ from rest_framework import serializers -class CommonCategorySerializer(serializers.JSONField): +class NoPasswordSerializer(serializers.JSONField): def to_representation(self, value): new_value = {} for k, v in value.items(): diff --git a/apps/applications/serializers/database_app.py b/apps/applications/serializers/database_app.py index 4f9df2e6d..84e4ee2c2 100644 --- a/apps/applications/serializers/database_app.py +++ b/apps/applications/serializers/database_app.py @@ -9,7 +9,7 @@ from common.serializers import AdaptedBulkListSerializer from .. import models -class DatabaseCategorySerializer(serializers.Serializer): +class DBAttrsSerializer(serializers.Serializer): host = serializers.CharField(max_length=128, label=_('Host')) port = serializers.IntegerField(label=_('Port')) database = serializers.CharField( @@ -17,19 +17,15 @@ class DatabaseCategorySerializer(serializers.Serializer): ) -class DatabaseAttrsSerializer(DatabaseCategorySerializer): - pass - - -class MySQLAttrsSerializer(DatabaseAttrsSerializer): +class MySQLAttrsSerializer(DBAttrsSerializer): port = serializers.IntegerField(default=3306, label=_('Port')) -class PostgreAttrsSerializer(DatabaseAttrsSerializer): +class PostgreAttrsSerializer(DBAttrsSerializer): port = serializers.IntegerField(default=5432, label=_('Port')) -class OracleAttrsSerializer(DatabaseAttrsSerializer): +class OracleAttrsSerializer(DBAttrsSerializer): port = serializers.IntegerField(default=1521, label=_('Port')) diff --git a/apps/applications/serializers/k8s_app.py b/apps/applications/serializers/k8s_app.py index bd5bbbbe2..bf05903dc 100644 --- a/apps/applications/serializers/k8s_app.py +++ b/apps/applications/serializers/k8s_app.py @@ -5,11 +5,11 @@ from orgs.mixins.serializers import BulkOrgResourceModelSerializer from .. import models -class CloudCategorySerializer(serializers.Serializer): +class CloudAttrsSerializer(serializers.Serializer): cluster = serializers.CharField(max_length=1024, label=_('Cluster')) -class K8sAttrsSerializer(CloudCategorySerializer): +class K8sAttrsSerializer(CloudAttrsSerializer): pass diff --git a/apps/applications/serializers/remote_app.py b/apps/applications/serializers/remote_app.py index d1304cd4b..81a18caec 100644 --- a/apps/applications/serializers/remote_app.py +++ b/apps/applications/serializers/remote_app.py @@ -14,11 +14,8 @@ from .. import const from ..models import RemoteApp, Category, Application -class RemmoteAppCategorySerializer(serializers.Serializer): +class RemoteAppAttrsSerializer(serializers.Serializer): asset = serializers.CharField(max_length=36, label=_('Assets')) - - -class RemoteAppAttrsSerializer(RemmoteAppCategorySerializer): path = serializers.CharField(max_length=128, label=_('Remote App path')) diff --git a/apps/assets/models/user.py b/apps/assets/models/user.py index add83e2ad..1dfd79ad2 100644 --- a/apps/assets/models/user.py +++ b/apps/assets/models/user.py @@ -141,14 +141,14 @@ class SystemUser(BaseUser): ] @property - def k8s_application_protocols(self): + def cloud_application_protocols(self): return [self.PROTOCOL_K8S] @property def application_category_protocols(self): protocols = [] protocols.extend(self.db_application_protocols) - protocols.extend(self.k8s_application_protocols) + protocols.extend(self.cloud_application_protocols) return protocols def is_need_push(self): diff --git a/apps/perms/api/application/__init__.py b/apps/perms/api/application/__init__.py index 5741ddc92..05e4ff6d2 100644 --- a/apps/perms/api/application/__init__.py +++ b/apps/perms/api/application/__init__.py @@ -1,4 +1,4 @@ from .user_permission import * from .application_permission import * from .application_permission_relation import * -from .user_group_permission_application import * +from .user_group_permission import * diff --git a/apps/perms/api/application/user_group_permission_application.py b/apps/perms/api/application/user_group_permission.py similarity index 100% rename from apps/perms/api/application/user_group_permission_application.py rename to apps/perms/api/application/user_group_permission.py