diff options
| author | Dolph Mathews <dolph.mathews@gmail.com> | 2012-11-06 17:01:59 +0000 |
|---|---|---|
| committer | Dolph Mathews <dolph.mathews@gmail.com> | 2012-11-06 17:01:59 +0000 |
| commit | df148a09fc1c7d44f2134a2dc6566ef1dbe772df (patch) | |
| tree | e1f3d95563ba26f43c82a9bfcc0c5055943a8fb6 | |
| parent | a6ef09d94300718197a4fa8757fd3a7a45876963 (diff) | |
Return non-indexed attrs, not 'extra' (bug 1075376)
(most of this is pulled from the v3 branch)
Change-Id: Id1118e7a2b245fb7ec95e41ec297c87036953db2
| -rw-r--r-- | keystone/catalog/backends/sql.py | 34 | ||||
| -rw-r--r-- | keystone/common/sql/core.py | 15 | ||||
| -rw-r--r-- | keystone/identity/backends/sql.py | 58 | ||||
| -rw-r--r-- | keystone/token/backends/sql.py | 18 |
4 files changed, 30 insertions, 95 deletions
diff --git a/keystone/catalog/backends/sql.py b/keystone/catalog/backends/sql.py index 6876bdec..b2012c69 100644 --- a/keystone/catalog/backends/sql.py +++ b/keystone/catalog/backends/sql.py @@ -28,29 +28,15 @@ CONF = config.CONF class Service(sql.ModelBase, sql.DictBase): __tablename__ = 'service' + attributes = ['id', 'type'] id = sql.Column(sql.String(64), primary_key=True) type = sql.Column(sql.String(255)) extra = sql.Column(sql.JsonBlob()) - @classmethod - def from_dict(cls, service_dict): - extra = {} - for k, v in service_dict.copy().iteritems(): - if k not in ['id', 'type', 'extra']: - extra[k] = service_dict.pop(k) - - service_dict['extra'] = extra - return cls(**service_dict) - - def to_dict(self): - extra_copy = self.extra.copy() - extra_copy['id'] = self.id - extra_copy['type'] = self.type - return extra_copy - class Endpoint(sql.ModelBase, sql.DictBase): __tablename__ = 'endpoint' + attributes = ['id', 'region', 'service_id'] id = sql.Column(sql.String(64), primary_key=True) region = sql.Column('region', sql.String(255)) service_id = sql.Column(sql.String(64), @@ -58,22 +44,6 @@ class Endpoint(sql.ModelBase, sql.DictBase): nullable=False) extra = sql.Column(sql.JsonBlob()) - @classmethod - def from_dict(cls, endpoint_dict): - extra = {} - for k, v in endpoint_dict.copy().iteritems(): - if k not in ['id', 'region', 'service_id', 'extra']: - extra[k] = endpoint_dict.pop(k) - endpoint_dict['extra'] = extra - return cls(**endpoint_dict) - - def to_dict(self): - extra_copy = self.extra.copy() - extra_copy['id'] = self.id - extra_copy['region'] = self.region - extra_copy['service_id'] = self.service_id - return extra_copy - class Catalog(sql.Base, catalog.Driver): def db_sync(self): diff --git a/keystone/common/sql/core.py b/keystone/common/sql/core.py index 27df8914..1a9d595f 100644 --- a/keystone/common/sql/core.py +++ b/keystone/common/sql/core.py @@ -57,9 +57,22 @@ class JsonBlob(sql_types.TypeDecorator): class DictBase(object): + attributes = [] + + @classmethod + def from_dict(cls, d): + new_d = d.copy() + + new_d['extra'] = dict((k, new_d.pop(k)) for k in d.iterkeys() + if k not in cls.attributes and k != 'extra') + + return cls(**new_d) def to_dict(self): - return dict(self.iteritems()) + d = self.extra.copy() + for attr in self.__class__.attributes: + d[attr] = getattr(self, attr) + return d def __setitem__(self, key, value): setattr(self, key, value) diff --git a/keystone/identity/backends/sql.py b/keystone/identity/backends/sql.py index f115303a..7c3eb975 100644 --- a/keystone/identity/backends/sql.py +++ b/keystone/identity/backends/sql.py @@ -14,7 +14,6 @@ # License for the specific language governing permissions and limitations # under the License. -import copy import functools from keystone import clean @@ -40,72 +39,41 @@ def handle_conflicts(type='object'): class User(sql.ModelBase, sql.DictBase): __tablename__ = 'user' + attributes = ['id', 'name'] id = sql.Column(sql.String(64), primary_key=True) name = sql.Column(sql.String(64), unique=True, nullable=False) - #password = sql.Column(sql.String(64)) extra = sql.Column(sql.JsonBlob()) - @classmethod - def from_dict(cls, user_dict): - # shove any non-indexed properties into extra - extra = {} - user = {} - for k, v in user_dict.iteritems(): - # TODO(termie): infer this somehow - if k in ['id', 'name', 'extra']: - user[k] = v - else: - extra[k] = v - - return cls(extra=extra, **user) - - def to_dict(self): - extra_copy = self.extra.copy() - extra_copy['id'] = self.id - extra_copy['name'] = self.name - return extra_copy - class Tenant(sql.ModelBase, sql.DictBase): __tablename__ = 'tenant' + attributes = ['id', 'name'] id = sql.Column(sql.String(64), primary_key=True) name = sql.Column(sql.String(64), unique=True, nullable=False) extra = sql.Column(sql.JsonBlob()) - @classmethod - def from_dict(cls, tenant_dict): - # shove any non-indexed properties into extra - extra = {} - for k, v in tenant_dict.copy().iteritems(): - # TODO(termie): infer this somehow - if k not in ['id', 'name', 'extra']: - extra[k] = tenant_dict.pop(k) - - return cls(extra=extra, **tenant_dict) - - def to_dict(self): - extra_copy = copy.deepcopy(self.extra) - extra_copy['id'] = self.id - extra_copy['name'] = self.name - return extra_copy - class Role(sql.ModelBase, sql.DictBase): __tablename__ = 'role' + attributes = ['id', 'name'] id = sql.Column(sql.String(64), primary_key=True) name = sql.Column(sql.String(64), unique=True, nullable=False) class Metadata(sql.ModelBase, sql.DictBase): __tablename__ = 'metadata' - #__table_args__ = ( - # sql.Index('idx_metadata_usertenant', 'user', 'tenant'), - # ) - user_id = sql.Column(sql.String(64), primary_key=True) tenant_id = sql.Column(sql.String(64), primary_key=True) data = sql.Column(sql.JsonBlob()) + def to_dict(self): + """Override parent to_dict() method with a simpler implementation. + + Metadata doesn't have non-indexed 'extra' attributes, so the parent + implementation is not applicable. + """ + return dict(self.iteritems()) + class UserTenantMembership(sql.ModelBase, sql.DictBase): """Tenant membership join table.""" @@ -369,7 +337,7 @@ class Identity(sql.Base, identity.Driver): user_ref.name = new_user.name user_ref.extra = new_user.extra session.flush() - return user_ref + return identity.filter_user(user_ref.to_dict()) def delete_user(self, user_id): session = self.get_session() @@ -410,7 +378,7 @@ class Identity(sql.Base, identity.Driver): tenant_ref.name = new_tenant.name tenant_ref.extra = new_tenant.extra session.flush() - return tenant_ref + return tenant_ref.to_dict() def delete_tenant(self, tenant_id): session = self.get_session() diff --git a/keystone/token/backends/sql.py b/keystone/token/backends/sql.py index be880f72..45919c01 100644 --- a/keystone/token/backends/sql.py +++ b/keystone/token/backends/sql.py @@ -18,7 +18,6 @@ import copy import datetime -from keystone.common import cms from keystone.common import sql from keystone import exception from keystone.openstack.common import timeutils @@ -27,27 +26,12 @@ from keystone import token class TokenModel(sql.ModelBase, sql.DictBase): __tablename__ = 'token' + attributes = ['id', 'expires'] id = sql.Column(sql.String(64), primary_key=True) expires = sql.Column(sql.DateTime(), default=None) extra = sql.Column(sql.JsonBlob()) valid = sql.Column(sql.Boolean(), default=True) - @classmethod - def from_dict(cls, token_dict): - # shove any non-indexed properties into extra - extra = copy.deepcopy(token_dict) - data = {} - for k in ('id', 'expires'): - data[k] = extra.pop(k, None) - data['extra'] = extra - return cls(**data) - - def to_dict(self): - out = copy.deepcopy(self.extra) - out['id'] = self.id - out['expires'] = self.expires - return out - class Token(sql.Base, token.Driver): # Public interface |
