Refactored some minor issues; Cleanup of exception handling in backends

This commit is contained in:
tmetsch 2012-10-31 10:43:46 +01:00
parent 5f12796b60
commit 6f618e8fa9
10 changed files with 68 additions and 79 deletions

View File

@ -79,7 +79,7 @@ class NetworkInterfaceBackend(backend.KindBackend):
"""
if link.target.identifier == '/network/public':
# public means floating IP in OS!
address = net.add_floating_ip_to_vm(link.source.attributes[
address = net.add_floating_ip(link.source.attributes[
'occi.core.id'],
extras['nova_ctx'])
link.attributes['occi.networkinterface.interface'] = 'eth0'

View File

@ -134,7 +134,7 @@ class SecurityRuleBackend(backend.KindBackend):
"""
try:
context = extras['nova_ctx']
rule = security.get_rule(entity.attributes['occi.core.id'],
rule = security.retrieve_rule(entity.attributes['occi.core.id'],
context)
security.remove_rule(rule, context)
@ -146,8 +146,6 @@ def make_sec_rule(entity, sec_grp_id):
"""
Create and validate the security rule.
"""
# TODO: add some checks for missing attributes!
name = random.randrange(0, 99999999)
sg_rule = {'id': name,
'parent_group_id': sec_grp_id}

View File

@ -34,10 +34,10 @@ from occi_os_api.nova_glue import vm
NETWORK_API = network.API()
COMPUTE_API = compute.API()
LOG = logging.getLogger('nova.occiosapi.wsgi.occi.nova_glue.net')
LOG = logging.getLogger(__name__)
def get_adapter_info(uid, context):
def get_network_details(uid, context):
"""
Extracts the VMs network adapter information.
@ -74,7 +74,7 @@ def get_adapter_info(uid, context):
return result
def add_floating_ip_to_vm(uid, context):
def add_floating_ip(uid, context):
"""
Adds an ip to an VM instance.
@ -104,9 +104,6 @@ def add_floating_ip_to_vm(uid, context):
except exception.NoFloatingIpInterface:
msg = 'l3driver call to add floating ip failed'
raise AttributeError(msg)
except Exception as error:
msg = 'Error. Unable to associate floating ip: ' + str(error)
raise AttributeError(msg)
return float_address
@ -120,7 +117,9 @@ def remove_floating_ip(uid, address, context):
"""
vm_instance = vm.get_vm(uid, context)
# TODO: check exception handling!
NETWORK_API.disassociate_floating_ip(context, vm_instance, address)
NETWORK_API.release_floating_ip(context, address)
try:
NETWORK_API.disassociate_floating_ip(context, vm_instance, address)
NETWORK_API.release_floating_ip(context, address)
except exception.FloatingIpNotAssociated:
raise AttributeError('Unable to disassociate an unassociated '
'floating up!')

View File

@ -20,17 +20,18 @@
Security related 'glue'
"""
# L8R: Check exception handling of this routines!
from nova import compute
from nova import db
from nova.flags import FLAGS
from nova.openstack.common import importutils
# connect to nova
from occi import exceptions
# connect to nova
COMPUTE_API = compute.API()
# SEC_HANDLER = utils.import_object(FLAGS.security_group_handler)
SEC_HANDLER = importutils.import_object(FLAGS.security_group_handler)
@ -102,8 +103,10 @@ def create_rule(rule, context):
rule -- The rule.
context -- The os context.
"""
# TODO: check exception handling!
db.security_group_rule_create(context, rule)
try:
db.security_group_rule_create(context, rule)
except Exception as err:
raise AttributeError('Unable to create rule: ' + str(err))
def remove_rule(rule, context):
@ -113,21 +116,17 @@ def remove_rule(rule, context):
rule -- The rule
context -- The os context.
"""
# TODO: check exception handling!
group_id = rule['parent_group_id']
# TODO(dizz): method seems to be gone!
# self.compute_api.ensure_default_security_group(extras['nova_ctx'])
security_group = db.security_group_get(context, group_id)
db.security_group_rule_destroy(context, rule['id'])
SEC_HANDLER.trigger_security_group_rule_destroy_refresh(context,
[rule['id']])
# TODO: method is one!
#COMPUTE_API.trigger_security_group_rules_refresh(context,
# security_group['id'])
try:
db.security_group_rule_destroy(context, rule['id'])
SEC_HANDLER.trigger_security_group_rule_destroy_refresh(context,
[rule['id']])
except Exception as err:
raise AttributeError('Unable to remove rule: ' + str(err))
def get_rule(uid, context):
def retrieve_rule(uid, context):
"""
Retrieve a rule.
@ -135,8 +134,7 @@ def get_rule(uid, context):
context -- The os context.
"""
try:
rule = db.security_group_rule_get(context,
return db.security_group_rule_get(context,
int(uid))
except Exception:
raise exceptions.HTTPError(404, 'Rule not found!')
return rule

View File

@ -26,6 +26,8 @@ from nova import exception
from nova import volume
from nova.image import glance
from cinder import exception as cinder_ex
from occi import exceptions
# Connection to the nova APIs
@ -45,8 +47,7 @@ def create_storage(size, context, name=None, description=None):
name -- defaults to a random number if needed.
description -- defaults to the name
"""
# TODO: exception handling!
# TODO(dizz): A blueprint?
# L8R: A blueprint?
# OpenStack deals with size in terms of integer.
# Need to convert float to integer for now and only if the float
# can be losslessly converted to integer
@ -67,11 +68,15 @@ def create_storage(size, context, name=None, description=None):
else:
disp_descr = disp_name
new_volume = VOLUME_API.create(context,
size,
disp_name,
disp_descr)
return new_volume
try:
return VOLUME_API.create(context,
size,
disp_name,
disp_descr)
except cinder_ex.VolumeSizeExceedsAvailableQuota:
raise AttributeError('The volume size quota has been reached!')
except cinder_ex.VolumeLimitExceeded:
raise AttributeError('The # of volumes quota has been reached!')
def delete_storage_instance(uid, context):
@ -81,9 +86,11 @@ def delete_storage_instance(uid, context):
uid -- Id of the volume.
context -- The os context.
"""
# TODO: exception handling!
instance = get_storage(uid, context)
VOLUME_API.delete(context, instance)
try:
instance = get_storage(uid, context)
VOLUME_API.delete(context, instance)
except cinder_ex.InvalidVolume:
raise AttributeError('Volume is in wrong state or still attached!')
def snapshot_storage_instance(uid, name, description, context):
@ -93,9 +100,11 @@ def snapshot_storage_instance(uid, name, description, context):
uid -- Id of the volume.
context -- The os context.
"""
# TODO: exception handling!
instance = get_storage(uid, context)
VOLUME_API.create_snapshot(context, instance, name, description)
try:
instance = get_storage(uid, context)
VOLUME_API.create_snapshot(context, instance, name, description)
except cinder_ex.InvalidVolume:
raise AttributeError('Volume is in wrong state!')
def get_image(uid, context):
@ -148,7 +157,7 @@ def get_storage(uid, context):
return instance
def get_storages(context):
def get_storage_volumes(context):
"""
Retrieve all storage entities from user.
"""

View File

@ -266,8 +266,8 @@ def stop_vm(uid, context):
instance = get_vm(uid, context)
try:
# TODO(dizz): There are issues with the stop and start methods of
# OS. For now we'll use suspend.
# There are issues with the stop and start methods of OS. For now
# we'll use suspend.
# self.compute_api.stop(context, instance)
COMPUTE_API.suspend(context, instance)
except Exception as error:
@ -300,8 +300,6 @@ def restart_vm(uid, method, context):
COMPUTE_API.reboot(context, instance, reboot_type)
except exception.InstanceInvalidState:
raise exceptions.HTTPError(406, 'VM is in an invalid state.')
except Exception as error:
raise exceptions.HTTPError(500, 'Error in reboot %s' % error)
def attach_volume(instance_id, volume_id, mount_point, context):
@ -313,23 +311,19 @@ def attach_volume(instance_id, volume_id, mount_point, context):
mount_point -- Where to mount.
context -- The os security context.
"""
# TODO: check exception handling!
instance = get_vm(instance_id, context)
try:
vol_instance = VOLUME_API.get(context, volume_id)
except exception.NotFound:
raise exceptions.HTTPError(404, 'Volume not found!')
volume_id = vol_instance['id']
try:
volume_id = vol_instance['id']
COMPUTE_API.attach_volume(
context,
instance,
volume_id,
mount_point)
except Exception as error:
LOG.error(str(error))
raise error
except exception.NotFound:
raise exceptions.HTTPError(404, 'Volume not found!')
except exception.InvalidDevicePath:
raise AttributeError('Invalid device path!')
def detach_volume(volume_id, context):
@ -339,18 +333,12 @@ def detach_volume(volume_id, context):
volume_id -- Id of the volume.
context -- the os context.
"""
#try:
# instance = VOLUME_API.get(context, volume_id)
#except exception.NotFound:
# raise exceptions.HTTPError(404, 'Volume not found!')
#volume_id = instance['id']
try:
#TODO(dizz): see issue #15
COMPUTE_API.detach_volume(context, volume_id)
except Exception as error:
LOG.error(str(error) + '; with id: ' + volume_id)
raise error
except exception.InvalidVolume:
raise AttributeError('Invalid volume!')
except exception.VolumeUnattached:
raise AttributeError('Volume is not attached!')
def set_password_for_vm(uid, password, context):
@ -371,7 +359,7 @@ def set_password_for_vm(uid, password, context):
def get_vnc(uid, context):
"""
Retrieve VNC console.
Retrieve VNC console or None is unavailable.
uid -- id of the instance
context -- the os context
@ -442,7 +430,6 @@ def get_occi_state(uid, context):
state = 'inactive'
# Some task states require a state
# TODO: check for others!
if instance['vm_state'] in [task_states.IMAGE_SNAPSHOT]:
state = 'inactive'
actions = []

View File

@ -127,7 +127,7 @@ class OCCIRegistry(occi_registry.NonePersistentRegistry):
vms = vm.get_vms(context)
vm_res_ids = [item['uuid'] for item in vms]
stors = storage.get_storages(context)
stors = storage.get_storage_volumes(context)
stor_res_ids = [item['id'] for item in stors]
if (key, context.user_id) in self.cache:
@ -205,7 +205,7 @@ class OCCIRegistry(occi_registry.NonePersistentRegistry):
vms = vm.get_vms(context)
vm_res_ids = [item['uuid'] for item in vms]
stors = storage.get_storages(context)
stors = storage.get_storage_volumes(context)
stor_res_ids = [item['id'] for item in stors]
for item in self.cache.values():
@ -301,7 +301,7 @@ class OCCIRegistry(occi_registry.NonePersistentRegistry):
entity.mixins.append(os_tmp)
# 3. network links & get links from cache!
net_links = net.get_adapter_info(identifier, context)
net_links = net.get_network_details(identifier, context)
for item in net_links['public']:
link = self._construct_network_link(item, entity, self.pub_net,
extras)

View File

@ -328,7 +328,6 @@ class SystemTest(unittest.TestCase):
"""
Test attaching and detaching storage volumes + snapshotting etc.
"""
# TODO: test snapshot!
# create new VM
cats = ['m1.tiny; scheme="http://schemas.openstack'
@ -350,11 +349,11 @@ class SystemTest(unittest.TestCase):
LOG.debug(get_node(self.token, vol_location)['x-occi-attribute'])
# snapshot volume
# snapshot will work - but than deletion of volume is impossible :-/
#trigger_action(self.token, vol_location +
# '?action=snapshot',
# 'snapshot; scheme="http://schemas.ogf'
# '.org/occi/infrastructure/storage/action#"')
# not doing above - since than I cannot delete the volume later...
# link volume and compute
cats = ['storagelink; scheme="http://schemas.ogf'

View File

@ -25,7 +25,6 @@ Test network resource backend.
import mox
import unittest
from occi import core_model
from occi.extensions import infrastructure
from occi_os_api import nova_glue
from occi_os_api.backends import network
@ -90,8 +89,8 @@ class TestNetworkInterfaceBackend(unittest.TestCase):
link = core_model.Link('foo', None, [], source, target)
self.mox.StubOutWithMock(nova_glue.net, 'add_floating_ip_to_vm')
nova_glue.net.add_floating_ip_to_vm(mox.IsA(object),
self.mox.StubOutWithMock(nova_glue.net, 'add_floating_ip')
nova_glue.net.add_floating_ip(mox.IsA(object),
mox.IsA(object)).AndReturn('10.0.0.1')
self.mox.ReplayAll()

View File

@ -20,7 +20,7 @@
Test network resource backend.
"""
#pylint: disable=W0102,C0103,R0904
#pylint: disable=W0102,C0103,R0904,R0801
import mox