mirror of https://github.com/jumpserver/jumpserver
feat: 批量测试账号可连接性
parent
78d0e3f485
commit
9cc048267b
|
@ -1,11 +1,12 @@
|
||||||
|
from django.db.models import Q
|
||||||
from rest_framework.generics import CreateAPIView
|
from rest_framework.generics import CreateAPIView
|
||||||
|
|
||||||
from accounts import serializers
|
from accounts import serializers
|
||||||
|
from accounts.models import Account
|
||||||
from accounts.permissions import AccountTaskActionPermission
|
from accounts.permissions import AccountTaskActionPermission
|
||||||
from accounts.tasks import (
|
from accounts.tasks import (
|
||||||
remove_accounts_task, verify_accounts_connectivity_task, push_accounts_to_assets_task
|
remove_accounts_task, verify_accounts_connectivity_task, push_accounts_to_assets_task
|
||||||
)
|
)
|
||||||
from assets.exceptions import NotSupportedTemporarilyError
|
|
||||||
from authentication.permissions import UserConfirmation, ConfirmType
|
from authentication.permissions import UserConfirmation, ConfirmType
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
@ -26,25 +27,35 @@ class AccountsTaskCreateAPI(CreateAPIView):
|
||||||
]
|
]
|
||||||
return super().get_permissions()
|
return super().get_permissions()
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
@staticmethod
|
||||||
data = serializer.validated_data
|
def get_account_ids(data, action):
|
||||||
accounts = data.get('accounts', [])
|
account_type = 'gather_accounts' if action == 'remove' else 'accounts'
|
||||||
params = data.get('params')
|
accounts = data.get(account_type, [])
|
||||||
account_ids = [str(a.id) for a in accounts]
|
account_ids = [str(a.id) for a in accounts]
|
||||||
|
|
||||||
if data['action'] == 'push':
|
if action == 'remove':
|
||||||
task = push_accounts_to_assets_task.delay(account_ids, params)
|
return account_ids
|
||||||
elif data['action'] == 'remove':
|
|
||||||
gather_accounts = data.get('gather_accounts', [])
|
assets = data.get('assets', [])
|
||||||
gather_account_ids = [str(a.id) for a in gather_accounts]
|
asset_ids = [str(a.id) for a in assets]
|
||||||
task = remove_accounts_task.delay(gather_account_ids)
|
ids = Account.objects.filter(
|
||||||
|
Q(id__in=account_ids) | Q(asset_id__in=asset_ids)
|
||||||
|
).distinct().values_list('id', flat=True)
|
||||||
|
return [str(_id) for _id in ids]
|
||||||
|
|
||||||
|
def perform_create(self, serializer):
|
||||||
|
data = serializer.validated_data
|
||||||
|
action = data['action']
|
||||||
|
ids = self.get_account_ids(data, action)
|
||||||
|
|
||||||
|
if action == 'push':
|
||||||
|
task = push_accounts_to_assets_task.delay(ids, data.get('params'))
|
||||||
|
elif action == 'remove':
|
||||||
|
task = remove_accounts_task.delay(ids)
|
||||||
|
elif action == 'verify':
|
||||||
|
task = verify_accounts_connectivity_task.delay(ids)
|
||||||
else:
|
else:
|
||||||
account = accounts[0]
|
raise ValueError(f"Invalid action: {action}")
|
||||||
asset = account.asset
|
|
||||||
if not asset.auto_config['ansible_enabled'] or \
|
|
||||||
not asset.auto_config['ping_enabled']:
|
|
||||||
raise NotSupportedTemporarilyError()
|
|
||||||
task = verify_accounts_connectivity_task.delay(account_ids)
|
|
||||||
|
|
||||||
data = getattr(serializer, '_data', {})
|
data = getattr(serializer, '_data', {})
|
||||||
data["task"] = task.id
|
data["task"] = task.id
|
||||||
|
|
|
@ -455,12 +455,14 @@ class AccountHistorySerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
class AccountTaskSerializer(serializers.Serializer):
|
class AccountTaskSerializer(serializers.Serializer):
|
||||||
ACTION_CHOICES = (
|
ACTION_CHOICES = (
|
||||||
('test', 'test'),
|
|
||||||
('verify', 'verify'),
|
('verify', 'verify'),
|
||||||
('push', 'push'),
|
('push', 'push'),
|
||||||
('remove', 'remove'),
|
('remove', 'remove'),
|
||||||
)
|
)
|
||||||
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|
||||||
|
assets = serializers.PrimaryKeyRelatedField(
|
||||||
|
queryset=Asset.objects, required=False, allow_empty=True, many=True
|
||||||
|
)
|
||||||
accounts = serializers.PrimaryKeyRelatedField(
|
accounts = serializers.PrimaryKeyRelatedField(
|
||||||
queryset=Account.objects, required=False, allow_empty=True, many=True
|
queryset=Account.objects, required=False, allow_empty=True, many=True
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue