mirror of https://github.com/jumpserver/jumpserver
perf: update account serializer remote unused code
parent
f29ba1319c
commit
04bf099dbe
|
@ -17,22 +17,22 @@ from orgs.mixins.api import OrgBulkModelViewSet
|
||||||
from .base import AutomationExecutionViewSet
|
from .base import AutomationExecutionViewSet
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"GatherAccountsAutomationViewSet",
|
"DiscoverAccountsAutomationViewSet",
|
||||||
"GatherAccountsExecutionViewSet",
|
"DiscoverAccountsExecutionViewSet",
|
||||||
"GatheredAccountViewSet",
|
"GatheredAccountViewSet",
|
||||||
]
|
]
|
||||||
|
|
||||||
from ...risk_handlers import RiskHandler
|
from ...risk_handlers import RiskHandler
|
||||||
|
|
||||||
|
|
||||||
class GatherAccountsAutomationViewSet(OrgBulkModelViewSet):
|
class DiscoverAccountsAutomationViewSet(OrgBulkModelViewSet):
|
||||||
model = GatherAccountsAutomation
|
model = GatherAccountsAutomation
|
||||||
filterset_fields = ("name",)
|
filterset_fields = ("name",)
|
||||||
search_fields = filterset_fields
|
search_fields = filterset_fields
|
||||||
serializer_class = serializers.GatherAccountAutomationSerializer
|
serializer_class = serializers.DiscoverAccountAutomationSerializer
|
||||||
|
|
||||||
|
|
||||||
class GatherAccountsExecutionViewSet(AutomationExecutionViewSet):
|
class DiscoverAccountsExecutionViewSet(AutomationExecutionViewSet):
|
||||||
rbac_perms = (
|
rbac_perms = (
|
||||||
("list", "accounts.view_gatheraccountsexecution"),
|
("list", "accounts.view_gatheraccountsexecution"),
|
||||||
("retrieve", "accounts.view_gatheraccountsexecution"),
|
("retrieve", "accounts.view_gatheraccountsexecution"),
|
||||||
|
@ -76,9 +76,9 @@ class GatheredAccountViewSet(OrgBulkModelViewSet):
|
||||||
filterset_class = GatheredAccountFilterSet
|
filterset_class = GatheredAccountFilterSet
|
||||||
ordering = ("status",)
|
ordering = ("status",)
|
||||||
serializer_classes = {
|
serializer_classes = {
|
||||||
"default": serializers.GatheredAccountSerializer,
|
"default": serializers.DiscoverAccountSerializer,
|
||||||
"status": serializers.GatheredAccountActionSerializer,
|
"status": serializers.DiscoverAccountActionSerializer,
|
||||||
"details": serializers.GatheredAccountDetailsSerializer
|
"details": serializers.DiscoverAccountDetailsSerializer
|
||||||
}
|
}
|
||||||
rbac_perms = {
|
rbac_perms = {
|
||||||
"status": "assets.change_gatheredaccount",
|
"status": "assets.change_gatheredaccount",
|
||||||
|
|
|
@ -2,40 +2,19 @@ from django.utils.translation import gettext_lazy as _
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from assets.const import AutomationTypes
|
from assets.const import AutomationTypes
|
||||||
from assets.models import Asset, Node, BaseAutomation
|
from assets.models import BaseAutomation
|
||||||
from assets.serializers.automations import AutomationExecutionSerializer as AssetAutomationExecutionSerializer
|
from assets.serializers.automations import AutomationExecutionSerializer as AssetAutomationExecutionSerializer
|
||||||
from common.serializers.fields import ObjectRelatedField
|
from assets.serializers.automations import BaseAutomationSerializer as AssetBaseAutomationSerializer
|
||||||
from common.utils import get_logger
|
from common.utils import get_logger
|
||||||
from ops.mixin import PeriodTaskSerializerMixin
|
|
||||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
|
||||||
|
|
||||||
logger = get_logger(__file__)
|
logger = get_logger(__file__)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'BaseAutomationSerializer', 'AutomationExecutionSerializer',
|
'BaseAutomationSerializer', 'AutomationExecutionSerializer',
|
||||||
'UpdateAssetSerializer', 'UpdateNodeSerializer', 'AutomationAssetsSerializer',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class BaseAutomationSerializer(PeriodTaskSerializerMixin, BulkOrgResourceModelSerializer):
|
class BaseAutomationSerializer(AssetBaseAutomationSerializer):
|
||||||
assets = ObjectRelatedField(many=True, required=False, queryset=Asset.objects, label=_('Assets'))
|
|
||||||
nodes = ObjectRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes'))
|
|
||||||
is_periodic = serializers.BooleanField(default=False, required=False, label=_("Periodic perform"))
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
read_only_fields = [
|
|
||||||
'date_created', 'date_updated', 'created_by',
|
|
||||||
'periodic_display', 'executed_amount', 'type'
|
|
||||||
]
|
|
||||||
fields = read_only_fields + [
|
|
||||||
'id', 'name', 'is_periodic', 'interval', 'crontab', 'comment',
|
|
||||||
'accounts', 'nodes', 'assets', 'is_active',
|
|
||||||
]
|
|
||||||
extra_kwargs = {
|
|
||||||
'name': {'required': True},
|
|
||||||
'executed_amount': {'label': _('Executions')},
|
|
||||||
}
|
|
||||||
|
|
||||||
def validate_name(self, name):
|
def validate_name(self, name):
|
||||||
if self.instance and self.instance.name == name:
|
if self.instance and self.instance.name == name:
|
||||||
return name
|
return name
|
||||||
|
@ -66,22 +45,3 @@ class AutomationExecutionSerializer(AssetAutomationExecutionSerializer):
|
||||||
'type_display': type_display,
|
'type_display': type_display,
|
||||||
}
|
}
|
||||||
return snapshot
|
return snapshot
|
||||||
|
|
||||||
|
|
||||||
class UpdateAssetSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = BaseAutomation
|
|
||||||
fields = ['id', 'assets']
|
|
||||||
|
|
||||||
|
|
||||||
class UpdateNodeSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = BaseAutomation
|
|
||||||
fields = ['id', 'nodes']
|
|
||||||
|
|
||||||
|
|
||||||
class AutomationAssetsSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = Asset
|
|
||||||
only_fields = ['id', 'name', 'address']
|
|
||||||
fields = tuple(only_fields)
|
|
||||||
|
|
|
@ -11,14 +11,14 @@ from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||||
from .base import BaseAutomationSerializer
|
from .base import BaseAutomationSerializer
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'GatheredAccountSerializer',
|
'DiscoverAccountSerializer',
|
||||||
'GatheredAccountActionSerializer',
|
'DiscoverAccountActionSerializer',
|
||||||
'GatherAccountAutomationSerializer',
|
'DiscoverAccountAutomationSerializer',
|
||||||
'GatheredAccountDetailsSerializer'
|
'DiscoverAccountDetailsSerializer'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class GatherAccountAutomationSerializer(BaseAutomationSerializer):
|
class DiscoverAccountAutomationSerializer(BaseAutomationSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = GatherAccountsAutomation
|
model = GatherAccountsAutomation
|
||||||
read_only_fields = BaseAutomationSerializer.Meta.read_only_fields
|
read_only_fields = BaseAutomationSerializer.Meta.read_only_fields
|
||||||
|
@ -43,7 +43,7 @@ class AccountAssetSerializer(_AccountAssetSerializer):
|
||||||
fields = [f for f in _AccountAssetSerializer.Meta.fields if f != 'auto_config']
|
fields = [f for f in _AccountAssetSerializer.Meta.fields if f != 'auto_config']
|
||||||
|
|
||||||
|
|
||||||
class GatheredAccountSerializer(BulkOrgResourceModelSerializer):
|
class DiscoverAccountSerializer(BulkOrgResourceModelSerializer):
|
||||||
asset = AccountAssetSerializer(label=_('Asset'))
|
asset = AccountAssetSerializer(label=_('Asset'))
|
||||||
|
|
||||||
class Meta(BaseAccountSerializer.Meta):
|
class Meta(BaseAccountSerializer.Meta):
|
||||||
|
@ -63,12 +63,12 @@ class GatheredAccountSerializer(BulkOrgResourceModelSerializer):
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
class GatheredAccountActionSerializer(GatheredAccountSerializer):
|
class DiscoverAccountActionSerializer(DiscoverAccountSerializer):
|
||||||
class Meta(GatheredAccountSerializer.Meta):
|
class Meta(DiscoverAccountSerializer.Meta):
|
||||||
read_only_fields = list(set(GatheredAccountSerializer.Meta.read_only_fields) - {'status'})
|
read_only_fields = list(set(DiscoverAccountSerializer.Meta.read_only_fields) - {'status'})
|
||||||
|
|
||||||
|
|
||||||
class GatheredAccountDetailsSerializer(serializers.Serializer):
|
class DiscoverAccountDetailsSerializer(serializers.Serializer):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
request = self.context.get('request')
|
request = self.context.get('request')
|
||||||
|
@ -78,6 +78,7 @@ class GatheredAccountDetailsSerializer(serializers.Serializer):
|
||||||
params = request.query_params
|
params = request.query_params
|
||||||
if params.get('format') == 'openapi':
|
if params.get('format') == 'openapi':
|
||||||
return
|
return
|
||||||
|
|
||||||
pk = request.parser_context['kwargs'].get('pk')
|
pk = request.parser_context['kwargs'].get('pk')
|
||||||
obj = get_object_or_404(GatheredAccount, pk=pk)
|
obj = get_object_or_404(GatheredAccount, pk=pk)
|
||||||
details = obj.detail
|
details = obj.detail
|
||||||
|
|
|
@ -19,8 +19,8 @@ router.register(r'account-backup-plan-executions', api.BackupAccountExecutionVie
|
||||||
router.register(r'change-secret-automations', api.ChangeSecretAutomationViewSet, 'change-secret-automation')
|
router.register(r'change-secret-automations', api.ChangeSecretAutomationViewSet, 'change-secret-automation')
|
||||||
router.register(r'change-secret-executions', api.ChangSecretExecutionViewSet, 'change-secret-execution')
|
router.register(r'change-secret-executions', api.ChangSecretExecutionViewSet, 'change-secret-execution')
|
||||||
router.register(r'change-secret-records', api.ChangeSecretRecordViewSet, 'change-secret-record')
|
router.register(r'change-secret-records', api.ChangeSecretRecordViewSet, 'change-secret-record')
|
||||||
router.register(r'gather-account-automations', api.GatherAccountsAutomationViewSet, 'gather-account-automation')
|
router.register(r'gather-account-automations', api.DiscoverAccountsAutomationViewSet, 'gather-account-automation')
|
||||||
router.register(r'gather-account-executions', api.GatherAccountsExecutionViewSet, 'gather-account-execution')
|
router.register(r'gather-account-executions', api.DiscoverAccountsExecutionViewSet, 'gather-account-execution')
|
||||||
router.register(r'push-account-automations', api.PushAccountAutomationViewSet, 'push-account-automation')
|
router.register(r'push-account-automations', api.PushAccountAutomationViewSet, 'push-account-automation')
|
||||||
router.register(r'push-account-executions', api.PushAccountExecutionViewSet, 'push-account-execution')
|
router.register(r'push-account-executions', api.PushAccountExecutionViewSet, 'push-account-execution')
|
||||||
router.register(r'push-account-records', api.PushAccountRecordViewSet, 'push-account-record')
|
router.register(r'push-account-records', api.PushAccountRecordViewSet, 'push-account-record')
|
||||||
|
@ -30,7 +30,6 @@ router.register(r'account-check-engines', api.CheckAccountEngineViewSet, 'accoun
|
||||||
router.register(r'account-risks', api.AccountRiskViewSet, 'account-risks')
|
router.register(r'account-risks', api.AccountRiskViewSet, 'account-risks')
|
||||||
router.register(r'integration-applications', api.IntegrationApplicationViewSet, 'integration-apps')
|
router.register(r'integration-applications', api.IntegrationApplicationViewSet, 'integration-apps')
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('accounts/bulk/', api.AssetAccountBulkCreateApi.as_view(), name='account-bulk-create'),
|
path('accounts/bulk/', api.AssetAccountBulkCreateApi.as_view(), name='account-bulk-create'),
|
||||||
path('accounts/tasks/', api.AccountsTaskCreateAPI.as_view(), name='account-task-create'),
|
path('accounts/tasks/', api.AccountsTaskCreateAPI.as_view(), name='account-task-create'),
|
||||||
|
|
|
@ -159,6 +159,10 @@ class AutomationExecution(OrgModelMixin):
|
||||||
)
|
)
|
||||||
verbose_name = _("Automation task execution")
|
verbose_name = _("Automation task execution")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def short_id(self):
|
||||||
|
return str(self.id)[:8]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_finished(self):
|
def is_finished(self):
|
||||||
return bool(self.date_finished)
|
return bool(self.date_finished)
|
||||||
|
|
|
@ -22,15 +22,20 @@ class BaseAutomationSerializer(PeriodTaskSerializerMixin, BulkOrgResourceModelSe
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
read_only_fields = [
|
read_only_fields = [
|
||||||
'date_created', 'date_updated', 'created_by', 'periodic_display'
|
'date_created', 'date_updated', 'created_by',
|
||||||
|
'periodic_display', 'executed_amount', 'type'
|
||||||
]
|
]
|
||||||
fields = [
|
mini_fields = [
|
||||||
'id', 'name', 'is_periodic', 'interval', 'crontab', 'comment',
|
'id', 'name', 'type', 'is_periodic', 'interval',
|
||||||
'type', 'accounts', 'nodes', 'assets', 'is_active'
|
'crontab', 'comment', 'is_active'
|
||||||
|
]
|
||||||
|
fields = mini_fields + [
|
||||||
|
'accounts', 'nodes', 'assets',
|
||||||
] + read_only_fields
|
] + read_only_fields
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
'name': {'required': True},
|
'name': {'required': True},
|
||||||
'type': {'read_only': True},
|
'type': {'read_only': True},
|
||||||
|
'executed_amount': {'label': _('Executions')},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,6 +43,7 @@ class AutomationExecutionSerializer(serializers.ModelSerializer):
|
||||||
snapshot = serializers.SerializerMethodField(label=_('Automation snapshot'))
|
snapshot = serializers.SerializerMethodField(label=_('Automation snapshot'))
|
||||||
trigger = LabeledChoiceField(choices=Trigger.choices, read_only=True, label=_("Trigger mode"))
|
trigger = LabeledChoiceField(choices=Trigger.choices, read_only=True, label=_("Trigger mode"))
|
||||||
status = LabeledChoiceField(choices=Status.choices, read_only=True, label=_('Status'))
|
status = LabeledChoiceField(choices=Status.choices, read_only=True, label=_('Status'))
|
||||||
|
short_id = serializers.CharField(read_only=True, label=_('Short ID'))
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = AutomationExecution
|
model = AutomationExecution
|
||||||
|
@ -45,7 +51,7 @@ class AutomationExecutionSerializer(serializers.ModelSerializer):
|
||||||
'trigger', 'date_start', 'date_finished',
|
'trigger', 'date_start', 'date_finished',
|
||||||
'snapshot', 'status', 'duration'
|
'snapshot', 'status', 'duration'
|
||||||
]
|
]
|
||||||
fields = ['id', 'automation'] + read_only_fields
|
fields = ['id', 'short_id', 'automation'] + read_only_fields
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_snapshot(obj):
|
def get_snapshot(obj):
|
||||||
|
|
Loading…
Reference in New Issue