Revision bbea0414

b/snf-cyclades-app/synnefo/db/models_factory.py
274 274
class QuotaHolderSerialFactory(factory.DjangoModelFactory):
275 275
    FACTORY_FOR = models.QuotaHolderSerial
276 276
    serial = factory.Sequence(lambda x: x, type=int)
277

  
278

  
279
class IPAddressLogFactory(factory.DjangoModelFactory):
280
    FACTORY_FOR = models.IPAddressLog
281
    address = "192.168.2.1"
282
    server_id = 1
283
    network_id = 1
284
    active = True
b/snf-cyclades-app/synnefo/helpdesk/templates/helpdesk/ip.html
1
{% extends "helpdesk/base.html" %}
2

  
3
{% block extraheader %}
4
<small>/ {{ account_name }}</small>
5
{% endblock %}
6

  
7
{% block content %}
8

  
9
{% if ip_exists %}
10
<table border 1>
11
  <tr>
12
    IP Address {{ search_query }}
13
  </tr>
14
  <tr>
15
          <td> Server  </td>
16
          <td> Network  </td>
17
          <td> Allocated  </td>
18
          <td> Released  </td>
19
          <td> Account  </td>
20
  </tr>
21
{% for ip in ips %}
22
  <tr>
23
          <td>
24
              {% if ip.released_at %}
25
                Server {{ ip.server_id }}
26
              {% else %}
27
              <a href="vm-{{ ip.server_id }}">Server {{ ip.server_id }}</a>
28
              {% endif %}
29
          </td>
30
          <td> Network {{ ip.network_id }} </td>
31
          <td> {{ ip.allocated_at|date:"r" }} </td>
32
          <td>
33
               {% if ip.released_at %}
34
                  {{ ip.released_at|date:"r" }}
35
               {% else %}
36
                  -
37
               {% endif %}
38
          </td>
39
          <td><a href="{{ ip.account }}">{{ ip.account }}</a></td>
40
  </tr>
41
{% endfor %}
42
</table>
43
{% else %}
44

  
45
<p> IP Address {{ search_query }} has never been allocated to any server. </p>
46
{% endif %}
47
{% endblock %}
b/snf-cyclades-app/synnefo/helpdesk/tests.py
150 150
                                          network__public=True,
151 151
                                          network__userid=None,
152 152
                                          address="195.251.222.211")
153
        mfactory.IPAddressLogFactory(address=ip2.address,
154
                                     server_id=vm1u1.id,
155
                                     network_id=ip2.network.id,
156
                                     active=True)
153 157

  
154 158
    def test_enabled_setting(self):
155 159
        settings.HELPDESK_ENABLED = False
......
166 170
        # ip does not exist, proper message gets displayed
167 171
        r = self.client.get(reverse('helpdesk-details',
168 172
                            args=["195.251.221.122"]), user_token='0001')
169
        self.assertContains(r, 'User with IP')
173
        self.assertFalse(r.context["ip_exists"])
174
        self.assertEqual(list(r.context["ips"]), [])
170 175

  
171
        # ip exists, 'test' account discovered
176
        # ip exists
172 177
        r = self.client.get(reverse('helpdesk-details',
173 178
                            args=["195.251.222.211"]), user_token='0001')
174
        self.assertEqual(r.context['account'], USER1)
179
        self.assertTrue(r.context["ip_exists"])
180
        ips = r.context["ips"]
181
        for ip in ips:
182
            self.assertEqual(ip.address, "195.251.222.211")
175 183

  
176 184
    def test_vm_lookup(self):
177 185
        # vm id does not exist
b/snf-cyclades-app/synnefo/helpdesk/views.py
46 46
import astakosclient
47 47
from snf_django.lib import astakos
48 48

  
49
from synnefo.db.models import VirtualMachine, IPAddress, Network
49
from synnefo.db.models import VirtualMachine, IPAddress, Network, IPAddressLog
50 50

  
51 51
# server actions specific imports
52 52
from synnefo.api import util
......
190 190
    auth_token = request.user['access']['token']['id']
191 191

  
192 192
    if is_ip:
193
        try:
194
            ip = IPAddress.objects.filter(address=search_query, deleted=False)\
195
                                  .get()
196
            search_query = ip.userid
197
            is_uuid = True
198
        except IPAddress.DoesNotExist:
199
            account_exists = False
200
            account = None
193
        # Search the IPAddressLog for the full use history of this IP
194
        return search_by_ip(request, search_query)
201 195

  
202 196
    if is_vm:
203 197
        vmid = is_vm.groupdict().get('vmid')
......
274 268
                              extra_context=user_context)
275 269

  
276 270

  
271
def search_by_ip(request, search_query):
272
    """Search IP history for all uses of an IP address."""
273
    auth_token = request.user['access']['token']['id']
274
    astakos_client = astakosclient.AstakosClient(auth_token,
275
                                                 settings.ASTAKOS_AUTH_URL,
276
                                                 retry=2, use_pool=True,
277
                                                 logger=logger)
278

  
279
    ips = IPAddressLog.objects.filter(address=search_query)
280

  
281
    for ip in ips:
282
        # Annotate IPs with the VM, Network and account attributes
283
        ip.vm = VirtualMachine.objects.get(id=ip.server_id)
284
        ip.network = Network.objects.get(id=ip.network_id)
285
        userid = ip.vm.userid
286

  
287
        try:
288
            ip.account = astakos_client.get_username(userid)
289
        except:
290
            ip.account = userid
291
            logger.info("Failed to resolve '%s' into account" % userid)
292

  
293
    user_context = {
294
        'ip_exists': bool(ips),
295
        'ips': ips,
296
        'search_query': search_query,
297
        'token': auth_token,
298
        'HELPDESK_MEDIA_URL': HELPDESK_MEDIA_URL,
299
        'UI_MEDIA_URL': UI_MEDIA_URL
300
    }
301

  
302
    return direct_to_template(request, "helpdesk/ip.html",
303
                              extra_context=user_context)
304

  
277 305
@helpdesk_user_required
278 306
@token_check
279 307
def vm_suspend(request, vm_id):

Also available in: Unified diff