~ubuntu-branches/ubuntu/trusty/horizon/trusty

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-03-09 11:50:22 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20120309115022-ymiww5i58rbg97my
Tags: 2012.1~rc1~20120308.1479-0ubuntu1
* New upstream version.
* debian/rules: Fix symlink when installing horizon.
  (LP: #947118)
* debian/control: Add python-django-nose as a dep. (LP: #944235)
* debian/control: Fix broken depends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2012 United States Government as represented by the
4
 
# Administrator of the National Aeronautics and Space Administration.
5
 
# All Rights Reserved.
6
 
#
7
 
# Copyright 2012 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 Quantum networks.
23
 
"""
24
 
 
25
 
import logging
26
 
 
27
 
from django import http
28
 
from django.contrib import messages
29
 
from django.core.urlresolvers import reverse
30
 
from django.utils.translation import ugettext as _
31
 
 
32
 
from horizon import api
33
 
from horizon import exceptions
34
 
from horizon import forms
35
 
from horizon import tables
36
 
from horizon.dashboards.nova.networks.forms import (CreateNetwork,
37
 
        RenameNetwork, AttachPort, CreatePort)
38
 
from .tables import NetworksTable, NetworkDetailsTable
39
 
 
40
 
 
41
 
LOG = logging.getLogger(__name__)
42
 
 
43
 
 
44
 
class IndexView(tables.DataTableView):
45
 
    table_class = NetworksTable
46
 
    template_name = 'nova/networks/index.html'
47
 
 
48
 
    def get_data(self):
49
 
        tenant_id = self.request.user.tenant_id
50
 
        networks = []
51
 
 
52
 
        try:
53
 
            networks_list = api.quantum_list_networks(self.request)
54
 
            details = []
55
 
            for network in networks_list['networks']:
56
 
                net_stats = _calc_network_stats(self.request, network['id'])
57
 
                # Get network details like name and id
58
 
                details = api.quantum_network_details(self.request,
59
 
                                                      network['id'])
60
 
                networks.append({
61
 
                        'name': details['network']['name'],
62
 
                        'id': network['id'],
63
 
                        'total': net_stats['total'],
64
 
                        'available': net_stats['available'],
65
 
                        'used': net_stats['used'],
66
 
                        'tenant': tenant_id})
67
 
        except Exception, e:
68
 
            LOG.exception("Unable to get network list.")
69
 
            if not hasattr(e, 'message'):
70
 
                e.message = str(e)
71
 
            messages.error(self.request,
72
 
                           _('Unable to get network list: %s') % e.message)
73
 
        return networks
74
 
 
75
 
 
76
 
class CreateView(forms.ModalFormView):
77
 
    form_class = CreateNetwork
78
 
    template_name = 'nova/networks/create.html'
79
 
 
80
 
 
81
 
class RenameView(forms.ModalFormView):
82
 
    form_class = RenameNetwork
83
 
    template_name = 'nova/networks/rename.html'
84
 
    context_object_name = 'network'
85
 
 
86
 
    def get_object(self, *args, **kwargs):
87
 
        network_id = kwargs['network_id']
88
 
        try:
89
 
            return api.quantum_network_details(self.request,
90
 
                                               network_id)['network']
91
 
        except:
92
 
            redirect = reverse("horizon:nova:networks:detail",
93
 
                               args=(network_id,))
94
 
            exceptions.handle(self.request,
95
 
                              _('Unable to retrieve network information.'),
96
 
                              redirect=redirect)
97
 
 
98
 
    def get_initial(self):
99
 
        return {'network': self.object['id']}
100
 
 
101
 
 
102
 
class DetailView(tables.DataTableView):
103
 
    table_class = NetworkDetailsTable
104
 
    template_name = 'nova/networks/detail.html'
105
 
 
106
 
    def get_data(self):
107
 
        network_id = self.kwargs['network_id']
108
 
        network_details = api.quantum_network_details(self.request, network_id)
109
 
        self.network = {'id': network_id,
110
 
                        'name': network_details['network']['name'],
111
 
                        'ports': _get_port_states(self.request, network_id)}
112
 
        return self.network['ports']
113
 
 
114
 
    def get_context_data(self, **kwargs):
115
 
        context = super(DetailView, self).get_context_data(**kwargs)
116
 
        context['network'] = self.network
117
 
        return context
118
 
 
119
 
 
120
 
def _get_port_states(request, network_id):
121
 
    """
122
 
    Helper method to find port states for a network
123
 
    """
124
 
    network_ports = []
125
 
    # Get all vifs for comparison with port attachments
126
 
    vifs = api.get_vif_ids(request)
127
 
 
128
 
    # Get all ports on this network
129
 
    ports = api.quantum_list_ports(request, network_id)
130
 
    for port in ports['ports']:
131
 
        port_details = api.quantum_port_details(request,
132
 
                                                network_id, port['id'])
133
 
        # Get port attachments
134
 
        port_attachment = api.quantum_port_attachment(request,
135
 
                                                      network_id, port['id'])
136
 
        # Find instance the attachment belongs to
137
 
        connected_instance = None
138
 
        if port_attachment['attachment']:
139
 
            for vif in vifs:
140
 
                if str(vif['id']) == str(port_attachment['attachment']['id']):
141
 
                    connected_instance = vif['id']
142
 
                    break
143
 
        network_ports.append({
144
 
            'id': port_details['port']['id'],
145
 
            'state': port_details['port']['state'],
146
 
            'attachment': port_attachment['attachment'],
147
 
            'instance': connected_instance})
148
 
    return network_ports
149
 
 
150
 
 
151
 
def _calc_network_stats(request, network_id):
152
 
    """
153
 
    Helper method to calculate statistics for a network
154
 
    """
155
 
    # Get all ports statistics for the network
156
 
    total = 0
157
 
    available = 0
158
 
    used = 0
159
 
    ports = api.quantum_list_ports(request, network_id)
160
 
    for port in ports['ports']:
161
 
        total += 1
162
 
        # Get port attachment
163
 
        port_attachment = api.quantum_port_attachment(request,
164
 
                                                      network_id, port['id'])
165
 
        if port_attachment['attachment']:
166
 
            used += 1
167
 
        else:
168
 
            available += 1
169
 
 
170
 
    return {'total': total, 'used': used, 'available': available}
171
 
 
172
 
 
173
 
class CreatePortView(forms.ModalFormView):
174
 
    form_class = CreatePort
175
 
    template_name = 'nova/networks/ports/create.html'
176
 
    context_object_name = 'port'
177
 
 
178
 
    def get_object(self, *args, **kwargs):
179
 
        network_id = kwargs['network_id']
180
 
        try:
181
 
            return api.quantum_network_details(self.request,
182
 
                                               network_id)['network']
183
 
        except:
184
 
            redirect = reverse("horizon:nova:networks:detail",
185
 
                               args=(network_id,))
186
 
            exceptions.handle(self.request,
187
 
                              _('Unable to retrieve network information.'),
188
 
                              redirect=redirect)
189
 
 
190
 
    def get_initial(self):
191
 
        return {'network': self.object['id']}
192
 
 
193
 
 
194
 
class AttachPortView(forms.ModalFormView):
195
 
    form_class = AttachPort
196
 
    template_name = 'nova/networks/ports/attach.html'
197
 
    context_object_name = 'network'
198
 
 
199
 
    def get_object(self, *args, **kwargs):
200
 
        network_id = kwargs['network_id']
201
 
        try:
202
 
            return api.quantum_network_details(self.request,
203
 
                                               network_id)['network']
204
 
        except:
205
 
            redirect = reverse("horizon:nova:networks:detail",
206
 
                               args=(network_id,))
207
 
            exceptions.handle(self.request,
208
 
                              _('Unable to attach port.'),
209
 
                              redirect=redirect)
210
 
 
211
 
    def get_initial(self):
212
 
        return {'network': self.object['id']}