Revision bbea0414 snf-cyclades-app/synnefo/helpdesk/views.py

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