def _validate_network_and_fill_or_validate_subnet(load_balancer): network = validate.network_exists_optionally_contains_subnet( network_id=load_balancer.vip_network_id, subnet_id=load_balancer.vip_subnet_id) if not load_balancer.vip_subnet_id: network_driver = utils.get_network_driver() if load_balancer.vip_address: for subnet_id in network.subnets: subnet = network_driver.get_subnet(subnet_id) if validate.is_ip_member_of_cidr(load_balancer.vip_address, subnet.cidr): load_balancer.vip_subnet_id = subnet_id break if not load_balancer.vip_subnet_id: raise exceptions.ValidationException(detail=_( "Supplied network does not contain a subnet for " "VIP address specified." )) else: # If subnet and IP are not provided, pick the first subnet, # preferring ipv4 for subnet_id in network.subnets: # Use the first subnet, in case there are no ipv4 subnets if not load_balancer.vip_subnet_id: load_balancer.vip_subnet_id = subnet_id subnet = network_driver.get_subnet(subnet_id) if subnet.ip_version == 4: load_balancer.vip_subnet_id = subnet_id break if not load_balancer.vip_subnet_id: raise exceptions.ValidationException(detail=_( "Supplied network does not contain a subnet." ))
def subnet_exists(subnet_id): """Raises an exception when a subnet does not exist.""" network_driver = utils.get_network_driver() try: subnet = network_driver.get_subnet(subnet_id) except Exception: raise exceptions.InvalidSubresource(resource='Subnet', id=subnet_id) return subnet
def port_exists(port_id): """Raises an exception when a port does not exist.""" network_driver = utils.get_network_driver() try: port = network_driver.get_port(port_id) except Exception: raise exceptions.InvalidSubresource(resource='Port', id=port_id) return port
def _check_for_lb_vip_deallocate(self, repo, lb_id): lb = repo.get(self.db_session, id=lb_id) if lb.vip.octavia_owned: vip = lb.vip # We need a backreference vip.load_balancer = lb # Only lookup the network driver if we have a VIP to deallocate network_driver = utils.get_network_driver() network_driver.deallocate_vip(vip)
def qos_policy_exists(qos_policy_id): network_driver = utils.get_network_driver() qos_extension_enabled(network_driver) try: qos_policy = network_driver.get_qos_policy(qos_policy_id) except Exception: raise exceptions.InvalidSubresource(resource='qos_policy', id=qos_policy_id) return qos_policy
def subnet_exists(subnet_id): network_driver = utils.get_network_driver() # Throws an exception when trying to get a subnet which # does not exist. try: network_driver.get_subnet(subnet_id) except Exception: return False return True
def put(self, id, load_balancer): """Updates a load balancer.""" load_balancer = load_balancer.loadbalancer context = pecan.request.context.get('octavia_context') db_lb = self._get_db_lb(context.session, id, show_deleted=False) self._auth_validate_action(context, db_lb.project_id, constants.RBAC_PUT) if not isinstance(load_balancer.vip_qos_policy_id, wtypes.UnsetType): network_driver = utils.get_network_driver() validate.qos_extension_enabled(network_driver) if load_balancer.vip_qos_policy_id is not None: if db_lb.vip.qos_policy_id != load_balancer.vip_qos_policy_id: validate.qos_policy_exists(load_balancer.vip_qos_policy_id) # Load the driver early as it also provides validation driver = driver_factory.get_driver(db_lb.provider) with db_api.get_lock_session() as lock_session: self._test_lb_status(lock_session, id) # Prepare the data for the driver data model lb_dict = load_balancer.to_dict(render_unsets=False) lb_dict['id'] = id vip_dict = lb_dict.pop('vip', {}) lb_dict = driver_utils.lb_dict_to_provider_dict(lb_dict) if 'qos_policy_id' in vip_dict: lb_dict['vip_qos_policy_id'] = vip_dict['qos_policy_id'] # Also prepare the baseline object data old_provider_lb = ( driver_utils.db_loadbalancer_to_provider_loadbalancer(db_lb)) # Dispatch to the driver LOG.info("Sending update Load Balancer %s to provider " "%s", id, driver.name) driver_utils.call_provider( driver.name, driver.loadbalancer_update, old_provider_lb, driver_dm.LoadBalancer.from_dict(lb_dict)) db_lb_dict = load_balancer.to_dict(render_unsets=False) if 'vip' in db_lb_dict: db_vip_dict = db_lb_dict.pop('vip') self.repositories.vip.update(lock_session, id, **db_vip_dict) if db_lb_dict: self.repositories.load_balancer.update(lock_session, id, **db_lb_dict) # Force SQL alchemy to query the DB, otherwise we get inconsistent # results context.session.expire_all() db_lb = self._get_db_lb(context.session, id) result = self._convert_db_to_type(db_lb, lb_types.LoadBalancerResponse) return lb_types.LoadBalancerRootResponse(loadbalancer=result)
def _create_vip_port_if_not_exist(self, load_balancer_db): """Create vip port.""" network_driver = utils.get_network_driver() try: return network_driver.allocate_vip(load_balancer_db) except network_base.AllocateVIPException as e: # Convert neutron style exception to octavia style # if the error was API ready if getattr(e, 'orig_code', None) is not None: e.code = e.orig_code if getattr(e, 'orig_msg', None) is not None: e.message = e.orig_msg e.msg = e.orig_msg raise e
def create_vip_port(self, loadbalancer_id, project_id, vip_dictionary): vip_obj = driver_utils.provider_vip_dict_to_vip_obj(vip_dictionary) lb_obj = data_models.LoadBalancer(id=loadbalancer_id, project_id=project_id, vip=vip_obj) network_driver = utils.get_network_driver() try: vip = network_driver.allocate_vip(lb_obj) except network_base.AllocateVIPException as e: raise exceptions.DriverError(user_fault_string=e.orig_msg, operator_fault_string=e.orig_msg) LOG.info('Amphora provider created VIP port %s for load balancer %s.', vip.port_id, loadbalancer_id) return driver_utils.vip_dict_to_provider_dict(vip.to_dict())
def network_exists_optionally_contains_subnet(network_id, subnet_id=None): """Raises an exception when a network does not exist. If a subnet is provided, also validate the network contains that subnet. """ network_driver = utils.get_network_driver() try: network = network_driver.get_network(network_id) except Exception: raise exceptions.InvalidSubresource(resource='Network', id=network_id) if subnet_id: if not network.subnets or subnet_id not in network.subnets: raise exceptions.InvalidSubresource(resource='Subnet', id=subnet_id) return network
def network_driver(self): if self._network_driver is None: self._network_driver = utils.get_network_driver() return self._network_driver