1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
# Copyright 2014: Mirantis Inc.
# All Rights Reserved.
#
# 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.
import abc
from neutronclient.common import exceptions as neutron_exceptions
from rally.common import cfg
from rally.common import logging
from rally import exceptions
from rally_openstack.common import consts
from rally_openstack.common.services.network import net_utils
from rally_openstack.common.services.network import neutron
LOG = logging.getLogger(__name__)
CONF = cfg.CONF
def generate_cidr(start_cidr="10.2.0.0/24"):
"""Generate next CIDR for network or subnet, without IP overlapping.
This is process and thread safe, because `cidr_incr' points to
value stored directly in RAM. This guarantees that CIDRs will be
serial and unique even under hard multiprocessing/threading load.
:param start_cidr: start CIDR str
:returns: next available CIDR str
"""
ip_version, cidr = net_utils.generate_cidr(start_cidr=start_cidr)
return cidr
class NetworkWrapperException(exceptions.RallyException):
error_code = 532
msg_fmt = "%(message)s"
class NetworkWrapper(object, metaclass=abc.ABCMeta):
"""Base class for network service implementations.
We actually have two network services implementations, with different API:
NovaNetwork and Neutron. The idea is (at least to try) to use unified
service, which hides most differences and routines behind the scenes.
This allows to significantly re-use and simplify code.
"""
START_CIDR = "10.2.0.0/24"
START_IPV6_CIDR = "dead:beaf::/64"
SERVICE_IMPL = None
def __init__(self, clients, owner, config=None):
"""Returns available network wrapper instance.
:param clients: rally.plugins.openstack.osclients.Clients instance
:param owner: The object that owns resources created by this
wrapper instance. It will be used to generate
random names, so must implement
rally.common.utils.RandomNameGeneratorMixin
:param config: The configuration of the network
wrapper. Currently only two config options are
recognized, 'start_cidr' and 'start_ipv6_cidr'.
:returns: NetworkWrapper subclass instance
"""
self.clients = clients
if hasattr(clients, self.SERVICE_IMPL):
self.client = getattr(clients, self.SERVICE_IMPL)()
else:
self.client = clients(self.SERVICE_IMPL)
self.config = config or {}
self.owner = owner
self.start_cidr = self.config.get("start_cidr", self.START_CIDR)
self.start_ipv6_cidr = self.config.get(
"start_ipv6_cidr", self.START_IPV6_CIDR)
@abc.abstractmethod
def create_network(self):
"""Create network."""
@abc.abstractmethod
def delete_network(self):
"""Delete network."""
@abc.abstractmethod
def list_networks(self):
"""List networks."""
@abc.abstractmethod
def create_floating_ip(self):
"""Create floating IP."""
@abc.abstractmethod
def delete_floating_ip(self):
"""Delete floating IP."""
@abc.abstractmethod
def supports_extension(self):
"""Checks whether a network extension is supported."""
class NeutronWrapper(NetworkWrapper):
SERVICE_IMPL = consts.Service.NEUTRON
SUBNET_IP_VERSION = 4
SUBNET_IPV6_VERSION = 6
LB_METHOD = "ROUND_ROBIN"
LB_PROTOCOL = "HTTP"
def __init__(self, *args, **kwargs):
super(NeutronWrapper, self).__init__(*args, **kwargs)
class _SingleClientWrapper(object):
def neutron(_self):
return self.client
@property
def credential(_self):
return self.clients.credential
self.neutron = neutron.NeutronService(
clients=_SingleClientWrapper(),
name_generator=self.owner.generate_random_name,
atomic_inst=getattr(self.owner, "_atomic_actions", [])
)
@property
def external_networks(self):
return self.neutron.list_networks(router_external=True)
@property
def ext_gw_mode_enabled(self):
"""Determine if the ext-gw-mode extension is enabled.
Without this extension, we can't pass the enable_snat parameter.
"""
return self.neutron.supports_extension("ext-gw-mode", silent=True)
def get_network(self, net_id=None, name=None):
net = None
try:
if net_id:
net = self.neutron.get_network(net_id)
else:
networks = self.neutron.list_networks(name=name)
if networks:
net = networks[0]
except neutron_exceptions.NeutronClientException:
pass
if net:
return {"id": net["id"],
"name": net["name"],
"tenant_id": net.get("tenant_id",
net.get("project_id", None)),
"status": net["status"],
"external": net.get("router:external", False),
"subnets": net.get("subnets", []),
"router_id": None}
else:
raise NetworkWrapperException(
"Network not found: %s" % (name or net_id))
def create_router(self, external=False, **kwargs):
"""Create neutron router.
:param external: bool, whether to set setup external_gateway_info
:param **kwargs: POST /v2.0/routers request options
:returns: neutron router dict
"""
kwargs.pop("name", None)
if "tenant_id" in kwargs and "project_id" not in kwargs:
kwargs["project_id"] = kwargs.pop("tenant_id")
return self.neutron.create_router(
discover_external_gw=external, **kwargs)
def create_v1_pool(self, tenant_id, subnet_id, **kwargs):
"""Create LB Pool (v1).
:param tenant_id: str, pool tenant id
:param subnet_id: str, neutron subnet-id
:param **kwargs: extra options
:returns: neutron lb-pool dict
"""
pool_args = {
"pool": {
"tenant_id": tenant_id,
"name": self.owner.generate_random_name(),
"subnet_id": subnet_id,
"lb_method": kwargs.get("lb_method", self.LB_METHOD),
"protocol": kwargs.get("protocol", self.LB_PROTOCOL)
}
}
return self.client.create_pool(pool_args)
def _generate_cidr(self, ip_version=4):
# TODO(amaretskiy): Generate CIDRs unique for network, not cluster
ip_version, cidr = net_utils.generate_cidr(
start_cidr=self.start_cidr if ip_version == 4
else self.start_ipv6_cidr)
return cidr
def _create_network_infrastructure(self, tenant_id, **kwargs):
"""Create network.
The following keyword arguments are accepted:
* add_router: Deprecated, please use router_create_args instead.
Create an external router and add an interface to each
subnet created. Default: False
* subnets_num: Number of subnets to create per network. Default: 0
* dualstack: Whether subnets should be of both IPv4 and IPv6
* dns_nameservers: Nameservers for each subnet. Default:
8.8.8.8, 8.8.4.4
* network_create_args: Additional network creation arguments.
* router_create_args: Additional router creation arguments.
:param tenant_id: str, tenant ID
:param kwargs: Additional options, left open-ended for compatbilitiy.
See above for recognized keyword args.
:returns: dict, network data
"""
network_args = dict(kwargs.get("network_create_args", {}))
network_args["project_id"] = tenant_id
router_args = dict(kwargs.get("router_create_args", {}))
add_router = kwargs.get("add_router", False)
if not (router_args or add_router):
router_args = None
else:
router_args["project_id"] = tenant_id
router_args["discover_external_gw"] = router_args.pop(
"external", False) or add_router
subnet_create_args = {"project_id": tenant_id}
if "dns_nameservers" in kwargs:
subnet_create_args["dns_nameservers"] = kwargs["dns_nameservers"]
net_topo = self.neutron.create_network_topology(
network_create_args=network_args,
router_create_args=router_args,
subnet_create_args=subnet_create_args,
subnets_dualstack=kwargs.get("dualstack", False),
subnets_count=kwargs.get("subnets_num", 0)
)
network = net_topo["network"]
subnets = net_topo["subnets"]
if net_topo["routers"]:
router = net_topo["routers"][0]
else:
router = None
return {
"network": {
"id": network["id"],
"name": network["name"],
"status": network["status"],
"subnets": [s["id"] for s in subnets],
"external": network.get("router:external", False),
"router_id": router and router["id"] or None,
"tenant_id": tenant_id
},
"subnets": subnets,
"router": router
}
def create_network(self, tenant_id, **kwargs):
"""Create network.
The following keyword arguments are accepted:
* add_router: Deprecated, please use router_create_args instead.
Create an external router and add an interface to each
subnet created. Default: False
* subnets_num: Number of subnets to create per network. Default: 0
* dualstack: Whether subnets should be of both IPv4 and IPv6
* dns_nameservers: Nameservers for each subnet. Default:
8.8.8.8, 8.8.4.4
* network_create_args: Additional network creation arguments.
* router_create_args: Additional router creation arguments.
:param tenant_id: str, tenant ID
:param kwargs: Additional options, left open-ended for compatbilitiy.
See above for recognized keyword args.
:returns: dict, network data
"""
return self._create_network_infrastructure(
tenant_id, **kwargs)["network"]
def delete_v1_pool(self, pool_id):
"""Delete LB Pool (v1)
:param pool_id: str, Lb-Pool-id
"""
self.client.delete_pool(pool_id)
def delete_network(self, network):
"""Delete network
:param network: network object returned by create_network method
"""
router = {"id": network["router_id"]} if network["router_id"] else None
# delete_network_topology uses only IDs, but let's transmit as much as
# possible info
topo = {
"network": {
"id": network["id"],
"name": network["name"],
"status": network["status"],
"subnets": network["subnets"],
"router:external": network["external"]
},
"subnets": [{"id": s} for s in network["subnets"]],
"routers": [router] if router else []
}
self.neutron.delete_network_topology(topo)
def _delete_subnet(self, subnet_id):
self.neutron.delete_subnet(subnet_id)
def list_networks(self):
return self.neutron.list_networks()
def create_port(self, network_id, **kwargs):
"""Create neutron port.
:param network_id: neutron network id
:param **kwargs: POST /v2.0/ports request options
:returns: neutron port dict
"""
return self.neutron.create_port(network_id=network_id, **kwargs)
def create_floating_ip(self, ext_network=None,
tenant_id=None, port_id=None, **kwargs):
"""Create Neutron floating IP.
:param ext_network: floating network name or dict
:param tenant_id: str tenant id
:param port_id: str port id
:param **kwargs: for compatibility, not used here
:returns: floating IP dict
"""
if not tenant_id:
raise ValueError("Missed tenant_id")
try:
fip = self.neutron.create_floatingip(
floating_network=ext_network, project_id=tenant_id,
port_id=port_id)
except (exceptions.NotFoundException,
exceptions.GetResourceFailure) as e:
raise NetworkWrapperException(str(e)) from None
return {"id": fip["id"], "ip": fip["floating_ip_address"]}
def delete_floating_ip(self, fip_id, **kwargs):
"""Delete floating IP.
:param fip_id: int floating IP id
:param **kwargs: for compatibility, not used here
"""
self.neutron.delete_floatingip(fip_id)
def supports_extension(self, extension):
"""Check whether a neutron extension is supported
:param extension: str, neutron extension
:returns: result tuple
:rtype: (bool, string)
"""
try:
self.neutron.supports_extension(extension)
except exceptions.NotFoundException as e:
return False, str(e)
return True, ""
def wrap(clients, owner, config=None):
"""Returns available network wrapper instance.
:param clients: rally.plugins.openstack.osclients.Clients instance
:param owner: The object that owns resources created by this
wrapper instance. It will be used to generate random
names, so must implement
rally.common.utils.RandomNameGeneratorMixin
:param config: The configuration of the network wrapper. Currently
only one config option is recognized, 'start_cidr',
and only for Nova network.
:returns: NetworkWrapper subclass instance
"""
if hasattr(clients, "services"):
services = clients.services()
else:
services = clients("services")
if consts.Service.NEUTRON in services.values():
return NeutronWrapper(clients, owner, config=config)
LOG.warning("NovaNetworkWrapper is deprecated since 0.9.0")
|