2016-08-09 09:27:37 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
#
|
2017-12-06 10:31:51 +00:00
|
|
|
from .models import Asset
|
2017-04-05 11:09:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_admin_user_connective_manual(asset):
|
2017-12-06 10:31:51 +00:00
|
|
|
from ops.utils import run_AdHoc
|
2017-04-05 11:09:51 +00:00
|
|
|
if not isinstance(asset, list):
|
|
|
|
asset = [asset]
|
|
|
|
task_tuple = (
|
|
|
|
('ping', ''),
|
|
|
|
)
|
|
|
|
summary, _ = run_AdHoc(task_tuple, asset, record=False)
|
|
|
|
if len(summary['failed']) != 0:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2016-09-04 09:43:03 +00:00
|
|
|
|
2017-12-06 10:31:51 +00:00
|
|
|
|
|
|
|
def get_assets_by_id_list(id_list):
|
|
|
|
return Asset.objects.filter(id__in=id_list)
|
2017-12-07 05:01:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_assets_by_hostname_list(hostname_list):
|
|
|
|
return Asset.objects.filter(hostname__in=hostname_list)
|
|
|
|
|
|
|
|
|
|
|
|
def get_asset_admin_user(user, asset):
|
|
|
|
if user.is_superuser:
|
|
|
|
return asset.admin_user
|
|
|
|
else:
|
|
|
|
msg = "{} have no permission for admin user".format(user.username)
|
|
|
|
raise PermissionError(msg)
|
|
|
|
|
|
|
|
|
|
|
|
def get_asset_system_user(user, asset, system_user_name):
|
|
|
|
from perms.utils import get_user_granted_assets
|
|
|
|
assets = get_user_granted_assets(user)
|
|
|
|
system_users = {system_user.name: system_user for system_user in assets.get(asset)}
|
|
|
|
|
|
|
|
if system_user_name in system_users:
|
|
|
|
return system_users[system_user_name]
|
|
|
|
else:
|
|
|
|
msg = "{} have no permission for {}".format(user.name, system_user_name)
|
|
|
|
raise PermissionError(msg)
|
|
|
|
|
|
|
|
|
|
|
|
def get_assets_with_admin_by_hostname_list(hostname_list):
|
|
|
|
assets = Asset.objects.filter(hostname__in=hostname_list)
|
|
|
|
return [(asset, asset.admin_user) for asset in assets]
|