From 68b7f81601af1c08e3596344e50289d70de2ce18 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 29 Jun 2016 17:39:41 +0200 Subject: [PATCH] Dissalow duplicate name for compute and project Fix #541 --- gns3server/controller/__init__.py | 20 +++++++++++++++----- tests/controller/test_controller.py | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 7dff2ed0..5ec53510 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -135,21 +135,25 @@ class Controller: return Config.instance().get_section_config("Server").getboolean("controller") @asyncio.coroutine - def add_compute(self, **kwargs): + def add_compute(self, compute_id=None, name=None, **kwargs): """ Add a server to the dictionary of compute servers controlled by this controller :param compute_id: Compute server identifier + :param name: Compute name :param kwargs: See the documentation of Compute """ - compute_id = kwargs.pop("compute_id", None) if compute_id not in self._computes: # We disallow to create from the outside the if compute_id == 'local': return None - compute = Compute(compute_id=compute_id, controller=self, **kwargs) + for compute in self._computes.values(): + if name and compute.name == name: + raise aiohttp.web.HTTPConflict(text="Compute name {} is duplicate".format(name)) + + compute = Compute(compute_id=compute_id, controller=self, name=name, **kwargs) self._computes[compute.id] = compute self.save() self.notification.emit("compute.created", compute.__json__()) @@ -200,14 +204,20 @@ class Controller: raise aiohttp.web.HTTPNotFound(text="Compute ID {} doesn't exist".format(compute_id)) @asyncio.coroutine - def add_project(self, project_id=None, **kwargs): + def add_project(self, project_id=None, name=None, **kwargs): """ Creates a project or returns an existing project + :param project_id: Project ID + :param name: Project name :param kwargs: See the documentation of Project """ if project_id not in self._projects: - project = Project(project_id=project_id, controller=self, **kwargs) + + for project in self._projects.values(): + if name and project.name == name: + raise aiohttp.web.HTTPConflict(text="Project name {} is duplicate".format(name)) + project = Project(project_id=project_id, controller=self, name=name, **kwargs) self._projects[project.id] = project return self._projects[project.id] return self._projects[project_id] diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index 08c1019f..1ff5a63e 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -107,6 +107,14 @@ def test_addCompute(controller, controller_config_path, async_run): assert len(controller.computes) == 2 +def test_addDuplicateCompute(controller, controller_config_path, async_run): + controller._notification = MagicMock() + c = async_run(controller.add_compute(compute_id="test1", name="Test")) + assert len(controller.computes) == 1 + with pytest.raises(aiohttp.web.HTTPConflict): + async_run(controller.add_compute(compute_id="test2", name="Test")) + + def test_deleteCompute(controller, controller_config_path, async_run): c = async_run(controller.add_compute(compute_id="test1")) assert len(controller.computes) == 1 @@ -170,6 +178,16 @@ def test_addProject(controller, async_run): assert len(controller.projects) == 2 +def test_addDuplicateProject(controller, async_run): + uuid1 = str(uuid.uuid4()) + uuid2 = str(uuid.uuid4()) + + async_run(controller.add_project(project_id=uuid1, name="Test")) + assert len(controller.projects) == 1 + with pytest.raises(aiohttp.web.HTTPConflict): + async_run(controller.add_project(project_id=uuid2, name="Test")) + + def test_remove_project(controller, async_run): uuid1 = str(uuid.uuid4())