Fix create_region_with_id raise 500 Error bug

1. add sql.handle_conflicts wrapper in sql backend driver.
2. check duplicate region id in kvs backend driver.
3. add 2 test cases to check exception.Conflict when create duplicate id region.

Closes-Bug: #1283904

Change-Id: Icfc5edf661501a5c134f324a76824812dbda2760
This commit is contained in:
ruichen 2014-02-24 17:39:09 +08:00 committed by kiwik-chenrui
parent a2135440cc
commit e8bbe4ccf3
4 changed files with 34 additions and 1 deletions

View File

@ -134,6 +134,7 @@ class Catalog(catalog.Driver):
session.query(Region).filter_by(id=region_id).delete()
session.delete(ref)
@sql.handle_conflicts(conflict_type='region')
def create_region(self, region_ref):
session = sql.get_session()
with session.begin():

View File

@ -69,6 +69,15 @@ class Manager(manager.Manager):
super(Manager, self).__init__(CONF.catalog.driver)
def create_region(self, region_ref):
# Check duplicate ID
try:
self.get_region(region_ref['id'])
except exception.RegionNotFound:
pass
else:
msg = _('Duplicate ID, %s.') % region_ref['id']
raise exception.Conflict(type='region', details=msg)
try:
return self.driver.create_region(region_ref)
except exception.NotFound:

View File

@ -3553,6 +3553,18 @@ class CatalogTests(object):
self.catalog_api.get_region,
region_id)
def test_create_region_with_duplicate_id(self):
region_id = uuid.uuid4().hex
new_region = {
'id': region_id,
'description': uuid.uuid4().hex
}
self.catalog_api.create_region(new_region)
# Create region again with duplicate id
self.assertRaises(exception.Conflict,
self.catalog_api.create_region,
new_region)
def test_get_region_404(self):
self.assertRaises(exception.RegionNotFound,
self.catalog_api.get_region,

View File

@ -49,6 +49,17 @@ class CatalogTestCase(test_v3.RestfulTestCase):
# populated with a UUID, as is the case with POST /v3/regions
self.assertEqual(region_id, r.json['region']['id'])
def test_create_region_with_duplicate_id(self):
"""Call ``PUT /regions/{region_id}``."""
ref = dict(description="my region")
self.put(
'/regions/myregion',
body={'region': ref}, expected_status=201)
# Create region again with duplicate id
self.put(
'/regions/myregion',
body={'region': ref}, expected_status=409)
def test_create_region(self):
"""Call ``POST /regions`` with an ID in the request body."""
# the ref will have an ID defined on it
@ -275,7 +286,7 @@ class CatalogTestCase(test_v3.RestfulTestCase):
expected_status=400)
def assertValidErrorResponse(self, response):
self.assertIn(response.status_code, [400])
self.assertIn(response.status_code, [400, 409])
def test_create_endpoint_400(self):
"""Call ``POST /endpoints``."""