Changes suggested in merge discussion
- Importing the quantum client module instead of class. - Moved computation logic to helper subs. - Fixed logger line. - Modified the api calls to fetch server virtual interfaces to the new openstackx virtual_interfaces class. - Fixed some nomenclature and styles.
This commit is contained in:
parent
a0d3bfa62c
commit
0462724d98
@ -47,10 +47,9 @@ import openstackx.admin
|
|||||||
import openstackx.api.exceptions as api_exceptions
|
import openstackx.api.exceptions as api_exceptions
|
||||||
import openstackx.extras
|
import openstackx.extras
|
||||||
import openstackx.auth
|
import openstackx.auth
|
||||||
from quantum.client import Client
|
import quantum.client
|
||||||
from urlparse import urlparse
|
from urlparse import urlparse
|
||||||
|
|
||||||
|
|
||||||
LOG = logging.getLogger('django_openstack.api')
|
LOG = logging.getLogger('django_openstack.api')
|
||||||
|
|
||||||
|
|
||||||
@ -163,7 +162,7 @@ class Server(APIResourceWrapper):
|
|||||||
"""
|
"""
|
||||||
_attrs = ['addresses', 'attrs', 'hostId', 'id', 'image', 'links',
|
_attrs = ['addresses', 'attrs', 'hostId', 'id', 'image', 'links',
|
||||||
'metadata', 'name', 'private_ip', 'public_ip', 'status', 'uuid',
|
'metadata', 'name', 'private_ip', 'public_ip', 'status', 'uuid',
|
||||||
'image_name', 'virtual_interfaces']
|
'image_name', 'VirtualInterfaces']
|
||||||
|
|
||||||
def __init__(self, apiresource, request):
|
def __init__(self, apiresource, request):
|
||||||
super(Server, self).__init__(apiresource)
|
super(Server, self).__init__(apiresource)
|
||||||
@ -241,11 +240,17 @@ class SwiftAuthentication(object):
|
|||||||
def authenticate(self):
|
def authenticate(self):
|
||||||
return (self.storage_url, '', self.auth_token)
|
return (self.storage_url, '', self.auth_token)
|
||||||
|
|
||||||
|
|
||||||
class ServiceCatalogException(api_exceptions.ApiException):
|
class ServiceCatalogException(api_exceptions.ApiException):
|
||||||
def __init__(self, service_name):
|
def __init__(self, service_name):
|
||||||
message = 'Invalid service catalog service: %s' % service_name
|
message = 'Invalid service catalog service: %s' % service_name
|
||||||
super(ServiceCatalogException, self).__init__(404, message)
|
super(ServiceCatalogException, self).__init__(404, message)
|
||||||
|
|
||||||
|
|
||||||
|
class VirtualInterface(APIResourceWrapper):
|
||||||
|
_attrs = ['id','mac_address']
|
||||||
|
|
||||||
|
|
||||||
def url_for(request, service_name, admin=False):
|
def url_for(request, service_name, admin=False):
|
||||||
catalog = request.user.service_catalog
|
catalog = request.user.service_catalog
|
||||||
try:
|
try:
|
||||||
@ -347,7 +352,7 @@ def swift_api(request):
|
|||||||
|
|
||||||
|
|
||||||
def quantum_api(request):
|
def quantum_api(request):
|
||||||
return Client(settings.QUANTUM_URL, settings.QUANTUM_PORT,
|
return quantum.client.Client(settings.QUANTUM_URL, settings.QUANTUM_PORT,
|
||||||
False, request.user.tenant, 'json')
|
False, request.user.tenant, 'json')
|
||||||
|
|
||||||
|
|
||||||
@ -651,7 +656,6 @@ def swift_get_object_data(request, container_name, object_name):
|
|||||||
def get_vif_ids(request):
|
def get_vif_ids(request):
|
||||||
vifs = []
|
vifs = []
|
||||||
attached_vifs = []
|
attached_vifs = []
|
||||||
|
|
||||||
# Get a list of all networks
|
# Get a list of all networks
|
||||||
networks_list = quantum_api(request).list_networks()
|
networks_list = quantum_api(request).list_networks()
|
||||||
for network in networks_list['networks']:
|
for network in networks_list['networks']:
|
||||||
@ -665,28 +669,23 @@ def get_vif_ids(request):
|
|||||||
instances = server_list(request)
|
instances = server_list(request)
|
||||||
# Get virtual interface ids by instance
|
# Get virtual interface ids by instance
|
||||||
for instance in instances:
|
for instance in instances:
|
||||||
instance_vifs = instance.virtual_interfaces
|
instance_vifs = extras_api(request).virtual_interfaces.list(instance.id)
|
||||||
for vif in instance_vifs:
|
for vif in instance_vifs:
|
||||||
# Check if this VIF is already connected to any port
|
# Check if this VIF is already connected to any port
|
||||||
if str(vif['id']) in attached_vifs:
|
if str(vif.id) in attached_vifs:
|
||||||
vifs.append({
|
vifs.append({
|
||||||
'id' : vif['id'],
|
'id' : vif.id,
|
||||||
'instance' : instance.id,
|
'instance' : instance.id,
|
||||||
'instance_name' : instance.name,
|
'instance_name' : instance.name,
|
||||||
'available' : False,
|
'available' : False
|
||||||
'network_id' : vif['network']['id'],
|
|
||||||
'network_name' : vif['network']['label']
|
|
||||||
})
|
})
|
||||||
else:
|
else:
|
||||||
vifs.append({
|
vifs.append({
|
||||||
'id' : vif['id'],
|
'id' : vif.id,
|
||||||
'instance' : instance.id,
|
'instance' : instance.id,
|
||||||
'instance_name' : instance.name,
|
'instance_name' : instance.name,
|
||||||
'available' : True,
|
'available' : True
|
||||||
'network_id' : vif['network']['id'],
|
|
||||||
'network_name' : vif['network']['label']
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return vifs
|
return vifs
|
||||||
|
|
||||||
class GlobalSummary(object):
|
class GlobalSummary(object):
|
||||||
|
@ -42,7 +42,7 @@ from django_openstack.dash.views.ports import TogglePort
|
|||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
LOG = logging.getLogger('django_openstack.dash')
|
LOG = logging.getLogger('django_openstack.dash.views.networks')
|
||||||
|
|
||||||
class CreateNetwork(forms.SelfHandlingForm):
|
class CreateNetwork(forms.SelfHandlingForm):
|
||||||
name = forms.CharField(required=True, label="Network Name")
|
name = forms.CharField(required=True, label="Network Name")
|
||||||
@ -117,30 +117,15 @@ def index(request, tenant_id):
|
|||||||
networks_list = api.quantum_api(request).list_networks()
|
networks_list = api.quantum_api(request).list_networks()
|
||||||
details = []
|
details = []
|
||||||
for network in networks_list['networks']:
|
for network in networks_list['networks']:
|
||||||
|
net_stats = _calc_network_stats(request, tenant_id, network['id'])
|
||||||
# Get all ports statistics for the network
|
|
||||||
total = 0
|
|
||||||
available = 0
|
|
||||||
used = 0
|
|
||||||
ports = api.quantum_api(request).list_ports(network['id'])
|
|
||||||
for port in ports['ports']:
|
|
||||||
total += 1
|
|
||||||
# Get port details
|
|
||||||
port_details = api.quantum_api(request).show_port_details(network['id'], port['id'])
|
|
||||||
# Get port attachment
|
|
||||||
port_attachment = api.quantum_api(request).show_port_attachment(network['id'], port['id'])
|
|
||||||
if port_attachment['attachment'] == None:
|
|
||||||
available += 1
|
|
||||||
else:
|
|
||||||
used += 1
|
|
||||||
# Get network details like name and id
|
# Get network details like name and id
|
||||||
details = api.quantum_api(request).show_network_details(network['id'])
|
details = api.quantum_api(request).show_network_details(network['id'])
|
||||||
networks.append({
|
networks.append({
|
||||||
'name' : details['network']['name'],
|
'name' : details['network']['name'],
|
||||||
'id' : network['id'],
|
'id' : network['id'],
|
||||||
'total' : total,
|
'total' : net_stats['total'],
|
||||||
'available' : available,
|
'available' : net_stats['available'],
|
||||||
'used' : used,
|
'used' : net_stats['used'],
|
||||||
'tenant' : tenant_id
|
'tenant' : tenant_id
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -171,41 +156,12 @@ def detail(request, tenant_id, network_id):
|
|||||||
toggle_port_form, port_toggle_handled = TogglePort.maybe_handle(request)
|
toggle_port_form, port_toggle_handled = TogglePort.maybe_handle(request)
|
||||||
|
|
||||||
network = {}
|
network = {}
|
||||||
network_ports = []
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
network_details = api.quantum_api(request).show_network_details(network_id)
|
network_details = api.quantum_api(request).show_network_details(network_id)
|
||||||
network['name'] = network_details['network']['name']
|
network['name'] = network_details['network']['name']
|
||||||
network['id'] = network_id
|
network['id'] = network_id
|
||||||
|
network['ports'] = _get_port_states(request, tenant_id, network_id)
|
||||||
# Get all vifs for comparison with port attachments
|
|
||||||
vifs = api.get_vif_ids(request)
|
|
||||||
|
|
||||||
# Get all ports on this network
|
|
||||||
ports = api.quantum_api(request).list_ports(network_id)
|
|
||||||
for port in ports['ports']:
|
|
||||||
port_details = api.quantum_api(request).show_port_details(network_id, port['id'])
|
|
||||||
# Get port attachments
|
|
||||||
port_attachment = api.quantum_api(request).show_port_attachment(network_id, port['id'])
|
|
||||||
# Find instance the attachment belongs to
|
|
||||||
# Get all instances
|
|
||||||
instances = api.server_list(request)
|
|
||||||
connected_instance = None
|
|
||||||
# Get virtual interface ids by instance
|
|
||||||
for instance in instances:
|
|
||||||
for vif in instance.virtual_interfaces:
|
|
||||||
|
|
||||||
if str(vif['id']) == str(port_attachment['attachment']):
|
|
||||||
connected_instance = instance.name
|
|
||||||
break
|
|
||||||
network_ports.append({
|
|
||||||
'id' : port_details['port']['id'],
|
|
||||||
'state' : port_details['port']['state'],
|
|
||||||
'attachment' : port_attachment['attachment'],
|
|
||||||
'instance' : connected_instance
|
|
||||||
})
|
|
||||||
network['ports'] = network_ports
|
|
||||||
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
messages.error(request, 'Unable to get network details: %s' % e.message)
|
messages.error(request, 'Unable to get network details: %s' % e.message)
|
||||||
|
|
||||||
@ -230,3 +186,54 @@ def rename(request, tenant_id, network_id):
|
|||||||
'network' : network_details,
|
'network' : network_details,
|
||||||
'rename_form' : rename_form
|
'rename_form' : rename_form
|
||||||
}, context_instance=template.RequestContext(request))
|
}, context_instance=template.RequestContext(request))
|
||||||
|
|
||||||
|
"""
|
||||||
|
Helper method to find port states for a network
|
||||||
|
"""
|
||||||
|
def _get_port_states(request, tenant_id, network_id):
|
||||||
|
network_ports = []
|
||||||
|
# Get all vifs for comparison with port attachments
|
||||||
|
vifs = api.get_vif_ids(request)
|
||||||
|
|
||||||
|
# Get all ports on this network
|
||||||
|
ports = api.quantum_api(request).list_ports(network_id)
|
||||||
|
for port in ports['ports']:
|
||||||
|
port_details = api.quantum_api(request).show_port_details(network_id, port['id'])
|
||||||
|
# Get port attachments
|
||||||
|
port_attachment = api.quantum_api(request).show_port_attachment(network_id, port['id'])
|
||||||
|
# Find instance the attachment belongs to
|
||||||
|
connected_instance = None
|
||||||
|
for vif in vifs:
|
||||||
|
if str(vif['id']) == str(port_attachment['attachment']):
|
||||||
|
connected_instance = vif['instance_name']
|
||||||
|
break
|
||||||
|
network_ports.append({
|
||||||
|
'id' : port_details['port']['id'],
|
||||||
|
'state' : port_details['port']['state'],
|
||||||
|
'attachment' : port_attachment['attachment'],
|
||||||
|
'instance' : connected_instance
|
||||||
|
})
|
||||||
|
|
||||||
|
return network_ports
|
||||||
|
|
||||||
|
"""
|
||||||
|
Helper method to calculate statistics for a network
|
||||||
|
"""
|
||||||
|
def _calc_network_stats(request, tenant_id, network_id):
|
||||||
|
# Get all ports statistics for the network
|
||||||
|
total = 0
|
||||||
|
available = 0
|
||||||
|
used = 0
|
||||||
|
ports = api.quantum_api(request).list_ports(network_id)
|
||||||
|
for port in ports['ports']:
|
||||||
|
total += 1
|
||||||
|
# Get port details
|
||||||
|
port_details = api.quantum_api(request).show_port_details(network_id, port['id'])
|
||||||
|
# Get port attachment
|
||||||
|
port_attachment = api.quantum_api(request).show_port_attachment(network_id, port['id'])
|
||||||
|
if port_attachment['attachment'] == None:
|
||||||
|
available += 1
|
||||||
|
else:
|
||||||
|
used += 1
|
||||||
|
|
||||||
|
return { 'total' : total, 'used' : used, 'available': available }
|
||||||
|
@ -35,7 +35,7 @@ from django_openstack import forms
|
|||||||
from django_openstack import api
|
from django_openstack import api
|
||||||
|
|
||||||
|
|
||||||
LOG = logging.getLogger('django_api.quantum_api(request).dash')
|
LOG = logging.getLogger('django_openstack.dash.views.ports')
|
||||||
|
|
||||||
|
|
||||||
class CreatePort(forms.SelfHandlingForm):
|
class CreatePort(forms.SelfHandlingForm):
|
||||||
@ -143,7 +143,7 @@ class TogglePort(forms.SelfHandlingForm):
|
|||||||
def create(request, tenant_id, network_id):
|
def create(request, tenant_id, network_id):
|
||||||
create_form, handled = CreatePort.maybe_handle(request)
|
create_form, handled = CreatePort.maybe_handle(request)
|
||||||
|
|
||||||
if (handled):
|
if handled:
|
||||||
return shortcuts.redirect(
|
return shortcuts.redirect(
|
||||||
'dash_networks_detail',
|
'dash_networks_detail',
|
||||||
tenant_id=request.user.tenant,
|
tenant_id=request.user.tenant,
|
||||||
@ -164,20 +164,29 @@ def attach(request, tenant_id, network_id, port_id):
|
|||||||
return shortcuts.redirect('dash_networks_detail', request.user.tenant, network_id)
|
return shortcuts.redirect('dash_networks_detail', request.user.tenant, network_id)
|
||||||
|
|
||||||
# Get all avaliable vifs
|
# Get all avaliable vifs
|
||||||
VIF_CHOICES = []
|
vifs = _get_available_vifs(request)
|
||||||
vifs = api.get_vif_ids(request)
|
|
||||||
|
|
||||||
for vif in vifs:
|
|
||||||
if vif['available']:
|
|
||||||
name = "Instance %s VIF %s" % (str(vif['instance_name']), str(vif['id']))
|
|
||||||
VIF_CHOICES.append({
|
|
||||||
'name' : str(name),
|
|
||||||
'id' : str(vif['id'])
|
|
||||||
})
|
|
||||||
|
|
||||||
return shortcuts.render_to_response('dash_port_attach.html', {
|
return shortcuts.render_to_response('dash_port_attach.html', {
|
||||||
'network' : network_id,
|
'network' : network_id,
|
||||||
'port' : port_id,
|
'port' : port_id,
|
||||||
'attach_form' : attach_form,
|
'attach_form' : attach_form,
|
||||||
'vifs' : VIF_CHOICES,
|
'vifs' : vifs,
|
||||||
}, context_instance=template.RequestContext(request))
|
}, context_instance=template.RequestContext(request))
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Method to get a list of available virtual interfaces
|
||||||
|
"""
|
||||||
|
def _get_available_vifs(request):
|
||||||
|
vif_choices = []
|
||||||
|
vifs = api.get_vif_ids(request)
|
||||||
|
|
||||||
|
for vif in vifs:
|
||||||
|
if vif['available']:
|
||||||
|
name = "Instance %s VIF %s" % (str(vif['instance_name']), str(vif['id']))
|
||||||
|
vif_choices.append({
|
||||||
|
'name' : str(name),
|
||||||
|
'id' : str(vif['id'])
|
||||||
|
})
|
||||||
|
|
||||||
|
return vif_choices
|
||||||
|
Loading…
x
Reference in New Issue
Block a user