Commit 841e3e1f by Arnau Comas Codina

Refactored openstackError for shared using along modules

parent 07df1fec
import logging
from openstack.exceptions import (
SDKException, BadRequestException)
class ErrorException(Exception):
""" Base class for exceptions in this module.
Attributes:
OpenStackError -- boolean
DuplicateError -- boolean
"""
def __init__(self, e):
super().__init__(e)
self.OpenStackError = True
self.DuplicateError = False
if hasattr(e, 'details'):
if 'duplicate' in e.details:
self.DuplicateError = True
def OpenStackError(func):
""" Decorator to wrap openstack error in simple Exception.
Return decorated function
"""
def wrap_OpenStackError(*args, **kw):
try:
try:
return func(*args, **kw)
except SDKException as e:
raise ErrorException(e)
except BadRequestException as e:
import pdb; pdb.set_trace()
raise ErrorException(e)
except Exception as e:
raise e
return wrap_OpenStackError
from openstack.exceptions import SDKException from openstack.exceptions import SDKException
from novaclient import client from novaclient import client
import logging
from interface_openstack.implementation.utils.connection import OpenStackConnection from interface_openstack.implementation.utils.connection import OpenStackConnection
from interface_openstack.implementation.utils.decorators import OpenStackError
import secrets import secrets
from interface_openstack.interface.vm.instance import InstanceInterface from interface_openstack.interface.vm.instance import InstanceInterface
from interface_openstack.interface.vm.resources import Instance, Flavor from interface_openstack.interface.vm.resources import Instance, Flavor
from interface_openstack.interface.image.image import Image from interface_openstack.interface.image.image import Image
def openstackError(func):
def wrap_OpenStackError(*args, **kw):
""" Decorator to wrap openstack error in simple Exception.
Return decorated function
"""
try:
return func(*args, **kw)
except SDKException as e:
logging.error(e)
new_e = Exception(e)
new_e.OpenStackError = True
raise new_e
return wrap_OpenStackError
class OSVirtualMachineManager(InstanceInterface, OpenStackConnection): class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
@openstackError @OpenStackError
def _get_openstack_server(self, name_or_id): def _get_openstack_server(self, name_or_id):
if not name_or_id: if not name_or_id:
raise ValueError("Name or id doesn't given") raise ValueError("Name or id doesn't given")
...@@ -35,7 +20,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection): ...@@ -35,7 +20,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
raise ValueError("Could not get the vm") raise ValueError("Could not get the vm")
return server_instance return server_instance
@openstackError @OpenStackError
def create_base_vm(self, name, flavor, networks, block_dev_map): def create_base_vm(self, name, flavor, networks, block_dev_map):
devices = [] devices = []
b_device = block_dev_map.__dict__ b_device = block_dev_map.__dict__
...@@ -49,12 +34,12 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection): ...@@ -49,12 +34,12 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
) )
return self.convert_server_to_instance(new_server) return self.convert_server_to_instance(new_server)
@openstackError @OpenStackError
def create_vm_from_template(self, name, image, flavor, networks): def create_vm_from_template(self, name, image, flavor, networks):
return self.create_multiple_vm_from_template(name, image, flavor, return self.create_multiple_vm_from_template(name, image, flavor,
networks, 1)[0] networks, 1)[0]
@openstackError @OpenStackError
def create_multiple_vm_from_template(self, name, image, flavor, networks, def create_multiple_vm_from_template(self, name, image, flavor, networks,
number, **args): number, **args):
compute = self.openstack.compute compute = self.openstack.compute
...@@ -77,7 +62,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection): ...@@ -77,7 +62,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
return server_ids return server_ids
@openstackError @OpenStackError
def create_flavor(self, name, ram, vcpus, initial_disk): def create_flavor(self, name, ram, vcpus, initial_disk):
flavor = self.openstack.compute.create_flavor(name=name, flavor = self.openstack.compute.create_flavor(name=name,
ram=ram, ram=ram,
...@@ -86,18 +71,18 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection): ...@@ -86,18 +71,18 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
return Flavor(flavor.name, flavor.id, flavor.ram, return Flavor(flavor.name, flavor.id, flavor.ram,
flavor.vcpus, flavor.disk) flavor.vcpus, flavor.disk)
@openstackError @OpenStackError
def get_flavor(self, flavor_id): def get_flavor(self, flavor_id):
flavor = self.openstack.compute.find_flavor(flavor_id) flavor = self.openstack.compute.find_flavor(flavor_id)
return Flavor(flavor.name, flavor.id, flavor.ram, return Flavor(flavor.name, flavor.id, flavor.ram,
flavor.vcpus, flavor.disk) flavor.vcpus, flavor.disk)
@openstackError @OpenStackError
def delete_flavor(self, flavor_id): def delete_flavor(self, flavor_id):
flavor = self.openstack.compute.find_flavor(flavor_id) flavor = self.openstack.compute.find_flavor(flavor_id)
self.openstack.compute.delete_flavor(flavor) self.openstack.compute.delete_flavor(flavor)
@openstackError @OpenStackError
def list_flavors(self): def list_flavors(self):
flavors = [] flavors = []
for flavor in self.openstack.compute.flavors(): for flavor in self.openstack.compute.flavors():
...@@ -105,66 +90,66 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection): ...@@ -105,66 +90,66 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
flavor.vcpus, flavor.disk)) flavor.vcpus, flavor.disk))
return flavors return flavors
@openstackError @OpenStackError
def get_vm(self, name_or_id=None): def get_vm(self, name_or_id=None):
instance = self._get_openstack_server(name_or_id) instance = self._get_openstack_server(name_or_id)
return self.convert_server_to_instance(instance) return self.convert_server_to_instance(instance)
@openstackError @OpenStackError
def start_vm(self, name_or_id=None): def start_vm(self, name_or_id=None):
instance = self._get_openstack_server(name_or_id) instance = self._get_openstack_server(name_or_id)
self.openstack.compute.start_server(instance) self.openstack.compute.start_server(instance)
@openstackError @OpenStackError
def stop_vm(self, name_or_id=None): def stop_vm(self, name_or_id=None):
instance = self._get_openstack_server(name_or_id) instance = self._get_openstack_server(name_or_id)
self.openstack.compute.stop_server(instance) self.openstack.compute.stop_server(instance)
@openstackError @OpenStackError
def suspend_vm(self, name_or_id=None): def suspend_vm(self, name_or_id=None):
instance = self._get_openstack_server(name_or_id) instance = self._get_openstack_server(name_or_id)
self.openstack.compute.suspend_server(instance) self.openstack.compute.suspend_server(instance)
@openstackError @OpenStackError
def wake_up_vm(self, name_or_id=None): def wake_up_vm(self, name_or_id=None):
instance = self._get_openstack_server(name_or_id) instance = self._get_openstack_server(name_or_id)
self.openstack.compute.resume_server(instance) self.openstack.compute.resume_server(instance)
@openstackError @OpenStackError
def reboot_vm(self, name_or_id): def reboot_vm(self, name_or_id):
instance = self._get_openstack_server(name_or_id) instance = self._get_openstack_server(name_or_id)
self.openstack.compute.reboot_server(instance, reboot_type='SOFT') self.openstack.compute.reboot_server(instance, reboot_type='SOFT')
@openstackError @OpenStackError
def reset_vm(self, name_or_id): def reset_vm(self, name_or_id):
instance = self._get_openstack_server(name_or_id) instance = self._get_openstack_server(name_or_id)
self.openstack.compute.reboot_server(instance, reboot_type='HARD') self.openstack.compute.reboot_server(instance, reboot_type='HARD')
@openstackError @OpenStackError
def destroy_vm(self, name_or_id): def destroy_vm(self, name_or_id):
instance = self._get_openstack_server(name_or_id) instance = self._get_openstack_server(name_or_id)
self.openstack.compute.delete_server(instance) self.openstack.compute.delete_server(instance)
@openstackError @OpenStackError
def get_status(self, name_or_id): def get_status(self, name_or_id):
instance = self._get_openstack_server(name_or_id) instance = self._get_openstack_server(name_or_id)
return instance.status return instance.status
@openstackError @OpenStackError
def list_all_vm(self): def list_all_vm(self):
servers = [] servers = []
for server in self.openstack.compute.servers(): for server in self.openstack.compute.servers():
servers.append(self.convert_server_to_instance(server)) servers.append(self.convert_server_to_instance(server))
return servers return servers
@openstackError @OpenStackError
def resize_vm(self, name_or_id, resource): def resize_vm(self, name_or_id, resource):
instance = self._get_openstack_server(name_or_id) instance = self._get_openstack_server(name_or_id)
flavor = self.openstack.compute.find_flavor(resource['name']) flavor = self.openstack.compute.find_flavor(resource['name'])
self.openstack.compute.resize_server(instance, flavor) self.openstack.compute.resize_server(instance, flavor)
@openstackError @OpenStackError
def create_image(self, instance_id, metadata=None): def create_image(self, instance_id, metadata=None):
image_name = secrets.token_hex(30) image_name = secrets.token_hex(30)
self.openstack.compute.create_server_image(instance_id, self.openstack.compute.create_server_image(instance_id,
...@@ -179,13 +164,13 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection): ...@@ -179,13 +164,13 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
instance = nova.servers.get(server_id) instance = nova.servers.get(server_id)
return instance.get_vnc_console("novnc") return instance.get_vnc_console("novnc")
@openstackError @OpenStackError
def attach_volume(self, server_id, volume_id, device=None): def attach_volume(self, server_id, volume_id, device=None):
self.openstack.compute.create_volume_attachment(volumeId=volume_id, self.openstack.compute.create_volume_attachment(volumeId=volume_id,
server=server_id, server=server_id,
) )
@openstackError @OpenStackError
def detach_volume(self, server_id, volume_id, device=None): def detach_volume(self, server_id, volume_id, device=None):
self.openstack.compute.delete_volume_attachment( self.openstack.compute.delete_volume_attachment(
volume_attachment=volume_id, volume_attachment=volume_id,
...@@ -205,7 +190,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection): ...@@ -205,7 +190,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
launched_at=server.launched_at, launched_at=server.launched_at,
terminated_at=server.terminated_at, terminated_at=server.terminated_at,
addresses=server.addresses) addresses=server.addresses)
def os_image_to_rc_image(self, os_image): def os_image_to_rc_image(self, os_image):
return Image( return Image(
os_image.id, os_image.id,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment