mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-24 17:28:08 +00:00
Support for link event, fix link not correctly deleted
This commit is contained in:
parent
fa0af7f4a2
commit
54747ee618
@ -208,6 +208,9 @@ The available notification are:
|
|||||||
* node.created
|
* node.created
|
||||||
* node.updated
|
* node.updated
|
||||||
* node.deleted
|
* node.deleted
|
||||||
|
* link.created
|
||||||
|
* link.updated
|
||||||
|
* link.deleted
|
||||||
* log.error
|
* log.error
|
||||||
* log.warning
|
* log.warning
|
||||||
* log.info
|
* log.info
|
||||||
|
@ -223,7 +223,6 @@ class BaseNode:
|
|||||||
log.info("{module}: {name} [{id}] created".format(module=self.manager.module_name,
|
log.info("{module}: {name} [{id}] created".format(module=self.manager.module_name,
|
||||||
name=self.name,
|
name=self.name,
|
||||||
id=self.id))
|
id=self.id))
|
||||||
self._project.emit("node.created", self)
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def delete(self):
|
def delete(self):
|
||||||
@ -231,7 +230,6 @@ class BaseNode:
|
|||||||
Delete the node (including all its files).
|
Delete the node (including all its files).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self._project.emit("node.deleted", self)
|
|
||||||
directory = self.project.node_working_directory(self)
|
directory = self.project.node_working_directory(self)
|
||||||
if os.path.exists(directory):
|
if os.path.exists(directory):
|
||||||
try:
|
try:
|
||||||
|
@ -151,7 +151,7 @@ class Controller:
|
|||||||
:param kwargs: See the documentation of Project
|
:param kwargs: See the documentation of Project
|
||||||
"""
|
"""
|
||||||
if project_id not in self._projects:
|
if project_id not in self._projects:
|
||||||
project = Project(project_id=project_id, **kwargs)
|
project = Project(project_id=project_id, controller=self, **kwargs)
|
||||||
self._projects[project.id] = project
|
self._projects[project.id] = project
|
||||||
for compute_server in self._computes.values():
|
for compute_server in self._computes.values():
|
||||||
yield from project.add_compute(compute_server)
|
yield from project.add_compute(compute_server)
|
||||||
|
@ -45,6 +45,8 @@ class Link:
|
|||||||
"adapter_number": adapter_number,
|
"adapter_number": adapter_number,
|
||||||
"port_number": port_number
|
"port_number": port_number
|
||||||
})
|
})
|
||||||
|
if len(self._nodes) == 2:
|
||||||
|
self._project.controller.notification.emit("link.created", self.__json__())
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def create(self):
|
def create(self):
|
||||||
@ -70,6 +72,7 @@ class Link:
|
|||||||
self._capturing = True
|
self._capturing = True
|
||||||
self._capture_file_name = capture_file_name
|
self._capture_file_name = capture_file_name
|
||||||
self._streaming_pcap = asyncio.async(self._start_streaming_pcap())
|
self._streaming_pcap = asyncio.async(self._start_streaming_pcap())
|
||||||
|
self._project.controller.notification.emit("link.updated", self.__json__())
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def _start_streaming_pcap(self):
|
def _start_streaming_pcap(self):
|
||||||
@ -94,6 +97,8 @@ class Link:
|
|||||||
Stop capture on the link
|
Stop capture on the link
|
||||||
"""
|
"""
|
||||||
self._capturing = False
|
self._capturing = False
|
||||||
|
self._project.controller.notification.emit("link.updated", self.__json__())
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def read_pcap_from_source(self):
|
def read_pcap_from_source(self):
|
||||||
@ -142,6 +147,7 @@ class Link:
|
|||||||
})
|
})
|
||||||
return {
|
return {
|
||||||
"nodes": res, "link_id": self._id,
|
"nodes": res, "link_id": self._id,
|
||||||
|
"project_id": self._project.id,
|
||||||
"capturing": self._capturing,
|
"capturing": self._capturing,
|
||||||
"capture_file_name": self._capture_file_name,
|
"capture_file_name": self._capture_file_name,
|
||||||
"capture_file_path": self.capture_file_path
|
"capture_file_path": self.capture_file_path
|
||||||
|
@ -37,8 +37,9 @@ class Project:
|
|||||||
:param temporary: boolean to tell if the project is a temporary project (destroy when closed)
|
:param temporary: boolean to tell if the project is a temporary project (destroy when closed)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name=None, project_id=None, path=None, temporary=False):
|
def __init__(self, name=None, project_id=None, path=None, temporary=False, controller=None):
|
||||||
|
|
||||||
|
self._controller = controller
|
||||||
self._name = name
|
self._name = name
|
||||||
if project_id is None:
|
if project_id is None:
|
||||||
self._id = str(uuid4())
|
self._id = str(uuid4())
|
||||||
@ -61,6 +62,10 @@ class Project:
|
|||||||
# Create the project on demand on the compute node
|
# Create the project on demand on the compute node
|
||||||
self._project_created_on_compute = set()
|
self._project_created_on_compute = set()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def controller(self):
|
||||||
|
return self._controller
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
return self._name
|
return self._name
|
||||||
@ -134,6 +139,7 @@ class Project:
|
|||||||
self._project_created_on_compute.add(compute)
|
self._project_created_on_compute.add(compute)
|
||||||
yield from node.create()
|
yield from node.create()
|
||||||
self._nodes[node.id] = node
|
self._nodes[node.id] = node
|
||||||
|
self.controller.notification.emit("node.created", node.__json__())
|
||||||
return node
|
return node
|
||||||
return self._nodes[node_id]
|
return self._nodes[node_id]
|
||||||
|
|
||||||
@ -141,7 +147,8 @@ class Project:
|
|||||||
def delete_node(self, node_id):
|
def delete_node(self, node_id):
|
||||||
node = self.get_node(node_id)
|
node = self.get_node(node_id)
|
||||||
del self._nodes[node.id]
|
del self._nodes[node.id]
|
||||||
yield from node.delete()
|
yield from node.destroy()
|
||||||
|
self.controller.notification.emit("node.deleted", node.__json__())
|
||||||
|
|
||||||
def get_node(self, node_id):
|
def get_node(self, node_id):
|
||||||
"""
|
"""
|
||||||
@ -168,6 +175,13 @@ class Project:
|
|||||||
self._links[link.id] = link
|
self._links[link.id] = link
|
||||||
return link
|
return link
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def delete_link(self, link_id):
|
||||||
|
link = self.get_link(link_id)
|
||||||
|
del self._links[link.id]
|
||||||
|
yield from link.delete()
|
||||||
|
self.controller.notification.emit("link.deleted", link.__json__())
|
||||||
|
|
||||||
def get_link(self, link_id):
|
def get_link(self, link_id):
|
||||||
"""
|
"""
|
||||||
Return the Link or raise a 404 if the link is unknown
|
Return the Link or raise a 404 if the link is unknown
|
||||||
|
@ -69,14 +69,21 @@ class UDPLink(Link):
|
|||||||
"""
|
"""
|
||||||
Delete the link and free the resources
|
Delete the link and free the resources
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
node1 = self._nodes[0]["node"]
|
node1 = self._nodes[0]["node"]
|
||||||
adapter_number1 = self._nodes[0]["adapter_number"]
|
adapter_number1 = self._nodes[0]["adapter_number"]
|
||||||
port_number1 = self._nodes[0]["port_number"]
|
port_number1 = self._nodes[0]["port_number"]
|
||||||
|
except IndexError:
|
||||||
|
return
|
||||||
|
|
||||||
|
yield from node1.delete("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number1, port_number=port_number1))
|
||||||
|
|
||||||
|
try:
|
||||||
node2 = self._nodes[1]["node"]
|
node2 = self._nodes[1]["node"]
|
||||||
adapter_number2 = self._nodes[1]["adapter_number"]
|
adapter_number2 = self._nodes[1]["adapter_number"]
|
||||||
port_number2 = self._nodes[1]["port_number"]
|
port_number2 = self._nodes[1]["port_number"]
|
||||||
|
except IndexError:
|
||||||
yield from node1.delete("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number1, port_number=port_number1))
|
return
|
||||||
yield from node2.delete("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number2, port_number=port_number2))
|
yield from node2.delete("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number2, port_number=port_number2))
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
|
@ -128,10 +128,8 @@ class LinkHandler:
|
|||||||
|
|
||||||
controller = Controller.instance()
|
controller = Controller.instance()
|
||||||
project = controller.get_project(request.match_info["project_id"])
|
project = controller.get_project(request.match_info["project_id"])
|
||||||
link = project.get_link(request.match_info["link_id"])
|
yield from project.delete_link(request.match_info["link_id"])
|
||||||
yield from link.delete()
|
|
||||||
response.set_status(204)
|
response.set_status(204)
|
||||||
response.json(link)
|
|
||||||
|
|
||||||
@Route.get(
|
@Route.get(
|
||||||
r"/projects/{project_id}/links/{link_id}/pcap",
|
r"/projects/{project_id}/links/{link_id}/pcap",
|
||||||
|
@ -28,6 +28,13 @@ LINK_OBJECT_SCHEMA = {
|
|||||||
"maxLength": 36,
|
"maxLength": 36,
|
||||||
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
|
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
|
||||||
},
|
},
|
||||||
|
"project_id": {
|
||||||
|
"description": "Project UUID",
|
||||||
|
"type": "string",
|
||||||
|
"minLength": 36,
|
||||||
|
"maxLength": 36,
|
||||||
|
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
|
||||||
|
},
|
||||||
"nodes": {
|
"nodes": {
|
||||||
"description": "List of the VMS",
|
"description": "List of the VMS",
|
||||||
"type": "array",
|
"type": "array",
|
||||||
|
@ -30,8 +30,8 @@ from tests.utils import AsyncioBytesIO
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def project():
|
def project(controller):
|
||||||
return Project()
|
return Project(controller=controller)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@ -73,6 +73,7 @@ def test_json(async_run, project, compute):
|
|||||||
async_run(link.add_node(node2, 1, 3))
|
async_run(link.add_node(node2, 1, 3))
|
||||||
assert link.__json__() == {
|
assert link.__json__() == {
|
||||||
"link_id": link.id,
|
"link_id": link.id,
|
||||||
|
"project_id": project.id,
|
||||||
"nodes": [
|
"nodes": [
|
||||||
{
|
{
|
||||||
"node_id": node1.id,
|
"node_id": node1.id,
|
||||||
|
@ -35,8 +35,8 @@ def compute():
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def node(compute):
|
def node(compute, controller):
|
||||||
project = Project(str(uuid.uuid4()))
|
project = Project(str(uuid.uuid4()), controller=controller)
|
||||||
node = Node(project, compute,
|
node = Node(project, compute,
|
||||||
name="demo",
|
name="demo",
|
||||||
node_id=str(uuid.uuid4()),
|
node_id=str(uuid.uuid4()),
|
||||||
|
@ -27,6 +27,10 @@ from uuid import uuid4
|
|||||||
from gns3server.controller.project import Project
|
from gns3server.controller.project import Project
|
||||||
from gns3server.config import Config
|
from gns3server.config import Config
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def project(controller):
|
||||||
|
return Project(controller=controller)
|
||||||
|
|
||||||
|
|
||||||
def test_affect_uuid():
|
def test_affect_uuid():
|
||||||
p = Project()
|
p = Project()
|
||||||
@ -76,13 +80,14 @@ def test_add_compute(async_run):
|
|||||||
assert compute in project._computes
|
assert compute in project._computes
|
||||||
|
|
||||||
|
|
||||||
def test_add_node_local(async_run):
|
def test_add_node_local(async_run, controller):
|
||||||
"""
|
"""
|
||||||
For a local server we send the project path
|
For a local server we send the project path
|
||||||
"""
|
"""
|
||||||
compute = MagicMock()
|
compute = MagicMock()
|
||||||
compute.id = "local"
|
compute.id = "local"
|
||||||
project = Project()
|
project = Project(controller=controller)
|
||||||
|
controller._notification = MagicMock()
|
||||||
|
|
||||||
response = MagicMock()
|
response = MagicMock()
|
||||||
response.json = {"console": 2048}
|
response.json = {"console": 2048}
|
||||||
@ -102,15 +107,17 @@ def test_add_node_local(async_run):
|
|||||||
'startup_config': 'test.cfg',
|
'startup_config': 'test.cfg',
|
||||||
'name': 'test'})
|
'name': 'test'})
|
||||||
assert compute in project._project_created_on_compute
|
assert compute in project._project_created_on_compute
|
||||||
|
controller.notification.emit.assert_any_call("node.created", node.__json__())
|
||||||
|
|
||||||
|
|
||||||
def test_add_node_non_local(async_run):
|
def test_add_node_non_local(async_run, controller):
|
||||||
"""
|
"""
|
||||||
For a non local server we do not send the project path
|
For a non local server we do not send the project path
|
||||||
"""
|
"""
|
||||||
compute = MagicMock()
|
compute = MagicMock()
|
||||||
compute.id = "remote"
|
compute.id = "remote"
|
||||||
project = Project()
|
project = Project(controller=controller)
|
||||||
|
controller._notification = MagicMock()
|
||||||
|
|
||||||
response = MagicMock()
|
response = MagicMock()
|
||||||
response.json = {"console": 2048}
|
response.json = {"console": 2048}
|
||||||
@ -128,14 +135,16 @@ def test_add_node_non_local(async_run):
|
|||||||
'startup_config': 'test.cfg',
|
'startup_config': 'test.cfg',
|
||||||
'name': 'test'})
|
'name': 'test'})
|
||||||
assert compute in project._project_created_on_compute
|
assert compute in project._project_created_on_compute
|
||||||
|
controller.notification.emit.assert_any_call("node.created", node.__json__())
|
||||||
|
|
||||||
|
|
||||||
def test_delete_node(async_run):
|
def test_delete_node(async_run, controller):
|
||||||
"""
|
"""
|
||||||
For a local server we send the project path
|
For a local server we send the project path
|
||||||
"""
|
"""
|
||||||
compute = MagicMock()
|
compute = MagicMock()
|
||||||
project = Project()
|
project = Project(controller=controller)
|
||||||
|
controller._notification = MagicMock()
|
||||||
|
|
||||||
response = MagicMock()
|
response = MagicMock()
|
||||||
response.json = {"console": 2048}
|
response.json = {"console": 2048}
|
||||||
@ -147,11 +156,12 @@ def test_delete_node(async_run):
|
|||||||
assert node.id not in project._nodes
|
assert node.id not in project._nodes
|
||||||
|
|
||||||
compute.delete.assert_any_call('/projects/{}/vpcs/nodes/{}'.format(project.id, node.id))
|
compute.delete.assert_any_call('/projects/{}/vpcs/nodes/{}'.format(project.id, node.id))
|
||||||
|
controller.notification.emit.assert_any_call("node.deleted", node.__json__())
|
||||||
|
|
||||||
|
|
||||||
def test_getVM(async_run):
|
def test_getVM(async_run, controller):
|
||||||
compute = MagicMock()
|
compute = MagicMock()
|
||||||
project = Project()
|
project = Project(controller=controller)
|
||||||
|
|
||||||
response = MagicMock()
|
response = MagicMock()
|
||||||
response.json = {"console": 2048}
|
response.json = {"console": 2048}
|
||||||
@ -164,9 +174,8 @@ def test_getVM(async_run):
|
|||||||
project.get_node("test")
|
project.get_node("test")
|
||||||
|
|
||||||
|
|
||||||
def test_addLink(async_run):
|
def test_addLink(async_run, project, controller):
|
||||||
compute = MagicMock()
|
compute = MagicMock()
|
||||||
project = Project()
|
|
||||||
|
|
||||||
response = MagicMock()
|
response = MagicMock()
|
||||||
response.json = {"console": 2048}
|
response.json = {"console": 2048}
|
||||||
@ -174,15 +183,16 @@ def test_addLink(async_run):
|
|||||||
|
|
||||||
vm1 = async_run(project.add_node(compute, None, name="test1", node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
vm1 = async_run(project.add_node(compute, None, name="test1", node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||||
vm2 = async_run(project.add_node(compute, None, name="test2", node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
vm2 = async_run(project.add_node(compute, None, name="test2", node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||||
|
controller._notification = MagicMock()
|
||||||
link = async_run(project.add_link())
|
link = async_run(project.add_link())
|
||||||
async_run(link.add_node(vm1, 3, 1))
|
async_run(link.add_node(vm1, 3, 1))
|
||||||
async_run(link.add_node(vm2, 4, 2))
|
async_run(link.add_node(vm2, 4, 2))
|
||||||
assert len(link._nodes) == 2
|
assert len(link._nodes) == 2
|
||||||
|
controller.notification.emit.assert_any_call("link.created", link.__json__())
|
||||||
|
|
||||||
|
|
||||||
def test_getLink(async_run):
|
def test_getLink(async_run, project):
|
||||||
compute = MagicMock()
|
compute = MagicMock()
|
||||||
project = Project()
|
|
||||||
|
|
||||||
response = MagicMock()
|
response = MagicMock()
|
||||||
response.json = {"console": 2048}
|
response.json = {"console": 2048}
|
||||||
@ -193,3 +203,19 @@ def test_getLink(async_run):
|
|||||||
|
|
||||||
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
|
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
|
||||||
project.get_link("test")
|
project.get_link("test")
|
||||||
|
|
||||||
|
|
||||||
|
def test_deleteLink(async_run, project, controller):
|
||||||
|
compute = MagicMock()
|
||||||
|
|
||||||
|
response = MagicMock()
|
||||||
|
response.json = {"console": 2048}
|
||||||
|
compute.post = AsyncioMagicMock(return_value=response)
|
||||||
|
|
||||||
|
assert len(project._links) == 0
|
||||||
|
link = async_run(project.add_link())
|
||||||
|
assert len(project._links) == 1
|
||||||
|
controller._notification = MagicMock()
|
||||||
|
async_run(project.delete_link(link.id))
|
||||||
|
controller.notification.emit.assert_any_call("link.deleted", link.__json__())
|
||||||
|
assert len(project._links) == 0
|
||||||
|
@ -27,8 +27,8 @@ from gns3server.controller.node import Node
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def project():
|
def project(controller):
|
||||||
return Project()
|
return Project(controller=controller)
|
||||||
|
|
||||||
|
|
||||||
def test_create(async_run, project):
|
def test_create(async_run, project):
|
||||||
|
Loading…
Reference in New Issue
Block a user