def test_serverfarm_get_all_by_lb_id(self):
     values1 = get_fake_sf("1")
     values2 = get_fake_sf("2")
     sf_ref1 = db_api.serverfarm_create(self.conf, values1)
     sf_ref2 = db_api.serverfarm_create(self.conf, values2)
     sfs1 = db_api.serverfarm_get_all_by_lb_id(self.conf, "1")
     sfs2 = db_api.serverfarm_get_all_by_lb_id(self.conf, "2")
     self.assertEqual([dict(sf_ref1.iteritems())], [dict(sf.iteritems()) for sf in sfs1])
     self.assertEqual([dict(sf_ref2.iteritems())], [dict(sf.iteritems()) for sf in sfs2])
示例#2
0
 def test_serverfarm_get_all_by_sf_id(self):
     values1 = get_fake_sf('1')
     values2 = get_fake_sf('2')
     sf_ref1 = db_api.serverfarm_create(self.conf, values1)
     sf_ref2 = db_api.serverfarm_create(self.conf, values2)
     sfs1 = db_api.serverfarm_get_all_by_lb_id(self.conf, '1')
     sfs2 = db_api.serverfarm_get_all_by_lb_id(self.conf, '2')
     self.assertEqual(len(sfs1), 1)
     self.assertEqual(sfs1[0]['id'], sf_ref1['id'])
     self.assertEqual(len(sfs2), 1)
     self.assertEqual(sfs2[0]['id'], sf_ref2['id'])
示例#3
0
def lb_show_nodes(conf, tenant_id, lb_id):
    node_list = []
    sf = db_api.serverfarm_get_all_by_lb_id(conf,
            lb_id, tenant_id=tenant_id)[0]
    node_list = map(db_api.unpack_extra,
                    db_api.server_get_all_by_sf_id(conf, sf['id']))
    return node_list
示例#4
0
def lb_add_vip(conf, tenant_id, lb_id, vip_dict):
    LOG.debug("Called lb_add_vip(), conf: %r, lb_id: %s, vip_dict: %r",
                 conf, lb_id, vip_dict)
    lb_ref = db_api.loadbalancer_get(conf, lb_id, tenant_id=tenant_id)
    # NOTE(akscram): server farms are really only create problems than
    #                they solve multiply use of the virtual IPs.
    try:
        sf_ref = db_api.serverfarm_get_all_by_lb_id(conf, lb_ref['id'])[0]
    except IndexError:
        raise exc.ServerFarmNotFound

    values = db_api.virtualserver_pack_extra(vip_dict)
    values['lb_id'] = lb_ref['id']
    values['sf_id'] = sf_ref['id']
    # XXX(akscram): Set default protocol from LoadBalancer to
    #               VirtualServer if it is not present.
    if not values.get('extra'):
        values['extra'] = {'protocol': lb_ref['protocol']}
    elif 'protocol' not in values['extra']:
        values['extra']['protocol'] = lb_ref['protocol']
    vip_ref = db_api.virtualserver_create(conf, values)
    device_driver = drivers.get_device_driver(conf, lb_ref['device_id'])
    with device_driver.request_context() as ctx:
        commands.create_vip(ctx, vip_ref, sf_ref)
    return db_api.unpack_extra(vip_ref)
示例#5
0
def lb_delete_probe(conf, lb_id, probe_id):
    lb = db_api.loadbalancer_get(conf, lb_id)
    sf = db_api.serverfarm_get_all_by_lb_id(conf, lb_id)[0]
    probe = db_api.probe_get(conf, probe_id)
    db_api.probe_destroy(conf, probe_id)
    device_driver = drivers.get_device_driver(conf, lb['device_id'])
    with device_driver.request_context() as ctx:
        commands.remove_probe_from_server_farm(ctx, sf, probe)
    return probe_id
示例#6
0
def update_lb(conf, tenant_id, lb_id, lb_body):
    lb_ref = db_api.loadbalancer_get(conf, lb_id, tenant_id=tenant_id)
    old_lb_ref = copy.deepcopy(lb_ref)
    db_api.pack_update(lb_ref, lb_body)
    lb_ref = db_api.loadbalancer_update(conf, lb_id, lb_ref)
    if (lb_ref['algorithm'] == old_lb_ref['algorithm'] and
        lb_ref['protocol'] == old_lb_ref['protocol']):
        LOG.debug("In LB %r algorithm and protocol have not changed, "
                     "nothing to do on the device %r.",
                     lb_ref['id'], lb_ref['device_id'])
        return

    sf_ref = db_api.serverfarm_get_all_by_lb_id(conf, lb_ref['id'])[0]
    if lb_ref['algorithm'] != old_lb_ref['algorithm']:
        predictor_ref = db_api.predictor_get_by_sf_id(conf, sf_ref['id'])
        db_api.predictor_update(conf, predictor_ref['id'],
                                {'type': lb_ref['algorithm']})

    vips = db_api.virtualserver_get_all_by_sf_id(conf, sf_ref['id'])
    if lb_ref['protocol'] != old_lb_ref['protocol']:
        vip_update_values = {'protocol': lb_ref['protocol']}
        for vip in vips:
            db_api.pack_update(vip, vip_update_values)
            db_api.virtualserver_update(conf, vip['id'], vip)

    servers = db_api.server_get_all_by_sf_id(conf, sf_ref['id'])
    probes = db_api.probe_get_all_by_sf_id(conf, sf_ref['id'])
    stickies = db_api.sticky_get_all_by_sf_id(conf, sf_ref['id'])

    device_ref = scheduler.reschedule(conf, lb_ref)
    if device_ref['id'] != lb_ref['device_id']:
        from_driver = drivers.get_device_driver(conf, lb_ref['device_id'])
        to_driver = drivers.get_device_driver(conf, device_ref['id'])
        lb_ref = db_api.loadbalancer_update(conf, lb_ref['id'],
                                            {'device_id': device_ref['id']})
    else:
        from_driver = drivers.get_device_driver(conf, device_ref['id'])
        to_driver = from_driver

    with from_driver.request_context() as ctx:
        try:
            commands.delete_loadbalancer(ctx, sf_ref, vips, servers, probes,
                                         stickies)
        except Exception:
            with utils.save_and_reraise_exception():
                db_api.loadbalancer_update(conf, lb_ref['id'],
                                           {'status': lb_status.ERROR})
    with to_driver.request_context() as ctx:
        try:
            commands.create_loadbalancer(ctx, sf_ref, vips, servers, probes,
                                         stickies)
        except Exception:
            with utils.save_and_reraise_exception():
                db_api.loadbalancer_update(conf, lb_ref['id'],
                                           {'status': lb_status.ERROR})
    db_api.loadbalancer_update(conf, lb_ref['id'],
                               {'status': lb_status.ACTIVE})
示例#7
0
def lb_delete_node(conf, lb_id, lb_node_id):
    lb = db_api.loadbalancer_get(conf, lb_id)
    sf = db_api.serverfarm_get_all_by_lb_id(conf, lb_id)[0]
    rs = db_api.server_get(conf, lb_node_id)
    db_api.server_destroy(conf, lb_node_id)
    device_driver = drivers.get_device_driver(conf, lb['device_id'])
    with device_driver.request_context() as ctx:
        commands.remove_node_from_loadbalancer(ctx, sf, rs)
    return lb_node_id
示例#8
0
def lb_add_sticky(conf, lb_id, st):
    logger.debug("Got new sticky description %s" % st)
    if st['persistenceType'] is None:
        return
    lb = db_api.loadbalancer_get(conf, lb_id)
    sf = db_api.serverfarm_get_all_by_lb_id(conf, lb_id)[0]
    values = db_api.sticky_pack_extra(st)
    values['sf_id'] = sf['id']
    sticky_ref = db_api.sticky_create(conf, values)
    device_driver = drivers.get_device_driver(conf, lb['device_id'])
    with device_driver.request_context() as ctx:
        commands.add_sticky_to_loadbalancer(ctx, lb, sticky_ref)
    return db_api.unpack_extra(sticky_ref)
示例#9
0
def lb_add_nodes(conf, lb_id, nodes):
    nodes_list = []
    lb = db_api.loadbalancer_get(conf, lb_id)
    sf = db_api.serverfarm_get_all_by_lb_id(conf, lb_id)[0]
    for node in nodes:
        values = db_api.server_pack_extra(node)
        values['sf_id'] = sf['id']
        rs_ref = db_api.server_create(conf, values)
        device_driver = drivers.get_device_driver(conf, lb['device_id'])
        with device_driver.request_context() as ctx:
            commands.add_node_to_loadbalancer(ctx, sf, rs_ref)
        nodes_list.append(db_api.unpack_extra(rs_ref))
    return nodes_list
示例#10
0
def lb_show_sticky(conf, lb_id):
    try:
        sf_ref = db_api.serverfarm_get_all_by_lb_id(conf, lb_id)[0]
    except IndexError:
        raise  exc.ServerFarmNotFound

    stickies = db_api.sticky_get_all_by_sf_id(conf, sf_ref['id'])

    list = []
    dict = {"sessionPersistence": {}}
    for sticky in stickies:
        list.append(db_api.unpack_extra(sticky))
    dict['sessionPersistence'] = list
    return dict
示例#11
0
def lb_show_probes(conf, lb_id):
    try:
        sf_ref = db_api.serverfarm_get_all_by_lb_id(conf, lb_id)[0]
    except IndexError:
        raise exc.ServerFarmNotFound

    probes = db_api.probe_get_all_by_sf_id(conf, sf_ref['id'])

    list = []
    dict = {"healthMonitoring": {}}
    for probe in probes:
        list.append(db_api.unpack_extra(probe))
    dict['healthMonitoring'] = list
    return dict
示例#12
0
def lb_show_details(conf, lb_id):
    lb = db_api.loadbalancer_get(conf, lb_id)
    sf = db_api.serverfarm_get_all_by_lb_id(conf, lb_id)[0]
    vips = db_api.virtualserver_get_all_by_sf_id(conf, sf['id'])
    rs = db_api.server_get_all_by_sf_id(conf, sf['id'])
    probes = db_api.probe_get_all_by_sf_id(conf, sf['id'])
    stickies = db_api.sticky_get_all_by_sf_id(conf, sf['id'])

    lb_ref = db_api.unpack_extra(lb)
    lb_ref['nodes'] = [db_api.unpack_extra(rserver) for rserver in rs]
    lb_ref['virtualIps'] = [db_api.unpack_extra(vip) for vip in vips]
    lb_ref['healthMonitor'] = [db_api.unpack_extra(probe) for probe in probes]
    lb_ref['sessionPersistence'] = [db_api.unpack_extra(sticky)\
            for sticky in stickies]
    return lb_ref
示例#13
0
def delete_lb(conf, tenant_id, lb_id):
    lb_ref = db_api.loadbalancer_get(conf, lb_id, tenant_id=tenant_id)
    sf_ref = db_api.serverfarm_get_all_by_lb_id(conf, lb_ref['id'])[0]
    vips = db_api.virtualserver_get_all_by_sf_id(conf, sf_ref['id'])
    servers = db_api.server_get_all_by_sf_id(conf, sf_ref['id'])
    probes = db_api.probe_get_all_by_sf_id(conf, sf_ref['id'])
    stickies = db_api.sticky_get_all_by_sf_id(conf, sf_ref['id'])
    device_driver = drivers.get_device_driver(conf, lb_ref['device_id'])
    with device_driver.request_context() as ctx:
        commands.delete_loadbalancer(ctx, sf_ref, vips, servers, probes,
                                     stickies)
    db_api.probe_destroy_by_sf_id(conf, sf_ref['id'])
    db_api.sticky_destroy_by_sf_id(conf, sf_ref['id'])
    db_api.server_destroy_by_sf_id(conf, sf_ref['id'])
    db_api.virtualserver_destroy_by_sf_id(conf, sf_ref['id'])
    db_api.predictor_destroy_by_sf_id(conf, sf_ref['id'])
    db_api.serverfarm_destroy(conf, sf_ref['id'])
    db_api.loadbalancer_destroy(conf, lb_ref['id'])
示例#14
0
def lb_add_vip(conf, lb_id, vip_dict):
    logger.debug("Called lb_add_vip(), conf: %r, lb_id: %s, vip_dict: %r",
                 conf, lb_id, vip_dict)
    lb_ref = db_api.loadbalancer_get(conf, lb_id)
    # NOTE(akscram): server farms are really only create problems than
    #                they solve multiply use of the virtual IPs.
    try:
        sf_ref = db_api.serverfarm_get_all_by_lb_id(conf, lb_ref['id'])[0]
    except IndexError:
        raise exc.ServerFarmNotFound

    values = db_api.virtualserver_pack_extra(vip_dict)
    values['lb_id'] = lb_ref['id']
    values['sf_id'] = sf_ref['id']
    vip_ref = db_api.virtualserver_create(conf, values)
    device_driver = drivers.get_device_driver(conf, lb_ref['device_id'])
    with device_driver.request_context() as ctx:
        commands.create_vip(ctx, vip_ref, sf_ref)
    return db_api.unpack_extra(vip_ref)
示例#15
0
def lb_add_probe(conf, lb_id, probe_dict):
    logger.debug("Got new probe description %s" % probe_dict)
    # NOTE(akscram): historically strange validation, wrong place for it.
    if probe_dict['type'] is None:
        return

    lb_ref = db_api.loadbalancer_get(conf, lb_id)
    # NOTE(akscram): server farms are really only create problems than
    #                they solve multiply use of the virtual IPs.
    try:
        sf_ref = db_api.serverfarm_get_all_by_lb_id(conf, lb_ref['id'])[0]
    except IndexError:
        raise exc.ServerFarmNotFound

    values = db_api.probe_pack_extra(probe_dict)
    values['lb_id'] = lb_ref['id']
    values['sf_id'] = sf_ref['id']
    probe_ref = db_api.probe_create(conf, values)
    device_driver = drivers.get_device_driver(conf, lb_ref['device_id'])
    with device_driver.request_context() as ctx:
        commands.add_probe_to_loadbalancer(ctx, sf_ref, probe_ref)
    return db_api.unpack_extra(probe_ref)
示例#16
0
    def loadFromDB(self, lb_id):
        self.lb = db_api.loadbalancer_get(self.conf, lb_id)
        self.sf = db_api.serverfarm_get_all_by_lb_id(self.conf, lb_id)[0]
        sf_id = self.sf['id']

        self.vips = db_api.virtualserver_get_all_by_sf_id(self.conf, sf_id)

        predictor = db_api.predictor_get_all_by_sf_id(self.conf, sf_id)[0]
        self.sf._predictor = predictor

        self.rs = db_api.server_get_all_by_sf_id(self.conf, sf_id)
        self.sf._rservers = []
        for rs in self.rs:
            self.sf._rservers.append(rs)

        self.probes = db_api.probe_get_all_by_sf_id(self.conf, sf_id)
        self.sf._probes = []
        for prob in self.probes:
            self.sf._probes.append(prob)

        sticks = db_api.sticky_get_all_by_sf_id(self.conf, sf_id)
        self.sf._sticky = []
        for st in sticks:
            self.sf._sticky.append(st)
示例#17
0
def delete_loadbalancer(ctx, lb):
    sf = db_api.serverfarm_get_all_by_lb_id(ctx.conf, lb['id'])[0]
    vips = db_api.virtualserver_get_all_by_sf_id(ctx.conf, sf['id'])
    rs = db_api.server_get_all_by_sf_id(ctx.conf, sf['id'])
    probes = db_api.probe_get_all_by_sf_id(ctx.conf, sf['id'])
    stickies = db_api.sticky_get_all_by_sf_id(ctx.conf, sf['id'])
    for vip in vips:
        delete_vip(ctx, vip)
    for rserver in rs:
        delete_rserver_from_server_farm(ctx, sf, rserver)
        delete_rserver(ctx, rserver)
    for probe in probes:
        remove_probe_from_server_farm(ctx, sf, probe)
        delete_probe(ctx, probe)
    for sticky in stickies:
        delete_sticky(ctx, sticky)
    delete_server_farm(ctx, sf)
    db_api.predictor_destroy_by_sf_id(ctx.conf, sf['id'])
    db_api.server_destroy_by_sf_id(ctx.conf, sf['id'])
    db_api.probe_destroy_by_sf_id(ctx.conf, sf['id'])
    db_api.virtualserver_destroy_by_sf_id(ctx.conf, sf['id'])
    db_api.sticky_destroy_by_sf_id(ctx.conf, sf['id'])
    db_api.serverfarm_destroy(ctx.conf, sf['id'])
    db_api.loadbalancer_destroy(ctx.conf, lb['id'])
示例#18
0
def update_loadbalancer(ctx, old_bal_ref,  new_bal_ref):
    if old_bal_ref['algorithm'] != new_bal_ref['algorithm']:
        sf_ref = db_api.serverfarm_get_all_by_lb_id(ctx.conf,
                                                    new_bal_ref['id'])[0]
        create_server_farm(ctx, sf_ref)
示例#19
0
 def test_serverfarm_get_all_by_lb_id1(self):
     lb_id, sf_id = self._create_lb_and_sf('1', 'tenant1')
     res = db_api.serverfarm_get_all_by_lb_id(self.conf,
             lb_id, tenant_id='tenant2')
     self.assertEqual([], res)