def delete(self, context, member): lb_id = member.pool.loadbalancer_id pool_id = member.pool.id pool_client = self.core_plugin.nsxlib.load_balancer.pool pool_binding = nsx_db.get_nsx_lbaas_pool_binding( context.session, lb_id, pool_id) if pool_binding: lb_pool_id = pool_binding.get('lb_pool_id') try: lb_pool = pool_client.get(lb_pool_id) network = lb_utils.get_network_from_subnet( context, self.core_plugin, member.subnet_id) if network.get('router:external'): fixed_ip, router_id = self._get_info_from_fip( context, member.address) else: fixed_ip = member.address if 'members' in lb_pool: m_list = lb_pool['members'] members = [ m for m in m_list if m['ip_address'] != fixed_ip ] pool_client.update_pool_with_members(lb_pool_id, members) except nsxlib_exc.ManagerError: self.lbv2_driver.member.failed_completion(context, member) msg = _('Failed to remove member from pool on NSX backend') raise n_exc.BadRequest(resource='lbaas-member', msg=msg) self.lbv2_driver.member.successful_completion(context, member, delete=True)
def _get_updated_pool_members(self, context, lb_pool, member): network = lb_utils.get_network_from_subnet(context, self.core_plugin, member.subnet_id) if network.get('router:external'): fixed_ip, router_id = self._get_info_from_fip( context, member.address) else: fixed_ip = member.address for m in lb_pool['members']: if m['ip_address'] == fixed_ip: m['display_name'] = member.name[:219] + '_' + member.id m['weight'] = member.weight return lb_pool['members']
def create(self, context, member): lb_id = member.pool.loadbalancer_id pool_id = member.pool.id loadbalancer = member.pool.loadbalancer if not lb_utils.validate_lb_subnet(context, self.core_plugin, member.subnet_id): msg = (_('Cannot add member %(member)s to pool as member subnet ' '%(subnet)s is neither public nor connected to router') % { 'member': member.id, 'subnet': member.subnet_id }) raise n_exc.BadRequest(resource='lbaas-subnet', msg=msg) pool_client = self.core_plugin.nsxlib.load_balancer.pool service_client = self.core_plugin.nsxlib.load_balancer.service pool_members = self.lbv2_driver.plugin.get_pool_members( context, pool_id) network = lb_utils.get_network_from_subnet(context, self.core_plugin, member.subnet_id) if network.get('router:external'): router_id, fixed_ip = self._get_info_from_fip( context, member.address) else: router_id = lb_utils.get_router_from_network( context, self.core_plugin, member.subnet_id) fixed_ip = member.address binding = nsx_db.get_nsx_lbaas_pool_binding(context.session, lb_id, pool_id) if binding: vs_id = binding.get('lb_vs_id') lb_pool_id = binding.get('lb_pool_id') lb_binding = nsx_db.get_nsx_lbaas_loadbalancer_binding( context.session, lb_id) if not lb_binding and len(pool_members) == 1: nsx_router_id = nsx_db.get_nsx_router_id( context.session, router_id) lb_service = service_client.get_router_lb_service( nsx_router_id) if not lb_service: lb_size = lb_utils.get_lb_flavor_size( self.flavor_plugin, context, loadbalancer.flavor_id) lb_service = self._create_lb_service( context, service_client, member.tenant_id, router_id, nsx_router_id, loadbalancer.id, lb_size) if lb_service: lb_service_id = lb_service['id'] self._add_loadbalancer_binding(context, loadbalancer.id, lb_service_id, nsx_router_id, loadbalancer.vip_address) if vs_id: try: service_client.add_virtual_server( lb_service_id, vs_id) except nsxlib_exc.ManagerError: self.lbv2_driver.member.failed_completion( context, member) msg = (_('Failed to attach virtual server %(vs)s ' 'to lb service %(service)s') % { 'vs': vs_id, 'service': lb_service_id }) raise n_exc.BadRequest(resource='lbaas-member', msg=msg) else: msg = (_('Failed to get lb service to attach virtual ' 'server %(vs)s for member %(member)s') % { 'vs': vs_id, 'member': member['id'] }) raise nsx_exc.NsxPluginException(err_msg=msg) lb_pool = pool_client.get(lb_pool_id) old_m = lb_pool.get('members', None) new_m = [{ 'display_name': member.name[:219] + '_' + member.id, 'ip_address': fixed_ip, 'port': member.protocol_port, 'weight': member.weight }] members = (old_m + new_m) if old_m else new_m pool_client.update_pool_with_members(lb_pool_id, members) else: msg = (_('Failed to get pool binding to add member %s') % member['id']) raise nsx_exc.NsxPluginException(err_msg=msg) self.lbv2_driver.member.successful_completion(context, member)
def delete(self, context, member): lb_id = member.pool.loadbalancer_id pool_id = member.pool.id service_client = self.core_plugin.nsxlib.load_balancer.service pool_client = self.core_plugin.nsxlib.load_balancer.pool pool_members = self.lbv2_driver.plugin.get_pool_members( context, pool_id) pool_binding = nsx_db.get_nsx_lbaas_pool_binding( context.session, lb_id, pool_id) if pool_binding: lb_pool_id = pool_binding['lb_pool_id'] lb_vs_id = pool_binding['lb_vs_id'] # If this is the last member of pool, detach virtual server from # the lb service. If this is the last load balancer for this lb # service, delete the lb service as well. if len(pool_members) == 1: lb_binding = nsx_db.get_nsx_lbaas_loadbalancer_binding( context.session, lb_id) if lb_binding: lb_service_id = lb_binding['lb_service_id'] try: lb_service = service_client.get(lb_service_id) vs_list = lb_service.get('virtual_server_ids') if vs_list and lb_vs_id in vs_list: vs_list.remove(lb_vs_id) else: LOG.error( 'virtual server id %s is not in the lb ' 'service virtual server list %s', lb_vs_id, vs_list) service_client.update(lb_service_id, virtual_server_ids=vs_list) if not vs_list: service_client.delete(lb_service_id) nsx_db.delete_nsx_lbaas_loadbalancer_binding( context.session, lb_id) except nsxlib_exc.ManagerError: self.lbv2_driver.member.failed_completion( context, member) msg = _('Failed to remove virtual server from lb ' 'service on NSX backend') raise n_exc.BadRequest(resource='lbaas-member', msg=msg) try: lb_pool = pool_client.get(lb_pool_id) network = lb_utils.get_network_from_subnet( context, self.core_plugin, member.subnet_id) if network.get('router:external'): fixed_ip, router_id = self._get_info_from_fip( context, member.address) else: fixed_ip = member.address if 'members' in lb_pool: m_list = lb_pool['members'] members = [ m for m in m_list if m['ip_address'] != fixed_ip ] pool_client.update_pool_with_members(lb_pool_id, members) except nsxlib_exc.ManagerError: self.lbv2_driver.member.failed_completion(context, member) msg = _('Failed to remove member from pool on NSX backend') raise n_exc.BadRequest(resource='lbaas-member', msg=msg) self.lbv2_driver.member.successful_completion(context, member, delete=True)