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

Support ip to account lookup

as an alternative method to helpdesk details view. If network interface
exists with the passed ip, the details of the owner of the machine linked to
the network interface will be displayed.
parent 8655a65d
......@@ -71,6 +71,15 @@
"link": 1
}
},
{
"model": "db.NetworkInterface",
"pk": 2,
"fields": {
"ipv4": "195.251.222.211",
"network": 2,
"machine": 1002
}
},
{
"model": "db.Network",
"pk": 2,
......
......@@ -74,6 +74,7 @@
</div>
</div>
{% else %}
<p>Account <em>{{ account }}</em> does not exist in cyclades database</p>
<p>{% if is_ip %}User with IP {% else %}Account {% endif %}<em>{{ account }}</em> does not exist in cyclades database</p>
{% endif %}
{% endblock %}
......@@ -36,6 +36,8 @@ from django.test import TestCase, Client
from django.conf import settings
from django.core.urlresolvers import reverse
from synnefo.db import models
class AaiClient(Client):
def request(self, **request):
......@@ -70,6 +72,17 @@ class HelpdeskTests(TestCase):
self.user = 'test'
self.keys_url = reverse('ui_keys_collection')
def test_ip_lookup(self):
# ip does not exist, proper message gets displayed
r = self.client.get(reverse('helpdesk-details',
args=["195.251.221.122"]), user_token='0001')
self.assertContains(r, 'User with IP')
# ip exists, 'test' account discovered
r = self.client.get(reverse('helpdesk-details',
args=["195.251.222.211"]), user_token='0001')
self.assertEqual(r.context['account'], 'testuser2@test.com')
def test_view_permissions(self):
# anonymous user gets 403
......
......@@ -4,7 +4,7 @@ urlpatterns = patterns('',
url(r'^$', 'synnefo.helpdesk.views.index', name='helpdesk-index'),
url(r'^api/users', 'synnefo.helpdesk.views.user_list',
name='helpdesk-userslist'),
url(r'^(?P<account>.*)$', 'synnefo.helpdesk.views.account',
url(r'^(?P<account_or_ip>.*)$', 'synnefo.helpdesk.views.account',
name='helpdesk-details'),
)
import re
from itertools import chain
from django.shortcuts import redirect
from django.shortcuts import redirect, get_object_or_404
from django.views.generic.simple import direct_to_template
from django.db.models import get_apps
from django.conf import settings
......@@ -13,6 +15,8 @@ from urllib import unquote
from synnefo.lib.astakos import get_user
from synnefo.db.models import *
IP_SEARCH_REGEX = re.compile('([0-9]+)(?:\.[0-9]+){3}')
def get_token_from_cookie(request, cookiename):
"""
Extract token from the cookie name provided. Cookie should be in the same
......@@ -40,7 +44,6 @@ def helpdesk_user_required(func, groups=['helpdesk']):
permissions (exists in helpdesk group)
"""
def wrapper(request, *args, **kwargs):
return func(request, *args, **kwargs)
token = get_token_from_cookie(request, HELPDESK_AUTH_COOKIE)
get_user(request, settings.ASTAKOS_URL, fallback_token=token)
if hasattr(request, 'user') and request.user:
......@@ -69,37 +72,51 @@ def index(request):
# if form submitted redirect to details
account = request.GET.get('account', None)
if account:
return redirect('synnefo.helpdesk.views.account', account=account)
return redirect('synnefo.helpdesk.views.account', account_or_ip=account)
# show index template
return direct_to_template(request, "helpdesk/index.html")
@helpdesk_user_required
def account(request, account):
def account(request, account_or_ip):
"""
Account details view.
"""
# all user vms
vms = VirtualMachine.objects.filter(userid=account).order_by('deleted')
# return all user private and public networks
public_networks = Network.objects.filter(public=True).order_by('state')
private_networks = Network.objects.filter(userid=account).order_by('state')
networks = list(public_networks) + list(private_networks)
account_exists = True
if vms.count() == 0 and private_networks.count() == 0:
account_exists = False
vms = []
networks = []
is_ip = IP_SEARCH_REGEX.match(account_or_ip)
account = account_or_ip
if is_ip:
try:
nic = NetworkInterface.objects.get(ipv4=account_or_ip)
account = nic.machine.userid
except NetworkInterface.DoesNotExist:
account_exists = False
else:
# all user vms
vms = VirtualMachine.objects.filter(userid=account).order_by('deleted')
# return all user private and public networks
public_networks = Network.objects.filter(public=True).order_by('state')
private_networks = Network.objects.filter(userid=account).order_by('state')
networks = list(public_networks) + list(private_networks)
if vms.count() == 0 and private_networks.count() == 0:
account_exists = False
user_context = {
'account_exists': account_exists,
'is_ip': is_ip,
'account': account,
'vms': vms,
'networks': networks,
'UI_MEDIA_URL': settings.UI_MEDIA_URL
}
return direct_to_template(request, "helpdesk/account.html",
extra_context=user_context)
......
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