Merge "Move handle_conflicts decorator into sql"

This commit is contained in:
Jenkins 2013-02-25 21:41:06 +00:00 committed by Gerrit Code Review
commit ac2fb0f861
3 changed files with 32 additions and 43 deletions

View File

@ -15,6 +15,7 @@
# under the License.
"""SQL backends for the various services."""
import functools
import sqlalchemy as sql
import sqlalchemy.engine.url
@ -27,6 +28,7 @@ from sqlalchemy.orm.attributes import InstrumentedAttribute
from keystone.common import logging
from keystone import config
from keystone import exception
from keystone.openstack.common import jsonutils
from keystone import exception
@ -259,3 +261,16 @@ class Base(object):
bind=engine,
autocommit=autocommit,
expire_on_commit=expire_on_commit)
def handle_conflicts(type='object'):
"""Converts IntegrityError into HTTP 409 Conflict."""
def decorator(method):
@functools.wraps(method)
def wrapper(*args, **kwargs):
try:
return method(*args, **kwargs)
except IntegrityError as e:
raise exception.Conflict(type=type, details=str(e.orig))
return wrapper
return decorator

View File

@ -25,19 +25,6 @@ from keystone import exception
from keystone import identity
def handle_conflicts(type='object'):
"""Converts IntegrityError into HTTP 409 Conflict."""
def decorator(method):
@functools.wraps(method)
def wrapper(*args, **kwargs):
try:
return method(*args, **kwargs)
except sql.IntegrityError as e:
raise exception.Conflict(type=type, details=str(e.orig))
return wrapper
return decorator
class User(sql.ModelBase, sql.DictBase):
__tablename__ = 'user'
attributes = ['id', 'name', 'domain_id', 'password', 'enabled']
@ -494,7 +481,7 @@ class Identity(sql.Base, identity.Driver):
raise exception.RoleNotFound(message=msg)
# CRUD
@handle_conflicts(type='project')
@sql.handle_conflicts(type='project')
def create_project(self, tenant_id, tenant):
tenant['name'] = clean.project_name(tenant['name'])
session = self.get_session()
@ -504,7 +491,7 @@ class Identity(sql.Base, identity.Driver):
session.flush()
return tenant_ref.to_dict()
@handle_conflicts(type='project')
@sql.handle_conflicts(type='project')
def update_project(self, tenant_id, tenant):
session = self.get_session()
@ -527,7 +514,7 @@ class Identity(sql.Base, identity.Driver):
session.flush()
return tenant_ref.to_dict(include_extra_dict=True)
@handle_conflicts(type='project')
@sql.handle_conflicts(type='project')
def delete_project(self, tenant_id):
session = self.get_session()
@ -556,7 +543,7 @@ class Identity(sql.Base, identity.Driver):
session.delete(tenant_ref)
session.flush()
@handle_conflicts(type='metadata')
@sql.handle_conflicts(type='metadata')
def create_metadata(self, user_id, tenant_id, metadata,
domain_id=None, group_id=None):
session = self.get_session()
@ -582,7 +569,7 @@ class Identity(sql.Base, identity.Driver):
session.flush()
return metadata
@handle_conflicts(type='metadata')
@sql.handle_conflicts(type='metadata')
def update_metadata(self, user_id, tenant_id, metadata,
domain_id=None, group_id=None):
session = self.get_session()
@ -614,7 +601,7 @@ class Identity(sql.Base, identity.Driver):
# domain crud
@handle_conflicts(type='domain')
@sql.handle_conflicts(type='domain')
def create_domain(self, domain_id, domain):
session = self.get_session()
with session.begin():
@ -643,7 +630,7 @@ class Identity(sql.Base, identity.Driver):
raise exception.DomainNotFound(domain_id=domain_name)
return ref.to_dict()
@handle_conflicts(type='domain')
@sql.handle_conflicts(type='domain')
def update_domain(self, domain_id, domain):
session = self.get_session()
with session.begin():
@ -689,7 +676,7 @@ class Identity(sql.Base, identity.Driver):
# user crud
@handle_conflicts(type='user')
@sql.handle_conflicts(type='user')
def create_user(self, user_id, user):
user['name'] = clean.user_name(user['name'])
if 'enabled' not in user:
@ -732,7 +719,7 @@ class Identity(sql.Base, identity.Driver):
return identity.filter_user(
self._get_user_by_name(user_name, domain_id))
@handle_conflicts(type='user')
@sql.handle_conflicts(type='user')
def update_user(self, user_id, user):
if 'name' in user:
user['name'] = clean.user_name(user['name'])
@ -842,7 +829,7 @@ class Identity(sql.Base, identity.Driver):
# group crud
@handle_conflicts(type='group')
@sql.handle_conflicts(type='group')
def create_group(self, group_id, group):
session = self.get_session()
with session.begin():
@ -866,7 +853,7 @@ class Identity(sql.Base, identity.Driver):
def get_group(self, group_id):
return self._get_group(group_id)
@handle_conflicts(type='group')
@sql.handle_conflicts(type='group')
def update_group(self, group_id, group):
session = self.get_session()
@ -914,7 +901,7 @@ class Identity(sql.Base, identity.Driver):
# credential crud
@handle_conflicts(type='credential')
@sql.handle_conflicts(type='credential')
def create_credential(self, credential_id, credential):
session = self.get_session()
with session.begin():
@ -935,7 +922,7 @@ class Identity(sql.Base, identity.Driver):
raise exception.CredentialNotFound(credential_id=credential_id)
return ref.to_dict()
@handle_conflicts(type='credential')
@sql.handle_conflicts(type='credential')
def update_credential(self, credential_id, credential):
session = self.get_session()
with session.begin():
@ -967,7 +954,7 @@ class Identity(sql.Base, identity.Driver):
# role crud
@handle_conflicts(type='role')
@sql.handle_conflicts(type='role')
def create_role(self, role_id, role):
session = self.get_session()
with session.begin():
@ -988,7 +975,7 @@ class Identity(sql.Base, identity.Driver):
raise exception.RoleNotFound(role_id=role_id)
return ref.to_dict()
@handle_conflicts(type='role')
@sql.handle_conflicts(type='role')
def update_role(self, role_id, role):
session = self.get_session()
with session.begin():

View File

@ -22,19 +22,6 @@ from keystone import exception
from keystone.policy.backends import rules
def handle_conflicts(type='object'):
"""Converts IntegrityError into HTTP 409 Conflict."""
def decorator(method):
@functools.wraps(method)
def wrapper(*args, **kwargs):
try:
return method(*args, **kwargs)
except sql.IntegrityError as e:
raise exception.Conflict(type=type, details=str(e))
return wrapper
return decorator
class PolicyModel(sql.ModelBase, sql.DictBase):
__tablename__ = 'policy'
attributes = ['id', 'blob', 'type']
@ -49,7 +36,7 @@ class Policy(sql.Base, rules.Policy):
def db_sync(self):
migration.db_sync()
@handle_conflicts(type='policy')
@sql.handle_conflicts(type='policy')
def create_policy(self, policy_id, policy):
session = self.get_session()
@ -78,7 +65,7 @@ class Policy(sql.Base, rules.Policy):
return self._get_policy(session, policy_id).to_dict()
@handle_conflicts(type='policy')
@sql.handle_conflicts(type='policy')
def update_policy(self, policy_id, policy):
session = self.get_session()