Commit d0137d0e by Belákovics Ádám

Fix get_vm method

parent 8edd7811
Pipeline #737 passed with stage
in 40 seconds
from interface_openstack.interface.vm.instance import InstanceInterface
from interface_openstack.interface.vm.resources import Instance, Flavor
from interface.vm.instance import InstanceInterface
from interface.vm.resources import Instance, Flavor
from openstack.exceptions import SDKException
from novaclient import client
import logging
......@@ -38,6 +38,15 @@ class OSVirtualMachineManager(InstanceInterface):
self.openstack = cloud
@openstackError
def _get_openstack_server(self, name_or_id):
if not name_or_id:
raise ValueError("Name or id doesn't given")
server_instance = self.openstack.compute.get_server(name_or_id)
if not server_instance:
raise ValueError("Could not get the vm")
return server_instance
@openstackError
def create_base_vm(self, name, flavor, networks, block_dev_map):
devices = []
b_device = block_dev_map.__dict__
......@@ -102,59 +111,47 @@ class OSVirtualMachineManager(InstanceInterface):
@openstackError
def get_vm(self, name_or_id=None):
if not name_or_id:
raise ValueError("Name or id doesn't given")
server_instance = self.openstack.compute.get_server(name_or_id)
if not server_instance:
raise ValueError("Could not get the vm")
return self.convert_server_to_instance(server_instance)
instance = self._get_openstack_server(name_or_id)
return self.convert_server_to_instance(instance)
@openstackError
def start_vm(self, name_or_id=None):
if name_or_id:
instance = self.openstack.compute.get_server(name_or_id)
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.start_server(instance)
@openstackError
def stop_vm(self, name_or_id=None):
if name_or_id:
instance = self.openstack.compute.get_server(name_or_id)
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.stop_server(instance)
@openstackError
def suspend_vm(self, name_or_id=None):
if name_or_id:
instance = self.openstack.compute.get_server(name_or_id)
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.suspend_server(instance)
@openstackError
def wake_up_vm(self, name_or_id=None):
if name_or_id:
instance = self.openstack.compute.get_server(name_or_id)
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.resume_server(instance)
@openstackError
def reboot_vm(self, name_or_id):
if name_or_id:
instance = self.openstack.compute.get_server(name_or_id)
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.reboot_server(instance, reboot_type='SOFT')
@openstackError
def reset_vm(self, name_or_id):
if name_or_id:
instance = self.openstack.compute.get_server(name_or_id)
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.reboot_server(instance, reboot_type='HARD')
@openstackError
def destroy_vm(self, name_or_id):
if name_or_id:
instance = self.openstack.compute.get_server(name_or_id)
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.delete_server(instance)
@openstackError
def get_status(self, name_or_id):
if name_or_id:
instance = self.openstack.compute.get_server(name_or_id)
instance = self._get_openstack_server(name_or_id)
return instance.status
@openstackError
......@@ -166,16 +163,14 @@ class OSVirtualMachineManager(InstanceInterface):
@openstackError
def resize_vm(self, name_or_id, resource):
if name_or_id:
instance = self.openstack.get_server(name_or_id)
instance = self._get_openstack_server(name_or_id)
flavor = self.openstack.compute.find_flavor(resource['name'])
self.openstack.compute.resize_server(instance, flavor)
@openstackError
def create_template(self, name_or_id, template_name, metadata=None):
if name_or_id:
instance = self.openstack.compute.get_server(name_or_id)
instance = self._get_openstack_server(name_or_id)
self.openstack.compute.create_server_image(instance,
template_name,
metadata)
......
......@@ -40,8 +40,13 @@ for vm in interface.list_all_vm():
print(vm.JSON())
print("*" * 50)
print(interface.create_base_vm("BaseTest", "2", networks, block_dev_map).JSON())
# print(interface.create_base_vm("BaseTest", "2", networks, block_dev_map).JSON())
print("*********TEST SERVER**********")
print(interface.get_vm("fb91a363-e23b-4fb6-8770-357509b753da").JSON())
print("*********TEST SUSPEND**********")
interface.suspend_vm("fb91a363-e23b-4fb6-8770-357509b753da")
# print(interface.convert_server_to_instance(server).JSON())
......@@ -128,9 +133,9 @@ print('Floating IP:')
print('\tFloating IP: %s' % floating_ip)
# print(router_manager.add_interface(router.id, inner_subnet.id)) # TODO: fix error msg
# print(router_manager.add_gateway(router.id, public_network.id)) # TODO: fix it!
print(router_manager.add_interface(router.id, inner_subnet.id)) # TODO: fix error msg
print(router_manager.add_gateway(router.id, public_network.id)) # TODO: fix it!
# print(floating_ip_manager.update(floating_ip.id, {
# 'port_id': vm_port.id
# }))
print(floating_ip_manager.update(floating_ip.id, {
'port_id': vm_port.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