def add_network_cidr_mapping(self,
        network_config):
        '''
        Method calculates and adds a CloudFormation mapping that is used to set VPC and Subnet CIDR blocks.  Calculated based on CIDR block sizes and additionally checks to ensure all network segments fit inside of the specified overall VPC CIDR
        @param network_config [dict] dictionary of values containing data for creating
        '''
        public_subnet_count = int(network_config.get('public_subnet_count', 2))
        private_subnet_count = int(network_config.get('private_subnet_count', 2))
        public_subnet_size = str(network_config.get('public_subnet_size', '24'))
        private_subnet_size = str(network_config.get('private_subnet_size', '22'))
        network_cidr_base = str(network_config.get('network_cidr_base', '172.16.0.0'))
        network_cidr_size = str(network_config.get('network_cidr_size', '20'))
        first_network_address_block = str(network_config.get('first_network_address_block', network_cidr_base))

        ret_val = {}
        cidr_info = Network(network_cidr_base + '/' + network_cidr_size)
        base_cidr = cidr_info.network().to_tuple()[0] + '/' + str(cidr_info.to_tuple()[1])
        ret_val['vpcBase'] = {'cidr': base_cidr}
        current_base_address = first_network_address_block
        for public_subnet_id in range(0, public_subnet_count):
            if not cidr_info.check_collision(current_base_address):
                raise RuntimeError('Cannot continue creating network--current base address is outside the range of the master Cidr block. Found on pass ' + str(public_subnet_id + 1) + ' when creating public subnet cidrs')
            ip_info = Network(current_base_address + '/' + str(public_subnet_size))
            range_info = ip_info.network().to_tuple()
            if 'subnet' + str(public_subnet_id) not in ret_val:
                ret_val['subnet' + str(public_subnet_id)] = dict()
            ret_val['subnet' + str(public_subnet_id)]['public'] = ip_info.network().to_tuple()[0] + '/' + str(ip_info.to_tuple()[1])
            current_base_address = IP(int(ip_info.host_last().hex(), 16) + 2).to_tuple()[0]
        range_reset = Network(current_base_address + '/' + str(private_subnet_size))
        current_base_address = IP(int(range_reset.host_last().hex(), 16) + 2).to_tuple()[0]
        for private_subnet_id in range(0, private_subnet_count):
            if not cidr_info.check_collision(current_base_address):
                raise RuntimeError('Cannot continue creating network--current base address is outside the range of the master Cidr block. Found on pass ' + str(private_subnet_id + 1) + ' when creating private subnet cidrs')
            ip_info = Network(current_base_address + '/' + str(private_subnet_size))
            range_info = ip_info.network().to_tuple()
            if 'subnet' + str(private_subnet_id) not in ret_val:
                ret_val['subnet' + str(private_subnet_id)] = dict()
            ret_val['subnet' + str(private_subnet_id)]['private'] = ip_info.network().to_tuple()[0] + '/' + str(ip_info.to_tuple()[1])
            current_base_address = IP(int(ip_info.host_last().hex(), 16) + 2).to_tuple()[0]
        return self.template.add_mapping('networkAddresses', ret_val)
    def add_network_cidr_mapping(self, network_config):
        """
        Method calculates and adds a CloudFormation mapping that is used to set VPC and Subnet CIDR blocks.  Calculated based on CIDR block sizes and additionally checks to ensure all network segments fit inside of the specified overall VPC CIDR
        @param network_config [dict] dictionary of values containing data for creating
        """
        az_count = int(network_config.get('az_count', '2'))
        network_cidr_base = str(
            network_config.get('network_cidr_base', '172.16.0.0'))
        network_cidr_size = str(network_config.get('network_cidr_size', '20'))
        first_network_address_block = str(
            network_config.get('first_network_address_block',
                               network_cidr_base))

        ret_val = {}
        cidr_info = Network(network_cidr_base + '/' + network_cidr_size)
        base_cidr = cidr_info.network().to_tuple()[0] + '/' + str(
            cidr_info.to_tuple()[1])
        ret_val['vpcBase'] = {'cidr': base_cidr}
        current_base_address = first_network_address_block

        subnet_types = network_config.get('subnet_types',
                                          ['public', 'private'])

        for index in range(0, len(subnet_types)):
            subnet_type = subnet_types[index]
            subnet_size = network_config.get(subnet_type + '_subnet_size',
                                             '22')

            if index != 0:
                range_reset = Network(current_base_address + '/' +
                                      str(subnet_size))
                current_base_address = IP(
                    int(range_reset.host_last().hex(), 16) + 2).to_tuple()[0]

            for subnet_id in range(0, az_count):
                if not cidr_info.check_collision(current_base_address):
                    raise RuntimeError(
                        'Cannot continue creating network--current base address is outside the range of the master Cidr block. Found on pass '
                        + str(index + 1) + ' when creating ' + subnet_type +
                        ' subnet cidrs')
                ip_info = Network(current_base_address + '/' +
                                  str(subnet_size))
                range_info = ip_info.network().to_tuple()
                if 'subnet' + str(subnet_id) not in ret_val:
                    ret_val['subnet' + str(subnet_id)] = dict()
                ret_val['subnet' +
                        str(subnet_id)][subnet_type] = ip_info.network(
                        ).to_tuple()[0] + '/' + str(ip_info.to_tuple()[1])
                current_base_address = IP(
                    int(ip_info.host_last().hex(), 16) + 2).to_tuple()[0]

        return self.template.add_mapping('networkAddresses', ret_val)
Пример #3
0
def is_overlapping_range(ip_range, subnetworks):
    """
    This method checks for IP range collision
    :return:
    """
    if not subnetworks:
        return

    try:
        ip_range = Network(ip_range)
        for subnet in subnetworks:
            subnet = Network(subnet)
            if ip_range.check_collision(subnet):
                return True

    except ValueError as e:
        current_app.logger.debug(e)
        return