2023-02-21 05:00:04 +00:00
|
|
|
from rest_framework.generics import CreateAPIView
|
|
|
|
|
|
|
|
from accounts import serializers
|
2023-12-06 10:48:35 +00:00
|
|
|
from accounts.permissions import AccountTaskActionPermission
|
|
|
|
from accounts.tasks import (
|
|
|
|
remove_accounts_task, verify_accounts_connectivity_task, push_accounts_to_assets_task
|
|
|
|
)
|
2023-02-21 14:57:31 +00:00
|
|
|
from assets.exceptions import NotSupportedTemporarilyError
|
2023-12-06 10:48:35 +00:00
|
|
|
from authentication.permissions import UserConfirmation, ConfirmType
|
2023-02-21 05:00:04 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'AccountsTaskCreateAPI',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class AccountsTaskCreateAPI(CreateAPIView):
|
|
|
|
serializer_class = serializers.AccountTaskSerializer
|
2023-12-06 10:48:35 +00:00
|
|
|
permission_classes = (AccountTaskActionPermission,)
|
2023-02-21 05:00:04 +00:00
|
|
|
|
2023-12-06 10:48:35 +00:00
|
|
|
def get_permissions(self):
|
|
|
|
act = self.request.data.get('action')
|
|
|
|
if act == 'remove':
|
|
|
|
self.permission_classes = [
|
|
|
|
AccountTaskActionPermission,
|
|
|
|
UserConfirmation.require(ConfirmType.PASSWORD)
|
|
|
|
]
|
|
|
|
return super().get_permissions()
|
2023-02-21 05:00:04 +00:00
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
data = serializer.validated_data
|
|
|
|
accounts = data.get('accounts', [])
|
2023-04-14 07:34:56 +00:00
|
|
|
params = data.get('params')
|
2023-02-22 12:31:20 +00:00
|
|
|
account_ids = [str(a.id) for a in accounts]
|
2023-02-21 05:00:04 +00:00
|
|
|
|
|
|
|
if data['action'] == 'push':
|
2023-04-14 07:34:56 +00:00
|
|
|
task = push_accounts_to_assets_task.delay(account_ids, params)
|
2023-12-06 10:48:35 +00:00
|
|
|
elif data['action'] == 'remove':
|
|
|
|
gather_accounts = data.get('gather_accounts', [])
|
|
|
|
gather_account_ids = [str(a.id) for a in gather_accounts]
|
|
|
|
task = remove_accounts_task.delay(gather_account_ids)
|
2023-02-21 05:00:04 +00:00
|
|
|
else:
|
2023-02-21 14:57:31 +00:00
|
|
|
account = accounts[0]
|
|
|
|
asset = account.asset
|
2023-04-10 02:57:44 +00:00
|
|
|
if not asset.auto_config['ansible_enabled'] or \
|
|
|
|
not asset.auto_config['ping_enabled']:
|
2023-02-21 14:57:31 +00:00
|
|
|
raise NotSupportedTemporarilyError()
|
2023-02-21 05:00:04 +00:00
|
|
|
task = verify_accounts_connectivity_task.delay(account_ids)
|
|
|
|
|
|
|
|
data = getattr(serializer, '_data', {})
|
|
|
|
data["task"] = task.id
|
|
|
|
setattr(serializer, '_data', data)
|
|
|
|
return task
|