-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
executable file
·59 lines (50 loc) · 1.68 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import ConfigParser
import datetime
import sys
from sqlalchemy import and_
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import MetaData
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select
from sqlalchemy import Table
def makeConnection(db_url):
engine = create_engine(db_url)
engine.connect()
Session = sessionmaker(bind=engine)
thisSession = Session()
metadata = MetaData()
metadata.bind = engine
Base = declarative_base()
tpl = thisSession, metadata, Base
return tpl
def update_quota_usages_db(meta, tenant_id, resource, in_use):
quota_usages = Table('quota_usages', meta, autoload=True)
now = datetime.datetime.utcnow()
quota = select(
columns=[quota_usages.c.tenant_id],
whereclause=and_(
quota_usages.c.tenant_id == tenant_id,
quota_usages.c.resource == resource)).execute().fetchone()
if not quota:
quota_usages.insert().values(
created=now,
updated=now,
tenant_id=tenant_id,
resource=resource,
in_use=in_use,
reserved=0).execute()
else:
quota_usages.update().where(
and_(quota_usages.c.tenant_id == tenant_id,
quota_usages.c.resource == resource)).values(
updated=now, in_use=in_use, reserved=0).execute()
def get_db_url(config_file):
parser = ConfigParser.SafeConfigParser()
try:
parser.read(config_file)
db_url = parser.get('database', 'connection')
except Exception:
print("ERROR: Check cinder configuration file.")
sys.exit(2)
return db_url