Example #1
0
    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)
Example #2
0
    def _remove_default_pool_binding(self, context, listener):
        if not listener.get('default_pool_id'):
            return

        # Remove the current default pool from the DB bindings
        lb_id = listener['loadbalancer']['id']
        pool_id = listener['default_pool_id']
        pool_binding = nsx_db.get_nsx_lbaas_pool_binding(
            context.session, lb_id, pool_id)
        if pool_binding:
            nsx_db.update_nsx_lbaas_pool_binding(context.session, lb_id,
                                                 pool_id, None)
Example #3
0
    def _remove_default_pool_binding(self, context, listener):
        if not listener.get('default_pool_id'):
            return

        # Remove the current default pool from the DB bindings
        lb_id = listener['loadbalancer']['id']
        pool_id = listener['default_pool_id']
        pool_binding = nsx_db.get_nsx_lbaas_pool_binding(
            context.session, lb_id, pool_id)
        if pool_binding:
            nsx_db.update_nsx_lbaas_pool_binding(
                context.session, lb_id, pool_id, None)
Example #4
0
    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)
Example #5
0
    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)
Example #6
0
 def _update_default_pool_and_binding(self, context, listener, vs_data,
                                      completor):
     vs_client = self.core_plugin.nsxlib.load_balancer.virtual_server
     if listener.get('default_pool_id'):
         vs_id = vs_data['id']
         lb_id = listener['loadbalancer']['id']
         pool_id = listener['default_pool_id']
         pool = listener['default_pool']
         try:
             (persistence_profile_id,
              post_process_func) = lb_utils.setup_session_persistence(
                  self.core_plugin.nsxlib, pool,
                  lb_utils.get_pool_tags(context, self.core_plugin, pool),
                  listener, vs_data)
         except nsxlib_exc.ManagerError:
             with excutils.save_and_reraise_exception():
                 completor(success=False)
                 LOG.error(
                     "Failed to configure session persistence "
                     "profile for listener %s", listener['id'])
         try:
             # Update persistence profile and pool on virtual server
             vs_client.update(vs_id,
                              persistence_profile_id=persistence_profile_id)
             LOG.debug(
                 "Updated NSX virtual server %(vs_id)s with "
                 "persistence profile %(prof)s", {
                     'vs_id': vs_id,
                     'prof': persistence_profile_id
                 })
             if post_process_func:
                 post_process_func()
         except nsxlib_exc.ManagerError:
             with excutils.save_and_reraise_exception():
                 completor(success=False)
                 LOG.error(
                     "Failed to attach persistence profile %s to "
                     "virtual server %s", persistence_profile_id, vs_id)
         # Update the DB binding of the default pool
         nsx_db.update_nsx_lbaas_pool_binding(context.session, lb_id,
                                              pool_id, vs_id)
Example #7
0
 def _update_default_pool_and_binding(self, context, listener, vs_data,
                                      completor):
     vs_client = self.core_plugin.nsxlib.load_balancer.virtual_server
     if listener.get('default_pool_id'):
         vs_id = vs_data['id']
         lb_id = listener['loadbalancer']['id']
         pool_id = listener['default_pool_id']
         pool = listener['default_pool']
         try:
             (persistence_profile_id,
              post_process_func) = lb_utils.setup_session_persistence(
                 self.core_plugin.nsxlib,
                 pool,
                 lb_utils.get_pool_tags(context, self.core_plugin, pool),
                 listener, vs_data)
         except nsxlib_exc.ManagerError:
             with excutils.save_and_reraise_exception():
                 completor(success=False)
                 LOG.error("Failed to configure session persistence "
                           "profile for listener %s", listener['id'])
         try:
             # Update persistence profile and pool on virtual server
             vs_client.update(
                 vs_id,
                 persistence_profile_id=persistence_profile_id)
             LOG.debug("Updated NSX virtual server %(vs_id)s with "
                       "persistence profile %(prof)s",
                       {'vs_id': vs_id,
                        'prof': persistence_profile_id})
             if post_process_func:
                 post_process_func()
         except nsxlib_exc.ManagerError:
             with excutils.save_and_reraise_exception():
                 completor(success=False)
                 LOG.error("Failed to attach persistence profile %s to "
                           "virtual server %s",
                           persistence_profile_id, vs_id)
         # Update the DB binding of the default pool
         nsx_db.update_nsx_lbaas_pool_binding(
             context.session, lb_id, pool_id, vs_id)
Example #8
0
    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)
Example #9
0
    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)