Merge "Add 'name' field to user object"
This commit is contained in:
commit
2c9d918525
@ -17,7 +17,7 @@ MAX_VALUE = 100000000
|
||||
MIN_RESOURCE_NUM = 1
|
||||
MAX_RESOURCE_NUM = 1000
|
||||
|
||||
RPC_ATTRs = (
|
||||
RPC_ATTRS = (
|
||||
ENGINE_TOPIC,
|
||||
SCHEDULER_TOPIC,
|
||||
NOTIFICATION_TOPICS,
|
||||
@ -31,6 +31,12 @@ RPC_ATTRs = (
|
||||
'1.0',
|
||||
)
|
||||
|
||||
USER_STATUSES = (
|
||||
USER_INIT, USER_FREE, USER_ACTIVE, USER_WARNING, USER_FREEZE,
|
||||
) = (
|
||||
'INIT', 'FREE', 'ACTIVE', 'WARNING', 'FREEZE',
|
||||
)
|
||||
|
||||
ACTION_NAMES = (
|
||||
USER_CREATE_RESOURCE, USER_UPDATE_RESOURCE, USER_DELETE_RESOURCE,
|
||||
USER_SETTLE_ACCOUNT,
|
||||
@ -58,11 +64,11 @@ RPC_PARAMS = (
|
||||
)
|
||||
|
||||
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_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',
|
||||
'updated_at', 'deleted_at',
|
||||
)
|
||||
|
@ -180,6 +180,7 @@ def user_get_all(context, show_deleted=False, limit=None,
|
||||
sort_key_map = {
|
||||
consts.USER_CREATED_AT: models.User.created_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_STATUS: models.User.status.key,
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ def upgrade(migrate_engine):
|
||||
'user', meta,
|
||||
sqlalchemy.Column('id', sqlalchemy.String(36), primary_key=True,
|
||||
nullable=False),
|
||||
sqlalchemy.Column('name', sqlalchemy.String(255)),
|
||||
sqlalchemy.Column('policy_id',
|
||||
sqlalchemy.String(36),
|
||||
sqlalchemy.ForeignKey('policy.id'),
|
||||
|
@ -97,6 +97,7 @@ class User(BASE, BileanBase, SoftDelete, StateAware, models.TimestampMixin):
|
||||
__tablename__ = 'user'
|
||||
|
||||
id = sqlalchemy.Column(sqlalchemy.String(36), primary_key=True)
|
||||
name = sqlalchemy.Column(sqlalchemy.String(255))
|
||||
policy_id = sqlalchemy.Column(
|
||||
sqlalchemy.String(36),
|
||||
sqlalchemy.ForeignKey('policy.id'),
|
||||
|
@ -269,6 +269,8 @@ class EngineService(service.Service):
|
||||
def user_create(self, cnxt, user_id, balance=None, credit=None,
|
||||
status=None):
|
||||
"""Create a new user from notification."""
|
||||
if status is None:
|
||||
status = consts.USER_INIT
|
||||
user = user_mod.User(user_id, balance=balance, credit=credit,
|
||||
status=status)
|
||||
user.store(cnxt)
|
||||
|
@ -41,6 +41,7 @@ class User(object):
|
||||
|
||||
def __init__(self, user_id, **kwargs):
|
||||
self.id = user_id
|
||||
self.name = kwargs.get('name')
|
||||
self.policy_id = kwargs.get('policy_id')
|
||||
self.balance = kwargs.get('balance', 0)
|
||||
self.rate = kwargs.get('rate', 0.0)
|
||||
@ -54,10 +55,14 @@ class User(object):
|
||||
self.updated_at = kwargs.get('updated_at')
|
||||
self.deleted_at = kwargs.get('deleted_at')
|
||||
|
||||
if self.name is None:
|
||||
self.name = self._retrieve_name(self.id)
|
||||
|
||||
def store(self, context):
|
||||
"""Store the user record into database table."""
|
||||
|
||||
values = {
|
||||
'name': self.name,
|
||||
'policy_id': self.policy_id,
|
||||
'balance': self.balance,
|
||||
'rate': self.rate,
|
||||
@ -90,17 +95,27 @@ class User(object):
|
||||
six.text_type(ex))
|
||||
return False
|
||||
|
||||
project_ids = [project.id for project in projects]
|
||||
users = cls.load_all(context)
|
||||
user_ids = [user.id for user in users]
|
||||
for pid in project_ids:
|
||||
if pid not in user_ids:
|
||||
user = cls(pid, status=cls.INIT,
|
||||
for project in projects:
|
||||
if project.id not in user_ids:
|
||||
user = cls(project.id, name=project.name, status=cls.INIT,
|
||||
status_reason='Init from keystone')
|
||||
user.store(context)
|
||||
users.append(user)
|
||||
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
|
||||
def _from_db_record(cls, 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;
|
||||
'''
|
||||
kwargs = {
|
||||
'name': record.name,
|
||||
'policy_id': record.policy_id,
|
||||
'balance': record.balance,
|
||||
'rate': record.rate,
|
||||
@ -175,6 +191,7 @@ class User(object):
|
||||
def to_dict(self):
|
||||
user_dict = {
|
||||
'id': self.id,
|
||||
'name': self.name,
|
||||
'policy_id': self.policy_id,
|
||||
'balance': self.balance,
|
||||
'rate': self.rate,
|
||||
|
Loading…
Reference in New Issue
Block a user