-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
103 lines (89 loc) · 4.4 KB
/
database.py
File metadata and controls
103 lines (89 loc) · 4.4 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import sqlite3
__author__ = 'samuels'
TEST_SUITE_DB_PATH = "vms.db"
VM_HOST_FOLDER_TABLE_NAME = "vmhost_folder_table"
VM_COMPUTE_RESOURCE_TABLE_NAME = "compute_resource_table"
VMS_TABLE_NAME = "vms_table"
class DataBase:
def __init__(self, logger):
self._logger = logger
self._db_connection = sqlite3.connect(TEST_SUITE_DB_PATH)
self._cursor = self._db_connection.cursor()
self._cursor.execute("DROP TABLE IF EXISTS %s" % VM_HOST_FOLDER_TABLE_NAME)
self._cursor.execute("DROP TABLE IF EXISTS %s" % VM_COMPUTE_RESOURCE_TABLE_NAME)
self._cursor.execute("DROP TABLE IF EXISTS %s" % VMS_TABLE_NAME)
self._cursor.execute('''CREATE TABLE IF NOT EXISTS %s
(
id INTEGER PRIMARY KEY,
name TEXT
)''' % VM_HOST_FOLDER_TABLE_NAME)
self._cursor.execute('''CREATE TABLE IF NOT EXISTS %s
(
id INTEGER PRIMARY KEY,
name TEXT,
folder_name TEXT,
FOREIGN KEY (name) REFERENCES %s (folder_name)
)''' % (VM_COMPUTE_RESOURCE_TABLE_NAME, VM_HOST_FOLDER_TABLE_NAME))
self._cursor.execute('''CREATE TABLE IF NOT EXISTS %s
(
id INTEGER PRIMARY KEY,
folder_name TEXT,
cmp_res_name TEXT,
name TEXT,
path TEXT,
guest TEXT,
uuid TEXT,
cpunum INTEGER,
ram text,
state text,
ip text,
FOREIGN KEY(cmp_res_name) REFERENCES %s(name)
)''' % (VMS_TABLE_NAME, VM_COMPUTE_RESOURCE_TABLE_NAME))
def insert_host_folder_object(self, host_folder):
""":type host_folder: VMHostFolder"""
try:
self._cursor.execute('insert into %s values (?,?)' % VM_HOST_FOLDER_TABLE_NAME,
(None, host_folder.name,))
self._db_connection.commit()
except Exception as err:
self._logger.exception('Table %s, Insert Query Failed: %s\n Error: %s' % (
VM_HOST_FOLDER_TABLE_NAME, (host_folder.name,),
str(err)))
raise err
def insert_compute_resource_object(self, compute_resource):
""":type compute_resource: ComputeResource"""
try:
self._cursor.execute('insert into %s values (?,?,?)' % VM_COMPUTE_RESOURCE_TABLE_NAME,
(None, compute_resource.name, compute_resource.folder_name,))
except Exception as err:
self._logger.exception('Table %s, Insert Query Failed: %s\n Error: %s' % (
VM_COMPUTE_RESOURCE_TABLE_NAME,
(compute_resource.name, compute_resource.folder_name),
str(err)))
self._db_connection.commit()
raise err
def insert_virtual_machine_object(self, virtual_machine):
""":type virtual_machine: VirtualMachine"""
try:
self._cursor.execute('insert into %s values (?,?,?,?,?,?,?,?,?,?,?)' % VMS_TABLE_NAME,
(
None, virtual_machine.folder_name, virtual_machine.cmp_res_name,
virtual_machine.name,
virtual_machine.path, virtual_machine.guest,
virtual_machine.UUID, virtual_machine.num_of_cpus, virtual_machine.ram,
virtual_machine.state, virtual_machine.ip,))
self._db_connection.commit()
except Exception as err:
self._logger.exception('Table %s, Insert Query Failed: %s\n Error: %s' % (
VMS_TABLE_NAME,
(virtual_machine.foder_name, virtual_machine.cmp_res_name, virtual_machine.name, virtual_machine.path,
virtual_machine.guest,
virtual_machine.UUID, virtual_machine.num_of_cpus, virtual_machine.ram,
virtual_machine.state, virtual_machine.ip),
str(err)))
raise err
# def insert(self, item):
# return item.insert(self)
def __del__(self):
self._logger.info("Closing DB connection...")
self._db_connection.close()