Merge "Add 'name' field to user object"

This commit is contained in:
Jenkins 2016-04-12 09:33:11 +00:00 committed by Gerrit Code Review
commit 2c9d918525
6 changed files with 35 additions and 7 deletions

View File

@ -17,7 +17,7 @@ MAX_VALUE = 100000000
MIN_RESOURCE_NUM = 1 MIN_RESOURCE_NUM = 1
MAX_RESOURCE_NUM = 1000 MAX_RESOURCE_NUM = 1000
RPC_ATTRs = ( RPC_ATTRS = (
ENGINE_TOPIC, ENGINE_TOPIC,
SCHEDULER_TOPIC, SCHEDULER_TOPIC,
NOTIFICATION_TOPICS, NOTIFICATION_TOPICS,
@ -31,6 +31,12 @@ RPC_ATTRs = (
'1.0', '1.0',
) )
USER_STATUSES = (
USER_INIT, USER_FREE, USER_ACTIVE, USER_WARNING, USER_FREEZE,
) = (
'INIT', 'FREE', 'ACTIVE', 'WARNING', 'FREEZE',
)
ACTION_NAMES = ( ACTION_NAMES = (
USER_CREATE_RESOURCE, USER_UPDATE_RESOURCE, USER_DELETE_RESOURCE, USER_CREATE_RESOURCE, USER_UPDATE_RESOURCE, USER_DELETE_RESOURCE,
USER_SETTLE_ACCOUNT, USER_SETTLE_ACCOUNT,
@ -58,11 +64,11 @@ RPC_PARAMS = (
) )
USER_KEYS = ( USER_KEYS = (
USER_ID, USER_POLICY_ID, USER_BALANCE, USER_RATE, USER_CREDIT, USER_ID, USER_NAME, USER_POLICY_ID, USER_BALANCE, USER_RATE, USER_CREDIT,
USER_LAST_BILL, USER_STATUS, USER_STATUS_REASION, USER_CREATED_AT, USER_LAST_BILL, USER_STATUS, USER_STATUS_REASION, USER_CREATED_AT,
USER_UPDATED_AT, USER_DELETED_AT, USER_UPDATED_AT, USER_DELETED_AT,
) = ( ) = (
'id', 'policy_id', 'balance', 'rate', 'credit', 'id', 'name', 'policy_id', 'balance', 'rate', 'credit',
'last_bill', 'status', 'status_reason', 'created_at', 'last_bill', 'status', 'status_reason', 'created_at',
'updated_at', 'deleted_at', 'updated_at', 'deleted_at',
) )

View File

@ -180,6 +180,7 @@ def user_get_all(context, show_deleted=False, limit=None,
sort_key_map = { sort_key_map = {
consts.USER_CREATED_AT: models.User.created_at.key, consts.USER_CREATED_AT: models.User.created_at.key,
consts.USER_UPDATED_AT: models.User.updated_at.key, consts.USER_UPDATED_AT: models.User.updated_at.key,
consts.USER_NAME: models.User.name.key,
consts.USER_BALANCE: models.User.balance.key, consts.USER_BALANCE: models.User.balance.key,
consts.USER_STATUS: models.User.status.key, consts.USER_STATUS: models.User.status.key,
} }

View File

@ -24,6 +24,7 @@ def upgrade(migrate_engine):
'user', meta, 'user', meta,
sqlalchemy.Column('id', sqlalchemy.String(36), primary_key=True, sqlalchemy.Column('id', sqlalchemy.String(36), primary_key=True,
nullable=False), nullable=False),
sqlalchemy.Column('name', sqlalchemy.String(255)),
sqlalchemy.Column('policy_id', sqlalchemy.Column('policy_id',
sqlalchemy.String(36), sqlalchemy.String(36),
sqlalchemy.ForeignKey('policy.id'), sqlalchemy.ForeignKey('policy.id'),

View File

@ -97,6 +97,7 @@ class User(BASE, BileanBase, SoftDelete, StateAware, models.TimestampMixin):
__tablename__ = 'user' __tablename__ = 'user'
id = sqlalchemy.Column(sqlalchemy.String(36), primary_key=True) id = sqlalchemy.Column(sqlalchemy.String(36), primary_key=True)
name = sqlalchemy.Column(sqlalchemy.String(255))
policy_id = sqlalchemy.Column( policy_id = sqlalchemy.Column(
sqlalchemy.String(36), sqlalchemy.String(36),
sqlalchemy.ForeignKey('policy.id'), sqlalchemy.ForeignKey('policy.id'),

View File

@ -269,6 +269,8 @@ class EngineService(service.Service):
def user_create(self, cnxt, user_id, balance=None, credit=None, def user_create(self, cnxt, user_id, balance=None, credit=None,
status=None): status=None):
"""Create a new user from notification.""" """Create a new user from notification."""
if status is None:
status = consts.USER_INIT
user = user_mod.User(user_id, balance=balance, credit=credit, user = user_mod.User(user_id, balance=balance, credit=credit,
status=status) status=status)
user.store(cnxt) user.store(cnxt)

View File

@ -41,6 +41,7 @@ class User(object):
def __init__(self, user_id, **kwargs): def __init__(self, user_id, **kwargs):
self.id = user_id self.id = user_id
self.name = kwargs.get('name')
self.policy_id = kwargs.get('policy_id') self.policy_id = kwargs.get('policy_id')
self.balance = kwargs.get('balance', 0) self.balance = kwargs.get('balance', 0)
self.rate = kwargs.get('rate', 0.0) self.rate = kwargs.get('rate', 0.0)
@ -54,10 +55,14 @@ class User(object):
self.updated_at = kwargs.get('updated_at') self.updated_at = kwargs.get('updated_at')
self.deleted_at = kwargs.get('deleted_at') self.deleted_at = kwargs.get('deleted_at')
if self.name is None:
self.name = self._retrieve_name(self.id)
def store(self, context): def store(self, context):
"""Store the user record into database table.""" """Store the user record into database table."""
values = { values = {
'name': self.name,
'policy_id': self.policy_id, 'policy_id': self.policy_id,
'balance': self.balance, 'balance': self.balance,
'rate': self.rate, 'rate': self.rate,
@ -90,17 +95,27 @@ class User(object):
six.text_type(ex)) six.text_type(ex))
return False return False
project_ids = [project.id for project in projects]
users = cls.load_all(context) users = cls.load_all(context)
user_ids = [user.id for user in users] user_ids = [user.id for user in users]
for pid in project_ids: for project in projects:
if pid not in user_ids: if project.id not in user_ids:
user = cls(pid, status=cls.INIT, user = cls(project.id, name=project.name, status=cls.INIT,
status_reason='Init from keystone') status_reason='Init from keystone')
user.store(context) user.store(context)
users.append(user) users.append(user)
return users return users
def _retrieve_name(cls, user_id):
'''Get user name form keystone.'''
keystoneclient = driver_base.BileanDriver().identity()
try:
project = keystoneclient.project_find(user_id)
except exception.InternalError as ex:
LOG.exception(_('Failed in retrieving project: %s'),
six.text_type(ex))
return None
return project.name
@classmethod @classmethod
def _from_db_record(cls, record): def _from_db_record(cls, record):
'''Construct a user object from database record. '''Construct a user object from database record.
@ -108,6 +123,7 @@ class User(object):
:param record: a DB user object that contains all fields; :param record: a DB user object that contains all fields;
''' '''
kwargs = { kwargs = {
'name': record.name,
'policy_id': record.policy_id, 'policy_id': record.policy_id,
'balance': record.balance, 'balance': record.balance,
'rate': record.rate, 'rate': record.rate,
@ -175,6 +191,7 @@ class User(object):
def to_dict(self): def to_dict(self):
user_dict = { user_dict = {
'id': self.id, 'id': self.id,
'name': self.name,
'policy_id': self.policy_id, 'policy_id': self.policy_id,
'balance': self.balance, 'balance': self.balance,
'rate': self.rate, 'rate': self.rate,