Commit f5b818e4 by Bodor Máté

Merge branch 'connection' into DEV

parents dc7e976d a0be9787
......@@ -4,14 +4,10 @@ from openstack.exceptions import ResourceNotFound
from interface_openstack.interface.image.image_manager import ImageManager
from interface_openstack.interface.image.image import Image
from interface_openstack.implementation.utils.connection import OpenStackConnection
class OpenstackImageManager(ImageManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
class OpenstackImageManager(ImageManager, OpenStackConnection):
@staticmethod
def os_image_to_rc_image(os_image) -> Image:
......
......@@ -4,14 +4,10 @@ from openstack.exceptions import ResourceNotFound
from interface_openstack.interface.network.FloatingIP import floating_ip
from interface_openstack.interface.network.FloatingIPManager import floating_ip_manager
from interface_openstack.implementation.utils.connection import OpenStackConnection
class OpenstackFloatingIPManager(FloatingIPManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
class OpenstackFloatingIPManager(FloatingIPManager, OpenStackConnection):
@staticmethod
def os_floating_ip_to_rc_floating_ip(os_floating_ip) -> FloatingIP:
......
......@@ -4,14 +4,10 @@ from openstack.exceptions import ResourceNotFound
from interface_openstack.interface.network.Network import network
from interface_openstack.interface.network.NetworkManager import network_manager
from interface_openstack.implementation.utils.connection import OpenStackConnection
class OpenstackNetworkManager(NetworkManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
class OpenstackNetworkManager(NetworkManager, OpenStackConnection):
@staticmethod
def os_network_to_rc_network(os_network) -> Network:
......
......@@ -4,14 +4,10 @@ from openstack.exceptions import ResourceNotFound
from interface_openstack.interface.network.Port import port
from interface_openstack.interface.network.PortManager import port_manager
from interface_openstack.implementation.utils.connection import OpenStackConnection
class OpenstackPortManager(PortManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
class OpenstackPortManager(PortManager, OpenStackConnection):
@staticmethod
def os_port_to_rc_port(os_port):
......
......@@ -4,14 +4,10 @@ from openstack.exceptions import ResourceNotFound
from interface_openstack.interface.network.Router import router
from interface_openstack.interface.network.RouterManager import router_manager
from interface_openstack.implementation.utils.connection import OpenStackConnection
class OpenstackRouterManager(RouterManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
class OpenstackRouterManager(RouterManager, OpenStackConnection):
@staticmethod
def os_router_to_rc_router(os_router) -> Router:
......
......@@ -4,14 +4,10 @@ from openstack.exceptions import ResourceNotFound
from interface_openstack.interface.network.Subnet import subnet
from interface_openstack.interface.network.SubnetManager import subnet_manager
from interface_openstack.implementation.utils.connection import OpenStackConnection
class OpenstackSubnetManager(SubnetManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
class OpenstackSubnetManager(SubnetManager, OpenStackConnection):
@staticmethod
def os_subnet_to_rc_subnet(os_subnet):
......
......@@ -4,14 +4,10 @@ from openstack.exceptions import ResourceNotFound
from interface_openstack.interface.storage.snapshot import Snapshot
from interface_openstack.interface.storage.snapshot_manager import SnapshotManager
from interface_openstack.implementation.utils.connection import OpenStackConnection
class OpenstackSnapshotManager(SnapshotManager):
def __init__(self, os) -> None:
super().__init__()
self.openstack = os
class OpenstackSnapshotManager(SnapshotManager, OpenStackConnection):
@staticmethod
def os_snapshot_to_rc_snapshot(os_snapshot) -> Snapshot:
......
......@@ -4,14 +4,10 @@ from openstack.exceptions import ResourceNotFound
from interface_openstack.interface.storage.storage_manager import StorageManager
from interface_openstack.interface.storage.volume import Volume
from interface_openstack.implementation.utils.connection import OpenStackConnection
class OpenstackStorageManager(StorageManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
class OpenstackStorageManager(StorageManager, OpenStackConnection):
@staticmethod
def os_volume_to_rc_volume(os_volume) -> Volume:
......
import openstack
from keystoneauth1.identity import v3
from keystoneauth1 import session
class OpenStackConnection(object):
"""Class for handling the connection to the OpenStack Manager.
It creates a connection member variable which holds the connection.
The connection only connects when something called in it."""
def __init__(self, auth):
super(OpenStackConnection, self).__init__()
self.openstack = openstack.connect(auth_url=auth["auth_url"],
username=auth["username"],
password=auth["password"],
project_id=auth["project_id"],
project_name=auth["project_name"],
region_name=auth["region_name"]
)
client_auth = v3.Password(auth_url=auth["auth_url"],
username=auth["username"],
password=auth["password"],
user_domain_id=auth["user_domain_id"],
project_id=auth["project_id"],)
self.client_session = session.Session(auth=client_auth)
from interface.vm.instance import InstanceInterface
from interface.vm.resources import Instance, Flavor
from interface_openstack.interface.vm.instance import InstanceInterface
from interface_openstack.interface.vm.resources import Instance, Flavor
from openstack.exceptions import SDKException
from novaclient import client
import logging
from keystoneauth1.identity import v3
from keystoneauth1 import session
auth = v3.Password(auth_url="http://10.34.0.113/identity/v3",
username="admin",
password="64c7ee341d03844c548c",
user_domain_id='default',
project_id="2db5309f541c4466bc80bc534cf579d7")
sess = session.Session(auth=auth)
from interface_openstack.implementation.utils.connection import OpenStackConnection
def openstackError(func):
......@@ -31,11 +22,7 @@ def openstackError(func):
return wrap_OpenStackError
class OSVirtualMachineManager(InstanceInterface):
def __init__(self, cloud):
super().__init__()
self.openstack = cloud
class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
@openstackError
def _get_openstack_server(self, name_or_id):
......@@ -52,16 +39,18 @@ class OSVirtualMachineManager(InstanceInterface):
b_device = block_dev_map.__dict__
devices.append(b_device)
flavor = self.get_flavor(flavor)
new_server = self.openstack.compute.create_server(name=name,
flavorRef=flavor.id,
networks=networks,
block_device_mapping=devices
)
new_server = self.openstack.compute.create_server(
name=name,
flavorRef=flavor.id,
networks=networks,
block_device_mapping=devices
)
return self.convert_server_to_instance(new_server)
@openstackError
def create_vm_from_template(self, name, image, flavor, networks):
return self.create_multiple_vm_from_template(name, image, flavor, networks, 1)
return self.create_multiple_vm_from_template(name, image, flavor,
networks, 1)
@openstackError
def create_multiple_vm_from_template(self, name, image, flavor, networks,
......@@ -176,7 +165,7 @@ class OSVirtualMachineManager(InstanceInterface):
metadata)
def get_vnc_console(self, server_id):
with client.Client("2", session=sess) as nova:
with client.Client("2", session=self.client_session) as nova:
if server_id:
instance = nova.servers.get(server_id)
return instance.get_vnc_console("novnc")
......
......@@ -13,7 +13,8 @@ class InstanceInterface:
def create_vm_from_template(self, name, image, flavor, networks):
raise NotImplementedError
def create_multiple_vm_from_template(self, image, flavor, networks, number):
def create_multiple_vm_from_template(self, image, flavor,
networks, number):
raise NotImplementedError
def get_vm(self, name_or_id):
......
......@@ -42,12 +42,12 @@ class BlockDeviceMapping:
def __init__(self, boot_index, uuid, source_type, volume_size,
destination_type, delete_on_termination, disk_bus):
self.boot_index = boot_index
self.uuid=uuid
self.source_type=source_type
self.volume_size=volume_size
self.destination_type=destination_type
self.delete_on_termination=delete_on_termination
self.disk_bus=disk_bus
self.uuid = uuid
self.source_type = source_type
self.volume_size = volume_size
self.destination_type = destination_type
self.delete_on_termination = delete_on_termination
self.disk_bus = disk_bus
def JSON(self):
return json.dumps(self.__dict__)
# This file is for testing the openstack api access
if __name__ == "__main__":
from sys import path
from os.path import dirname as dir
path.append(dir(path[0]))
from implementation.vm.instance import OSVirtualMachineManager
from interface.vm.resources import BlockDeviceMapping, Flavor
import openstack
from implementation.network.OpenstackFloatingIPManager import OpenstackFloatingIPManager
from implementation.network.OpenstackNetworkManager import OpenstackNetworkManager
from implementation.network.OpenstackPortManager import OpenstackPortManager
from implementation.network.OpenstackRouterManager import OpenstackRouterManager
from implementation.network.OpenstackSubnetManager import OpenstackSubnetManager
# from implementation.network.OpenstackFloatingIPManager import OpenstackFloatingIPManager
# from implementation.network.OpenstackNetworkManager import OpenstackNetworkManager
# from implementation.network.OpenstackPortManager import OpenstackPortManager
# from implementation.network.OpenstackRouterManager import OpenstackRouterManager
# from implementation.network.OpenstackSubnetManager import OpenstackSubnetManager
# openstack.enable_logging(debug=True)
conn = openstack.connect(cloud='openstack')
auth = {
"auth_url": "http://10.34.0.113/identity/v3",
"username": "admin",
"project_id": "2db5309f541c4466bc80bc534cf579d7",
"project_name": "admin",
"password": "64c7ee341d03844c548c",
"user_domain_name": "Default",
"user_domain_id": "Default",
"region_name": "RegionOne",
}
interface = OSVirtualMachineManager(auth=auth)
block_dev_map = BlockDeviceMapping(
boot_index=0,
......@@ -23,7 +38,7 @@ block_dev_map = BlockDeviceMapping(
)
networks = [{"uuid": "c03d0d4b-413e-4cc6-9ebe-c0b5ca0dac3a"}]
interface = OSVirtualMachineManager(conn)
print('#'*40)
# interface.create_vm_from_template('new_server', resource, "cirros-0.4.0-x86_64-disk")
......
import unittest
from interface.image.Image import Image
class TestImage(unittest.TestCase):
def test_fields(self):
image = Image('id', 'name', 'format')
self.assertEqual(image.id, 'id')
self.assertEqual(image.name, 'name')
self.assertEqual(image.format, 'format')
if __name__ == '__main__':
unittest.main()
import unittest
from interface.storage.Snapshot import Snapshot
class TestSnapshot(unittest.TestCase):
def test_fields(self):
snapshot = Snapshot('id', 'volume_id', 1, 'status', 'created_at')
self.assertEqual(snapshot.id, 'id')
self.assertEqual(snapshot.volume_id, 'volume_id')
self.assertEqual(snapshot.size, 1)
self.assertEqual(snapshot.status, 'status')
self.assertEqual(snapshot.created_at, 'created_at')
if __name__ == '__main__':
unittest.main()
import unittest
from unittest.mock import MagicMock
from unittest.mock import MagicMock, create_autospec
from implementation.vm.instance import OSVirtualMachineManager
from interface.vm.resources import Instance
import openstack
servers = [
MagicMock(
......@@ -9,7 +9,7 @@ servers = [
name="test1",
flavorRef="flav1",
imageRef="image1",
networks= [{"uuid": "network1"}],
networks=[{"uuid": "network1"}],
terminated_at="20200320-15-31",
launched_at="20190510-13-22",
disks=["disk1"],
......@@ -28,6 +28,8 @@ class MockOpenStackCompute(MagicMock):
self.compute.wait_for_server = MagicMock(return_value=True)
self.compute.list_servers = MagicMock(return_value=servers)
self.compute.get_server = create_autospec()
class InstanceCreateTestCase(unittest.TestCase):
def setUp(self):
......@@ -40,16 +42,22 @@ class InstanceCreateTestCase(unittest.TestCase):
pass
def test_create_from_template(self):
instance = self.manager.create_vm_from_template('test', 'imageid', 'flavorid',
['networkid1'])
instance = self.manager.create_vm_from_template('test1',
'imageid',
'flavorid',
['networkid1'])
self.conn.compute.create_server.assert_called()
self.assertEqual(instance.name, servers[0].name)
def test_create_from_template_params(self):
self.manager.create_vm_from_template('test', 'imageid', 'flavorid',
['networkid1'])
self.conn.compute.create_server.assert_called_with(
name='test',
flavorRef="uuid1",
imageRef="uuid2",
networks=['networkid1'],
min_count=1)
name='test',
flavorRef="uuid1",
imageRef="uuid2",
networks=['networkid1'],
min_count=1)
def test_suspend(self):
self.compute.suspend_server()
import unittest
from interface.storage.Volume import Volume
class TestVolume(unittest.TestCase):
def test_fields(self):
volume = Volume('id', 'image_id', 1, False, 'status', 'created_at')
self.assertEqual(volume.id, 'id')
self.assertEqual(volume.image_id, 'image_id')
self.assertEqual(volume.size, 1)
self.assertEqual(volume.bootable, False)
self.assertEqual(volume.status, 'status')
self.assertEqual(volume.created_at, 'created_at')
if __name__ == '__main__':
unittest.main()
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