Skip to content

Commit

Permalink
Return non-indexed attrs, not 'extra' (bug 1075376)
Browse files Browse the repository at this point in the history
(most of this is pulled from the v3 branch)

Change-Id: Id1118e7a2b245fb7ec95e41ec297c87036953db2
  • Loading branch information
dolph committed Nov 6, 2012
1 parent a6ef09d commit df148a0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 95 deletions.
34 changes: 2 additions & 32 deletions keystone/catalog/backends/sql.py
Expand Up @@ -28,52 +28,22 @@

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),
sql.ForeignKey('service.id'),
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):
Expand Down
15 changes: 14 additions & 1 deletion keystone/common/sql/core.py
Expand Up @@ -57,9 +57,22 @@ def process_result_value(self, value, dialect):


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)
Expand Down
58 changes: 13 additions & 45 deletions keystone/identity/backends/sql.py
Expand Up @@ -14,7 +14,6 @@
# License for the specific language governing permissions and limitations
# under the License.

import copy
import functools

from keystone import clean
Expand All @@ -40,72 +39,41 @@ def wrapper(*args, **kwargs):

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."""
Expand Down Expand Up @@ -369,7 +337,7 @@ def update_user(self, user_id, user):
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()
Expand Down Expand Up @@ -410,7 +378,7 @@ def update_tenant(self, tenant_id, tenant):
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()
Expand Down
18 changes: 1 addition & 17 deletions keystone/token/backends/sql.py
Expand Up @@ -18,7 +18,6 @@
import datetime


from keystone.common import cms
from keystone.common import sql
from keystone import exception
from keystone.openstack.common import timeutils
Expand All @@ -27,27 +26,12 @@

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
Expand Down

0 comments on commit df148a0

Please sign in to comment.