Example #1
0
    def test_get_all_load_balancers(self):
        admin_context = self.context.elevated(read_deleted='no')
        self.mox.StubOutWithMock(db, 'load_balancer_get_all')
        self.mox.StubOutWithMock(self.context, 'elevated')

        load_balancer_ref = copy.deepcopy(self.lb_ref)
        load_balancer_ref.config = self.config_ref
        expect = dict()
        expect['created_at'] = None
        expect['updated_at'] = None
        expect['user_id'] = load_balancer_ref.user_id
        expect['project_id'] = load_balancer_ref.project_id
        expect['free'] = load_balancer_ref.free
        expect['uuid'] = load_balancer_ref.uuid
        expect['name'] = load_balancer_ref.name
        expect['state'] = load_balancer_ref.state
        expect['protocol'] = load_balancer_ref.protocol
        expect['listen_port'] = load_balancer_ref.listen_port
        expect['instance_port'] = load_balancer_ref.instance_port
        expect_configs = copy.deepcopy(self.config)
        del expect_configs['load_balancer_id']
        expect['config'] = expect_configs
        expect['dns_names'] = []
        expect['instance_uuids'] = []
        expect['http_server_names'] = []
        for index, domain in enumerate(self.http_server_names):
            domain_values = {
                'id': index + 1,
                'load_balancer_id': load_balancer_ref.id,
                'name': domain,
            }
            expect['http_server_names'].append(domain)
            domain_ref = models.LoadBalancerDomain()
            domain_ref.update(domain_values)
            load_balancer_ref.domains.append(domain_ref)
        for uuid in self.instance_uuids:
            association_values = {
                'load_balancer_id': load_balancer_ref.id,
                'instance_uuid': uuid,
            }
            expect['instance_uuids'].append(uuid)
            association_ref = models.LoadBalancerInstanceAssociation()
            association_ref.update(association_values)
            load_balancer_ref.instances.append(association_ref)
        filters = dict()
        if self.context.is_admin and kwargs['all_tenants']:
            pass
        else:
            filters['project_id'] = self.project_id
            self.context.elevated(read_deleted='no').AndReturn(admin_context)
        db.load_balancer_get_all(
            admin_context, filters=filters).AndReturn([load_balancer_ref])
        self.mox.ReplayAll()
        r = api.get_all_load_balancers(self.context, **self.kwargs)
        self.mox.VerifyAll()
        self.assertEqual(r, {'data': [expect]})
Example #2
0
def gen_dns_prefix():
    ctxt = context.get_admin_context(read_deleted="yes")
    all_load_balancers = db.load_balancer_get_all(ctxt)
    all_prefixs = map(lambda x: x['dns_prefix'], all_load_balancers)
    prefix = generate_uid(size=10)
    while prefix in all_prefixs:
        prefix = generate_uid(size=10)
    return prefix
Example #3
0
def allocate_listen_port():
    filters = dict({'protocol': 'tcp'})
    ctxt = context.get_admin_context(read_deleted="yes")
    all_lbs = db.load_balancer_get_all(ctxt, filters=filters)

    active_load_balancers = filter(lambda x: not x['deleted'], all_lbs)
    deleted_load_balancers = filter(lambda x: x['deleted'], all_lbs)
    allocated_ports = map(lambda x: x['listen_port'], active_load_balancers)
    available_ports = filter(lambda x: x not in allocated_ports,
                             map(lambda y: y['listen_port'],
                                 deleted_load_balancers))

    if available_ports:
        return available_ports[0]
    elif allocated_ports:
        return max(allocated_ports) + 1
    else:
        return 11000
Example #4
0
def get_all_load_balancers(context, **kwargs):
    expect_keys = [
        'user_id', 'tenant_id', 'all_tenants',
    ]
    utils.check_input_parameters(expect_keys, **kwargs)

    result = []
    try:
        all_tenants = int(kwargs['all_tenants'])
        if context.is_admin and all_tenants:
            filters = {}
        else:
            filters = {'project_id': kwargs['tenant_id']}
            context = context.elevated(read_deleted='no')
        all_load_balancers = db.load_balancer_get_all(context, filters=filters)
        for load_balancer_ref in all_load_balancers:
            result.append(format_msg_to_client(load_balancer_ref))
    except Exception, exp:
        raise exception.GetAllLoadBalancerFailed(msg=str(exp))
Example #5
0
def checker_routine(*args, **kwargs):
    LOG.info('nozzle checker starting...')

    broadcast = kwargs['broadcast']
    states = [state.CREATING, state.UPDATING, state.DELETING]
    while True:
        eventlet.sleep(6)
        msg_type = 'lb'
        msg_uuid = utils.str_uuid()
        try:
            ctxt = context.get_admin_context()
            all_load_balancers = db.load_balancer_get_all(ctxt)
            transient_load_balancers = filter(lambda x: x.state in states,
                                              all_load_balancers)
            for load_balancer_ref in transient_load_balancers:
                try:
                    result = dict()
                    message = dict()
                    if load_balancer_ref.state == state.CREATING:
                        message['cmd'] = 'create_lb'
                        result = api.format_msg_to_worker(load_balancer_ref)
                    elif load_balancer_ref.state == state.UPDATING:
                        message['cmd'] = 'update_lb'
                        result = api.format_msg_to_worker(load_balancer_ref)
                    elif load_balancer_ref.state == state.DELETING:
                        message['cmd'] = 'delete_lb'
                        result['user_id'] = load_balancer_ref['user_id']
                        result['tenant_id'] = load_balancer_ref['project_id']
                        result['uuid'] = load_balancer_ref['uuid']
                        result['protocol'] = load_balancer_ref['protocol']
                    message['args'] = result
                    request_msg = jsonutils.dumps(message)
                    LOG.debug(">>>>>>> worker: %s" % request_msg)
                    broadcast.send_multipart([msg_type, msg_uuid, request_msg])
                except Exception as exp:
                    LOG.exception(str(exp))
                    continue
        except Exception as exp:
            LOG.exception(str(exp))
            continue
Example #6
0
def get_all_load_balancers(filters=None):
    ctxt = context.get_admin_context()
    all_load_balancers = db.load_balancer_get_all(ctxt, filters=filters)
    return all_load_balancers