def check_ip_port_used(network_id, ip): db = dbpools.get_neutron() try: sql = "select count(*) as num from ipallocations where 1= 1 and network_id = %s and ip_address = %s" cur = yield db.execute(sql, (network_id, ip)) ips = cur.fetchone() except Exception, e: raise e
def check_mac(mac): try: db = dbpools.get_neutron() sql = "select id from ports where mac_address = %s" cur = yield db.execute(sql, (mac)) resp = cur.fetchone() if resp: raise MacHasExist except Exception, e: LOG.error("check mac error: %s" % e) raise e
def count_subnet(): db = dbpools.get_neutron() network_count = {} try: cur = yield db.execute( "select network_id, count(*) as count from subnets GROUP BY network_id" ) netwrok_subnet = cur.fetchall() for subnet in netwrok_subnet: network_count[subnet["network_id"]] = subnet["count"] except Exception, e: LOG.error("subnet count error: %s" % e) raise e
def get_port(port_id): try: db = dbpools.get_neutron() sql = ( "select a.port_id,a.ip_address as ip ,b.`name` ,b.id as vlan_id, " "b.tenant_id as tenant_id from ipallocations as a LEFT JOIN " "networks as b on a.network_id = b.id where a.port_id = %s") cur = yield db.execute(sql, [port_id]) result = cur.fetchone() except Exception as e: LOG.error("get port from db error: %s" % e) raise e raise gen.Return(result)
def check_vlan_id(vlan_type, phy_network, vlan_id): db = dbpools.get_neutron() try: if "vlan" == vlan_type: cur = yield db.execute( "select * from ml2_vlan_allocations where allocated != 0 and physical_network = %s and vlan_id = %s", (phy_network, vlan_id)) elif "flat" == vlan_type: cur = yield db.execute( "select * from ml2_flat_allocations where physical_network = %s ", (phy_network)) vlans = cur.fetchall() except Exception as e: LOG.error("check vlan id used , db error: %s" % e) raise e if vlans: raise VlanUsedError