mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-28 11:18:11 +00:00
VPCS Mac address / project
This commit is contained in:
parent
72c6182062
commit
2c50bb607f
@ -31,15 +31,16 @@ class VPCS(BaseManager):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self._free_mac_ids = list(range(0, 255))
|
self._free_mac_ids = {}
|
||||||
self._used_mac_ids = {}
|
self._used_mac_ids = {}
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def create_vm(self, *args, **kwargs):
|
def create_vm(self, *args, **kwargs):
|
||||||
|
|
||||||
vm = yield from super().create_vm(*args, **kwargs)
|
vm = yield from super().create_vm(*args, **kwargs)
|
||||||
|
self._free_mac_ids.setdefault(vm.project.uuid, list(range(0, 255)))
|
||||||
try:
|
try:
|
||||||
self._used_mac_ids[vm.uuid] = self._free_mac_ids.pop(0)
|
self._used_mac_ids[vm.uuid] = self._free_mac_ids[vm.project.uuid].pop(0)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
raise VPCSError("No mac address available")
|
raise VPCSError("No mac address available")
|
||||||
return vm
|
return vm
|
||||||
@ -47,8 +48,9 @@ class VPCS(BaseManager):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def delete_vm(self, uuid, *args, **kwargs):
|
def delete_vm(self, uuid, *args, **kwargs):
|
||||||
|
|
||||||
|
vm = self.get_vm(uuid)
|
||||||
i = self._used_mac_ids[uuid]
|
i = self._used_mac_ids[uuid]
|
||||||
self._free_mac_ids.insert(0, i)
|
self._free_mac_ids[vm.project.uuid].insert(0, i)
|
||||||
del self._used_mac_ids[uuid]
|
del self._used_mac_ids[uuid]
|
||||||
yield from super().delete_vm(uuid, *args, **kwargs)
|
yield from super().delete_vm(uuid, *args, **kwargs)
|
||||||
|
|
||||||
|
@ -44,6 +44,24 @@ def test_get_mac_id(loop, project, port_manager):
|
|||||||
assert vpcs.get_mac_id(vm3_uuid) == 0
|
assert vpcs.get_mac_id(vm3_uuid) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_mac_id_multiple_project(loop, port_manager):
|
||||||
|
# Cleanup the VPCS object
|
||||||
|
VPCS._instance = None
|
||||||
|
vpcs = VPCS.instance()
|
||||||
|
vpcs.port_manager = port_manager
|
||||||
|
vm1_uuid = str(uuid.uuid4())
|
||||||
|
vm2_uuid = str(uuid.uuid4())
|
||||||
|
vm3_uuid = str(uuid.uuid4())
|
||||||
|
project1 = ProjectManager.instance().create_project()
|
||||||
|
project2 = ProjectManager.instance().create_project()
|
||||||
|
loop.run_until_complete(vpcs.create_vm("PC 1", project1.uuid, vm1_uuid))
|
||||||
|
loop.run_until_complete(vpcs.create_vm("PC 2", project1.uuid, vm2_uuid))
|
||||||
|
loop.run_until_complete(vpcs.create_vm("PC 2", project2.uuid, vm3_uuid))
|
||||||
|
assert vpcs.get_mac_id(vm1_uuid) == 0
|
||||||
|
assert vpcs.get_mac_id(vm2_uuid) == 1
|
||||||
|
assert vpcs.get_mac_id(vm3_uuid) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_get_mac_id_no_id_available(loop, project, port_manager):
|
def test_get_mac_id_no_id_available(loop, project, port_manager):
|
||||||
# Cleanup the VPCS object
|
# Cleanup the VPCS object
|
||||||
VPCS._instance = None
|
VPCS._instance = None
|
||||||
|
Loading…
Reference in New Issue
Block a user