Example #1
0
 def create(self, context, lb):
     if lb_utils.validate_lb_subnet(context, self.core_plugin,
                                    lb.vip_subnet_id):
         self.lbv2_driver.load_balancer.successful_completion(context, lb)
     else:
         msg = _('Cannot create lb on subnet %(sub)s for '
                 'loadbalancer %(lb)s as it does not connect '
                 'to router') % {'sub': lb.vip_subnet_id,
                                 'lb': lb.id}
         raise n_exc.BadRequest(resource='lbaas-subnet', msg=msg)
Example #2
0
 def create(self, context, lb):
     if lb_utils.validate_lb_subnet(context, self.core_plugin,
                                    lb.vip_subnet_id):
         self.lbv2_driver.load_balancer.successful_completion(context, lb)
     else:
         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)
Example #3
0
    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)