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 novaclient import client
import logging
from interface_openstack.implementation.utils.connection import OpenStackConnection
from interface_openstack.implementation.utils.decorators import OpenStackError
import secrets
from interface_openstack.interface.vm.instance import InstanceInterface
from interface_openstack.interface.vm.resources import Instance, Flavor
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):
@openstackError
@OpenStackError
def _get_openstack_server(self, name_or_id):
if not name_or_id:
raise ValueError("Name or id doesn't given")
......@@ -35,7 +20,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
raise ValueError("Could not get the vm")
return server_instance
@openstackError
@OpenStackError
def create_base_vm(self, name, flavor, networks, block_dev_map):
devices = []
b_device = block_dev_map.__dict__
......@@ -49,12 +34,12 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
)
return self.convert_server_to_instance(new_server)
@openstackError
@OpenStackError
def create_vm_from_template(self, name, image, flavor, networks):
return self.create_multiple_vm_from_template(name, image, flavor,
networks, 1)[0]
@openstackError
@OpenStackError
def create_multiple_vm_from_template(self, name, image, flavor, networks,
number, **args):
compute = self.openstack.compute
......@@ -77,7 +62,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
return server_ids
@openstackError
@OpenStackError
def create_flavor(self, name, ram, vcpus, initial_disk):
flavor = self.openstack.compute.create_flavor(name=name,
ram=ram,
......@@ -86,18 +71,18 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
return Flavor(flavor.name, flavor.id, flavor.ram,
flavor.vcpus, flavor.disk)
@openstackError
@OpenStackError
def get_flavor(self, flavor_id):
flavor = self.openstack.compute.find_flavor(flavor_id)
return Flavor(flavor.name, flavor.id, flavor.ram,
flavor.vcpus, flavor.disk)
@openstackError
@OpenStackError
def delete_flavor(self, flavor_id):
flavor = self.openstack.compute.find_flavor(flavor_id)
self.openstack.compute.delete_flavor(flavor)
@openstackError
@OpenStackError
def list_flavors(self):
flavors = []
for flavor in self.openstack.compute.flavors():
......@@ -105,66 +90,66 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
flavor.vcpus, flavor.disk))
return flavors
@openstackError
@OpenStackError
def get_vm(self, name_or_id=None):
instance = self._get_openstack_server(name_or_id)
return self.convert_server_to_instance(instance)
@openstackError
@OpenStackError
def start_vm(self, name_or_id=None):
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.start_server(instance)
@openstackError
@OpenStackError
def stop_vm(self, name_or_id=None):
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.stop_server(instance)
@openstackError
@OpenStackError
def suspend_vm(self, name_or_id=None):
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.suspend_server(instance)
@openstackError
@OpenStackError
def wake_up_vm(self, name_or_id=None):
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.resume_server(instance)
@openstackError
@OpenStackError
def reboot_vm(self, name_or_id):
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.reboot_server(instance, reboot_type='SOFT')
@openstackError
@OpenStackError
def reset_vm(self, name_or_id):
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.reboot_server(instance, reboot_type='HARD')
@openstackError
@OpenStackError
def destroy_vm(self, name_or_id):
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.delete_server(instance)
@openstackError
@OpenStackError
def get_status(self, name_or_id):
instance = self._get_openstack_server(name_or_id)
return instance.status
@openstackError
@OpenStackError
def list_all_vm(self):
servers = []
for server in self.openstack.compute.servers():
servers.append(self.convert_server_to_instance(server))
return servers
@openstackError
@OpenStackError
def resize_vm(self, name_or_id, resource):
instance = self._get_openstack_server(name_or_id)
flavor = self.openstack.compute.find_flavor(resource['name'])
self.openstack.compute.resize_server(instance, flavor)
@openstackError
@OpenStackError
def create_image(self, instance_id, metadata=None):
image_name = secrets.token_hex(30)
self.openstack.compute.create_server_image(instance_id,
......@@ -179,13 +164,13 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
instance = nova.servers.get(server_id)
return instance.get_vnc_console("novnc")
@openstackError
@OpenStackError
def attach_volume(self, server_id, volume_id, device=None):
self.openstack.compute.create_volume_attachment(volumeId=volume_id,
server=server_id,
)
@openstackError
@OpenStackError
def detach_volume(self, server_id, volume_id, device=None):
self.openstack.compute.delete_volume_attachment(
volume_attachment=volume_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