~ubuntu-branches/ubuntu/precise/horizon/precise-updates

« back to all changes in this revision

Viewing changes to horizon/horizon/dashboards/nova/floating_ips/views.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2011-12-16 16:34:56 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20111216163456-ts9ldy8skhsg0scb
Tags: 2012.1~e2-0ubuntu1
* New upstream release (LP: #904039)
* debian/control: Update build-depends.
* debian/watch: Fix to fetch from Launchpad ad well.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2011 United States Government as represented by the
4
 
# Administrator of the National Aeronautics and Space Administration.
5
 
# All Rights Reserved.
6
 
#
7
 
# Copyright 2011 Nebula, Inc.
8
 
#
9
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
10
 
#    not use this file except in compliance with the License. You may obtain
11
 
#    a copy of the License at
12
 
#
13
 
#         http://www.apache.org/licenses/LICENSE-2.0
14
 
#
15
 
#    Unless required by applicable law or agreed to in writing, software
16
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18
 
#    License for the specific language governing permissions and limitations
19
 
#    under the License.
20
 
 
21
 
"""
22
 
Views for managing Nova floating IPs.
23
 
"""
24
 
import logging
25
 
 
26
 
from django import template
27
 
from django.contrib import messages
28
 
from django.contrib.auth.decorators import login_required
29
 
from django import shortcuts
30
 
from django.utils.translation import ugettext as _
31
 
from novaclient import exceptions as novaclient_exceptions
32
 
 
33
 
from horizon import api
34
 
from horizon.dashboards.nova.floating_ips.forms import (ReleaseFloatingIp,
35
 
        FloatingIpAssociate, FloatingIpDisassociate, FloatingIpAllocate)
36
 
 
37
 
 
38
 
LOG = logging.getLogger(__name__)
39
 
 
40
 
 
41
 
@login_required
42
 
def index(request):
43
 
    for f in (ReleaseFloatingIp, FloatingIpDisassociate, FloatingIpAllocate):
44
 
        _unused, handled = f.maybe_handle(request)
45
 
        if handled:
46
 
            return handled
47
 
    try:
48
 
        floating_ips = api.tenant_floating_ip_list(request)
49
 
    except novaclient_exceptions.ClientException, e:
50
 
        floating_ips = []
51
 
        LOG.exception("ClientException in floating ip index")
52
 
        messages.error(request,
53
 
                       _('Error fetching floating ips: %s') % e.message)
54
 
    allocate_form = FloatingIpAllocate(initial={
55
 
                                        'tenant_id': request.user.tenant_id})
56
 
    return shortcuts.render(request,
57
 
                            'nova/floating_ips/index.html', {
58
 
                                'allocate_form': allocate_form,
59
 
                                'disassociate_form': FloatingIpDisassociate(),
60
 
                                'floating_ips': floating_ips,
61
 
                                'release_form': ReleaseFloatingIp()})
62
 
 
63
 
 
64
 
@login_required
65
 
def associate(request, ip_id):
66
 
    instancelist = [(server.id, 'id: %s, name: %s' %
67
 
            (server.id, server.name))
68
 
            for server in api.server_list(request)]
69
 
 
70
 
    form, handled = FloatingIpAssociate().maybe_handle(request, initial={
71
 
                'floating_ip_id': ip_id,
72
 
                'floating_ip': api.tenant_floating_ip_get(request, ip_id).ip,
73
 
                'instances': instancelist})
74
 
    if handled:
75
 
        return handled
76
 
 
77
 
    context = {'floating_ip_id': ip_id,
78
 
               'form': form}
79
 
 
80
 
    if request.is_ajax():
81
 
        template = 'nova/floating_ips/_associate.html'
82
 
        context['hide'] = True
83
 
    else:
84
 
        template = 'nova/floating_ips/associate.html'
85
 
 
86
 
    return shortcuts.render(request, template, context)
87
 
 
88
 
 
89
 
@login_required
90
 
def disassociate(request, ip_id):
91
 
    form, handled = FloatingIpDisassociate().maybe_handle(request)
92
 
    if handled:
93
 
        return handled
94
 
 
95
 
    return shortcuts.render(request, 'nova/floating_ips/associate.html', {
96
 
                                        'floating_ip_id': ip_id})