2022-11-02 09:27:47 +00:00
|
|
|
from celery import shared_task
|
2023-02-17 11:23:42 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-02-20 10:00:29 +00:00
|
|
|
from django.utils.translation import gettext_noop
|
2022-11-02 09:27:47 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from accounts.const import AutomationTypes
|
2023-02-20 15:50:33 +00:00
|
|
|
from accounts.tasks.common import quickstart_automation_by_snapshot
|
2023-02-09 12:48:25 +00:00
|
|
|
from assets.const import GATEWAY_NAME
|
|
|
|
from common.utils import get_logger
|
2023-01-16 11:02:09 +00:00
|
|
|
from orgs.utils import org_aware_func
|
2022-11-02 09:27:47 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
__all__ = [
|
2023-02-20 10:00:29 +00:00
|
|
|
'verify_accounts_connectivity_task'
|
2022-11-02 09:27:47 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-01-18 09:14:02 +00:00
|
|
|
def verify_connectivity_util(assets, tp, accounts, task_name):
|
|
|
|
if not assets or not accounts:
|
|
|
|
return
|
2022-11-02 09:27:47 +00:00
|
|
|
account_usernames = list(accounts.values_list('username', flat=True))
|
2023-02-09 12:48:25 +00:00
|
|
|
task_snapshot = {
|
2022-11-02 09:27:47 +00:00
|
|
|
'accounts': account_usernames,
|
2023-01-16 11:02:09 +00:00
|
|
|
'assets': [str(asset.id) for asset in assets],
|
2022-11-02 09:27:47 +00:00
|
|
|
}
|
2023-02-20 15:50:33 +00:00
|
|
|
quickstart_automation_by_snapshot(task_name, tp, task_snapshot)
|
2022-11-02 09:27:47 +00:00
|
|
|
|
|
|
|
|
2023-01-18 09:14:02 +00:00
|
|
|
@org_aware_func("assets")
|
2023-02-21 05:00:04 +00:00
|
|
|
def verify_accounts_connectivity_util(accounts, task_name):
|
|
|
|
from assets.models import Asset
|
|
|
|
|
|
|
|
asset_ids = [a.asset_id for a in accounts]
|
|
|
|
assets = Asset.objects.filter(id__in=asset_ids)
|
|
|
|
|
|
|
|
gateways = assets.filter(platform__name=GATEWAY_NAME)
|
2023-01-18 09:14:02 +00:00
|
|
|
verify_connectivity_util(
|
2023-02-21 05:00:04 +00:00
|
|
|
gateways, AutomationTypes.verify_gateway_account,
|
|
|
|
accounts, task_name
|
2023-01-18 09:14:02 +00:00
|
|
|
)
|
|
|
|
|
2023-02-21 05:00:04 +00:00
|
|
|
common_assets = assets.exclude(platform__name=GATEWAY_NAME)
|
2023-01-18 09:14:02 +00:00
|
|
|
verify_connectivity_util(
|
2023-02-21 05:00:04 +00:00
|
|
|
common_assets, AutomationTypes.verify_account,
|
|
|
|
accounts, task_name
|
2023-01-18 09:14:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-02-17 09:14:53 +00:00
|
|
|
@shared_task(
|
|
|
|
queue="ansible", verbose_name=_('Verify asset account availability'),
|
2023-02-23 02:46:59 +00:00
|
|
|
activity_callback=lambda self, account_ids, *args, **kwargs: (account_ids, None)
|
2023-02-17 09:14:53 +00:00
|
|
|
)
|
2023-02-21 05:00:04 +00:00
|
|
|
def verify_accounts_connectivity_task(account_ids):
|
2023-01-18 09:14:02 +00:00
|
|
|
from accounts.models import Account, VerifyAccountAutomation
|
2023-01-16 11:02:09 +00:00
|
|
|
accounts = Account.objects.filter(id__in=account_ids)
|
2022-11-02 09:27:47 +00:00
|
|
|
task_name = gettext_noop("Verify accounts connectivity")
|
2023-01-18 09:14:02 +00:00
|
|
|
task_name = VerifyAccountAutomation.generate_unique_name(task_name)
|
2023-02-21 05:00:04 +00:00
|
|
|
return verify_accounts_connectivity_util(accounts, task_name)
|