<commit_before># Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from pycontrail import exceptions
import pycontrail.types as types
import pytest


def _get_ipam_refs(network):
    for ipam_ref in network.get_network_ipam_refs() or []:
        yield ipam_ref


def _get_subnets(network):
    for ipam_ref in _get_ipam_refs(network):
        for subnet in ipam_ref['attr'].ipam_subnets:
            yield subnet


@pytest.fixture
def contrail_create_subnet(contrail_api_client, contrail_default_ipam):
    """Callable fixture to create subnet and delete it after test."""

    networks = []

    def _contrail_create_subnet(network,
                                ipam=None,
                                ip_prefix='10.0.0.0',
                                ip_prefix_len=24):
        # Refresh network data
        network = contrail_api_client.virtual_network_read(id=network.uuid)
        ipam = ipam or contrail_default_ipam

        ipam_subnets = []
        update_exists = False
        for ipam_ref in _get_ipam_refs(network):
            if ipam_ref['uuid'] == ipam.uuid:
                ipam_subnets = ipam_ref['attr'].ipam_subnets
                update_exists = True

        # Store subnet uuids before adding new
        subnet_uuids = {
            x.subnet_uuid
            for x in _get_subnets(network) if x.subnet_uuid is not None
        }

        networks.append((network, ipam))

        subnet_type = types.SubnetType(
            ip_prefix=ip_prefix, ip_prefix_len=ip_prefix_len)
        ipam_subnets.append(types.IpamSubnetType(subnet=subnet_type))
        vn_sub = types.VnSubnetsType(ipam_subnets=ipam_subnets)
        if update_exists:
            network.set_network_ipam(ipam, vn_sub)
        else:
            network.add_network_ipam(ipam, vn_sub)
        contrail_api_client.virtual_network_update(network)

        # Reread network data after update
        network = contrail_api_client.virtual_network_read(id=network.uuid)
        for subnet in _get_subnets(network):
            if subnet.subnet_uuid not in subnet_uuids:
                break
        return subnet<commit_msg>Add Google DNS to subnet<commit_after># Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from pycontrail import exceptions
import pycontrail.types as types
import pytest
from stepler import config as stepler_config


def _get_ipam_refs(network):
    for ipam_ref in network.get_network_ipam_refs() or []:
        yield ipam_ref


def _get_subnets(network):
    for ipam_ref in _get_ipam_refs(network):
        for subnet in ipam_ref['attr'].ipam_subnets:
            yield subnet


@pytest.fixture
def contrail_create_subnet(contrail_api_client, contrail_default_ipam):
    """Callable fixture to create subnet and delete it after test."""

    networks = []

    def _contrail_create_subnet(network,
                                ipam=None,
                                ip_prefix='10.0.0.0',
                                ip_prefix_len=24,
                                dns_server_address=None):
        dns_server_address = dns_server_address or stepler_config.GOOGLE_DNS_IP

        # Refresh network data
        network = contrail_api_client.virtual_network_read(id=network.uuid)
        ipam = ipam or contrail_default_ipam

        ipam_subnets = []
        update_exists = False
        for ipam_ref in _get_ipam_refs(network):
            if ipam_ref['uuid'] == ipam.uuid:
                ipam_subnets = ipam_ref['attr'].ipam_subnets
                update_exists = True

        # Store subnet uuids before adding new
        subnet_uuids = {
            x.subnet_uuid
            for x in _get_subnets(network) if x.subnet_uuid is not None
        }

        networks.append((network, ipam))

        subnet_type = types.SubnetType(
            ip_prefix=ip_prefix, ip_prefix_len=ip_prefix_len)
        ipam_subnets.append(
            types.IpamSubnetType(
                subnet=subnet_type,
                dhcp_option_list={
                    'dhcp_option': [{
                        'dhcp_option_value': dns_server_address,
                        'dhcp_option_name': '6'
                    }]
                }))
        vn_sub = types.VnSubnetsType(ipam_subnets=ipam_subnets)
        if update_exists:
            network.set_network_ipam(ipam, vn_sub)
        else:
            network.add_network_ipam(ipam, vn_sub)
        contrail_api_client.virtual_network_update(network)

        # Reread network data after update
        network = contrail_api_client.virtual_network_read(id=network.uuid)
        for subnet in _get_subnets(network):
            if subnet.subnet_uuid not in subnet_uuids:
                break
        return subnet
{'fraction_non_alphanumeric': 0.06624383368569416, 'fraction_numerical': 0.00405214940098661, 'mean_word_length': 2.3994011976047904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '517373', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1652, 'n_words': 434}