Added a hosts db table, added update_host
This commit is contained in:
parent
5be7485e9c
commit
f2a21599d5
38
neat/db.py
38
neat/db.py
@ -21,15 +21,20 @@ class Database(object):
|
||||
""" A class representing the database, where fields are tables.
|
||||
"""
|
||||
|
||||
@contract(connection=Connection, vms=Table, vm_resource_usage=Table)
|
||||
def __init__(self, connection, vms, vm_resource_usage):
|
||||
@contract(connection=Connection,
|
||||
hosts=Table,
|
||||
vms=Table,
|
||||
vm_resource_usage=Table)
|
||||
def __init__(self, connection, hosts, vms, vm_resource_usage):
|
||||
""" Initialize the database.
|
||||
|
||||
:param connection: A database connection table.
|
||||
:param hosts: The hosts table.
|
||||
:param vms: The vms table.
|
||||
:param vm_resource_usage: The vm_resource_usage table.
|
||||
"""
|
||||
self.connection = connection
|
||||
self.hosts = hosts
|
||||
self.vms = vms
|
||||
self.vm_resource_usage = vm_resource_usage
|
||||
|
||||
@ -86,3 +91,32 @@ class Database(object):
|
||||
query.append({'vm_id': vm_id,
|
||||
'cpu_mhz': cpu_mhz})
|
||||
self.vm_resource_usage.insert().execute(query)
|
||||
|
||||
@contract
|
||||
def update_host(self, hostname, cpu_mhz, ram):
|
||||
""" Insert new or update the corresponding host record.
|
||||
|
||||
:param hostname: A host name.
|
||||
:type hostname: str
|
||||
|
||||
:param cpu_mhz: The total CPU frequency of the host in MHz.
|
||||
:type cpu_mhz: int,>0
|
||||
|
||||
:param ram: The total amount of RAM of the host in MB.
|
||||
:type ram: int,>0
|
||||
|
||||
:return: The ID of the host.
|
||||
:rtype: int
|
||||
"""
|
||||
sel = select([self.hosts.c.id]).where(self.hosts.c.hostname == hostname)
|
||||
row = self.connection.execute(sel).fetchone()
|
||||
if row == None:
|
||||
return self.hosts.insert().execute(hostname=hostname,
|
||||
cpu_mhz=cpu_mhz,
|
||||
ram=ram).inserted_primary_key[0]
|
||||
else:
|
||||
self.connection.execute(self.hosts.update().
|
||||
where(self.hosts.c.id == row['id']).
|
||||
values(cpu_mhz=cpu_mhz,
|
||||
ram=ram))
|
||||
return row['id']
|
||||
|
@ -34,6 +34,12 @@ def init_db(sql_connection):
|
||||
metadata = MetaData()
|
||||
metadata.bind = engine
|
||||
|
||||
hosts = Table('hosts', metadata,
|
||||
Column('id', Integer, primary_key=True),
|
||||
Column('hostname', String(255), nullable=False),
|
||||
Column('cpu_mhz', Integer, nullable=False),
|
||||
Column('ram', Integer, nullable=False))
|
||||
|
||||
vms = Table('vms', metadata,
|
||||
Column('id', Integer, primary_key=True),
|
||||
Column('uuid', String(36), nullable=False))
|
||||
@ -48,4 +54,4 @@ def init_db(sql_connection):
|
||||
metadata.create_all()
|
||||
connection = engine.connect()
|
||||
|
||||
return Database(connection, vms, vm_resource_usage)
|
||||
return Database(connection, hosts, vms, vm_resource_usage)
|
||||
|
@ -84,3 +84,22 @@ class Db(TestCase):
|
||||
|
||||
for uuid, data in final_data.items():
|
||||
assert db.select_cpu_mhz_for_vm(uuid, 11) == data
|
||||
|
||||
@qc(1)
|
||||
def update_host():
|
||||
db = db_utils.init_db('sqlite:///:memory:')
|
||||
db.update_host('host1', 3000, 4000)
|
||||
hosts = db.hosts.select().execute().fetchall()
|
||||
assert len(hosts) == 1
|
||||
host = hosts[0]
|
||||
assert host['hostname'] == 'host1'
|
||||
assert host['cpu_mhz'] == 3000
|
||||
assert host['ram'] == 4000
|
||||
|
||||
db.update_host('host1', 3500, 8000)
|
||||
hosts = db.hosts.select().execute().fetchall()
|
||||
assert len(hosts) == 1
|
||||
host = hosts[0]
|
||||
assert host['hostname'] == 'host1'
|
||||
assert host['cpu_mhz'] == 3500
|
||||
assert host['ram'] == 8000
|
||||
|
@ -27,8 +27,11 @@ class DbUtils(TestCase):
|
||||
def init_db():
|
||||
db = db_utils.init_db('sqlite:///:memory:')
|
||||
assert type(db) is neat.db.Database
|
||||
assert isinstance(db.hosts, Table)
|
||||
assert isinstance(db.vms, Table)
|
||||
assert isinstance(db.vm_resource_usage, Table)
|
||||
assert db.hosts.c.keys() == \
|
||||
['id', 'hostname', 'cpu_mhz', 'ram']
|
||||
assert db.vms.c.keys() == \
|
||||
['id', 'uuid']
|
||||
assert db.vm_resource_usage.c.keys() == \
|
||||
|
Loading…
x
Reference in New Issue
Block a user