def _validate_integer(data, valid_values=None): """This function validates if the data is an integer. It checks both number or string provided to validate it's an integer and returns a message with the error if it's not :param data: The string or number to validate as integer :param valid_values: None (for future usage) :return: Message if not an integer. """ if valid_values and (data not in valid_values): msg = (_("'%(data)s' is not within '%(valid_values)s'") % {'data': data, 'valid_values': valid_values}) return msg msg = _("'%s' is not an integer") % data try: fl_n = float(data) int_n = int(data) except (ValueError, TypeError, OverflowError): LOG.debug(msg) return msg else: # Fail test if non equal or boolean if fl_n != int_n or isinstance(data, bool): LOG.debug(msg) return msg
def _wait_for_load_balancer_status(cls, load_balancer_id, provisioning_status='ACTIVE', operating_status='ONLINE', delete=False): interval_time = 1 timeout = 600 end_time = time.time() + timeout lb = {} # When running with no-op drivers there is no actual health to # observe, so disable operating_status checks when running no-op. if CONF.lbaas.test_with_noop: operating_status = None while time.time() < end_time: try: lb = cls.load_balancers_client.get_load_balancer( load_balancer_id) if not lb: # loadbalancer not found if delete: break else: raise Exception( _("loadbalancer {lb_id} not" " found").format( lb_id=load_balancer_id)) if lb.get('provisioning_status') == provisioning_status: if operating_status is None: break elif lb.get('operating_status') == operating_status: break time.sleep(interval_time) except exceptions.NotFound: # if wait is for delete operation do break if delete: break else: # raise original exception raise else: if delete: raise exceptions.TimeoutException( _("Waited for load balancer {lb_id} to be deleted for " "{timeout} seconds but can still observe that it " "exists.").format( lb_id=load_balancer_id, timeout=timeout)) else: raise exceptions.TimeoutException( _("Wait for load balancer ran for {timeout} seconds and " "did not observe {lb_id} reach {provisioning_status} " "provisioning status and {operating_status} " "operating status.").format( timeout=timeout, lb_id=load_balancer_id, provisioning_status=provisioning_status, operating_status=operating_status)) return lb
def _wait_for_load_balancer_status( cls, load_balancer_id, provisioning_status="ACTIVE", operating_status="ONLINE", delete=False ): interval_time = 1 timeout = 600 end_time = time.time() + timeout lb = {} while time.time() < end_time: try: lb = cls.load_balancers_client.get_load_balancer(load_balancer_id) if not lb: # loadbalancer not found if delete: break else: raise Exception(_("loadbalancer {lb_id} not" " found").format(lb_id=load_balancer_id)) if ( lb.get("provisioning_status") == provisioning_status and lb.get("operating_status") == operating_status ): break time.sleep(interval_time) except exceptions.NotFound: # if wait is for delete operation do break if delete: break else: # raise original exception raise else: if delete: raise exceptions.TimeoutException( _( "Waited for load balancer {lb_id} to be deleted for " "{timeout} seconds but can still observe that it " "exists." ).format(lb_id=load_balancer_id, timeout=timeout) ) else: raise exceptions.TimeoutException( _( "Wait for load balancer ran for {timeout} seconds and " "did not observe {lb_id} reach {provisioning_status} " "provisioning status and {operating_status} " "operating status." ).format( timeout=timeout, lb_id=load_balancer_id, provisioning_status=provisioning_status, operating_status=operating_status, ) ) return lb
def _check_session_persistence_info(self, info): """Performs sanity check on session persistence info. :param info: Session persistence info """ if info['type'] == 'APP_COOKIE': if not info.get('cookie_name'): raise ValueError(_("'cookie_name' should be specified for this" " type of session persistence.")) else: if 'cookie_name' in info: raise ValueError(_("'cookie_name' is not allowed for this type" " of session persistence"))
def l7policy_rule(self, l7policy_id, fmt=None, value='value1', type=lb_const.L7_RULE_TYPE_HOST_NAME, compare_type=lb_const.L7_RULE_COMPARE_TYPE_EQUAL_TO, no_delete=False, **kwargs): if not fmt: fmt = self.fmt res = self._create_l7policy_rule(fmt, l7policy_id=l7policy_id, type=type, compare_type=compare_type, value=value, **kwargs) if res.status_int >= webob.exc.HTTPClientError.code: raise webob.exc.HTTPClientError( explanation=_("Unexpected error code: %s") % res.status_int ) rule = self.deserialize(fmt or self.fmt, res) yield rule if not no_delete: self.plugin.db.update_status(context.get_admin_context(), models.L7Rule, rule['rule']['id'], constants.ACTIVE) del_req = self.new_delete_request( 'l7policies', fmt=fmt, id=l7policy_id, subresource='rules', sub_id=rule['rule']['id']) del_res = del_req.get_response(self.ext_api) self.assertEqual(del_res.status_int, webob.exc.HTTPNoContent.code)
def get_session(): """Initializes a Keystone session. :returns: a Keystone Session object :raises Exception: if the session cannot be established """ global _SESSION if not _SESSION: auth_url = cfg.CONF.service_auth.auth_url kwargs = {'auth_url': auth_url, 'username': cfg.CONF.service_auth.admin_user, 'password': cfg.CONF.service_auth.admin_password} if cfg.CONF.service_auth.auth_version == '2': client = v2_client kwargs['tenant_name'] = cfg.CONF.service_auth.admin_tenant_name elif cfg.CONF.service_auth.auth_version == '3': client = v3_client kwargs['project_name'] = cfg.CONF.service_auth.admin_tenant_name kwargs['user_domain_name'] = (cfg.CONF.service_auth. admin_user_domain) kwargs['project_domain_name'] = (cfg.CONF.service_auth. admin_project_domain) else: raise Exception(_('Unknown keystone version!')) try: kc = client.Password(**kwargs) _SESSION = session.Session(auth=kc) except Exception: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Error creating Keystone session.")) return _SESSION
def healthmonitor(self, fmt=None, pool_id='pool1id', type='TCP', delay=1, timeout=1, max_retries=2, no_delete=False, **kwargs): if not fmt: fmt = self.fmt res = self._create_healthmonitor(fmt, pool_id=pool_id, type=type, delay=delay, timeout=timeout, max_retries=max_retries, **kwargs) if res.status_int >= webob.exc.HTTPClientError.code: raise webob.exc.HTTPClientError( explanation=_("Unexpected error code: %s") % res.status_int ) healthmonitor = self.deserialize(fmt or self.fmt, res) yield healthmonitor if not no_delete: del_req = self.new_delete_request( 'healthmonitors', fmt=fmt, id=healthmonitor['healthmonitor']['id']) del_res = del_req.get_response(self.ext_api) self.assertEqual(webob.exc.HTTPNoContent.code, del_res.status_int)
def member(self, fmt=None, pool_id='pool1id', address='127.0.0.1', protocol_port=80, subnet=None, no_delete=False, **kwargs): if not fmt: fmt = self.fmt subnet = subnet or self.test_subnet with test_db_base_plugin_v2.optional_ctx( subnet, self.subnet) as tmp_subnet: res = self._create_member(fmt, pool_id=pool_id, address=address, protocol_port=protocol_port, subnet_id=tmp_subnet['subnet']['id'], **kwargs) if res.status_int >= webob.exc.HTTPClientError.code: raise webob.exc.HTTPClientError( explanation=_("Unexpected error code: %s") % res.status_int ) member = self.deserialize(fmt or self.fmt, res) yield member if not no_delete: self._delete('pools', id=pool_id, subresource='members', sub_id=member['member']['id'])
def pool(self, fmt=None, protocol='HTTP', lb_algorithm='ROUND_ROBIN', no_delete=False, listener_id=None, loadbalancer_id=None, **kwargs): if not fmt: fmt = self.fmt if listener_id and loadbalancer_id: res = self._create_pool(fmt, protocol=protocol, lb_algorithm=lb_algorithm, listener_id=listener_id, loadbalancer_id=loadbalancer_id, **kwargs) elif listener_id: res = self._create_pool(fmt, protocol=protocol, lb_algorithm=lb_algorithm, listener_id=listener_id, **kwargs) else: res = self._create_pool(fmt, protocol=protocol, lb_algorithm=lb_algorithm, loadbalancer_id=loadbalancer_id, **kwargs) if res.status_int >= webob.exc.HTTPClientError.code: raise webob.exc.HTTPClientError( explanation=_("Unexpected error code: %s") % res.status_int ) pool = self.deserialize(fmt or self.fmt, res) yield pool if not no_delete: self._delete('pools', pool['pool']['id'])
def listener(self, fmt=None, protocol='HTTP', loadbalancer_id=None, protocol_port=80, default_pool_id=None, no_delete=False, **kwargs): if not fmt: fmt = self.fmt if loadbalancer_id and default_pool_id: res = self._create_listener(fmt, protocol, protocol_port, loadbalancer_id=loadbalancer_id, default_pool_id=default_pool_id, **kwargs) elif loadbalancer_id: res = self._create_listener(fmt, protocol, protocol_port, loadbalancer_id=loadbalancer_id, **kwargs) else: res = self._create_listener(fmt, protocol, protocol_port, default_pool_id=default_pool_id, **kwargs) if res.status_int >= webob.exc.HTTPClientError.code: raise webob.exc.HTTPClientError( explanation=_("Unexpected error code: %s") % res.status_int ) listener = self.deserialize(fmt or self.fmt, res) yield listener if not no_delete: self._delete('listeners', listener['listener']['id'])
def _validate_db_limit(data, max_value=db_const.DB_INTEGER_MAX_VALUE): if int(data) > max_value: msg = (_("'%(data)s' is not a valid value, " "because it is more than %(max_value)s") % {'data': data, 'max_value': max_value}) LOG.debug(msg) return msg
def __init__(self, cert_container): if not isinstance(cert_container, barbican_client.containers.CertificateContainer): raise TypeError(_( "Retrieved Barbican Container is not of the correct type " "(certificate).")) self._cert_container = cert_container
def validate_non_negative_int(self, key, value): if value < 0: data = {'key': key, 'value': value} raise ValueError(_('The %(key)s field can not have ' 'negative value. ' 'Current value is %(value)d.') % data) return value
def create_entity(self, context, hm): ncc_hm = self.payload_preparer.prepare_healthmonitor_for_creation(hm) msg = _("NetScaler driver healthmonitor creation: %s") % repr(ncc_hm) LOG.debug(msg) resource_path = "%s/%s" % (RESOURCE_PREFIX, MONITORS_RESOURCE) self.client.create_resource(context.tenant_id, resource_path, MONITOR_RESOURCE, ncc_hm)
def delete_entity(self, context, pool): """Delete a pool on a NetScaler device.""" resource_path = "%s/%s/%s" % (RESOURCE_PREFIX, POOLS_RESOURCE, pool.id) msg = _("NetScaler driver pool removal: %s") % pool.id LOG.debug(msg) self.client.remove_resource(context.tenant_id, resource_path)
def run(self): while not self.stoprequest.isSet(): try: oper = self.queue.get(timeout=1) # Get the current queue size (N) and set the counter with it. # Handle N operations with no intermission. # Once N operations handles, get the size again and repeat. if self.opers_to_handle_before_rest <= 0: self.opers_to_handle_before_rest = self.queue.qsize() + 1 LOG.debug('Operation consumed from the queue: %s', oper) # check the status - if oper is done: update the db , # else push the oper again to the queue if not self.handle_operation_completion(oper): LOG.debug('Operation %s is not completed yet..', oper) # Not completed - push to the queue again self.queue.put_nowait(oper) self.queue.task_done() self.opers_to_handle_before_rest -= 1 # Take one second rest before start handling # new operations or operations handled before if self.opers_to_handle_before_rest <= 0: time.sleep(1) except Queue.Empty: continue except Exception: m = _("Exception was thrown inside OperationCompletionHandler") LOG.exception(m)
def l7policy(self, listener_id, fmt=None, action=lb_const.L7_POLICY_ACTION_REJECT, no_delete=False, **kwargs): if not fmt: fmt = self.fmt res = self._create_l7policy(fmt, listener_id=listener_id, action=action, **kwargs) if res.status_int >= webob.exc.HTTPClientError.code: raise webob.exc.HTTPClientError( explanation=_("Unexpected error code: %s") % res.status_int ) l7policy = self.deserialize(fmt or self.fmt, res) yield l7policy if not no_delete: self.plugin.db.update_status(context.get_admin_context(), models.L7Policy, l7policy['l7policy']['id'], constants.ACTIVE) del_req = self.new_delete_request( 'l7policies', fmt=fmt, id=l7policy['l7policy']['id']) del_res = del_req.get_response(self.ext_api) self.assertEqual(del_res.status_int, webob.exc.HTTPNoContent.code)
def _load_drivers(self): self.device_drivers = {} for driver in self.conf.device_driver: driver = provconfig.get_provider_driver_class(driver, DEVICE_DRIVERS) try: driver_inst = importutils.import_object(driver, self.conf, self.plugin_rpc) except ImportError: msg = _("Error importing loadbalancer device driver: %s") raise SystemExit(msg % driver) driver_name = driver_inst.get_name() if driver_name not in self.device_drivers: self.device_drivers[driver_name] = driver_inst else: msg = _("Multiple device drivers with the same name found: %s") raise SystemExit(msg % driver_name)
def update_status(self, context, obj_type, obj_id, provisioning_status=None, operating_status=None): if not provisioning_status and not operating_status: LOG.warning('update_status for %(obj_type)s %(obj_id)s called ' 'without specifying provisioning_status or ' 'operating_status' % {'obj_type': obj_type, 'obj_id': obj_id}) return model_mapping = { 'loadbalancer': db_models.LoadBalancer, 'pool': db_models.PoolV2, 'listener': db_models.Listener, 'member': db_models.MemberV2, 'healthmonitor': db_models.HealthMonitorV2 } if obj_type not in model_mapping: raise n_exc.Invalid(_('Unknown object type: %s') % obj_type) try: self.plugin.db.update_status( context, model_mapping[obj_type], obj_id, provisioning_status=provisioning_status, operating_status=operating_status) except n_exc.NotFound: # update_status may come from agent on an object which was # already deleted from db with other request LOG.warning('Cannot update status: %(obj_type)s %(obj_id)s ' 'not found in the DB, it was probably deleted ' 'concurrently', {'obj_type': obj_type, 'obj_id': obj_id})
def _validate_connection_limit(data, min_value=lb_const.MIN_CONNECT_VALUE): if int(data) < min_value: msg = (_("'%(data)s' is not a valid value, " "because it cannot be less than %(min_value)s") % {'data': data, 'min_value': min_value}) LOG.debug(msg) return msg
def delete_entity(self, context, hm): """Delete a healthmonitor on a NetScaler device.""" resource_path = "%s/%s/%s" % (RESOURCE_PREFIX, MONITORS_RESOURCE, hm.id) msg = _("NetScaler driver healthmonitor removal: %s") % hm.id LOG.debug(msg) self.client.remove_resource(context.tenant_id, resource_path)
def delete_entity(self, context, listener): """Delete a listener on a NetScaler device.""" resource_path = "%s/%s/%s" % (RESOURCE_PREFIX, LISTENERS_RESOURCE, listener.id) msg = _("NetScaler driver listener removal: %s") % listener.id LOG.debug(msg) self.client.remove_resource(context.tenant_id, resource_path)
def update_entity(self, context, old_lb_obj, lb_obj): update_lb = self.payload_preparer.prepare_lb_for_update(lb_obj) resource_path = "%s/%s/%s" % (RESOURCE_PREFIX, LBS_RESOURCE, lb_obj.id) msg = (_("NetScaler driver lb_obj %(lb_obj_id)s update: %(lb_obj)s") % {"lb_obj_id": old_lb_obj.id, "lb_obj": repr(lb_obj)}) LOG.debug(msg) self.client.update_resource(context.tenant_id, resource_path, LB_RESOURCE, update_lb)
def create_entity(self, context, pool): ncc_pool = self.payload_preparer.prepare_pool_for_creation( pool) msg = _("NetScaler driver pool creation: %s") % repr(ncc_pool) LOG.debug(msg) resource_path = "%s/%s" % (RESOURCE_PREFIX, POOLS_RESOURCE) self.client.create_resource(context.tenant_id, resource_path, POOL_RESOURCE, ncc_pool)
def update_entity(self, context, old_pool, pool): update_pool = self.payload_preparer.prepare_pool_for_update(pool) resource_path = "%s/%s/%s" % (RESOURCE_PREFIX, POOLS_RESOURCE, pool.id) msg = (_("NetScaler driver pool %(pool_id)s update: %(pool_obj)s") % {"pool_id": old_pool.id, "pool_obj": repr(pool)}) LOG.debug(msg) self.client.update_resource(context.tenant_id, resource_path, POOL_RESOURCE, update_pool)
def create_entity(self, context, listener): """Listener is created with loadbalancer """ ncc_listener = self.payload_preparer.prepare_listener_for_creation( listener) msg = _("NetScaler driver listener creation: %s") % repr(ncc_listener) LOG.debug(msg) resource_path = "%s/%s" % (RESOURCE_PREFIX, LISTENERS_RESOURCE) self.client.create_resource(context.tenant_id, resource_path, LISTENER_RESOURCE, ncc_listener)
def prevent_lbaasv2_port_deletion(self, context, port_id): try: port_db = self._core_plugin._get_port(context, port_id) except n_exc.PortNotFound: return if port_db["device_owner"] == n_const.DEVICE_OWNER_LOADBALANCERV2: filters = {"vip_port_id": [port_id]} if len(self.get_loadbalancers(context, filters=filters)) > 0: reason = _("has device owner %s") % port_db["device_owner"] raise n_exc.ServicePortInUse(port_id=port_db["id"], reason=reason)
def _wait_for_load_balancer_status(self, load_balancer_id, provisioning_status='ACTIVE', operating_status='ONLINE', delete=False): interval_time = 1 timeout = 600 end_time = time.time() + timeout while time.time() < end_time: try: lb = self.load_balancers_client.get_load_balancer( load_balancer_id) except lib_exc.NotFound as e: if delete: return else: raise e if (lb.get('provisioning_status') == provisioning_status and lb.get('operating_status') == operating_status): break elif (lb.get('provisioning_status') == 'ERROR' or lb.get('operating_status') == 'ERROR'): raise Exception( _("Wait for load balancer for load balancer: {lb_id} " "ran for {timeout} seconds and an ERROR was encountered " "with provisioning status: {provisioning_status} and " "operating status: {operating_status}").format( timeout=timeout, lb_id=lb.get('id'), provisioning_status=provisioning_status, operating_status=operating_status)) time.sleep(interval_time) else: raise Exception( _("Wait for load balancer ran for {timeout} seconds and did " "not observe {lb_id} reach {provisioning_status} " "provisioning status and {operating_status} " "operating status.").format( timeout=timeout, lb_id=lb.get('id'), provisioning_status=provisioning_status, operating_status=operating_status)) return lb
def prevent_lbaasv2_port_deletion(self, context, port_id): try: port_db = self._core_plugin._get_port(context, port_id) except n_exc.PortNotFound: return if port_db['device_owner'] == n_const.DEVICE_OWNER_LOADBALANCERV2: filters = {'vip_port_id': [port_id]} if len(self.get_loadbalancer_ids(context, filters=filters)) > 0: reason = _('has device owner %s') % port_db['device_owner'] raise n_exc.ServicePortInUse(port_id=port_db['id'], reason=reason)
def update_entity(self, context, old_healthmonitor, hm): update_hm = self.payload_preparer.prepare_healthmonitor_for_update(hm) resource_path = "%s/%s/%s" % (RESOURCE_PREFIX, MONITORS_RESOURCE, hm.id) msg = (_("NetScaler driver healthmonitor %(healthmonitor_id)s " "update: %(healthmonitor_obj)s") % {"healthmonitor_id": old_healthmonitor.id, "healthmonitor_obj": repr(hm)}) LOG.debug(msg) self.client.update_resource(context.tenant_id, resource_path, MONITOR_RESOURCE, update_hm)
def create_entity(self, context, lb_obj): ncc_lb = self.payload_preparer.prepare_lb_for_creation(lb_obj) vip_subnet_id = lb_obj.vip_subnet_id network_info = self.payload_preparer.\ get_network_info(context, self.driver.plugin, vip_subnet_id) ncc_lb = dict(ncc_lb.items() + network_info.items()) msg = _("NetScaler driver lb creation: %s") % repr(ncc_lb) LOG.debug(msg) resource_path = "%s/%s" % (RESOURCE_PREFIX, LBS_RESOURCE) self.client.create_resource(context.tenant_id, resource_path, LB_RESOURCE, ncc_lb)
def delete_entity(self, context, member): """Delete a member on a NetScaler device.""" parent_pool_id = member.pool.id resource_path = "%s/%s/%s/%s/%s" % (RESOURCE_PREFIX, POOLS_RESOURCE, parent_pool_id, MEMBERS_RESOURCE, member.id) msg = _("NetScaler driver member removal: %s") % member.id LOG.debug(msg) self.client.remove_resource(context.tenant_id, resource_path)
def update_entity(self, context, old_lb_obj, lb_obj): update_lb = self.payload_preparer.prepare_lb_for_update(lb_obj) resource_path = "%s/%s/%s" % (RESOURCE_PREFIX, LBS_RESOURCE, lb_obj.id) msg = (_("NetScaler driver lb_obj %(lb_obj_id)s update: %(lb_obj)s") % { "lb_obj_id": old_lb_obj.id, "lb_obj": repr(lb_obj) }) LOG.debug(msg) self.client.update_resource(context.tenant_id, resource_path, LB_RESOURCE, update_lb)
def update_entity(self, context, old_listener, listener): update_listener = self.payload_preparer.prepare_listener_for_update( listener) resource_path = "%s/%s/%s" % (RESOURCE_PREFIX, LISTENERS_RESOURCE, listener.id) msg = (_("NetScaler driver listener %(listener_id)s " "update: %(listener_obj)s") % {"listener_id": old_listener.id, "listener_obj": repr(listener)}) LOG.debug(msg) self.client.update_resource(context.tenant_id, resource_path, LISTENER_RESOURCE, update_listener)
def _load_drivers(self): self.device_drivers = {} for driver in self.conf.device_driver: driver = provconfig.get_provider_driver_class(driver, DEVICE_DRIVERS) try: driver_inst = importutils.import_object( driver, self.conf, self.plugin_rpc ) except ImportError: msg = _('Error importing loadbalancer device driver: %s') raise SystemExit(msg % driver) driver_name = driver_inst.get_name() if driver_name not in self.device_drivers: self.device_drivers[driver_name] = driver_inst else: msg = _('Multiple device drivers with the same name found: %s') raise SystemExit(msg % driver_name)
def _wait_for_load_balancer_status(self, load_balancer_id, provisioning_status='ACTIVE', operating_status='ONLINE'): interval_time = 1 timeout = 600 end_time = time.time() + timeout while time.time() < end_time: try: lb = self.load_balancers_client.get_load_balancer( load_balancer_id) except lib_exc.NotFound: raise if (lb.get('provisioning_status') == provisioning_status and lb.get('operating_status') == operating_status): break elif (lb.get('provisioning_status') == 'ERROR' or lb.get('operating_status') == 'ERROR'): raise Exception( _("Wait for load balancer for load balancer: {lb_id} " "ran for {timeout} seconds and an ERROR was encountered " "with provisioning status: {provisioning_status} and " "operating status: {operating_status}").format( timeout=timeout, lb_id=lb.get('id'), provisioning_status=provisioning_status, operating_status=operating_status)) time.sleep(interval_time) else: raise Exception( _("Wait for load balancer ran for {timeout} seconds and did " "not observe {lb_id} reach {provisioning_status} " "provisioning status and {operating_status} " "operating status.").format( timeout=timeout, lb_id=lb.get('id'), provisioning_status=provisioning_status, operating_status=operating_status)) return lb
def graph(self, fmt=None, subnet=None, no_delete=False, **kwargs): if not fmt: fmt = self.fmt with test_db_base_plugin_v2.optional_ctx(subnet, self.subnet) as tmp_subnet: res = self._create_graph(fmt, tmp_subnet['subnet']['id'], **kwargs) if res.status_int >= webob.exc.HTTPClientError.code: exc = webob.exc.HTTPClientError( explanation=_("Unexpected error code: %s") % res.status_int) exc.code = res.status_int exc.status_code = res.status_int raise exc graph = self.deserialize(fmt or self.fmt, res) yield graph if not no_delete: # delete loadbalancer children if this was a loadbalancer # graph create call lb = graph['graph']['loadbalancer'] for listener in lb.get('listeners', []): pool = listener.get('default_pool') if pool: hm = pool.get('healthmonitor') if hm: self._delete('healthmonitors', hm['id']) members = pool.get('members', []) for member in members: self._delete('pools', pool['id'], subresource='members', sub_id=member['id']) self._delete('pools', pool['id']) policies = listener.get('l7policies', []) for policy in policies: r_pool = policy.get('redirect_pool') if r_pool: r_hm = r_pool.get('healthmonitor') if r_hm: self._delete('healthmonitors', r_hm['id']) r_members = r_pool.get('members', []) for r_member in r_members: self._delete('pools', r_pool['id'], subresource='members', sub_id=r_member['id']) self._delete('pools', r_pool['id']) self._delete('l7policies', policy['id']) self._delete('listeners', listener['id']) self._delete('loadbalancers', lb['id'])
def update_entity(self, context, old_member, member): parent_pool_id = member.pool.id update_member = self.payload_preparer.prepare_member_for_update(member) resource_path = "%s/%s/%s/%s/%s" % (RESOURCE_PREFIX, POOLS_RESOURCE, parent_pool_id, MEMBERS_RESOURCE, member.id) msg = (_("NetScaler driver member %(member_id)s " "update: %(member_obj)s") % { "member_id": old_member.id, "member_obj": repr(member) }) LOG.debug(msg) self.client.update_resource(context.tenant_id, resource_path, MEMBER_RESOURCE, update_member)
def create_entity(self, context, member): ncc_member = self.payload_preparer.prepare_member_for_creation(member) subnet_id = member.subnet_id network_info = (self.payload_preparer.get_network_info( context, self.driver.plugin, subnet_id)) ncc_member.update(network_info) msg = _("NetScaler driver member creation: %s") % repr(ncc_member) LOG.debug(msg) parent_pool_id = member.pool.id resource_path = "%s/%s/%s/%s" % (RESOURCE_PREFIX, POOLS_RESOURCE, parent_pool_id, MEMBERS_RESOURCE) self.client.create_resource(context.tenant_id, resource_path, MEMBER_RESOURCE, ncc_member)
def get_session(): """Initializes a Keystone session. :returns: a Keystone Session object :raises Exception: if the session cannot be established """ global _SESSION if not _SESSION: auth_url = cfg.CONF.service_auth.auth_url insecure = cfg.CONF.service_auth.insecure cacert = cfg.CONF.service_auth.cafile cert = cfg.CONF.service_auth.certfile key = cfg.CONF.service_auth.keyfile if insecure: verify = False else: verify = cacert or True if cert and key: cert = (cert, key) kwargs = { 'auth_url': auth_url, 'username': cfg.CONF.service_auth.admin_user, 'password': cfg.CONF.service_auth.admin_password } if cfg.CONF.service_auth.auth_version == '2': client = v2_client kwargs['tenant_name'] = cfg.CONF.service_auth.admin_tenant_name elif cfg.CONF.service_auth.auth_version == '3': client = v3_client kwargs['project_name'] = cfg.CONF.service_auth.admin_tenant_name kwargs['user_domain_name'] = ( cfg.CONF.service_auth.admin_user_domain) kwargs['project_domain_name'] = ( cfg.CONF.service_auth.admin_project_domain) else: raise Exception(_('Unknown keystone version!')) try: kc = client.Password(**kwargs) _SESSION = session.Session(auth=kc, verify=verify, cert=cert) except Exception: with excutils.save_and_reraise_exception(): LOG.exception("Error creating Keystone session.") return _SESSION
def __init__(self, conf, plugin_rpc): self.conf = conf self.state_path = conf.haproxy.loadbalancer_state_path try: vif_driver_class = n_utils.load_class_by_alias_or_classname( 'neutron.interface_drivers', conf.interface_driver) except ImportError: with excutils.save_and_reraise_exception(): msg = (_('Error importing interface driver: %s') % conf.interface_driver) LOG.error(msg) self.vif_driver = vif_driver_class(conf) self.plugin_rpc = plugin_rpc self.pool_to_port_id = {}
def _wait_for_pool_session_persistence(self, pool_id, sp_type=None): interval_time = 1 timeout = 10 end_time = time.time() + timeout while time.time() < end_time: pool = self.pools_client.get_pool(pool_id) sp = pool.get('session_persistence', None) if (not (sp_type or sp) or pool['session_persistence']['type'] == sp_type): return pool time.sleep(interval_time) raise Exception( _("Wait for pool ran for {timeout} seconds and did " "not observe {pool_id} update session persistence type " "to {type}.").format(timeout=timeout, pool_id=pool_id, type=sp_type))
def _wait_for_resource_deletion(self, resource_type_name, resource_get_method, resource_id): interval_time = 1 timeout = 600 end_time = time.time() + timeout while time.time() < end_time: try: resource_get_method(resource_id) except lib_exc.NotFound: return time.sleep(interval_time) else: raise Exception( _("Wait for {res_name} ran for {timeout} seconds and did " "not observe {res_id} deletion processes ended").format( res_name=resource_type_name, timeout=timeout, res_id=resource_id))
def _create_port_for_load_balancer(self, context, lb_db, ip_address, network_id=None): if lb_db.vip_subnet_id: assign_subnet = False # resolve subnet and create port subnet = self._core_plugin.get_subnet(context, lb_db.vip_subnet_id) network_id = subnet['network_id'] fixed_ip = {'subnet_id': subnet['id']} if ip_address and ip_address != n_const.ATTR_NOT_SPECIFIED: fixed_ip['ip_address'] = ip_address fixed_ips = [fixed_ip] elif network_id and network_id != n_const.ATTR_NOT_SPECIFIED: assign_subnet = True fixed_ips = n_const.ATTR_NOT_SPECIFIED else: attrs = _("vip_subnet_id or vip_network_id") raise loadbalancerv2.RequiredAttributeNotSpecified(attr_name=attrs) port_data = { 'tenant_id': lb_db.tenant_id, 'name': 'loadbalancer-' + lb_db.id, 'network_id': network_id, 'mac_address': n_const.ATTR_NOT_SPECIFIED, 'admin_state_up': False, 'device_id': lb_db.id, 'device_owner': n_const.DEVICE_OWNER_LOADBALANCERV2, 'fixed_ips': fixed_ips } port = self._core_plugin.create_port(context, {'port': port_data}) lb_db.vip_port_id = port['id'] if assign_subnet: fixed_ip = self._create_port_choose_fixed_ip(port['fixed_ips']) lb_db.vip_address = fixed_ip['ip_address'] lb_db.vip_subnet_id = fixed_ip['subnet_id'] else: for fixed_ip in port['fixed_ips']: if fixed_ip['subnet_id'] == lb_db.vip_subnet_id: lb_db.vip_address = fixed_ip['ip_address'] break
def _wait_for_neutron_port_delete(cls, port_id): """ Neutron port deletion is asynchronous. This method waits, up to a timeout for the port deletion to complete. """ interval_time = 1 timeout = 600 end_time = time.time() + timeout while time.time() < end_time: try: cls.ports_client.show_port(port_id) time.sleep(interval_time) except exceptions.NotFound: break else: raise exceptions.TimeoutException( _("Waited for port {port_id} to be deleted for " "{timeout} seconds but can still observe that it " "exists.").format(port_id=port_id, timeout=timeout))
def loadbalancer(self, fmt=None, subnet=None, no_delete=False, **kwargs): if not fmt: fmt = self.fmt with test_db_base_plugin_v2.optional_ctx(subnet, self.subnet) as tmp_subnet: res = self._create_loadbalancer(fmt, tmp_subnet['subnet']['id'], **kwargs) if res.status_int >= webob.exc.HTTPClientError.code: exc = webob.exc.HTTPClientError( explanation=_("Unexpected error code: %s") % res.status_int) exc.code = res.status_int exc.status_code = res.status_int raise exc lb = self.deserialize(fmt or self.fmt, res) yield lb if not no_delete: self._delete('loadbalancers', lb['loadbalancer']['id'])
def update_status(self, context, obj_type, obj_id, provisioning_status=None, operating_status=None): if not provisioning_status and not operating_status: LOG.warning( _LW('update_status for %(obj_type)s %(obj_id)s called ' 'without specifying provisioning_status or ' 'operating_status') % { 'obj_type': obj_type, 'obj_id': obj_id }) return model_mapping = { 'loadbalancer': db_models.LoadBalancer, 'pool': db_models.PoolV2, 'listener': db_models.Listener, 'member': db_models.MemberV2, 'healthmonitor': db_models.HealthMonitorV2 } if obj_type not in model_mapping: raise n_exc.Invalid(_('Unknown object type: %s') % obj_type) try: self.plugin.db.update_status( context, model_mapping[obj_type], obj_id, provisioning_status=provisioning_status, operating_status=operating_status) except n_exc.NotFound: # update_status may come from agent on an object which was # already deleted from db with other request LOG.warning( _LW('Cannot update status: %(obj_type)s %(obj_id)s ' 'not found in the DB, it was probably deleted ' 'concurrently'), { 'obj_type': obj_type, 'obj_id': obj_id })
def __init__(self, conf, plugin_rpc): super(HaproxyNSDriver, self).__init__(conf, plugin_rpc) self.state_path = conf.haproxy.loadbalancer_state_path self.state_path = os.path.join( self.conf.haproxy.loadbalancer_state_path, STATE_PATH_V2_APPEND) try: vif_driver_class = n_utils.load_class_by_alias_or_classname( 'neutron.interface_drivers', conf.interface_driver) except ImportError: with excutils.save_and_reraise_exception(): msg = (_('Error importing interface driver: %s') % conf.interface_driver) LOG.error(msg) self.vif_driver = vif_driver_class(conf) self.deployed_loadbalancers = {} self._loadbalancer = LoadBalancerManager(self) self._listener = ListenerManager(self) self._pool = PoolManager(self) self._member = MemberManager(self) self._healthmonitor = HealthMonitorManager(self)
def _get_pip(self, context, tenant_id, port_name, network_id, subnet_id): """Get proxy IP Creates or get port on network_id, returns that port's IP on the subnet_id. """ port_filter = { 'name': [port_name], } ports = self.plugin._core_plugin.get_ports(context, filters=port_filter) if not ports: # create port, we just want any IP allocated to the port # based on the network id and subnet_id port_data = { 'tenant_id': tenant_id, 'name': port_name, 'network_id': network_id, 'mac_address': attributes.ATTR_NOT_SPECIFIED, 'admin_state_up': False, 'device_id': '', 'device_owner': 'neutron:' + constants.LOADBALANCER, 'fixed_ips': [{ 'subnet_id': subnet_id }] } port = self.plugin._core_plugin.create_port( context, {'port': port_data}) else: port = ports[0] ips_on_subnet = [ ip for ip in port['fixed_ips'] if ip['subnet_id'] == subnet_id ] if not ips_on_subnet: raise Exception( _('Could not find or allocate ' 'IP address for subnet id %s'), subnet_id) else: return ips_on_subnet[0]['ip_address']
def listener(self, fmt=None, protocol='HTTP', loadbalancer_id=None, protocol_port=80, default_pool_id=None, no_delete=False, **kwargs): if not fmt: fmt = self.fmt if loadbalancer_id and default_pool_id: res = self._create_listener(fmt, protocol, protocol_port, loadbalancer_id=loadbalancer_id, default_pool_id=default_pool_id, **kwargs) elif loadbalancer_id: res = self._create_listener(fmt, protocol, protocol_port, loadbalancer_id=loadbalancer_id, **kwargs) else: res = self._create_listener(fmt, protocol, protocol_port, default_pool_id=default_pool_id, **kwargs) if res.status_int >= webob.exc.HTTPClientError.code: raise webob.exc.HTTPClientError( explanation=_("Unexpected error code: %s") % res.status_int) listener = self.deserialize(fmt or self.fmt, res) yield listener if not no_delete: self._delete('listeners', listener['listener']['id'])
def pool(self, fmt=None, protocol='HTTP', lb_algorithm='ROUND_ROBIN', no_delete=False, listener_id=None, loadbalancer_id=None, **kwargs): if not fmt: fmt = self.fmt if listener_id and loadbalancer_id: res = self._create_pool(fmt, protocol=protocol, lb_algorithm=lb_algorithm, listener_id=listener_id, loadbalancer_id=loadbalancer_id, **kwargs) elif listener_id: res = self._create_pool(fmt, protocol=protocol, lb_algorithm=lb_algorithm, listener_id=listener_id, **kwargs) else: res = self._create_pool(fmt, protocol=protocol, lb_algorithm=lb_algorithm, loadbalancer_id=loadbalancer_id, **kwargs) if res.status_int >= webob.exc.HTTPClientError.code: raise webob.exc.HTTPClientError( explanation=_("Unexpected error code: %s") % res.status_int) pool = self.deserialize(fmt or self.fmt, res) yield pool if not no_delete: self._delete('pools', pool['pool']['id'])
def update_status(self, context, obj_type, obj_id, status): model_mapping = { 'pool': loadbalancer_db.Pool, 'vip': loadbalancer_db.Vip, 'member': loadbalancer_db.Member, 'health_monitor': loadbalancer_db.PoolMonitorAssociation } if obj_type not in model_mapping: raise n_exc.Invalid(_('Unknown object type: %s') % obj_type) try: if obj_type == 'health_monitor': self.plugin.update_pool_health_monitor( context, obj_id['monitor_id'], obj_id['pool_id'], status) else: self.plugin.update_status( context, model_mapping[obj_type], obj_id, status) except n_exc.NotFound: # update_status may come from agent on an object which was # already deleted from db with other request LOG.warning(_LW('Cannot update status: %(obj_type)s %(obj_id)s ' 'not found in the DB, it was probably deleted ' 'concurrently'), {'obj_type': obj_type, 'obj_id': obj_id})
def get_session(): """Initializes a Keystone session. :returns: a Keystone Session object :raises Exception: if the session cannot be established """ global _SESSION if not _SESSION: auth_url = cfg.CONF.service_auth.auth_url kwargs = { 'auth_url': auth_url, 'username': cfg.CONF.service_auth.admin_user, 'password': cfg.CONF.service_auth.admin_password } if cfg.CONF.service_auth.auth_version == '2': client = v2_client kwargs['tenant_name'] = cfg.CONF.service_auth.admin_tenant_name elif cfg.CONF.service_auth.auth_version == '3': client = v3_client kwargs['project_name'] = cfg.CONF.service_auth.admin_tenant_name kwargs['user_domain_name'] = ( cfg.CONF.service_auth.admin_user_domain) kwargs['project_domain_name'] = ( cfg.CONF.service_auth.admin_project_domain) else: raise Exception(_('Unknown keystone version!')) try: kc = client.Password(**kwargs) _SESSION = session.Session(auth=kc) except Exception: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Error creating Keystone session.")) return _SESSION
class DriverNotSpecified(n_exc.NeutronException): message = _("Device driver for agent should be specified " "in plugin driver.")
from neutron_lbaas.drivers import driver_base from neutron_lbaas.extensions import lbaas_agentschedulerv2 from neutron_lbaas.services.loadbalancer import constants as lb_const from neutron_lbaas.services.loadbalancer import data_models LB_SCHEDULERS = 'loadbalancer_schedulers' AGENT_SCHEDULER_OPTS = [ cfg.StrOpt('loadbalancer_scheduler_driver', default='neutron_lbaas.agent_scheduler.ChanceScheduler', deprecated_for_removal=True, deprecated_since='Queens', deprecated_reason='The neutron-lbaas project is now ' 'deprecated. See: https://wiki.openstack.org/' 'wiki/Neutron/LBaaS/Deprecation', help=_('Driver to use for scheduling ' 'to a default loadbalancer agent')), cfg.BoolOpt('allow_automatic_lbaas_agent_failover', default=False, deprecated_for_removal=True, deprecated_since='Queens', deprecated_reason='The neutron-lbaas project is now ' 'deprecated. See: https://wiki.openstack.org' '/wiki/Neutron/LBaaS/Deprecation', help=_('Automatically reschedule loadbalancer from offline ' 'to online lbaas agents. This is only supported for ' 'drivers who use the neutron LBaaSv2 agent')), ] cfg.CONF.register_opts(AGENT_SCHEDULER_OPTS)
class MemberExists(nexception.Conflict): message = _("Member with address %(address)s and protocol_port %(port)s " "already present in pool %(pool)s")
class MemberNotFoundForPool(nexception.NotFound): message = _("Member %(member_id)s could not be found in pool " "%(pool_id)s")
class StateInvalid(nexception.Conflict): message = _("Invalid state %(state)s of loadbalancer resource %(id)s")