예제 #1
0
    def subnet_set_dns_delegated(self, operator, identifier, force=False):
        self.ba.assert_dns_superuser(operator.get_entity_id())
        
        s = Subnet(self.db)
        try:
            s.find(identifier)
        except SubnetError:
            s = IPv6Subnet(self.db)
            s.find(identifier)

        subnet_id = "%s/%s" % (s.subnet_ip, s.subnet_mask)

        if s.dns_delegated:
            return ("Subnet %s is already set as " % subnet_id +
                    "being delegated to external DNS server")

        in_use = ""
        if s.has_adresses_in_use():
            if force:
                in_use = "\nNote! Subnet has addresses in use!"
            else:
                raise CerebrumError, ("Subnet '%s' has addresses " % subnet_id +
                                      "in use; must force to delegate")

        s.dns_delegated = True
        s.write_db(perform_checks=False)
        return "Subnet %s set as delegated to external DNS server%s" % (subnet_id, in_use)
예제 #2
0
    def subnet_set_dns_delegated(self, operator, identifier, force=False):
        self.ba.assert_dns_superuser(operator.get_entity_id())

        s = Subnet(self.db)
        try:
            s.find(identifier)
        except SubnetError:
            s = IPv6Subnet(self.db)
            s.find(identifier)

        subnet_id = "%s/%s" % (s.subnet_ip, s.subnet_mask)

        if s.dns_delegated:
            return ("Subnet %s is already set as " % subnet_id +
                    "being delegated to external DNS server")

        in_use = ""
        if s.has_adresses_in_use():
            if force:
                in_use = "\nNote! Subnet has addresses in use!"
            else:
                raise CerebrumError, (
                    "Subnet '%s' has addresses " % subnet_id +
                    "in use; must force to delegate")

        s.dns_delegated = True
        s.write_db(perform_checks=False)
        return "Subnet %s set as delegated to external DNS server%s" % (
            subnet_id, in_use)
예제 #3
0
    def subnet_set_description(self, operator, identifier, new_description):
        raise CerebrumError('Description updates are not allowed for the time beeing.')
        self.ba.assert_dns_superuser(operator.get_entity_id())
        
        s = Subnet(self.db)
        try:
            s.find(identifier)
        except SubnetError:
            s = IPv6Subnet(self.db)
            s.find(identifier)

        s.description = new_description
        s.write_db(perform_checks=False)
        subnet_id = "%s/%s" % (s.subnet_ip, s.subnet_mask)
        return "OK; description for subnet %s updated to '%s'" % (subnet_id, new_description)
예제 #4
0
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)
예제 #5
0
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)
예제 #6
0
    def subnet_set_name_prefix(self, operator, identifier, new_prefix):
        self.ba.assert_dns_superuser(operator.get_entity_id())
        
        s = Subnet(self.db)
        try:
            s.find(identifier)
        except SubnetError:
            s = IPv6Subnet(self.db)
            s.find(identifier)

        old_prefix = s.name_prefix
        s.name_prefix = new_prefix
        s.write_db(perform_checks=False)
        subnet_id = "%s/%s" % (s.subnet_ip, s.subnet_mask)
        return ("OK; name_prefix for subnet %s updated " % subnet_id +
                "from '%s' to '%s'" % (old_prefix, new_prefix))
예제 #7
0
    def subnet_set_name_prefix(self, operator, identifier, new_prefix):
        self.ba.assert_dns_superuser(operator.get_entity_id())

        s = Subnet(self.db)
        try:
            s.find(identifier)
        except SubnetError:
            s = IPv6Subnet(self.db)
            s.find(identifier)

        old_prefix = s.name_prefix
        s.name_prefix = new_prefix
        s.write_db(perform_checks=False)
        subnet_id = "%s/%s" % (s.subnet_ip, s.subnet_mask)
        return ("OK; name_prefix for subnet %s updated " % subnet_id +
                "from '%s' to '%s'" % (old_prefix, new_prefix))
예제 #8
0
    def subnet_set_description(self, operator, identifier, new_description):
        raise CerebrumError(
            'Description updates are not allowed for the time beeing.')
        self.ba.assert_dns_superuser(operator.get_entity_id())

        s = Subnet(self.db)
        try:
            s.find(identifier)
        except SubnetError:
            s = IPv6Subnet(self.db)
            s.find(identifier)

        s.description = new_description
        s.write_db(perform_checks=False)
        subnet_id = "%s/%s" % (s.subnet_ip, s.subnet_mask)
        return "OK; description for subnet %s updated to '%s'" % (
            subnet_id, new_description)
예제 #9
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_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))
예제 #10
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)
예제 #11
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_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))
예제 #12
0
    def subnet_unset_dns_delegated(self, operator, identifier):
        self.ba.assert_dns_superuser(operator.get_entity_id())
        
        s = Subnet(self.db)
        try:
            s.find(identifier)
        except SubnetError:
            s = IPv6Subnet(self.db)
            s.find(identifier)

        subnet_id = "%s/%s" % (s.subnet_ip, s.subnet_mask)

        if not s.dns_delegated:
            return ("Subnet %s is already set as not " % subnet_id +
                    "being delegated to external DNS server" )

        s.dns_delegated = False
        s.write_db(perform_checks=False)
        return "Subnet %s no longer set as delegated to external DNS server" % subnet_id
예제 #13
0
    def subnet_unset_dns_delegated(self, operator, identifier):
        self.ba.assert_dns_superuser(operator.get_entity_id())

        s = Subnet(self.db)
        try:
            s.find(identifier)
        except SubnetError:
            s = IPv6Subnet(self.db)
            s.find(identifier)

        subnet_id = "%s/%s" % (s.subnet_ip, s.subnet_mask)

        if not s.dns_delegated:
            return ("Subnet %s is already set as not " % subnet_id +
                    "being delegated to external DNS server")

        s.dns_delegated = False
        s.write_db(perform_checks=False)
        return "Subnet %s no longer set as delegated to external DNS server" % subnet_id
예제 #14
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)
예제 #15
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
예제 #16
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