diff options
author | Jonas Lindstad <jonaslindstad@gmail.com> | 2015-02-19 04:49:49 +0100 |
---|---|---|
committer | Jonas Lindstad <jonaslindstad@gmail.com> | 2015-02-19 04:49:49 +0100 |
commit | cfe6e335dbceab9166ac12047e7d66c7728515b8 (patch) | |
tree | 460f2d7a656421a80821735b3aa9b5f7b017f32f | |
parent | 2f6de0f4099d83cde0f59eb12f0ded56385018f8 (diff) |
Replaced initial implementation - now supports updating fields. A bit more query heavy now, but should not cause any problems
-rw-r--r-- | junos-bootstrap/dhcpd/module_lease.py | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/junos-bootstrap/dhcpd/module_lease.py b/junos-bootstrap/dhcpd/module_lease.py index 8538723..306f13a 100644 --- a/junos-bootstrap/dhcpd/module_lease.py +++ b/junos-bootstrap/dhcpd/module_lease.py @@ -83,3 +83,92 @@ class lease(object): else: print('identifiers (%s) not found' % self.row) return False + + +# +# TESTING - Bruker ID fra DB-en som identifier, og kjører en query per lease.get_x() +# +class lease2(object): + debug = False + hostname = False + identifiers = False + + # identifiers = dict of field/values + def __init__(self, identifiers): + cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) + + if len(identifiers) > 0: # 1 or more identifiers - we're good to go + self.identifiers = identifiers # Used to debug if no match for the identifiers is given + + # build query string + where_pieces = [] + for identifier in identifiers.items(): + where_pieces.append(str(identifier[0]) + " = '" + str(identifier[1]) + "'") + where = ' AND '.join(where_pieces) + select = "SELECT hostname FROM switches WHERE " + where + " LIMIT 1" + + if self.debug is True: + print('Executing query: ' + select) + + cur.execute(select) + rows = cur.fetchall() + cur.close() + if len(rows) is 1: + if self.debug is True: + print('returned from DB:') + print(rows[0][0]) + self.hostname = rows[0][0] + else: + self.hostname = False + else: + print('Missing identifier parameter') + exit() + + # Used to fetch fields from DB + def get(self, field): + if self.hostname is not False: + cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) + + query = "SELECT %s FROM switches WHERE hostname = '%s' LIMIT 1" % (field, self.hostname) + if self.debug is True: + print('Query: %s' % query) + + try: + cur.execute(query) + rows = cur.fetchall() + + if len(rows) is 1: + if self.debug is True: + print('returned from DB:') + print(rows[0][0]) + return rows[0][0] + else: + if self.debug is True: + print('No data found - field: %s' % field) + return False + except psycopg2.ProgrammingError: + print('Field (%s) not found' % field) + conn.rollback() # Prevents DB from locking up the next queries - http://initd.org/psycopg/docs/connection.html#connection.rollback + return False + else: + print('identifiers (%s) not found' % self.identifiers) + return False + + # Used to set fields in DB + def set(self, field, value): + if self.hostname is not False: + cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) + query = "UPDATE switches SET %s = '%s' WHERE hostname = '%s'" % (field, value, self.hostname) + if self.debug is True: + print('Query: %s' % query) + try: + cur.execute(query) + conn.commit() + return True + except psycopg2.ProgrammingError: + print('Field (%s) not found' % field) + conn.rollback() + return False + else: + print('identifiers (%s) not found' % self.identifiers) + return False |