Example #1
0
    def _data_to_zi(self, name, zi_data, change_by, normalize_ttls=False,
                admin_privilege=False, helpdesk_privilege=False):
        """
        Construct a new ZI, RRS and comments, from zone_data.
        """
            
        def set_missing_zi_data():
            """
            Set missing fields in supplied zi_data to prevent problems
            """
            # Set ZI Zone ttl if not already set
            if 'zone_ttl' not in zi_data:
                zi_data['zone_ttl'] = zone_ttl
            # Set other SOA values in zi_data from defaults 
            # if they are not there. soa_ttl can be None
            for field in ['soa_mname', 'soa_rname', 'soa_refresh', 'soa_retry', 
                    'soa_expire', 'soa_minimum']:
                if not zi_data.get(field):
                    zi_data[field] = zone_cfg.get_row_exc(db_session, field,
                                                        sg=zone_sm.sg)
            # We always update serial number on zone udpdate/publish
            # but it is nicer and probably less troublesome to replace 
            # an existing serial number that may be out there
            if not zi_data.get('soa_serial'):
                if zone_sm.soa_serial:
                    zi_data['soa_serial'] = zone_sm.soa_serial
                else:
                    # Obviously a new zone
                    zi_data['soa_serial'] = new_zone_soa_serial(db_session)

        def check_zi_data():
            """
            Check incoming zi_data attributes for correctness
            """
            for field in ['soa_mname', 'soa_rname']:
                validate_zi_hostname(name, field, zi_data[field])
            for field in ['soa_refresh', 'soa_retry', 'soa_expire',
                    'soa_minimum', 'soa_ttl', 'zone_ttl']:
                if field == 'soa_ttl' and not zi_data.get(field):
                    # SOA TTL can be None
                    continue
                validate_zi_ttl(name, field, zi_data[field])
            for field in ['soa_serial']:
                if field == 'soa_serial' and zi_data.get(field, None) == None:
                    # SOA serial can be None
                    continue
                # Check incoming data type of soa_serial
                if not isinstance(zi_data['soa_serial'], int):
                    raise SOASerialTypeError(name)
                if not ( 0 < zi_data['soa_serial'] <= (2**32-1)):
                    # RFC 2136 Section 4.2 AO serial cannot be zero
                    raise SOASerialRangeError(name)
            
        # Function start
        db_session = self.db_session
        # Get zone_sm to get zone ID etc
        zone_sm = self._get_zone_sm(name)
        zone_id = zone_sm.id_
        
        # initialise data and zone consistency checking
        data_tools = DataTools(db_session, zone_sm)
        
        # Sort out a candidate value for zone_ttl so that RRs can be created
        zone_ttl = zi_data.get('zone_ttl',
                zone_cfg.get_row_exc(db_session, 'zone_ttl', sg=zone_sm.sg))
        zone_ttl_supplied = 'zone_ttl' in zi_data

        # Create comments, and set up comment IDs, and stuff for handlng
        # RR Groups zi_data structures
        data_tools.rr_data_create_comments(zi_data, zone_ttl)

        # Deal with ZI data problems, and supply defaults if missing
        set_missing_zi_data()
        check_zi_data()
        # This constructor call sets attributes in zi as well!
        zi = ZoneInstance(change_by=change_by, **zi_data)
        db_session.add(zi)
        apex_comment = data_tools.get_apex_comment()
        if apex_comment:
            zi.add_apex_comment(apex_comment)
        # Get zi.id_ zi.zone_id from database
        db_session.flush()
        
        # Add RRs to zi
        # Note use of lambda so that list of rrs is always refreshed in
        # function
        data_tools.add_rrs(lambda :zi.rrs, zi.add_rr,
                admin_privilege, helpdesk_privilege)

        # tie zi into data_structures
        zone_sm.all_zis.append(zi)
        zi.zone = zone_sm
        db_session.flush()
        # Normalise TTLs here
        if normalize_ttls and zone_ttl_supplied:
            zi.normalize_ttls()
        # Update SOA and NS records - can't hurt to do it here
        # This also cleans out any incoming apex NS records if
        # client should not be setting them.
        zi.update_apex(db_session)
        # Update Zone TTLs for clean initialisation
        zi.update_zone_ttls()
        db_session.flush()
        # Check zone consistency. Do this here as Apex RRs need to be complete.
        data_tools.check_zi_consistency(zi.rrs)
        return zi, data_tools.get_auto_ptr_data()
Example #2
0
    def _data_to_zi(self,
                    name,
                    zi_data,
                    change_by,
                    normalize_ttls=False,
                    admin_privilege=False,
                    helpdesk_privilege=False):
        """
        Construct a new ZI, RRS and comments, from zone_data.
        """
        def set_missing_zi_data():
            """
            Set missing fields in supplied zi_data to prevent problems
            """
            # Set ZI Zone ttl if not already set
            if 'zone_ttl' not in zi_data:
                zi_data['zone_ttl'] = zone_ttl
            # Set other SOA values in zi_data from defaults
            # if they are not there. soa_ttl can be None
            for field in [
                    'soa_mname', 'soa_rname', 'soa_refresh', 'soa_retry',
                    'soa_expire', 'soa_minimum'
            ]:
                if not zi_data.get(field):
                    zi_data[field] = zone_cfg.get_row_exc(db_session,
                                                          field,
                                                          sg=zone_sm.sg)
            # We always update serial number on zone udpdate/publish
            # but it is nicer and probably less troublesome to replace
            # an existing serial number that may be out there
            if not zi_data.get('soa_serial'):
                if zone_sm.soa_serial:
                    zi_data['soa_serial'] = zone_sm.soa_serial
                else:
                    # Obviously a new zone
                    zi_data['soa_serial'] = new_zone_soa_serial(db_session)

        def check_zi_data():
            """
            Check incoming zi_data attributes for correctness
            """
            for field in ['soa_mname', 'soa_rname']:
                validate_zi_hostname(name, field, zi_data[field])
            for field in [
                    'soa_refresh', 'soa_retry', 'soa_expire', 'soa_minimum',
                    'soa_ttl', 'zone_ttl'
            ]:
                if field == 'soa_ttl' and not zi_data.get(field):
                    # SOA TTL can be None
                    continue
                validate_zi_ttl(name, field, zi_data[field])
            for field in ['soa_serial']:
                if field == 'soa_serial' and zi_data.get(field, None) == None:
                    # SOA serial can be None
                    continue
                # Check incoming data type of soa_serial
                if not isinstance(zi_data['soa_serial'], int):
                    raise SOASerialTypeError(name)
                if not (0 < zi_data['soa_serial'] <= (2**32 - 1)):
                    # RFC 2136 Section 4.2 AO serial cannot be zero
                    raise SOASerialRangeError(name)

        # Function start
        db_session = self.db_session
        # Get zone_sm to get zone ID etc
        zone_sm = self._get_zone_sm(name)
        zone_id = zone_sm.id_

        # initialise data and zone consistency checking
        data_tools = DataTools(db_session, zone_sm)

        # Sort out a candidate value for zone_ttl so that RRs can be created
        zone_ttl = zi_data.get(
            'zone_ttl',
            zone_cfg.get_row_exc(db_session, 'zone_ttl', sg=zone_sm.sg))
        zone_ttl_supplied = 'zone_ttl' in zi_data

        # Create comments, and set up comment IDs, and stuff for handlng
        # RR Groups zi_data structures
        data_tools.rr_data_create_comments(zi_data, zone_ttl)

        # Deal with ZI data problems, and supply defaults if missing
        set_missing_zi_data()
        check_zi_data()
        # This constructor call sets attributes in zi as well!
        zi = ZoneInstance(change_by=change_by, **zi_data)
        db_session.add(zi)
        apex_comment = data_tools.get_apex_comment()
        if apex_comment:
            zi.add_apex_comment(apex_comment)
        # Get zi.id_ zi.zone_id from database
        db_session.flush()

        # Add RRs to zi
        # Note use of lambda so that list of rrs is always refreshed in
        # function
        data_tools.add_rrs(lambda: zi.rrs, zi.add_rr, admin_privilege,
                           helpdesk_privilege)

        # tie zi into data_structures
        zone_sm.all_zis.append(zi)
        zi.zone = zone_sm
        db_session.flush()
        # Normalise TTLs here
        if normalize_ttls and zone_ttl_supplied:
            zi.normalize_ttls()
        # Update SOA and NS records - can't hurt to do it here
        # This also cleans out any incoming apex NS records if
        # client should not be setting them.
        zi.update_apex(db_session)
        # Update Zone TTLs for clean initialisation
        zi.update_zone_ttls()
        db_session.flush()
        # Check zone consistency. Do this here as Apex RRs need to be complete.
        data_tools.check_zi_consistency(zi.rrs)
        return zi, data_tools.get_auto_ptr_data()