def add_subnet(subnet, description, vlan, perform_checks=True): """Utility method for adding a (new) subnet based on the information given. If errors occur during the addition, Cerebrum- and/or Subnet-errors will be raised. TODO: parameter doc. """ s = Subnet(db) s.clear() s.populate(subnet, description, vlan=vlan) try: s.write_db(perform_checks=perform_checks) except db.DatabaseError as m: raise CerebrumError("Database error: %s" % m)
def compare_file_to_db(subnets_in_file, force): """Analyzes the info obtained from the files that are to be imported, compares to content of database and makes changes were needed to bring the database in sync with the datafile. TODO: parameter doc. """ errors = [] changes = [] perform_checks = not force s = Subnet(db) subnets_in_db = s.search() for row in subnets_in_db: subnet_ID = "%s/%s" % (row['subnet_ip'], row['subnet_mask']) if subnet_ID in subnets_in_file: # Subnet is in file; nothing should be done with it if row['description'] != subnets_in_file[subnet_ID][1]: s.clear() s.find(row['entity_id']) s.description = subnets_in_file[subnet_ID][1] s.write_db(perform_checks=False) logger.debug("Updating description of subnet '%s'" % subnet_ID) changes.append("Updated description of subnet '%s'" % subnet_ID) else: logger.debug("Subnet '%s' in both DB and file; " % subnet_ID + "no description update") del subnets_in_file[subnet_ID] else: # Subnet is in DB, but not in file; try to remove it from DB try: logger.info("Deleting subnet '%s'" % subnet_ID) s.clear() s.find(subnet_ID) description = s.description s.delete(perform_checks=perform_checks) changes.append("Deleted subnet '%s' (%s)" % (subnet_ID, description)) except CerebrumError, ce: logger.error(str(ce)) errors.append(str(ce))
def same_subnet(s1, s2): from Cerebrum.Utils import Factory from Cerebrum.modules.dns.Errors import SubnetError from Cerebrum.modules.dns.Subnet import Subnet db = Factory.get('Database')() sub = Subnet(db) try: sub.find(s1) tmp = sub.subnet_ip sub.clear() sub.find(s2) except SubnetError: return False if tmp == sub.subnet_ip: return True else: return False
def compare_file_to_db(subnets_in_file, force): """Analyzes the info obtained from the files that are to be imported, compares to content of database and makes changes were needed to bring the database in sync with the datafile. TODO: parameter doc. """ errors = [] changes = [] perform_checks = not force s = Subnet(db) subnets_in_db = s.search() for row in subnets_in_db: subnet_ID = "%s/%s" % (row['subnet_ip'], row['subnet_mask']) if subnet_ID in subnets_in_file: # Subnet is in file; nothing should be done with it if row['description'] != subnets_in_file[subnet_ID][1]: s.clear() s.find(row['entity_id']) s.description = subnets_in_file[subnet_ID][1] s.write_db(perform_checks=False) logger.debug("Updating description of subnet '%s'" % subnet_ID) changes.append("Updated description of subnet '%s'" %subnet_ID) else: logger.debug("Subnet '%s' in both DB and file; " % subnet_ID + "no description update") del subnets_in_file[subnet_ID] else: # Subnet is in DB, but not in file; try to remove it from DB try: logger.info("Deleting subnet '%s'" % subnet_ID) s.clear() s.find(subnet_ID) description = s.description s.delete(perform_checks=perform_checks) changes.append("Deleted subnet '%s' (%s)" % (subnet_ID, description)) except CerebrumError, ce: logger.error(str(ce)) errors.append(str(ce))
def compare_file_to_db(subnets_in_file, force): """Analyzes the info obtained from the files that are to be imported, compares to content of database and makes changes were needed to bring the database in sync with the datafile. TODO: parameter doc. """ errors = [] changes = [] perform_checks = not force s = Subnet(db) subnets_in_db = s.search() for row in subnets_in_db: subnet = "%s/%s" % (row['subnet_ip'], row['subnet_mask']) if subnet in subnets_in_file: (description, vlan) = subnets_in_file[subnet] # Subnet is in file. Check if description or vlan have changed if row['description'] != description: s.clear() s.find(row['entity_id']) s.description = description s.write_db(perform_checks=False) logger.debug("Updating description of subnet '%s'" % subnet) changes.append("Updated description of subnet '%s'" % subnet) else: logger.debug("Subnet '%s' in both DB and file; " % subnet + "no description update") if vlan and row['vlan_number'] != vlan: s.clear() s.find(row['entity_id']) s.vlan_number = vlan s.write_db(perform_checks=False) logger.debug("Updating vlan for subnet '%s'" % subnet) changes.append("Updated vlan for subnet '%s'" % subnet) else: logger.debug("Subnet '%s' in both DB and file; " % subnet + "no vlan update") del subnets_in_file[subnet] else: # Subnet is in DB, but not in file; try to remove it from DB try: logger.info("Deleting subnet '%s'" % subnet) s.clear() s.find(subnet) description = s.description s.delete(perform_checks=perform_checks) changes.append("Deleted subnet '%s' (%s)" % (subnet, description)) except CerebrumError as ce: logger.error(str(ce)) errors.append(str(ce)) # What is left in subnets_in_file now are subnets that should be added for subnet in subnets_in_file: try: description, vlan = subnets_in_file[subnet] logger.info("Adding subnet '%s' (%s)" % (subnet, description)) add_subnet(subnet, description, vlan, perform_checks=perform_checks) changes.append("Added subnet '%s' (%s)" % (subnet, description)) except CerebrumError as ce: logger.error(str(ce)) errors.append(str(ce)) return changes, errors