mirror of
https://github.com/GNS3/gns3-server
synced 2024-12-03 13:48:11 +00:00
Send event when adding compute node or modify it (for server sumary)
This commit is contained in:
parent
f6a3899603
commit
0aa81b5fa5
@ -205,6 +205,9 @@ You can receive notification from the server if you listen the HTTP stream /noti
|
|||||||
|
|
||||||
The available notification are:
|
The available notification are:
|
||||||
* ping
|
* ping
|
||||||
|
* compute.created
|
||||||
|
* compute.updated
|
||||||
|
* compute.deleted
|
||||||
* node.created
|
* node.created
|
||||||
* node.updated
|
* node.updated
|
||||||
* node.deleted
|
* node.deleted
|
||||||
|
@ -93,15 +93,21 @@ class Controller:
|
|||||||
:param compute_id: Compute server identifier
|
:param compute_id: Compute server identifier
|
||||||
:param kwargs: See the documentation of Compute
|
:param kwargs: See the documentation of Compute
|
||||||
"""
|
"""
|
||||||
|
if compute_id not in self._computes:
|
||||||
|
|
||||||
|
|
||||||
# We disallow to create from the outside the
|
# We disallow to create from the outside the
|
||||||
if compute_id == 'local':
|
if compute_id == 'local':
|
||||||
return self._create_local_compute()
|
compute_server = self._create_local_compute()
|
||||||
|
self.notification.emit("compute.created", compute_server.__json__())
|
||||||
|
return compute_server
|
||||||
|
|
||||||
if compute_id not in self._computes:
|
|
||||||
compute_server = Compute(compute_id=compute_id, controller=self, **kwargs)
|
compute_server = Compute(compute_id=compute_id, controller=self, **kwargs)
|
||||||
self._computes[compute_id] = compute_server
|
self._computes[compute_id] = compute_server
|
||||||
self.save()
|
self.save()
|
||||||
|
self.notification.emit("compute.created", compute_server.__json__())
|
||||||
|
else:
|
||||||
|
self.notification.emit("compute.updated", self._computes[compute_id].__json__())
|
||||||
return self._computes[compute_id]
|
return self._computes[compute_id]
|
||||||
|
|
||||||
def _create_local_compute(self):
|
def _create_local_compute(self):
|
||||||
|
@ -92,7 +92,7 @@ class Compute:
|
|||||||
if name is not None:
|
if name is not None:
|
||||||
self._name = name
|
self._name = name
|
||||||
elif self._id == "local":
|
elif self._id == "local":
|
||||||
self._name = "local"
|
self._name = "Local"
|
||||||
else:
|
else:
|
||||||
self._name = "{}://{}:{}".format(self._protocol, self._host, self._port)
|
self._name = "{}://{}:{}".format(self._protocol, self._host, self._port)
|
||||||
|
|
||||||
@ -199,6 +199,7 @@ class Compute:
|
|||||||
|
|
||||||
self._notifications = asyncio.async(self._connect_notification())
|
self._notifications = asyncio.async(self._connect_notification())
|
||||||
self._connected = True
|
self._connected = True
|
||||||
|
self._controller.notification.emit("compute.updated", self.__json__())
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def _connect_notification(self):
|
def _connect_notification(self):
|
||||||
|
@ -29,8 +29,8 @@ from tests.utils import asyncio_patch, AsyncioMagicMock
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def compute():
|
def compute(controller):
|
||||||
compute = Compute("my_compute_id", protocol="https", host="example.com", port=84, controller=MagicMock())
|
compute = Compute("my_compute_id", protocol="https", host="example.com", port=84, controller=controller)
|
||||||
compute._connected = True
|
compute._connected = True
|
||||||
return compute
|
return compute
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ def test_name():
|
|||||||
assert c.name == "https://example.com:84"
|
assert c.name == "https://example.com:84"
|
||||||
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
|
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
|
||||||
c = Compute("local", protocol="https", host="example.com", port=84, controller=MagicMock(), name=None)
|
c = Compute("local", protocol="https", host="example.com", port=84, controller=MagicMock(), name=None)
|
||||||
assert c.name == "local"
|
assert c.name == "Local"
|
||||||
c = Compute("world", protocol="https", host="example.com", port=84, controller=MagicMock(), name="hello")
|
c = Compute("world", protocol="https", host="example.com", port=84, controller=MagicMock(), name="hello")
|
||||||
assert c.name == "hello"
|
assert c.name == "hello"
|
||||||
|
|
||||||
@ -86,7 +86,8 @@ def test_compute_httpQueryAuth(compute, async_run):
|
|||||||
assert compute._auth.password == "toor"
|
assert compute._auth.password == "toor"
|
||||||
|
|
||||||
|
|
||||||
def test_compute_httpQueryNotConnected(compute, async_run):
|
def test_compute_httpQueryNotConnected(compute, controller, async_run):
|
||||||
|
controller._notification = MagicMock()
|
||||||
compute._connected = False
|
compute._connected = False
|
||||||
response = AsyncioMagicMock()
|
response = AsyncioMagicMock()
|
||||||
response.read = AsyncioMagicMock(return_value=json.dumps({"version": __version__}).encode())
|
response.read = AsyncioMagicMock(return_value=json.dumps({"version": __version__}).encode())
|
||||||
@ -97,6 +98,7 @@ def test_compute_httpQueryNotConnected(compute, async_run):
|
|||||||
mock.assert_any_call("POST", "https://example.com:84/v2/compute/projects", data='{"a": "b"}', headers={'content-type': 'application/json'}, auth=None)
|
mock.assert_any_call("POST", "https://example.com:84/v2/compute/projects", data='{"a": "b"}', headers={'content-type': 'application/json'}, auth=None)
|
||||||
assert compute._connected
|
assert compute._connected
|
||||||
assert compute.version == __version__
|
assert compute.version == __version__
|
||||||
|
controller.notification.emit.assert_called_with("compute.updated", compute.__json__())
|
||||||
|
|
||||||
|
|
||||||
def test_compute_httpQueryNotConnectedInvalidVersion(compute, async_run):
|
def test_compute_httpQueryNotConnectedInvalidVersion(compute, async_run):
|
||||||
@ -170,7 +172,7 @@ def test_connectNotification(compute, async_run):
|
|||||||
response.tp = aiohttp.MsgType.closed
|
response.tp = aiohttp.MsgType.closed
|
||||||
return response
|
return response
|
||||||
|
|
||||||
compute._controller._notifications = MagicMock()
|
compute._controller._notification = MagicMock()
|
||||||
compute._session = AsyncioMagicMock(return_value=ws_mock)
|
compute._session = AsyncioMagicMock(return_value=ws_mock)
|
||||||
compute._session.ws_connect = AsyncioMagicMock(return_value=ws_mock)
|
compute._session.ws_connect = AsyncioMagicMock(return_value=ws_mock)
|
||||||
ws_mock.receive = receive
|
ws_mock.receive = receive
|
||||||
|
@ -76,9 +76,12 @@ def test_isEnabled(controller):
|
|||||||
|
|
||||||
|
|
||||||
def test_addCompute(controller, controller_config_path, async_run):
|
def test_addCompute(controller, controller_config_path, async_run):
|
||||||
async_run(controller.add_compute("test1"))
|
controller._notification = MagicMock()
|
||||||
|
c = async_run(controller.add_compute("test1"))
|
||||||
|
controller._notification.emit.assert_called_with("compute.created", c.__json__())
|
||||||
assert len(controller.computes) == 1
|
assert len(controller.computes) == 1
|
||||||
async_run(controller.add_compute("test1"))
|
async_run(controller.add_compute("test1"))
|
||||||
|
controller._notification.emit.assert_called_with("compute.updated", c.__json__())
|
||||||
assert len(controller.computes) == 1
|
assert len(controller.computes) == 1
|
||||||
async_run(controller.add_compute("test2"))
|
async_run(controller.add_compute("test2"))
|
||||||
assert len(controller.computes) == 2
|
assert len(controller.computes) == 2
|
||||||
|
Loading…
Reference in New Issue
Block a user