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:
Arvind Somya 2011-08-23 13:29:27 -04:00
parent a0d3bfa62c
commit 0462724d98
3 changed files with 94 additions and 79 deletions

View File

@ -47,10 +47,9 @@ import openstackx.admin
import openstackx.api.exceptions as api_exceptions
import openstackx.extras
import openstackx.auth
from quantum.client import Client
import quantum.client
from urlparse import urlparse
LOG = logging.getLogger('django_openstack.api')
@ -163,7 +162,7 @@ class Server(APIResourceWrapper):
"""
_attrs = ['addresses', 'attrs', 'hostId', 'id', 'image', 'links',
'metadata', 'name', 'private_ip', 'public_ip', 'status', 'uuid',
'image_name', 'virtual_interfaces']
'image_name', 'VirtualInterfaces']
def __init__(self, apiresource, request):
super(Server, self).__init__(apiresource)
@ -241,11 +240,17 @@ class SwiftAuthentication(object):
def authenticate(self):
return (self.storage_url, '', self.auth_token)
class ServiceCatalogException(api_exceptions.ApiException):
def __init__(self, service_name):
message = 'Invalid service catalog service: %s' % service_name
super(ServiceCatalogException, self).__init__(404, message)
class VirtualInterface(APIResourceWrapper):
_attrs = ['id','mac_address']
def url_for(request, service_name, admin=False):
catalog = request.user.service_catalog
try:
@ -347,7 +352,7 @@ def swift_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')
@ -651,7 +656,6 @@ def swift_get_object_data(request, container_name, object_name):
def get_vif_ids(request):
vifs = []
attached_vifs = []
# Get a list of all networks
networks_list = quantum_api(request).list_networks()
for network in networks_list['networks']:
@ -665,28 +669,23 @@ def get_vif_ids(request):
instances = server_list(request)
# Get virtual interface ids by instance
for instance in instances:
instance_vifs = instance.virtual_interfaces
instance_vifs = extras_api(request).virtual_interfaces.list(instance.id)
for vif in instance_vifs:
# 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({
'id' : vif['id'],
'id' : vif.id,
'instance' : instance.id,
'instance_name' : instance.name,
'available' : False,
'network_id' : vif['network']['id'],
'network_name' : vif['network']['label']
'available' : False
})
else:
vifs.append({
'id' : vif['id'],
'id' : vif.id,
'instance' : instance.id,
'instance_name' : instance.name,
'available' : True,
'network_id' : vif['network']['id'],
'network_name' : vif['network']['label']
'available' : True
})
return vifs
class GlobalSummary(object):

View File

@ -42,7 +42,7 @@ from django_openstack.dash.views.ports import TogglePort
import warnings
LOG = logging.getLogger('django_openstack.dash')
LOG = logging.getLogger('django_openstack.dash.views.networks')
class CreateNetwork(forms.SelfHandlingForm):
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()
details = []
for network in networks_list['networks']:
# 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
net_stats = _calc_network_stats(request, tenant_id, network['id'])
# Get network details like name and id
details = api.quantum_api(request).show_network_details(network['id'])
networks.append({
'name' : details['network']['name'],
'id' : network['id'],
'total' : total,
'available' : available,
'used' : used,
'total' : net_stats['total'],
'available' : net_stats['available'],
'used' : net_stats['used'],
'tenant' : tenant_id
})
@ -171,41 +156,12 @@ def detail(request, tenant_id, network_id):
toggle_port_form, port_toggle_handled = TogglePort.maybe_handle(request)
network = {}
network_ports = []
try:
network_details = api.quantum_api(request).show_network_details(network_id)
network['name'] = network_details['network']['name']
network['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
network['ports'] = _get_port_states(request, tenant_id, network_id)
except Exception, e:
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,
'rename_form' : rename_form
}, 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 }

View File

@ -35,7 +35,7 @@ from django_openstack import forms
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):
@ -143,7 +143,7 @@ class TogglePort(forms.SelfHandlingForm):
def create(request, tenant_id, network_id):
create_form, handled = CreatePort.maybe_handle(request)
if (handled):
if handled:
return shortcuts.redirect(
'dash_networks_detail',
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)
# Get all avaliable vifs
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'])
})
vifs = _get_available_vifs(request)
return shortcuts.render_to_response('dash_port_attach.html', {
'network' : network_id,
'port' : port_id,
'attach_form' : attach_form,
'vifs' : VIF_CHOICES,
'vifs' : vifs,
}, 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