def _create_network(self, ip_range, **kwargs): """Create nova network. :param ip_range: IP range in CIDR notation to create """ net_label = self.generate_random_name() ip_range = network_wrapper.generate_cidr(start_cidr=ip_range) return self.admin_clients("nova").networks.create( label=net_label, cidr=ip_range, **kwargs)
def _create_subnet(self, network, subnet_create_args, start_cidr=None): """Create neutron subnet. :param network: neutron network dict :param subnet_create_args: POST /v2.0/subnets request options :returns: neutron subnet dict """ network_id = network["network"]["id"] if not subnet_create_args.get("cidr"): start_cidr = start_cidr or "10.2.0.0/24" subnet_create_args["cidr"] = network_wrapper.generate_cidr(start_cidr=start_cidr) subnet_create_args["network_id"] = network_id subnet_create_args.setdefault("name", self._generate_random_name("rally_subnet_")) subnet_create_args.setdefault("ip_version", self.SUBNET_IP_VERSION) return self.clients("neutron").create_subnet({"subnet": subnet_create_args})
def test_generate_cidr(self): with mock.patch("rally.plugins.openstack.wrappers.network.cidr_incr", iter(range(1, 4))): self.assertEqual("10.2.1.0/24", network.generate_cidr()) self.assertEqual("10.2.2.0/24", network.generate_cidr()) self.assertEqual("10.2.3.0/24", network.generate_cidr()) with mock.patch("rally.plugins.openstack.wrappers.network.cidr_incr", iter(range(1, 4))): start_cidr = "1.1.0.0/26" self.assertEqual("1.1.0.64/26", network.generate_cidr(start_cidr)) self.assertEqual("1.1.0.128/26", network.generate_cidr(start_cidr)) self.assertEqual("1.1.0.192/26", network.generate_cidr(start_cidr))
def test_generate_cidr(self): with mock.patch("rally.plugins.openstack.wrappers.network.cidr_incr", iter(range(1, 4))): self.assertEqual(network.generate_cidr(), "10.2.1.0/24") self.assertEqual(network.generate_cidr(), "10.2.2.0/24") self.assertEqual(network.generate_cidr(), "10.2.3.0/24") with mock.patch("rally.plugins.openstack.wrappers.network.cidr_incr", iter(range(1, 4))): start_cidr = "1.1.0.0/26" self.assertEqual(network.generate_cidr(start_cidr), "1.1.0.64/26") self.assertEqual(network.generate_cidr(start_cidr), "1.1.0.128/26") self.assertEqual(network.generate_cidr(start_cidr), "1.1.0.192/26")
def _create_subnet(self, network, subnet_create_args, start_cidr=None): """Create neutron subnet. :param network: neutron network dict :param subnet_create_args: POST /v2.0/subnets request options :returns: neutron subnet dict """ network_id = network["network"]["id"] if not subnet_create_args.get("cidr"): start_cidr = start_cidr or "10.2.0.0/24" subnet_create_args["cidr"] = (network_wrapper.generate_cidr( start_cidr=start_cidr)) subnet_create_args["network_id"] = network_id subnet_create_args["name"] = self.generate_random_name() subnet_create_args.setdefault("ip_version", self.SUBNET_IP_VERSION) return self.clients("neutron").create_subnet( {"subnet": subnet_create_args})
def _create_floating_ips_bulk(self, ip_range, **kwargs): """Create floating IPs by range.""" ip_range = network_wrapper.generate_cidr(start_cidr=ip_range) pool_name = self.generate_random_name() return self.admin_clients("nova").floating_ips_bulk.create( ip_range=ip_range, pool=pool_name, **kwargs)
def create_network(neutron_client, neutron_admin_client, network_suffix, tenant_id=None, DVR_flag=True, ext_net_name=None): """Create neutron network, subnet, router :param neutron_client: neutron client :param neutron_admin_client: neutron client with admin credentials :param network_suffix: str, suffix name of the new network :param tenant_id: uuid of the tenant :param DVR_flag: True - creates a DVR router False - creates a non DVR router :param ext_net_name: external network that is to be used :return: router, subnet, network, subnet_cidr """ subnet_cidr = network_wrapper.generate_cidr(start_cidr=START_CIDR) def _create_network(neutron_client, network_suffix, is_external=False): """Creates neutron network""" network_name = "rally_network_" + network_suffix network_args = {"name": network_name, "router:external": is_external } if tenant_id: network_args["tenant_id"] = tenant_id LOG.debug("ADDING NEW NETWORK %s", network_name) return neutron_client.create_network({"network": network_args}) def _create_subnet(neutron_client, rally_network, network_suffix, cidr): """Create neutron subnet""" network_id = rally_network["network"]["id"] subnet_name = "rally_subnet_" + network_suffix subnet_args = {"name": subnet_name, "cidr": cidr, "network_id": network_id, "ip_version": SUBNET_IP_VERSION } if tenant_id: subnet_args["tenant_id"] = tenant_id LOG.debug("ADDING SUBNET %s", subnet_name) return neutron_client.create_subnet({"subnet": subnet_args}) def _create_router(neutron_client, ext_network_id, rally_subnet, dvr_flag): """Create router, set the external gateway and add router interface :param neutron_client: neutron_client :param ext_network_id: uuid of the external network :param rally_subnet: subnet to add router interface :param dvr_flag: True - creates a DVR router False - creates a non DVR router :return: router """ router_name = "rally_router_" + network_suffix gw_info = {"network_id": ext_network_id} router_args = {"name": router_name, "external_gateway_info": gw_info } if not dvr_flag: router_args["distributed"] = dvr_flag if tenant_id: router_args["tenant_id"] = 'tenant_id' LOG.debug("ADDING ROUTER %s", router_name) rally_router = neutron_client.create_router({"router": router_args}) LOG.debug("[%s]: ADDING ROUTER INTERFACE") neutron_client.add_interface_router( rally_router['router']["id"], {"subnet_id": rally_subnet["subnet"]["id"]}) return rally_router def _get_external_network_id(ext_net_name): """Fetch the network id for the given external network, if it exists. Else fetch the first external network present. """ ext_nets = neutron_client.list_networks( **{'router:external': True})['networks'] ext_nets_searched = [n for n in ext_nets if n['name'] == ext_net_name] if ext_nets_searched: return ext_nets_searched[0]['id'] elif ext_nets: return ext_nets[0]['id'] else: return None def _create_external_network(): """Creat external network and subnet""" ext_net = _create_network(neutron_admin_client, "public", True) _create_subnet(neutron_admin_client, ext_net, "public", EXT_NET_CIDR) return ext_net['network']['id'] ext_network_id = _get_external_network_id(ext_net_name) if not ext_network_id: ext_network_id = _create_external_network() rally_network = _create_network(neutron_client, network_suffix) rally_subnet = _create_subnet(neutron_client, rally_network, network_suffix, subnet_cidr) rally_router = _create_router(neutron_client, ext_network_id, rally_subnet, DVR_flag) return rally_router, rally_network, rally_subnet, subnet_cidr
def create_network(neutron_client, neutron_admin_client, network_suffix, tenant_id=None, DVR_flag=True, ext_net=None): """Create neutron network, subnet, router :param neutron_client: neutron client :param neutron_admin_client: neutron client with admin credentials :param network_suffix: str, suffix name of the new network :param tenant_id: uuid of the tenant :param DVR_flag: True - creates a DVR router False - creates a non DVR router :param ext_net: external network that is to be used :return: router, subnet, network, subnet_cidr """ subnet_cidr = network_wrapper.generate_cidr(start_cidr=START_CIDR) def _create_network(neutron_client, network_suffix, is_external=False): """Creates neutron network""" network_name = "rally_network_" + network_suffix network_args = {"name": network_name, "router:external": is_external} if tenant_id: network_args["tenant_id"] = tenant_id LOG.debug("ADDING NEW NETWORK %s", network_name) return neutron_client.create_network({"network": network_args}) def _create_subnet(neutron_client, rally_network, network_suffix, cidr): """Create neutron subnet""" network_id = rally_network["network"]["id"] subnet_name = "rally_subnet_" + network_suffix subnet_args = {"name": subnet_name, "cidr": cidr, "network_id": network_id, "ip_version": SUBNET_IP_VERSION} if tenant_id: subnet_args["tenant_id"] = tenant_id LOG.debug("ADDING SUBNET %s", subnet_name) return neutron_client.create_subnet({"subnet": subnet_args}) def _create_router(neutron_client, ext_network_id, rally_subnet, DVR_flag): """Create router, set the external gateway and add router interface :param neutron_client: neutron_client :param ext_network_id: uuid of the external network :param rally_subnet: subnet to add router interface :return: router """ router_name = "rally_router_" + network_suffix gw_info = {"network_id": ext_network_id} router_args = {"name": router_name, "external_gateway_info": gw_info} if not DVR_flag: router_args["distributed"] = DVR_flag if tenant_id: router_args["tenant_id"] = "tenant_id" LOG.debug("ADDING ROUTER %s", router_name) rally_router = neutron_client.create_router({"router": router_args}) LOG.debug("[%s]: ADDING ROUTER INTERFACE") neutron_client.add_interface_router(rally_router["router"]["id"], {"subnet_id": rally_subnet["subnet"]["id"]}) return rally_router def _get_external_network_id(ext_net): """Fetch the external network id, if external network exists""" ext_network_id = None for network in neutron_client.list_networks()["networks"]: if network["router:external"] and network["name"] == ext_net: ext_network_id = network["id"] LOG.debug("EXTERNAL NETWORK ALREADY EXISTS") break else: for network in neutron_client.list_networks()["networks"]: if network["router:external"] and network["name"]: ext_network_id = network["id"] LOG.debug("EXTERNAL NETWORK ALREADY EXISTS") return ext_network_id def _create_external_network(): """Creat external network and subnet""" ext_net = _create_network(neutron_admin_client, "public", True) _create_subnet(neutron_admin_client, ext_net, "public", EXT_NET_CIDR) return ext_net["network"]["id"] ext_network_id = _get_external_network_id(ext_net) if not ext_network_id: ext_network_id = _create_external_network() rally_network = _create_network(neutron_client, network_suffix) rally_subnet = _create_subnet(neutron_client, rally_network, network_suffix, subnet_cidr) rally_router = _create_router(neutron_client, ext_network_id, rally_subnet, DVR_flag) return rally_router, rally_network, rally_subnet, subnet_cidr