Send event when adding compute node or modify it (for server sumary)

This commit is contained in:
Julien Duponchelle 2016-05-23 18:44:20 +02:00
parent f6a3899603
commit 0aa81b5fa5
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
5 changed files with 29 additions and 14 deletions

View File

@ -205,6 +205,9 @@ You can receive notification from the server if you listen the HTTP stream /noti
The available notification are:
* ping
* compute.created
* compute.updated
* compute.deleted
* node.created
* node.updated
* node.deleted

View File

@ -93,15 +93,21 @@ class Controller:
:param compute_id: Compute server identifier
:param kwargs: See the documentation of Compute
"""
# We disallow to create from the outside the
if compute_id == 'local':
return self._create_local_compute()
if compute_id not in self._computes:
# We disallow to create from the outside the
if compute_id == 'local':
compute_server = self._create_local_compute()
self.notification.emit("compute.created", compute_server.__json__())
return compute_server
compute_server = Compute(compute_id=compute_id, controller=self, **kwargs)
self._computes[compute_id] = compute_server
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]
def _create_local_compute(self):

View File

@ -92,7 +92,7 @@ class Compute:
if name is not None:
self._name = name
elif self._id == "local":
self._name = "local"
self._name = "Local"
else:
self._name = "{}://{}:{}".format(self._protocol, self._host, self._port)
@ -199,6 +199,7 @@ class Compute:
self._notifications = asyncio.async(self._connect_notification())
self._connected = True
self._controller.notification.emit("compute.updated", self.__json__())
@asyncio.coroutine
def _connect_notification(self):

View File

@ -29,8 +29,8 @@ from tests.utils import asyncio_patch, AsyncioMagicMock
@pytest.fixture
def compute():
compute = Compute("my_compute_id", protocol="https", host="example.com", port=84, controller=MagicMock())
def compute(controller):
compute = Compute("my_compute_id", protocol="https", host="example.com", port=84, controller=controller)
compute._connected = True
return compute
@ -44,7 +44,7 @@ def test_name():
assert c.name == "https://example.com:84"
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)
assert c.name == "local"
assert c.name == "Local"
c = Compute("world", protocol="https", host="example.com", port=84, controller=MagicMock(), name="hello")
assert c.name == "hello"
@ -86,7 +86,8 @@ def test_compute_httpQueryAuth(compute, async_run):
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
response = AsyncioMagicMock()
response.read = AsyncioMagicMock(return_value=json.dumps({"version": __version__}).encode())
@ -95,8 +96,9 @@ def test_compute_httpQueryNotConnected(compute, async_run):
async_run(compute.post("/projects", {"a": "b"}))
mock.assert_any_call("GET", "https://example.com:84/v2/compute/version", headers={'content-type': 'application/json'}, data=None, 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.version == __version__
assert compute._connected
assert compute.version == __version__
controller.notification.emit.assert_called_with("compute.updated", compute.__json__())
def test_compute_httpQueryNotConnectedInvalidVersion(compute, async_run):
@ -170,7 +172,7 @@ def test_connectNotification(compute, async_run):
response.tp = aiohttp.MsgType.closed
return response
compute._controller._notifications = MagicMock()
compute._controller._notification = MagicMock()
compute._session = AsyncioMagicMock(return_value=ws_mock)
compute._session.ws_connect = AsyncioMagicMock(return_value=ws_mock)
ws_mock.receive = receive

View File

@ -76,9 +76,12 @@ def test_isEnabled(controller):
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
async_run(controller.add_compute("test1"))
controller._notification.emit.assert_called_with("compute.updated", c.__json__())
assert len(controller.computes) == 1
async_run(controller.add_compute("test2"))
assert len(controller.computes) == 2