import ng_openstack.settings import pymysql.cursors import logging import exceptions import os,settings SQL_HOST = os.getenv("SQL_HOST") SQL_USER = os.getenv("SQL_USER") SQL_PASS = os.getenv("SQL_PASS") SQL_DB = os.getenv("SQL_DB") def lookupIP_FromSQL(IP): SQL = " SELECT project_id,status FROM neutron.floatingips where floating_ip_address=%s" connection = pymysql.connect(user=SQL_USER, db=SQL_DB, host=SQL_HOST, password=SQL_PASS, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) with connection.cursor() as cursor: if cursor.execute(SQL, (IP))>=1: result = cursor.fetchone() connection.close() returnData = ipLookupResult() returnData.customerID = result['project_id'] returnData.bypassLogging = 0 returnData.IP = IP return returnData else: connection.close() raise exceptions.ItemNotFoundError("No results in floatingIP database for IP:{}".format(IP)) def getCountbyProject_FromSQL(projectID): print("Querying Floating IP data from database") # Connect to the database connection = pymysql.connect(user=SQL_USER, db=SQL_DB, host=SQL_HOST, password=SQL_PASS, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) totalFloatingIPs = 0 returndata = {} IPArray = [] try: with connection.cursor() as cursor: # Read a single record sql = "SELECT floating_ip_address FROM neutron.floatingips where project_id=%s;" cursor.execute(sql, (projectID,)) result = cursor.fetchall() for thisItem in result: IPArray.append(thisItem['floating_ip_address']) totalFloatingIPs = len(result) finally: connection.close() returndata['total'] = totalFloatingIPs returndata['floating_ip_address'] = IPArray return returndata def getAllBYOSubnetsfromSQL(): sql = "SELECT * , inet_ntoa(network) as readable_network, inet_ntoa(netmask) as readable_netmask, 32 - bit_count(power(2, 32) - netmask - 1) as cidr FROM `networks` where byo=1 ORDER BY `netmask` ASC " connection = pymysql.connect(user=SQL_USER, db=SQL_DB, host=SQL_HOST, password=SQL_PASS, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) returnData = [] try: with connection.cursor() as cursor: cursor.execute(sql, ()) result = cursor.fetchall() for thisItem in result: returnData.append({"readable_network": thisItem['readable_network'], "readable_netmask": thisItem['readable_netmask'],"int_netmask": thisItem['netmask'],"int_network": thisItem['network'] }) connection.close() return returnData except: connection.close()