From 2eaedf2d797de86a299ec9e00990a1ae460524d7 Mon Sep 17 00:00:00 2001 From: Dmytro Dovbii Date: Tue, 19 May 2015 14:46:10 +0300 Subject: [PATCH] Improve exception handling during enviroment editing Change-Id: Ie40cc99bb9b69caf4fe552a95c55c31a4d191f5a Partial-Bug: #1455142 --- doc/source/specification/murano-api.rst | 15 +++++ murano/api/v1/environments.py | 9 ++- murano/tests/unit/api/v1/test_environments.py | 57 +++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/doc/source/specification/murano-api.rst b/doc/source/specification/murano-api.rst index ed627bfc..74a7c5db 100644 --- a/doc/source/specification/murano-api.rst +++ b/doc/source/specification/murano-api.rst @@ -204,6 +204,21 @@ Update Environment "version": 0 } ++----------------+-----------------------------------------------------------+ +| Code | Description | ++================+===========================================================+ +| 200 | Edited environment | ++----------------+-----------------------------------------------------------+ +| 400 | Environment name must contain only alphanumeric or '_-.' | +| | characters, must start with alpha | ++----------------+-----------------------------------------------------------+ +| 401 | User is not authorized to access environment | ++----------------+-----------------------------------------------------------+ +| 404 | Environment not found | ++----------------+-----------------------------------------------------------+ +| 409 | Environment with specified name already exists | ++----------------+-----------------------------------------------------------+ + Get Environment Details ----------------------- diff --git a/murano/api/v1/environments.py b/murano/api/v1/environments.py index 8818cbde..b63824b7 100644 --- a/murano/api/v1/environments.py +++ b/murano/api/v1/environments.py @@ -126,8 +126,13 @@ class Controller(object): LOG.debug('ENV NAME: {0}>'.format(body['name'])) if VALID_NAME_REGEX.match(str(body['name'])): - environment.update(body) - environment.save(session) + try: + environment.update(body) + environment.save(session) + except db_exc.DBDuplicateEntry: + msg = _('Environment with specified name already exists') + LOG.exception(msg) + raise exc.HTTPConflict(explanation=msg) else: msg = _('Environment name must contain only alphanumeric ' 'or "_-." characters, must start with alpha') diff --git a/murano/tests/unit/api/v1/test_environments.py b/murano/tests/unit/api/v1/test_environments.py index 5aad6a8e..07facb1c 100644 --- a/murano/tests/unit/api/v1/test_environments.py +++ b/murano/tests/unit/api/v1/test_environments.py @@ -196,6 +196,63 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): self.assertEqual(expected, json.loads(result.body)) + def test_update_environment_with_existing_name(self): + self._set_policy_rules( + {'update_environment': '@'} + ) + + fake_now = timeutils.utcnow() + timeutils.utcnow.override_time = fake_now + + expected = dict( + id='111', + name='env1', + version=0, + networking={}, + created=fake_now, + updated=fake_now, + tenant_id=self.tenant, + description={ + 'Objects': { + '?': {'id': '111'} + }, + 'Attributes': [] + } + ) + e = models.Environment(**expected) + test_utils.save_models(e) + + fake_now = timeutils.utcnow() + timeutils.utcnow.override_time = fake_now + + expected = dict( + id='222', + name='env2', + version=0, + networking={}, + created=fake_now, + updated=fake_now, + tenant_id=self.tenant, + description={ + 'Objects': { + '?': {'id': '222'} + }, + 'Attributes': [] + } + ) + e = models.Environment(**expected) + test_utils.save_models(e) + + self.expect_policy_check('update_environment', + {'environment_id': '111'}) + + body = { + 'name': 'env2' + } + req = self._put('/environments/111', json.dumps(body)) + result = req.get_response(self.api) + self.assertEqual(409, result.status_code) + def test_delete_environment(self): """Test that environment deletion results in the correct rpc call.""" self._set_policy_rules(