Commit 623d23b5 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

helpdesk: Support quering history of public IPs

Add basic support for quering a public IP address. All uses of this IP
address are returned which are currently rendered to a dummy template.
parent ceb8087e
......@@ -274,3 +274,11 @@ class MacPrefixPoolTableFactory(factory.DjangoModelFactory):
class QuotaHolderSerialFactory(factory.DjangoModelFactory):
FACTORY_FOR = models.QuotaHolderSerial
serial = factory.Sequence(lambda x: x, type=int)
class IPAddressLogFactory(factory.DjangoModelFactory):
FACTORY_FOR = models.IPAddressLog
address = "192.168.2.1"
server_id = 1
network_id = 1
active = True
{% extends "helpdesk/base.html" %}
{% block extraheader %}
<small>/ {{ account_name }}</small>
{% endblock %}
{% block content %}
{% if ip_exists %}
<table border 1>
<tr>
IP Address {{ search_query }}
</tr>
<tr>
<td> Server </td>
<td> Network </td>
<td> Allocated </td>
<td> Released </td>
<td> Account </td>
</tr>
{% for ip in ips %}
<tr>
<td>
{% if ip.released_at %}
Server {{ ip.server_id }}
{% else %}
<a href="vm-{{ ip.server_id }}">Server {{ ip.server_id }}</a>
{% endif %}
</td>
<td> Network {{ ip.network_id }} </td>
<td> {{ ip.allocated_at|date:"r" }} </td>
<td>
{% if ip.released_at %}
{{ ip.released_at|date:"r" }}
{% else %}
-
{% endif %}
</td>
<td><a href="{{ ip.account }}">{{ ip.account }}</a></td>
</tr>
{% endfor %}
</table>
{% else %}
<p> IP Address {{ search_query }} has never been allocated to any server. </p>
{% endif %}
{% endblock %}
......@@ -150,6 +150,10 @@ class HelpdeskTests(TestCase):
network__public=True,
network__userid=None,
address="195.251.222.211")
mfactory.IPAddressLogFactory(address=ip2.address,
server_id=vm1u1.id,
network_id=ip2.network.id,
active=True)
def test_enabled_setting(self):
settings.HELPDESK_ENABLED = False
......@@ -166,12 +170,16 @@ class HelpdeskTests(TestCase):
# 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')
self.assertFalse(r.context["ip_exists"])
self.assertEqual(list(r.context["ips"]), [])
# ip exists, 'test' account discovered
# ip exists
r = self.client.get(reverse('helpdesk-details',
args=["195.251.222.211"]), user_token='0001')
self.assertEqual(r.context['account'], USER1)
self.assertTrue(r.context["ip_exists"])
ips = r.context["ips"]
for ip in ips:
self.assertEqual(ip.address, "195.251.222.211")
def test_vm_lookup(self):
# vm id does not exist
......
......@@ -46,7 +46,7 @@ from urllib import unquote
import astakosclient
from snf_django.lib import astakos
from synnefo.db.models import VirtualMachine, IPAddress, Network
from synnefo.db.models import VirtualMachine, IPAddress, Network, IPAddressLog
# server actions specific imports
from synnefo.api import util
......@@ -190,14 +190,8 @@ def account(request, search_query):
auth_token = request.user['access']['token']['id']
if is_ip:
try:
ip = IPAddress.objects.filter(address=search_query, deleted=False)\
.get()
search_query = ip.userid
is_uuid = True
except IPAddress.DoesNotExist:
account_exists = False
account = None
# Search the IPAddressLog for the full use history of this IP
return search_by_ip(request, search_query)
if is_vm:
vmid = is_vm.groupdict().get('vmid')
......@@ -274,6 +268,40 @@ def account(request, search_query):
extra_context=user_context)
def search_by_ip(request, search_query):
"""Search IP history for all uses of an IP address."""
auth_token = request.user['access']['token']['id']
astakos_client = astakosclient.AstakosClient(auth_token,
settings.ASTAKOS_AUTH_URL,
retry=2, use_pool=True,
logger=logger)
ips = IPAddressLog.objects.filter(address=search_query)
for ip in ips:
# Annotate IPs with the VM, Network and account attributes
ip.vm = VirtualMachine.objects.get(id=ip.server_id)
ip.network = Network.objects.get(id=ip.network_id)
userid = ip.vm.userid
try:
ip.account = astakos_client.get_username(userid)
except:
ip.account = userid
logger.info("Failed to resolve '%s' into account" % userid)
user_context = {
'ip_exists': bool(ips),
'ips': ips,
'search_query': search_query,
'token': auth_token,
'HELPDESK_MEDIA_URL': HELPDESK_MEDIA_URL,
'UI_MEDIA_URL': UI_MEDIA_URL
}
return direct_to_template(request, "helpdesk/ip.html",
extra_context=user_context)
@helpdesk_user_required
@token_check
def vm_suspend(request, vm_id):
......
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