Ejemplo n.º 1
0
    def subnet_set_vlan(self, operator, identifier, new_vlan):
        self.ba.assert_dns_superuser(operator.get_entity_id())
        try:
            int(new_vlan)
        except:
            raise CerebrumError("VLAN must be an integer; '%s' isn't" % new_vlan)

        s = Subnet(self.db)
        try:
            s.find(identifier)
        except SubnetError:
            s = IPv6Subnet(self.db)
            s.find(identifier)
        old_vlan = s.vlan_number
        s.vlan_number = new_vlan
        s.write_db(perform_checks=False)
        subnet_id = "%s/%s" % (s.subnet_ip, s.subnet_mask)
        return "OK; VLAN for subnet %s updated from '%s' to '%s'" % (subnet_id, old_vlan, new_vlan)
Ejemplo n.º 2
0
    def subnet_set_vlan(self, operator, identifier, new_vlan):
        self.ba.assert_dns_superuser(operator.get_entity_id())
        try:
            int(new_vlan)
        except:
            raise CerebrumError("VLAN must be an integer; '%s' isn't" %
                                new_vlan)

        s = Subnet(self.db)
        try:
            s.find(identifier)
        except SubnetError:
            s = IPv6Subnet(self.db)
            s.find(identifier)
        old_vlan = s.vlan_number
        s.vlan_number = new_vlan
        s.write_db(perform_checks=False)
        subnet_id = "%s/%s" % (s.subnet_ip, s.subnet_mask)
        return "OK; VLAN for subnet %s updated from '%s' to '%s'" % (
            subnet_id, old_vlan, new_vlan)
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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