Beispiel #1
0
 def _add_loadbalancer_binding(self, context, lb_id, lbs_id, nsx_router_id,
                               vip_address):
     # First check if there is already binding for the lb.
     # If there is no binding for the lb, add the db binding.
     binding = nsx_db.get_nsx_lbaas_loadbalancer_binding(
         context.session, lb_id)
     if not binding:
         nsx_db.add_nsx_lbaas_loadbalancer_binding(context.session, lb_id,
                                                   lbs_id, nsx_router_id,
                                                   vip_address)
     else:
         LOG.debug("LB binding has already been added, and no need "
                   "to add here.")
Beispiel #2
0
    def create(self, context, lb, completor):
        if not lb_utils.validate_lb_subnet(context, self.core_plugin,
                                           lb['vip_subnet_id']):
            completor(success=False)
            msg = (_('Cannot create lb on subnet %(sub)s for '
                     'loadbalancer %(lb)s. The subnet needs to connect a '
                     'router which is already set gateway.') % {
                         'sub': lb['vip_subnet_id'],
                         'lb': lb['id']
                     })
            raise n_exc.BadRequest(resource='lbaas-subnet', msg=msg)

        service_client = self.core_plugin.nsxlib.load_balancer.service
        nsx_router_id = None
        lb_service = None
        nsx_router_id = lb_utils.NO_ROUTER_ID
        router_id = lb_utils.get_router_from_network(context, self.core_plugin,
                                                     lb['vip_subnet_id'])
        if router_id:
            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,
                                                  lb.get('flavor_id'))
            if router_id:
                # Make sure the NSX service router exists
                if not self.core_plugin.service_router_has_services(
                        context, router_id):
                    self.core_plugin.create_service_router(context, router_id)
                lb_service = self._create_lb_service(context, service_client,
                                                     lb['tenant_id'],
                                                     router_id, nsx_router_id,
                                                     lb['id'], lb_size)
            else:
                lb_service = self._create_lb_service_without_router(
                    context, service_client, lb['tenant_id'], lb, lb_size)
            if not lb_service:
                completor(success=False)
                msg = (_('Failed to create lb service for loadbalancer '
                         '%s') % lb['id'])
                raise nsx_exc.NsxPluginException(err_msg=msg)

        nsx_db.add_nsx_lbaas_loadbalancer_binding(context.session, lb['id'],
                                                  lb_service['id'],
                                                  nsx_router_id,
                                                  lb['vip_address'])

        completor(success=True)
    def create(self, context, lb, completor):
        if not lb_utils.validate_lb_subnet(context, self.core_plugin,
                                           lb['vip_subnet_id']):
            completor(success=False)
            msg = (_('Cannot create lb on subnet %(sub)s for '
                     'loadbalancer %(lb)s. The subnet needs to connect a '
                     'router which is already set gateway.') %
                   {'sub': lb['vip_subnet_id'], 'lb': lb['id']})
            raise n_exc.BadRequest(resource='lbaas-subnet', msg=msg)

        service_client = self.core_plugin.nsxlib.load_balancer.service
        nsx_router_id = None
        lb_service = None
        nsx_router_id = lb_utils.NO_ROUTER_ID
        router_id = lb_utils.get_router_from_network(
            context, self.core_plugin, lb['vip_subnet_id'])
        if router_id:
            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, lb.get('flavor_id'))
            if router_id:
                # Make sure the NSX service router exists
                if not self.core_plugin.service_router_has_services(
                        context, router_id):
                    self.core_plugin.create_service_router(context, router_id)
                lb_service = self._create_lb_service(
                    context, service_client, lb['tenant_id'],
                    router_id, nsx_router_id, lb['id'], lb_size)
            else:
                lb_service = self._create_lb_service_without_router(
                    context, service_client, lb['tenant_id'],
                    lb, lb_size)
            if not lb_service:
                completor(success=False)
                msg = (_('Failed to create lb service for loadbalancer '
                         '%s') % lb['id'])
                raise nsx_exc.NsxPluginException(err_msg=msg)

        nsx_db.add_nsx_lbaas_loadbalancer_binding(
            context.session, lb['id'], lb_service['id'],
            nsx_router_id, lb['vip_address'])

        completor(success=True)
Beispiel #4
0
    def create(self, context, member):
        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,
                                                    loadbalancer.id, pool_id)
        if binding:
            vs_id = binding['lb_vs_id']
            lb_pool_id = binding['lb_pool_id']

            if 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']
                    nsx_db.add_nsx_lbaas_loadbalancer_binding(
                        context.session, loadbalancer.id, lb_service_id,
                        nsx_router_id, loadbalancer.vip_address)
                    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,
                '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)