def update(self, context, old_lb, new_lb, completor): vs_client = self.core_plugin.nsxlib.load_balancer.virtual_server app_client = self.core_plugin.nsxlib.load_balancer.application_profile if new_lb['name'] != old_lb['name']: for listener in new_lb['listeners']: binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, new_lb['id'], listener['id']) if binding: vs_id = binding['lb_vs_id'] app_profile_id = binding['app_profile_id'] new_lb_name = new_lb['name'][:utils.MAX_TAG_LEN] try: # Update tag on virtual server with new lb name vs = vs_client.get(vs_id) updated_tags = utils.update_v3_tags( vs['tags'], [{'scope': lb_const.LB_LB_NAME, 'tag': new_lb_name}]) vs_client.update(vs_id, tags=updated_tags) # Update tag on application profile with new lb name app_profile = app_client.get(app_profile_id) app_client.update( app_profile_id, tags=updated_tags, resource_type=app_profile['resource_type']) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): completor(success=False) LOG.error('Failed to update tag %(tag)s for lb ' '%(lb)s', {'tag': updated_tags, 'lb': new_lb['name']}) completor(success=True)
def stats(self, context, lb): # Since multiple LBaaS loadbalancer can share the same LB service, # get the corresponding virtual servers' stats instead of LB service. stats = {'active_connections': 0, 'bytes_in': 0, 'bytes_out': 0, 'total_connections': 0} service_client = self.core_plugin.nsxlib.load_balancer.service for listener in lb.listeners: lb_binding = nsx_db.get_nsx_lbaas_loadbalancer_binding( context.session, lb.id) vs_binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb.id, listener.id) if lb_binding and vs_binding: lb_service_id = lb_binding.get('lb_service_id') vs_id = vs_binding.get('lb_vs_id') try: rsp = service_client.get_stats(lb_service_id) if rsp: vs_stats = rsp['virtual_servers'][vs_id]['statistics'] for stat in lb_const.LB_STATS_MAP: lb_stat = lb_const.LB_STATS_MAP[stat] if lb_stat in vs_stats: stats[stat] += stats[stat] + vs_stats[lb_stat] except nsxlib_exc.ManagerError: msg = _('Failed to retrieve stats from LB service ' 'for loadbalancer %(lb)s') % {'lb': lb.id} raise n_exc.BadRequest(resource='lbaas-lb', msg=msg) return stats
def _get_lb_virtual_servers(self, context, lb): # Get all virtual servers that belong to this loadbalancer vs_list = [] for listener in lb['listeners']: vs_binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb['id'], listener['id']) if vs_binding: vs_list.append(vs_binding.get('lb_vs_id')) return vs_list
def create(self, context, pool, completor): lb_id = pool['loadbalancer_id'] pool_client = self.core_plugin.nsxlib.load_balancer.pool pool_name = utils.get_name_and_uuid(pool['name'] or 'pool', pool['id']) tags = self._get_pool_tags(context, pool) description = pool.get('description') lb_algorithm = lb_const.LB_POOL_ALGORITHM_MAP.get(pool['lb_algorithm']) if pool.get('listeners') and len(pool['listeners']) > 1: completor(success=False) msg = (_('Failed to create pool: Multiple listeners are not ' 'supported.')) raise n_exc.BadRequest(resource='lbaas-pool', msg=msg) # NOTE(salv-orlando): Guard against accidental compat breakages try: listener = pool['listener'] or pool['listeners'][0] except IndexError: # If listeners is an empty list we hit this exception listener = None # Perform additional validation for session persistence before # creating resources in the backend lb_common.validate_session_persistence(pool, listener, completor) try: kwargs = self._get_pool_kwargs(pool_name, tags, lb_algorithm, description) lb_pool = pool_client.create(**kwargs) nsx_db.add_nsx_lbaas_pool_binding(context.session, lb_id, pool['id'], lb_pool['id']) except nsxlib_exc.ManagerError: completor(success=False) msg = (_('Failed to create pool on NSX backend: %(pool)s') % { 'pool': pool['id'] }) raise n_exc.BadRequest(resource='lbaas-pool', msg=msg) # The pool object can be created with either --listener or # --loadbalancer option. If listener is present, the virtual server # will be updated with the pool. Otherwise, just return. The binding # will be added later when the pool is associated with layer7 rule. # FIXME(salv-orlando): This two-step process can leave a zombie pool on # NSX if the VS update operation fails if listener: listener_id = listener['id'] binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener_id) if binding: vs_id = binding['lb_vs_id'] self._process_vs_update(context, pool, listener, lb_pool['id'], vs_id, completor) nsx_db.update_nsx_lbaas_pool_binding(context.session, lb_id, pool['id'], vs_id) else: completor(success=False) msg = (_("Couldn't find binding on the listener: %s") % listener['id']) raise nsx_exc.NsxPluginException(err_msg=msg) completor(success=True)
def update(self, context, old_listener, new_listener, completor, certificate=None): nsxlib_lb = self.core_plugin.nsxlib.load_balancer vs_client = nsxlib_lb.virtual_server app_client = nsxlib_lb.application_profile vs_name = None tags = None if new_listener['name'] != old_listener['name']: vs_name = utils.get_name_and_uuid( new_listener['name'] or 'listener', new_listener['id']) tags = self._get_listener_tags(context, new_listener) binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, old_listener['loadbalancer_id'], old_listener['id']) if not binding: msg = (_('Cannot find listener %(listener)s binding on NSX ' 'backend'), {'listener': old_listener['id']}) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) # Validate default pool self._validate_default_pool( context, new_listener, binding['lb_vs_id'], completor, old_listener=old_listener) try: vs_id = binding['lb_vs_id'] app_profile_id = binding['app_profile_id'] updated_kwargs = self._get_virtual_server_kwargs( context, new_listener, vs_name, tags, app_profile_id, certificate) vs_data = vs_client.update(vs_id, **updated_kwargs) if vs_name: app_client.update(app_profile_id, display_name=vs_name, tags=tags) except Exception as e: with excutils.save_and_reraise_exception(): completor(success=False) LOG.error('Failed to update listener %(listener)s with ' 'error %(error)s', {'listener': old_listener['id'], 'error': e}) # Update default pool and session persistence if (old_listener.get('default_pool_id') != new_listener.get('default_pool_id')): self._remove_default_pool_binding(context, old_listener) self._update_default_pool_and_binding(context, new_listener, vs_data, completor) completor(success=True)
def create(self, context, pool): lb_id = pool.loadbalancer_id pool_client = self.core_plugin.nsxlib.load_balancer.pool vs_client = self.core_plugin.nsxlib.load_balancer.virtual_server pool_name = utils.get_name_and_uuid(pool.name, pool.id) tags = lb_utils.get_tags(self.core_plugin, pool.id, lb_const.LB_POOL_TYPE, pool.tenant_id, context.project_name) lb_algorithm = lb_const.LB_POOL_ALGORITHM_MAP.get(pool.lb_algorithm) try: snat_translation = {'type': "LbSnatAutoMap"} lb_pool = pool_client.create(display_name=pool_name, tags=tags, algorithm=lb_algorithm, snat_translation=snat_translation) nsx_db.add_nsx_lbaas_pool_binding(context.session, lb_id, pool.id, lb_pool['id']) except nsxlib_exc.ManagerError: self.lbv2_driver.pool.failed_completion(context, pool) msg = (_('Failed to create pool on NSX backend: %(pool)s') % { 'pool': pool.id }) raise n_exc.BadRequest(resource='lbaas-pool', msg=msg) # The pool object can be created with either --listener or # --loadbalancer option. If listener is present, the virtual server # will be updated with the pool. Otherwise, just return. The binding # will be added later when the pool is associated with layer7 rule. if pool.listener: listener_id = pool.listener.id binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener_id) if binding: vs_id = binding['lb_vs_id'] try: vs_client.update(vs_id, pool_id=lb_pool['id']) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): self.lbv2_driver.pool.failed_completion(context, pool) LOG.error( 'Failed to attach pool %s to virtual ' 'server %s', lb_pool['id'], vs_id) nsx_db.update_nsx_lbaas_pool_binding(context.session, lb_id, pool.id, vs_id) else: msg = (_("Couldn't find binding on the listener: %s") % listener_id) raise nsx_exc.NsxPluginException(err_msg=msg) self.lbv2_driver.pool.successful_completion(context, pool)
def update(self, context, old_listener, new_listener, completor, certificate=None): nsxlib_lb = self.core_plugin.nsxlib.load_balancer vs_client = nsxlib_lb.virtual_server app_client = nsxlib_lb.application_profile vs_name = None tags = None if new_listener['name'] != old_listener['name']: vs_name = utils.get_name_and_uuid( new_listener['name'] or 'listener', new_listener['id']) tags = self._get_listener_tags(context, new_listener) binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, old_listener['loadbalancer_id'], old_listener['id']) if not binding: msg = (_('Cannot find listener %(listener)s binding on NSX ' 'backend'), { 'listener': old_listener['id'] }) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) try: vs_id = binding['lb_vs_id'] app_profile_id = binding['app_profile_id'] updated_kwargs = self._get_virtual_server_kwargs( context, new_listener, vs_name, tags, app_profile_id, certificate) vs_client.update(vs_id, **updated_kwargs) if vs_name: app_client.update(app_profile_id, display_name=vs_name, tags=tags) completor(success=True) except Exception as e: with excutils.save_and_reraise_exception(): completor(success=False) LOG.error( 'Failed to update listener %(listener)s with ' 'error %(error)s', { 'listener': old_listener['id'], 'error': e })
def create(self, context, pool, completor): lb_id = pool['loadbalancer_id'] pool_client = self.core_plugin.nsxlib.load_balancer.pool vs_client = self.core_plugin.nsxlib.load_balancer.virtual_server pool_name = utils.get_name_and_uuid(pool['name'] or 'pool', pool['id']) tags = self._get_pool_tags(context, pool) description = pool.get('description') lb_algorithm = lb_const.LB_POOL_ALGORITHM_MAP.get(pool['lb_algorithm']) try: kwargs = self._get_pool_kwargs(pool_name, tags, lb_algorithm, description) lb_pool = pool_client.create(**kwargs) nsx_db.add_nsx_lbaas_pool_binding( context.session, lb_id, pool['id'], lb_pool['id']) except nsxlib_exc.ManagerError: completor(success=False) msg = (_('Failed to create pool on NSX backend: %(pool)s') % {'pool': pool['id']}) raise n_exc.BadRequest(resource='lbaas-pool', msg=msg) # The pool object can be created with either --listener or # --loadbalancer option. If listener is present, the virtual server # will be updated with the pool. Otherwise, just return. The binding # will be added later when the pool is associated with layer7 rule. if pool['listener']: listener_id = pool['listener']['id'] binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener_id) if binding: vs_id = binding['lb_vs_id'] try: vs_client.update(vs_id, pool_id=lb_pool['id']) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): completor(success=False) LOG.error('Failed to attach pool %s to virtual ' 'server %s', lb_pool['id'], vs_id) nsx_db.update_nsx_lbaas_pool_binding( context.session, lb_id, pool['id'], vs_id) else: msg = (_("Couldn't find binding on the listener: %s") % listener_id) raise nsx_exc.NsxPluginException(err_msg=msg) completor(success=True)
def create(self, context, policy, completor): lb_id = policy['listener']['loadbalancer_id'] listener_id = policy['listener_id'] rule_client = self.core_plugin.nsxlib.load_balancer.rule tags = lb_utils.get_tags(self.core_plugin, policy['id'], lb_const.LB_L7POLICY_TYPE, policy['tenant_id'], context.project_name) binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener_id) if not binding: completor(success=False) msg = _('Cannot find nsx lbaas binding for listener ' '%(listener_id)s') % { 'listener_id': listener_id } raise n_exc.BadRequest(resource='lbaas-l7policy-create', msg=msg) vs_id = binding['lb_vs_id'] rule_body = lb_utils.convert_l7policy_to_lb_rule(context, policy) try: lb_rule = rule_client.create(tags=tags, **rule_body) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): completor(success=False) LOG.error('Failed to create lb rule at NSX backend') try: self._update_policy_position(vs_id, lb_rule['id'], policy['position']) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): completor(success=False) LOG.error( 'Failed to add rule %(rule)% to virtual server ' '%(vs)s at NSX backend', { 'rule': lb_rule['id'], 'vs': vs_id }) nsx_db.add_nsx_lbaas_l7policy_binding(context.session, policy['id'], lb_rule['id'], vs_id) completor(success=True)
def create(self, context, rule): lb_id = rule.policy.listener.loadbalancer_id listener_id = rule.policy.listener_id vs_client = self.core_plugin.nsxlib.load_balancer.virtual_server rule_client = self.core_plugin.nsxlib.load_balancer.rule tags = lb_utils.get_tags(self.core_plugin, rule.id, lb_const.LB_L7RULE_TYPE, rule.tenant_id, context.project_name) binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener_id) if not binding: msg = _('Cannot find nsx lbaas binding for listener ' '%(listener_id)s') % { 'listener_id': listener_id } raise n_exc.BadRequest(resource='lbaas-l7rule-create', msg=msg) vs_id = binding['lb_vs_id'] rule_body = self._convert_l7policy_to_lb_rule(context, rule) try: lb_rule = rule_client.create(tags=tags, **rule_body) except nsxlib_exc.ManagerError: self.lbv2_driver.l7rule.failed_completion(context, rule) msg = _('Failed to create lb rule at NSX backend') raise n_exc.BadRequest(resource='lbaas-l7rule-create', msg=msg) try: vs_client.add_rule(vs_id, lb_rule['id']) except nsxlib_exc.ManagerError: self.lbv2_driver.l7rule.failed_completion(context, rule) msg = (_('Failed to add rule %(rule)% to virtual server ' '%(vs)s at NSX backend') % { 'rule': lb_rule['id'], 'vs': vs_id }) raise n_exc.BadRequest(resource='lbaas-l7rule-create', msg=msg) nsx_db.add_nsx_lbaas_l7rule_binding(context.session, lb_id, rule.l7policy_id, rule.id, lb_rule['id'], vs_id) self.lbv2_driver.l7rule.successful_completion(context, rule)
def delete(self, context, listener): lb_id = listener.loadbalancer_id load_balancer = self.core_plugin.nsxlib.load_balancer vs_client = load_balancer.virtual_server app_client = load_balancer.application_profile binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener.id) if binding: vs_id = binding['lb_vs_id'] app_profile_id = binding['app_profile_id'] try: if listener.default_pool_id: vs_client.update(vs_id, pool_id='') vs_client.delete(vs_id) except nsx_exc.NsxResourceNotFound: msg = (_("virtual server not found on nsx: %(vs)s") % {'vs': vs_id}) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) except nsxlib_exc.ManagerError: self.lbv2_driver.listener.failed_completion(context, listener) msg = (_('Failed to delete virtual server: %(listener)s') % {'listener': listener.id}) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) try: app_client.delete(app_profile_id) except nsx_exc.NsxResourceNotFound: msg = (_("application profile not found on nsx: %s") % app_profile_id) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) except nsxlib_exc.ManagerError: self.lbv2_driver.listener.failed_completion(context, listener) msg = (_('Failed to delete application profile: %(app)s') % {'app': app_profile_id}) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) nsx_db.delete_nsx_lbaas_listener_binding( context.session, lb_id, listener.id) self.lbv2_driver.listener.successful_completion( context, listener, delete=True)
def update(self, context, old_lb, new_lb): vs_client = self.core_plugin.nsxlib.load_balancer.virtual_server app_client = self.core_plugin.nsxlib.load_balancer.application_profile if new_lb.name != old_lb.name: for listener in new_lb.listeners: binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, new_lb.id, listener.id) if binding: vs_id = binding['lb_vs_id'] app_profile_id = binding['app_profile_id'] new_lb_name = new_lb.name[:utils.MAX_TAG_LEN] try: # Update tag on virtual server with new lb name vs = vs_client.get(vs_id) updated_tags = utils.update_v3_tags( vs['tags'], [{ 'scope': lb_const.LB_LB_NAME, 'tag': new_lb_name }]) vs_client.update(vs_id, tags=updated_tags) # Update tag on application profile with new lb name app_profile = app_client.get(app_profile_id) app_client.update( app_profile_id, tags=updated_tags, resource_type=app_profile['resource_type']) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): self.lbv2_driver.pool.failed_completion( context, new_lb) LOG.error( 'Failed to update tag %(tag)s for lb ' '%(lb)s', { 'tag': updated_tags, 'lb': new_lb.name }) self.lbv2_driver.load_balancer.successful_completion(context, new_lb)
def create(self, context, policy, completor): lb_id = policy['listener']['loadbalancer_id'] listener_id = policy['listener_id'] rule_client = self.core_plugin.nsxlib.load_balancer.rule tags = lb_utils.get_tags(self.core_plugin, policy['id'], lb_const.LB_L7POLICY_TYPE, policy['tenant_id'], context.project_name) binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener_id) if not binding: completor(success=False) msg = _('Cannot find nsx lbaas binding for listener ' '%(listener_id)s') % {'listener_id': listener_id} raise n_exc.BadRequest(resource='lbaas-l7policy-create', msg=msg) vs_id = binding['lb_vs_id'] rule_body = lb_utils.convert_l7policy_to_lb_rule(context, policy) try: lb_rule = rule_client.create(tags=tags, **rule_body) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): completor(success=False) LOG.error('Failed to create lb rule at NSX backend') try: self._update_policy_position(vs_id, lb_rule['id'], policy['position']) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): completor(success=False) LOG.error('Failed to add rule %(rule)% to virtual server ' '%(vs)s at NSX backend', {'rule': lb_rule['id'], 'vs': vs_id}) nsx_db.add_nsx_lbaas_l7policy_binding( context.session, policy['id'], lb_rule['id'], vs_id) completor(success=True)
def delete(self, context, listener, completor): lb_id = listener['loadbalancer_id'] nsxlib_lb = self.core_plugin.nsxlib.load_balancer service_client = nsxlib_lb.service vs_client = nsxlib_lb.virtual_server app_client = nsxlib_lb.application_profile binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener['id']) if binding: vs_id = binding['lb_vs_id'] app_profile_id = binding['app_profile_id'] lb_binding = nsx_db.get_nsx_lbaas_loadbalancer_binding( context.session, lb_id) if lb_binding: try: lbs_id = lb_binding.get('lb_service_id') lb_service = service_client.get(lbs_id) vs_list = lb_service.get('virtual_server_ids') if vs_list and vs_id in vs_list: service_client.remove_virtual_server(lbs_id, vs_id) except nsxlib_exc.ManagerError: completor(success=False) msg = (_('Failed to remove virtual server: %(listener)s ' 'from lb service %(lbs)s') % { 'listener': listener['id'], 'lbs': lbs_id }) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) try: if listener.get('default_pool_id'): vs_client.update(vs_id, pool_id='') # Update pool binding to disassociate virtual server pool_binding = nsx_db.get_nsx_lbaas_pool_binding( context.session, lb_id, listener['default_pool_id']) if pool_binding: nsx_db.update_nsx_lbaas_pool_binding( context.session, lb_id, listener['default_pool_id'], None) vs_client.delete(vs_id) except nsx_exc.NsxResourceNotFound: msg = (_("virtual server not found on nsx: %(vs)s") % { 'vs': vs_id }) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) except nsxlib_exc.ManagerError: completor(success=False) msg = (_('Failed to delete virtual server: %(listener)s') % { 'listener': listener['id'] }) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) try: app_client.delete(app_profile_id) except nsx_exc.NsxResourceNotFound: msg = (_("application profile not found on nsx: %s") % app_profile_id) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) except nsxlib_exc.ManagerError: completor(success=False) msg = (_('Failed to delete application profile: %(app)s') % { 'app': app_profile_id }) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) # Delete imported NSX cert if there is any cert_tags = [{ 'scope': lb_const.LB_LISTENER_TYPE, 'tag': listener['id'] }] results = self.core_plugin.nsxlib.search_by_tags(tags=cert_tags) # Only delete object related to certificate used by listener for res_obj in results['results']: res_type = res_obj.get('resource_type') if res_type in lb_const.LB_CERT_RESOURCE_TYPE: tm_client = self.core_plugin.nsxlib.trust_management try: tm_client.delete_cert(res_obj['id']) except nsxlib_exc.ManagerError: LOG.error( "Exception thrown when trying to delete " "certificate: %(cert)s", {'cert': res_obj['id']}) nsx_db.delete_nsx_lbaas_listener_binding(context.session, lb_id, listener['id']) completor(success=True)
def delete(self, context, listener): lb_id = listener.loadbalancer_id load_balancer = self.core_plugin.nsxlib.load_balancer service_client = load_balancer.service vs_client = load_balancer.virtual_server app_client = load_balancer.application_profile binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener.id) if binding: vs_id = binding['lb_vs_id'] app_profile_id = binding['app_profile_id'] lb_binding = nsx_db.get_nsx_lbaas_loadbalancer_binding( context.session, lb_id) if lb_binding: try: lbs_id = lb_binding.get('lb_service_id') lb_service = service_client.get(lbs_id) vs_list = lb_service.get('virtual_server_ids') if vs_list and vs_id in vs_list: service_client.remove_virtual_server(lbs_id, vs_id) except nsxlib_exc.ManagerError: self.lbv2_driver.listener.failed_completion( context, listener) msg = (_('Failed to remove virtual server: %(listener)s ' 'from lb service %(lbs)s') % { 'listener': listener.id, 'lbs': lbs_id }) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) try: if listener.default_pool_id: vs_client.update(vs_id, pool_id='') # Update pool binding to disassociate virtual server pool_binding = nsx_db.get_nsx_lbaas_pool_binding( context.session, lb_id, listener.default_pool_id) if pool_binding: nsx_db.update_nsx_lbaas_pool_binding( context.session, lb_id, listener.default_pool_id, None) vs_client.delete(vs_id) except nsx_exc.NsxResourceNotFound: msg = (_("virtual server not found on nsx: %(vs)s") % { 'vs': vs_id }) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) except nsxlib_exc.ManagerError: self.lbv2_driver.listener.failed_completion(context, listener) msg = (_('Failed to delete virtual server: %(listener)s') % { 'listener': listener.id }) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) try: app_client.delete(app_profile_id) except nsx_exc.NsxResourceNotFound: msg = (_("application profile not found on nsx: %s") % app_profile_id) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) except nsxlib_exc.ManagerError: self.lbv2_driver.listener.failed_completion(context, listener) msg = (_('Failed to delete application profile: %(app)s') % { 'app': app_profile_id }) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) nsx_db.delete_nsx_lbaas_listener_binding(context.session, lb_id, listener.id) self.lbv2_driver.listener.successful_completion(context, listener, delete=True)
def delete(self, context, listener, completor): lb_id = listener['loadbalancer_id'] nsxlib_lb = self.core_plugin.nsxlib.load_balancer service_client = nsxlib_lb.service vs_client = nsxlib_lb.virtual_server app_client = nsxlib_lb.application_profile binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener['id']) if binding: vs_id = binding['lb_vs_id'] app_profile_id = binding['app_profile_id'] lb_binding = nsx_db.get_nsx_lbaas_loadbalancer_binding( context.session, lb_id) if not lb_binding: completor(success=False) msg = (_('Failed to delete virtual server: %(listener)s: ' 'loadbalancer %(lb)s mapping was not found') % {'listener': listener['id'], 'lb': lb_id}) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) try: lbs_id = lb_binding.get('lb_service_id') lb_service = service_client.get(lbs_id) vs_list = lb_service.get('virtual_server_ids') if vs_list and vs_id in vs_list: service_client.remove_virtual_server(lbs_id, vs_id) except nsxlib_exc.ManagerError: completor(success=False) msg = (_('Failed to remove virtual server: %(listener)s ' 'from lb service %(lbs)s') % {'listener': listener['id'], 'lbs': lbs_id}) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) try: persist_profile_id = None if listener.get('default_pool_id'): vs_data = vs_client.update(vs_id, pool_id='') persist_profile_id = vs_data.get('persistence_profile_id') # Update pool binding to disassociate virtual server self._remove_default_pool_binding(context, listener) vs_client.delete(vs_id) # Also delete the old session persistence profile if persist_profile_id: lb_utils.delete_persistence_profile( self.core_plugin.nsxlib, persist_profile_id) except nsx_exc.NsxResourceNotFound: msg = (_("virtual server not found on nsx: %(vs)s") % {'vs': vs_id}) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) except nsxlib_exc.ManagerError: completor(success=False) msg = (_('Failed to delete virtual server: %(listener)s') % {'listener': listener['id']}) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) try: app_client.delete(app_profile_id) except nsx_exc.NsxResourceNotFound: msg = (_("application profile not found on nsx: %s") % app_profile_id) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) except nsxlib_exc.ManagerError: completor(success=False) msg = (_('Failed to delete application profile: %(app)s') % {'app': app_profile_id}) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) # Delete imported NSX cert if there is any cert_tags = [{'scope': lb_const.LB_LISTENER_TYPE, 'tag': listener['id']}] results = self.core_plugin.nsxlib.search_by_tags( tags=cert_tags) # Only delete object related to certificate used by listener for res_obj in results['results']: res_type = res_obj.get('resource_type') if res_type in lb_const.LB_CERT_RESOURCE_TYPE: tm_client = self.core_plugin.nsxlib.trust_management try: tm_client.delete_cert(res_obj['id']) except nsxlib_exc.ManagerError: LOG.error("Exception thrown when trying to delete " "certificate: %(cert)s", {'cert': res_obj['id']}) nsx_db.delete_nsx_lbaas_listener_binding( context.session, lb_id, listener['id']) completor(success=True)