From 3a825e2e078fe487d9fdc738bafae9f2c9d597a5 Mon Sep 17 00:00:00 2001 From: lvdongbing Date: Tue, 12 Apr 2016 05:00:40 -0400 Subject: [PATCH] Add 'name' field to user object Change-Id: I47bd9e0b35e61e9999c4563b797764a7e002b086 --- bilean/common/consts.py | 12 ++++++--- bilean/db/sqlalchemy/api.py | 1 + .../migrate_repo/versions/001_bilean_init.py | 1 + bilean/db/sqlalchemy/models.py | 1 + bilean/engine/service.py | 2 ++ bilean/engine/user.py | 25 ++++++++++++++++--- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/bilean/common/consts.py b/bilean/common/consts.py index e7a4e70..60b69dc 100644 --- a/bilean/common/consts.py +++ b/bilean/common/consts.py @@ -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', ) diff --git a/bilean/db/sqlalchemy/api.py b/bilean/db/sqlalchemy/api.py index 9bdf882..3ba1b68 100644 --- a/bilean/db/sqlalchemy/api.py +++ b/bilean/db/sqlalchemy/api.py @@ -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, } diff --git a/bilean/db/sqlalchemy/migrate_repo/versions/001_bilean_init.py b/bilean/db/sqlalchemy/migrate_repo/versions/001_bilean_init.py index 1b26629..c3825ce 100644 --- a/bilean/db/sqlalchemy/migrate_repo/versions/001_bilean_init.py +++ b/bilean/db/sqlalchemy/migrate_repo/versions/001_bilean_init.py @@ -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'), diff --git a/bilean/db/sqlalchemy/models.py b/bilean/db/sqlalchemy/models.py index 6ae853a..ca7bd2d 100644 --- a/bilean/db/sqlalchemy/models.py +++ b/bilean/db/sqlalchemy/models.py @@ -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'), diff --git a/bilean/engine/service.py b/bilean/engine/service.py index 7d33959..5fc0b03 100644 --- a/bilean/engine/service.py +++ b/bilean/engine/service.py @@ -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) diff --git a/bilean/engine/user.py b/bilean/engine/user.py index ea3388a..54ba477 100644 --- a/bilean/engine/user.py +++ b/bilean/engine/user.py @@ -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,