timestamp consistency for insert_tenant

Change-Id: I7765b38061a91915e1195afbeb8c6efdef0de966
This commit is contained in:
Chris Forbes 2014-03-25 16:29:25 +13:00
parent 0a2a978096
commit 2f35343b51
3 changed files with 7 additions and 6 deletions

View File

@ -105,8 +105,9 @@ def keystone(func):
def collect_usage(tenant, db, session, resp, end): def collect_usage(tenant, db, session, resp, end):
timestamp = datetime.now()
db.insert_tenant(tenant.conn['id'], tenant.conn['name'], db.insert_tenant(tenant.conn['id'], tenant.conn['name'],
tenant.conn['description']) tenant.conn['description'], timestamp)
session.begin(subtransactions=True) session.begin(subtransactions=True)
start = session.query(func.max(UsageEntry.end).label('end')).\ start = session.query(func.max(UsageEntry.end).label('end')).\
filter(UsageEntry.tenant_id == tenant.conn['id']).first().end filter(UsageEntry.tenant_id == tenant.conn['id']).first().end
@ -114,7 +115,6 @@ def collect_usage(tenant, db, session, resp, end):
start = datetime.strptime(dawn_of_time, iso_date) start = datetime.strptime(dawn_of_time, iso_date)
usage = tenant.usage(start, end) usage = tenant.usage(start, end)
timestamp = datetime.now()
# enter all resources into the db # enter all resources into the db
db.enter(usage.values(), start, end, timestamp) db.enter(usage.values(), start, end, timestamp)

View File

@ -12,7 +12,7 @@ class Database(object):
# engine = create_engine(os.environ["DATABASE_URL"]) # engine = create_engine(os.environ["DATABASE_URL"])
# Base.metadata.create_all(engine) # Base.metadata.create_all(engine)
def insert_tenant(self, tenant_id, tenant_name, metadata): def insert_tenant(self, tenant_id, tenant_name, metadata, timestamp):
"""If a tenant exists does nothing, """If a tenant exists does nothing,
and if it doesn't, creates and inserts it.""" and if it doesn't, creates and inserts it."""
# Have we seen this tenant before? # Have we seen this tenant before?
@ -22,7 +22,7 @@ class Database(object):
self.session.add(Tenant(id=tenant_id, self.session.add(Tenant(id=tenant_id,
info=metadata, info=metadata,
name=tenant_name, name=tenant_name,
created=datetime.now() created=timestamp
)) ))
self.session.flush() self.session.flush()

View File

@ -12,16 +12,17 @@ class TestDatabaseModule(test_interface.TestInterface):
"""Tests adding all the data to the database.""" """Tests adding all the data to the database."""
usage = helpers.get_usage() usage = helpers.get_usage()
timestamp = datetime.now()
db = database.Database(self.session) db = database.Database(self.session)
db.insert_tenant(TENANT_ID, db.insert_tenant(TENANT_ID,
"demo", "") "demo", "", timestamp)
# patch to mock out the novaclient call # patch to mock out the novaclient call
with mock.patch('artifice.helpers.flavor_name') as flavor_name: with mock.patch('artifice.helpers.flavor_name') as flavor_name:
flavor_name.side_effect = lambda x: x flavor_name.side_effect = lambda x: x
db.enter(usage.values(), self.start, self.end, datetime.now()) db.enter(usage.values(), self.start, self.end, timestamp)
count = 0 count = 0
for val in usage.values(): for val in usage.values():