Revision d59d86d4
b/snf-cyclades-app/synnefo/helpdesk/fixtures/helpdesk_test.json | ||
---|---|---|
72 | 72 |
} |
73 | 73 |
}, |
74 | 74 |
{ |
75 |
"model": "db.NetworkInterface", |
|
76 |
"pk": 2, |
|
77 |
"fields": { |
|
78 |
"ipv4": "195.251.222.211", |
|
79 |
"network": 2, |
|
80 |
"machine": 1002 |
|
81 |
} |
|
82 |
}, |
|
83 |
{ |
|
75 | 84 |
"model": "db.Network", |
76 | 85 |
"pk": 2, |
77 | 86 |
"fields": { |
b/snf-cyclades-app/synnefo/helpdesk/templates/helpdesk/account.html | ||
---|---|---|
74 | 74 |
</div> |
75 | 75 |
</div> |
76 | 76 |
{% else %} |
77 |
<p>Account <em>{{ account }}</em> does not exist in cyclades database</p> |
|
77 |
|
|
78 |
<p>{% if is_ip %}User with IP {% else %}Account {% endif %}<em>{{ account }}</em> does not exist in cyclades database</p> |
|
78 | 79 |
{% endif %} |
79 | 80 |
{% endblock %} |
b/snf-cyclades-app/synnefo/helpdesk/tests.py | ||
---|---|---|
36 | 36 |
from django.conf import settings |
37 | 37 |
from django.core.urlresolvers import reverse |
38 | 38 |
|
39 |
from synnefo.db import models |
|
40 |
|
|
39 | 41 |
class AaiClient(Client): |
40 | 42 |
|
41 | 43 |
def request(self, **request): |
... | ... | |
70 | 72 |
self.user = 'test' |
71 | 73 |
self.keys_url = reverse('ui_keys_collection') |
72 | 74 |
|
75 |
def test_ip_lookup(self): |
|
76 |
# ip does not exist, proper message gets displayed |
|
77 |
r = self.client.get(reverse('helpdesk-details', |
|
78 |
args=["195.251.221.122"]), user_token='0001') |
|
79 |
self.assertContains(r, 'User with IP') |
|
80 |
|
|
81 |
# ip exists, 'test' account discovered |
|
82 |
r = self.client.get(reverse('helpdesk-details', |
|
83 |
args=["195.251.222.211"]), user_token='0001') |
|
84 |
self.assertEqual(r.context['account'], 'testuser2@test.com') |
|
85 |
|
|
73 | 86 |
def test_view_permissions(self): |
74 | 87 |
|
75 | 88 |
# anonymous user gets 403 |
b/snf-cyclades-app/synnefo/helpdesk/urls.py | ||
---|---|---|
4 | 4 |
url(r'^$', 'synnefo.helpdesk.views.index', name='helpdesk-index'), |
5 | 5 |
url(r'^api/users', 'synnefo.helpdesk.views.user_list', |
6 | 6 |
name='helpdesk-userslist'), |
7 |
url(r'^(?P<account>.*)$', 'synnefo.helpdesk.views.account', |
|
7 |
url(r'^(?P<account_or_ip>.*)$', 'synnefo.helpdesk.views.account',
|
|
8 | 8 |
name='helpdesk-details'), |
9 | 9 |
) |
10 | 10 |
|
b/snf-cyclades-app/synnefo/helpdesk/views.py | ||
---|---|---|
1 |
import re |
|
2 |
|
|
1 | 3 |
from itertools import chain |
2 | 4 |
|
3 |
from django.shortcuts import redirect |
|
5 |
from django.shortcuts import redirect, get_object_or_404
|
|
4 | 6 |
from django.views.generic.simple import direct_to_template |
5 | 7 |
from django.db.models import get_apps |
6 | 8 |
from django.conf import settings |
... | ... | |
13 | 15 |
from synnefo.lib.astakos import get_user |
14 | 16 |
from synnefo.db.models import * |
15 | 17 |
|
18 |
IP_SEARCH_REGEX = re.compile('([0-9]+)(?:\.[0-9]+){3}') |
|
19 |
|
|
16 | 20 |
def get_token_from_cookie(request, cookiename): |
17 | 21 |
""" |
18 | 22 |
Extract token from the cookie name provided. Cookie should be in the same |
... | ... | |
40 | 44 |
permissions (exists in helpdesk group) |
41 | 45 |
""" |
42 | 46 |
def wrapper(request, *args, **kwargs): |
43 |
return func(request, *args, **kwargs) |
|
44 | 47 |
token = get_token_from_cookie(request, HELPDESK_AUTH_COOKIE) |
45 | 48 |
get_user(request, settings.ASTAKOS_URL, fallback_token=token) |
46 | 49 |
if hasattr(request, 'user') and request.user: |
... | ... | |
69 | 72 |
# if form submitted redirect to details |
70 | 73 |
account = request.GET.get('account', None) |
71 | 74 |
if account: |
72 |
return redirect('synnefo.helpdesk.views.account', account=account)
|
|
75 |
return redirect('synnefo.helpdesk.views.account', account_or_ip=account)
|
|
73 | 76 |
|
74 | 77 |
# show index template |
75 | 78 |
return direct_to_template(request, "helpdesk/index.html") |
76 | 79 |
|
77 | 80 |
|
78 | 81 |
@helpdesk_user_required |
79 |
def account(request, account): |
|
82 |
def account(request, account_or_ip):
|
|
80 | 83 |
""" |
81 | 84 |
Account details view. |
82 | 85 |
""" |
83 | 86 |
|
84 |
# all user vms |
|
85 |
vms = VirtualMachine.objects.filter(userid=account).order_by('deleted') |
|
86 |
|
|
87 |
# return all user private and public networks |
|
88 |
public_networks = Network.objects.filter(public=True).order_by('state') |
|
89 |
private_networks = Network.objects.filter(userid=account).order_by('state') |
|
90 |
networks = list(public_networks) + list(private_networks) |
|
91 |
|
|
92 | 87 |
account_exists = True |
93 |
if vms.count() == 0 and private_networks.count() == 0: |
|
94 |
account_exists = False |
|
88 |
vms = [] |
|
89 |
networks = [] |
|
90 |
is_ip = IP_SEARCH_REGEX.match(account_or_ip) |
|
91 |
account = account_or_ip |
|
92 |
|
|
93 |
if is_ip: |
|
94 |
try: |
|
95 |
nic = NetworkInterface.objects.get(ipv4=account_or_ip) |
|
96 |
account = nic.machine.userid |
|
97 |
except NetworkInterface.DoesNotExist: |
|
98 |
account_exists = False |
|
99 |
else: |
|
100 |
# all user vms |
|
101 |
vms = VirtualMachine.objects.filter(userid=account).order_by('deleted') |
|
102 |
|
|
103 |
# return all user private and public networks |
|
104 |
public_networks = Network.objects.filter(public=True).order_by('state') |
|
105 |
private_networks = Network.objects.filter(userid=account).order_by('state') |
|
106 |
networks = list(public_networks) + list(private_networks) |
|
107 |
|
|
108 |
if vms.count() == 0 and private_networks.count() == 0: |
|
109 |
account_exists = False |
|
95 | 110 |
|
96 | 111 |
user_context = { |
97 | 112 |
'account_exists': account_exists, |
113 |
'is_ip': is_ip, |
|
98 | 114 |
'account': account, |
99 | 115 |
'vms': vms, |
100 | 116 |
'networks': networks, |
101 | 117 |
'UI_MEDIA_URL': settings.UI_MEDIA_URL |
102 | 118 |
} |
119 |
|
|
103 | 120 |
return direct_to_template(request, "helpdesk/account.html", |
104 | 121 |
extra_context=user_context) |
105 | 122 |
|
Also available in: Unified diff