1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
7
# Copyright 2012 Nebula, Inc.
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
13
# http://www.apache.org/licenses/LICENSE-2.0
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
22
Views for managing Quantum networks.
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 _
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
41
LOG = logging.getLogger(__name__)
44
class IndexView(tables.DataTableView):
45
table_class = NetworksTable
46
template_name = 'nova/networks/index.html'
49
tenant_id = self.request.user.tenant_id
53
networks_list = api.quantum_list_networks(self.request)
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,
61
'name': details['network']['name'],
63
'total': net_stats['total'],
64
'available': net_stats['available'],
65
'used': net_stats['used'],
68
LOG.exception("Unable to get network list.")
69
if not hasattr(e, 'message'):
71
messages.error(self.request,
72
_('Unable to get network list: %s') % e.message)
76
class CreateView(forms.ModalFormView):
77
form_class = CreateNetwork
78
template_name = 'nova/networks/create.html'
81
class RenameView(forms.ModalFormView):
82
form_class = RenameNetwork
83
template_name = 'nova/networks/rename.html'
84
context_object_name = 'network'
86
def get_object(self, *args, **kwargs):
87
network_id = kwargs['network_id']
89
return api.quantum_network_details(self.request,
90
network_id)['network']
92
redirect = reverse("horizon:nova:networks:detail",
94
exceptions.handle(self.request,
95
_('Unable to retrieve network information.'),
98
def get_initial(self):
99
return {'network': self.object['id']}
102
class DetailView(tables.DataTableView):
103
table_class = NetworkDetailsTable
104
template_name = 'nova/networks/detail.html'
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']
114
def get_context_data(self, **kwargs):
115
context = super(DetailView, self).get_context_data(**kwargs)
116
context['network'] = self.network
120
def _get_port_states(request, network_id):
122
Helper method to find port states for a network
125
# Get all vifs for comparison with port attachments
126
vifs = api.get_vif_ids(request)
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']:
140
if str(vif['id']) == str(port_attachment['attachment']['id']):
141
connected_instance = vif['id']
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})
151
def _calc_network_stats(request, network_id):
153
Helper method to calculate statistics for a network
155
# Get all ports statistics for the network
159
ports = api.quantum_list_ports(request, network_id)
160
for port in ports['ports']:
162
# Get port attachment
163
port_attachment = api.quantum_port_attachment(request,
164
network_id, port['id'])
165
if port_attachment['attachment']:
170
return {'total': total, 'used': used, 'available': available}
173
class CreatePortView(forms.ModalFormView):
174
form_class = CreatePort
175
template_name = 'nova/networks/ports/create.html'
176
context_object_name = 'port'
178
def get_object(self, *args, **kwargs):
179
network_id = kwargs['network_id']
181
return api.quantum_network_details(self.request,
182
network_id)['network']
184
redirect = reverse("horizon:nova:networks:detail",
186
exceptions.handle(self.request,
187
_('Unable to retrieve network information.'),
190
def get_initial(self):
191
return {'network': self.object['id']}
194
class AttachPortView(forms.ModalFormView):
195
form_class = AttachPort
196
template_name = 'nova/networks/ports/attach.html'
197
context_object_name = 'network'
199
def get_object(self, *args, **kwargs):
200
network_id = kwargs['network_id']
202
return api.quantum_network_details(self.request,
203
network_id)['network']
205
redirect = reverse("horizon:nova:networks:detail",
207
exceptions.handle(self.request,
208
_('Unable to attach port.'),
211
def get_initial(self):
212
return {'network': self.object['id']}