Commit fc96f6ef authored by Kostas Papadimitriou's avatar Kostas Papadimitriou
Browse files

Allow user to search by vm id in helpdesk app

Refs #3453

VM lookup is triggered when search query matches the following patterns

- vm<id>
- vm-<id>
parent bb7b1a0c
......@@ -75,6 +75,7 @@
</div>
{% else %}
<p>{% if is_ip %}User with IP {% else %}Account {% endif %}<em>{{ account }}</em> does not exist in cyclades database</p>
<p>{% if is_ip %}User with IP {% else %}{% if is_vm %}User with Virtual Machine
{% else %}Account {% endif %}{% endif %}<em>{{ search_query }}</em> does not exist in cyclades database</p>
{% endif %}
{% endblock %}
......@@ -7,7 +7,7 @@ urlpatterns = patterns('',
url(r'^suspend_release/(?P<vm_id>[0-9]+)$',
'synnefo.helpdesk.views.suspend_vm_release',
name='helpdesk-suspend-vm-release'),
url(r'^(?P<account_or_ip>.*)$', 'synnefo.helpdesk.views.account',
url(r'^(?P<search_query>.*)$', 'synnefo.helpdesk.views.account',
name='helpdesk-details'),
)
......@@ -51,8 +51,8 @@ logger = logging.getLogger(__name__)
IP_SEARCH_REGEX = re.compile('([0-9]+)(?:\.[0-9]+){3}')
UUID_SEARCH_REGEX = re.compile('([0-9a-z]{8}-([0-9a-z]{4}-){3}[0-9a-z]{12})')
VM_SEARCH_REGEX = re.compile('vm(-){0,}(?P<vmid>[0-9]+)')
USER_CATALOG_URL = settings.CYCLADES_USER_CATALOG_URL
def get_token_from_cookie(request, cookiename):
......@@ -77,6 +77,11 @@ AUTH_COOKIE_NAME = getattr(settings, 'HELPDESK_AUTH_COOKIE_NAME',
PERMITTED_GROUPS = getattr(settings, 'HELPDESK_PERMITTED_GROUPS', ['helpdesk'])
SHOW_DELETED_VMS = getattr(settings, 'HELPDESK_SHOW_DELETED_VMS', False)
# guess cyclades setting too
USER_CATALOG_URL = getattr(settings, 'CYCLADES_USER_CATALOG_URL', None)
USER_CATALOG_URL = getattr(settings, 'HELPDESK_USER_CATALOG_URL',
USER_CATALOG_URL)
def token_check(func):
"""
......@@ -139,14 +144,14 @@ def index(request):
account = request.GET.get('account', None)
if account:
return redirect('synnefo.helpdesk.views.account',
account_or_ip=account)
search_query=account)
# show index template
return direct_to_template(request, "helpdesk/index.html")
@helpdesk_user_required
def account(request, account_or_ip):
def account(request, search_query):
"""
Account details view.
"""
......@@ -156,28 +161,45 @@ def account(request, account_or_ip):
account_exists = True
vms = []
networks = []
is_ip = IP_SEARCH_REGEX.match(account_or_ip)
is_uuid = UUID_SEARCH_REGEX.match(account_or_ip)
account_name = account_or_ip
is_ip = IP_SEARCH_REGEX.match(search_query)
is_uuid = UUID_SEARCH_REGEX.match(search_query)
is_vm = VM_SEARCH_REGEX.match(search_query)
account_name = search_query
auth_token = request.user.get('auth_token')
if is_ip:
try:
nic = NetworkInterface.objects.get(ipv4=account_or_ip)
account_or_ip = nic.machine.userid
nic = NetworkInterface.objects.get(ipv4=search_query)
search_query = nic.machine.userid
is_uuid = True
except NetworkInterface.DoesNotExist:
account_exists = False
account = None
if is_vm:
vmid = is_vm.groupdict().get('vmid')
try:
vm = VirtualMachine.objects.get(pk=int(vmid))
search_query = vm.userid
is_uuid = True
except VirtualMachine.DoesNotExist:
account_exists = False
account = None
search_query = vmid
if is_uuid:
account = account_or_ip
account = search_query
account_name = astakos.get_displayname(auth_token, account,
USER_CATALOG_URL)
else:
account_name = account_or_ip
if account_exists and not is_uuid:
account_name = search_query
account = astakos.get_user_uuid(auth_token, account_name,
USER_CATALOG_URL)
if not account:
account_exists = False
filter_extra = {}
if not show_deleted:
filter_extra['deleted'] = False
......@@ -200,7 +222,10 @@ def account(request, account_or_ip):
user_context = {
'account_exists': account_exists,
'is_ip': is_ip,
'is_vm': is_vm,
'is_uuid': is_uuid,
'account': account,
'search_query': search_query,
'vms': vms,
'show_deleted': show_deleted,
'account_name': account_name,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment