def check_network(session, configure_network, network, subnet,
                  dns_nameservers=None, allocation_pool=None):
    """Check the network status for the deployment.

    If needed, it creates a dedicated :
        * private network /subnet
        * router between this network and the external network
    """
    nclient = neutron.Client('2', session=session,
                             region_name=os.environ['OS_REGION_NAME'])

    # Check the security groups
    secgroups = nclient.list_security_groups()['security_groups']
    secgroup_name = SECGROUP_NAME
    if secgroup_name not in list(map(itemgetter('name'), secgroups)):
        secgroup = {'name': secgroup_name,
                    'description': 'Enos Security Group'}
        res = nclient.create_security_group({'security_group': secgroup})
        secgroup = res['security_group']
        logger.info("[neutron]: %s security group created" % secgroup_name)
        # create the rules
        tcp = {'protocol': 'tcp',
               'direction': 'ingress',
               'port_range_min': '1',
               'port_range_max': '65535',
               'security_group_id': secgroup['id']}
        icmp = {'protocol': 'icmp',
                'direction': 'ingress',
                'security_group_id': secgroup['id']}
        nclient.create_security_group_rule({'security_group_rule': tcp})
        logger.info("[neutron]: %s rule (all tcp) created" % secgroup_name)
        nclient.create_security_group_rule({'security_group_rule': icmp})
        logger.info("[neutron]: %s rule (all icmp) created" % secgroup_name)
    else:
        logger.info("[neutron]: Reusing security-groups %s " % secgroup_name)

    networks = nclient.list_networks()['networks']
    network_name = network['name']
    if network_name not in list(map(itemgetter('name'), networks)):
        network = {'name': network_name}
        res = nclient.create_network({'network': network})
        network = res['network']
        logger.info("[neutron]: %s network created" % network_name)
    else:
        network = [n for n in networks if n['name'] == network_name][0]
        logger.info("[neutron]: Reusing existing %s network", network)

    # find ext_net
    ext_net = [n for n in networks if n['router:external']]
    if len(ext_net) < 1:
        raise Exception("No external network found")
    ext_net = ext_net[0]

    subnets = nclient.list_subnets()['subnets']
    subnet_name = subnet['name']
    if (subnet_name not in list(map(itemgetter('name'), subnets))
            and configure_network):
        subnet = {'name': subnet['name'],
                  'network_id': network['id'],
                  # NOTE(msimonin): using the dns of chameleon
                  # for a generic openstack provider we should think to
                  # parameteried this or use public available dns
                  'cidr': subnet['cidr'],
                  'ip_version': 4}
        if dns_nameservers is not None:
            subnet.update({'dns_nameservers': dns_nameservers})
        if allocation_pool is not None:
            subnet.update({'allocation_pools': [allocation_pool]})

        s = nclient.create_subnet({'subnet': subnet})
        logger.debug(s)
        subnet = s['subnet']
        logger.info("[neutron]: %s subnet created" % subnet_name)
    else:
        subnet = [s for s in subnets if s['name'] == subnet_name][0]
        logger.info("[neutron]: Reusing %s subnet", subnet)

    # create a router
    routers = nclient.list_routers()
    router_name = ROUTER_NAME
    logger.debug(routers)
    router_present = router_name not in [r['name'] for r in routers['routers']]
    if (router_present and configure_network):
        router = {
            'name': router_name,
            'external_gateway_info': {
                'network_id': ext_net['id']
            }
        }
        r = nclient.create_router({'router': router})
        logger.info("[neutron]  %s router created" % router_name)
        # NOTE(msimonin): We should check the interface outside this block
        # in case the router is created but the interface is not added
        interface = {
            'subnet_id': subnet['id'].encode('UTF-8')
        }
        nclient.add_interface_router(str(r['router']['id']), interface)

    return (ext_net, network, subnet)
Check the network status for the deployment.

    If needed, it creates a dedicated :
        * private network /subnet
        * router between this network and the external network