Example #1
0
 def test_virtualserver_get_all_by_sf_id(self):
     values = get_fake_virtualserver('1', '1')
     virtualserver1 = db_api.virtualserver_create(self.conf, values)
     values = get_fake_virtualserver('2', '1')
     virtualserver2 = db_api.virtualserver_create(self.conf, values)
     virtualservers1 = db_api.virtualserver_get_all_by_sf_id(self.conf, '1')
     virtualservers2 = db_api.virtualserver_get_all_by_sf_id(self.conf, '2')
     self.assertEqual(len(virtualservers1), 1)
     self.assertEqual(virtualservers1[0]['id'], virtualserver1['id'])
     self.assertEqual(len(virtualservers2), 1)
     self.assertEqual(virtualservers2[0]['id'], virtualserver2['id'])
 def test_virtualserver_destroy_by_sf_id(self):
     values = get_fake_virtualserver("1", "1")
     virtualserver_ref1 = db_api.virtualserver_create(self.conf, values)
     values = get_fake_virtualserver("2", "1")
     virtualserver_ref2 = db_api.virtualserver_create(self.conf, values)
     db_api.virtualserver_destroy_by_sf_id(self.conf, "1")
     with self.assertRaises(exception.VirtualServerNotFound) as cm:
         db_api.virtualserver_get(self.conf, virtualserver_ref1["id"])
     err = cm.exception
     expected = {"virtualserver_id": virtualserver_ref1["id"]}
     self.assertEqual(err.kwargs, expected)
     virtualserver_ref3 = db_api.virtualserver_get(self.conf, virtualserver_ref2["id"])
     self.assertEqual(dict(virtualserver_ref2.iteritems()), dict(virtualserver_ref3.iteritems()))
Example #3
0
 def test_virtualserver_destroy_by_sf_id(self):
     values = get_fake_virtualserver('1', '1')
     virtualserver_ref1 = db_api.virtualserver_create(self.conf, values)
     values = get_fake_virtualserver('2', '1')
     virtualserver_ref2 = db_api.virtualserver_create(self.conf, values)
     db_api.virtualserver_destroy_by_sf_id(self.conf, '1')
     with self.assertRaises(exception.VirtualServerNotFound) as cm:
         db_api.virtualserver_get(self.conf, virtualserver_ref1['id'])
     err = cm.exception
     expected = {'virtualserver_id': virtualserver_ref1['id']}
     self.assertEqual(err.kwargs, expected)
     virtualserver_ref3 = db_api.virtualserver_get(self.conf,
                                                   virtualserver_ref2['id'])
     self.assertEqual(virtualserver_ref3['id'], virtualserver_ref2['id'])
Example #4
0
 def test_virtualserver_get_all_by_sf_id(self):
     values = get_fake_virtualserver('1', '1')
     virtualserver1 = db_api.virtualserver_create(self.conf, values)
     virtualserver2 = db_api.virtualserver_create(self.conf, values)
     values = get_fake_virtualserver('2', '1')
     virtualserver3 = db_api.virtualserver_create(self.conf, values)
     virtualserver4 = db_api.virtualserver_create(self.conf, values)
     virtualservers1 = db_api.virtualserver_get_all_by_sf_id(self.conf, '1')
     virtualservers2 = db_api.virtualserver_get_all_by_sf_id(self.conf, '2')
     self.assertEqual([dict(vs.iteritems()) for vs in virtualservers1],
                      [dict(vs.iteritems()) for vs in [virtualserver1,
                                                       virtualserver2]])
     self.assertEqual([dict(vs.iteritems()) for vs in virtualservers2],
                      [dict(vs.iteritems()) for vs in [virtualserver3,
                                                       virtualserver4]])
Example #5
0
 def test_virtualserver_get(self):
     values = get_fake_virtualserver('1', '1')
     virtualserver_ref1 = db_api.virtualserver_create(self.conf, values)
     virtualserver_ref2 = db_api.virtualserver_get(self.conf,
                                                   virtualserver_ref1['id'])
     self.assertEqual(dict(virtualserver_ref1.iteritems()),
                      dict(virtualserver_ref2.iteritems()))
Example #6
0
 def test_virtualserver_create(self):
     values = get_fake_virtualserver('1', '1')
     virtualserver_ref = db_api.virtualserver_create(self.conf, values)
     virtualserver = dict(virtualserver_ref.iteritems())
     self.assertIsNotNone(virtualserver['id'])
     values['id'] = virtualserver['id']
     self.assertEqual(virtualserver, values)
 def test_virtualserver_create(self):
     values = get_fake_virtualserver("1", "1")
     virtualserver_ref = db_api.virtualserver_create(self.conf, values)
     virtualserver = dict(virtualserver_ref.iteritems())
     self.assertIsNotNone(virtualserver["id"])
     values["id"] = virtualserver["id"]
     self.assertEqual(virtualserver, values)
Example #8
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)
Example #9
0
 def test_virtualserver_get_with_tenant_fails(self):
     lb_id, sf_id = self._create_lb_and_sf('1', 'tenant1')
     values = get_fake_virtualserver(sf_id, lb_id)
     virtualserver_ref1 = db_api.virtualserver_create(self.conf, values)
     with self.assertRaises(exception.VirtualServerNotFound):
         db_api.virtualserver_get(self.conf,
             virtualserver_ref1['id'], tenant_id='tenant2')
Example #10
0
def create_loadbalancer(ctx, balancer, nodes, probes, vips):
    lb = db_api.unpack_extra(balancer)
    sf = db_api.serverfarm_create(ctx.conf, {'lb_id': lb['id']})
    if 'algorithm' in lb:
        predictor_params = {'sf_id': sf['id'], 'type': lb['algorithm']}
    else:
        predictor_params = {'sf_id': sf['id']}
    db_api.predictor_create(ctx.conf, predictor_params)
    create_server_farm(ctx, sf)
    for node in nodes:
        node_values = db_api.server_pack_extra(node)
        node_values['sf_id'] = sf['id']
        rs_ref = db_api.server_create(ctx.conf, node_values)
        create_rserver(ctx, rs_ref)
        add_rserver_to_server_farm(ctx, sf, rs_ref)

    for probe in probes:
        probe_values = db_api.probe_pack_extra(probe)
        probe_values['lb_id'] = lb['id']
        probe_values['sf_id'] = sf['id']
        probe_ref = db_api.probe_create(ctx.conf, probe_values)
        create_probe(ctx,  probe_ref)
        add_probe_to_server_farm(ctx, sf, probe_ref)

    for vip in vips:
        vip_values = db_api.virtualserver_pack_extra(vip)
        vip_values['lb_id'] = lb['id']
        vip_values['sf_id'] = sf['id']
        vip_ref = db_api.virtualserver_create(ctx.conf, vip_values)
        create_vip(ctx, vip_ref, sf)
Example #11
0
 def test_virtualserver_destroy(self):
     values = get_fake_virtualserver('1', '1')
     virtualserver = db_api.virtualserver_create(self.conf, values)
     db_api.virtualserver_destroy(self.conf, virtualserver['id'])
     with self.assertRaises(exception.VirtualServerNotFound) as cm:
         db_api.virtualserver_get(self.conf, virtualserver['id'])
     err = cm.exception
     self.assertEqual(err.kwargs, {'virtualserver_id': virtualserver['id']})
Example #12
0
 def test_virtualserver_get_with_tenant(self):
     lb_id, sf_id = self._create_lb_and_sf('1', 'tenant1')
     values = get_fake_virtualserver(sf_id, lb_id)
     virtualserver_ref1 = db_api.virtualserver_create(self.conf, values)
     virtualserver_ref2 = db_api.virtualserver_get(self.conf,
             virtualserver_ref1['id'], tenant_id='tenant1')
     self.assertEqual(dict(virtualserver_ref1.iteritems()),
                      dict(virtualserver_ref2.iteritems()))
Example #13
0
 def test_virtualserver_get_all_by_lb_id_with_tenant(self):
     lb_id1, sf_id1 = self._create_lb_and_sf('1', 'tenant1')
     values = get_fake_virtualserver(sf_id1, lb_id1)
     virtualserver1 = db_api.virtualserver_create(self.conf, values)
     virtualserver2 = db_api.virtualserver_create(self.conf, values)
     lb_id2, sf_id2 = self._create_lb_and_sf('1', 'tenant2')
     values = get_fake_virtualserver(sf_id2, lb_id2)
     virtualserver3 = db_api.virtualserver_create(self.conf, values)
     virtualserver4 = db_api.virtualserver_create(self.conf, values)
     virtualservers1 = db_api.virtualserver_get_all_by_lb_id(self.conf,
             lb_id1, tenant_id='tenant1')
     virtualservers2 = db_api.virtualserver_get_all_by_lb_id(self.conf,
             lb_id2, tenant_id='tenant1')
     self.assertEqual([dict(vs.iteritems()) for vs in virtualservers1],
                      [dict(vs.iteritems()) for vs in [virtualserver1,
                                                       virtualserver2]])
     self.assertEqual([], virtualservers2)
 def test_virtualserver_update(self):
     values = get_fake_virtualserver("1", "1")
     virtualserver_ref = db_api.virtualserver_create(self.conf, values)
     update = {"port": "80", "deployed": "True", "extra": {"ipVersion": "IPv4", "VLAN": 400, "ICMPreply": False}}
     virtualserver_ref = db_api.virtualserver_update(self.conf, virtualserver_ref["id"], update)
     virtualserver = dict(virtualserver_ref.iteritems())
     values.update(update)
     self.assertIsNotNone(virtualserver["id"])
     values["id"] = virtualserver["id"]
     self.assertEqual(virtualserver, values)
Example #15
0
def create_lb(conf, params):
    node_values = params.pop('nodes', [])
    probe_values = params.pop('healthMonitor', [])
    vip_values = params.pop('virtualIps', [])
    lb_values = db_api.loadbalancer_pack_extra(params)

    lb_ref = db_api.loadbalancer_create(conf, lb_values)
    sf_ref = db_api.serverfarm_create(conf, {'lb_id': lb_ref['id']})
    db_api.predictor_create(conf, {'sf_id': sf_ref['id'],
                                   'type': lb_ref['algorithm']})
    vip_update_values = {'protocol': lb_ref['protocol']}

    vips = []
    for vip in vip_values:
        vip = db_api.virtualserver_pack_extra(vip)
        db_api.pack_update(vip, vip_update_values)
        vip['lb_id'] = lb_ref['id']
        vip['sf_id'] = sf_ref['id']
        vips.append(db_api.virtualserver_create(conf, vip))

    servers = []
    for server in node_values:
        server = db_api.server_pack_extra(server)
        server['sf_id'] = sf_ref['id']
        servers.append(db_api.server_create(conf, server))

    probes = []
    for probe in probe_values:
        probe = db_api.probe_pack_extra(probe)
        probe['lb_id'] = lb_ref['id']
        probe['sf_id'] = sf_ref['id']
        probes.append(db_api.probe_create(conf, probe))

    device_ref = scheduler.schedule(conf, lb_ref)
    db_api.loadbalancer_update(conf, lb_ref['id'],
                               {'device_id': device_ref['id']})
    device_driver = drivers.get_device_driver(conf, device_ref['id'])
    with device_driver.request_context() as ctx:
        try:
            commands.create_loadbalancer(ctx, sf_ref, vips, servers, probes,
                                         [])
        except Exception:
            with utils.save_and_reraise_exception():
                db_api.loadbalancer_update(conf, lb_ref['id'],
                                           {'status': lb_status.ERROR,
                                            'deployed': False})
    db_api.loadbalancer_update(conf, lb_ref['id'],
                               {'status': lb_status.ACTIVE,
                                'deployed': True})
    return lb_ref['id']
Example #16
0
 def test_virtualserver_update(self):
     values = get_fake_virtualserver('1', '1')
     virtualserver_ref = db_api.virtualserver_create(self.conf, values)
     update = {'port': '80',
               'deployed': 'True',
               'extra': {'ipVersion': 'IPv4',
                         'VLAN': 400,
                         'ICMPreply': False}}
     virtualserver_ref = db_api.virtualserver_update(self.conf,
                                 virtualserver_ref['id'], update)
     virtualserver = dict(virtualserver_ref.iteritems())
     values.update(update)
     self.assertIsNotNone(virtualserver['id'])
     values['id'] = virtualserver['id']
     self.assertEqual(virtualserver, values)
Example #17
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)
Example #18
0
    def savetoDB(self):
        try:
            lb_ref = db_api.loadbalancer_update(self.conf, self.lb['id'],
                                                self.lb)
        except exception.LoadBalancerNotFound:
            lb_ref = db_api.loadbalancer_create(self.conf, self.lb)

        self.sf['lb_id'] = lb_ref['id']
        try:
            sf_ref = db_api.serverfarm_update(self.conf, self.sf['id'],
                                              self.sf)
        except exception.ServerFarmNotFound:
            sf_ref = db_api.serverfarm_create(self.conf, self.sf)

        self.sf._predictor['sf_id'] = sf_ref['id']
        try:
            db_api.predictor_update(self.conf, self.sf._predictor['id'],
                                    self.sf._predictor)
        except exception.PredictorNotFound:
            db_api.predictor_create(self.conf, self.sf._predictor)

        stickies = self.sf._sticky
        vips = []

        self.sf = sf_ref
        self.sf._rservers = []
        self.sf._probes = []
        self.sf._sticky = []

        for rs in self.rs:
            rs['sf_id'] = sf_ref['id']
            try:
                rs_ref = db_api.server_update(self.conf, rs['id'], rs)
            except exception.ServerNotFound:
                rs_ref = db_api.server_create(self.conf, rs)
            self.sf._rservers.append(rs_ref)

        for pr in self.probes:
            pr['sf_id'] = sf_ref['id']
            try:
                pr_ref = db_api.probe_update(self.conf, pr['id'], pr)
            except exception.ProbeNotFound:
                pr_ref = db_api.probe_create(self.conf, pr)
            self.sf._probes.append(pr_ref)

        for vip in self.vips:
            vip['sf_id'] = sf_ref['id']
            vip['lb_id'] = lb_ref['id']
            try:
                vip_ref = db_api.virtualserver_update(self.conf, vip['id'],
                                                      vip)
            except exception.VirtualServerNotFound:
                vip_ref = db_api.virtualserver_create(self.conf, vip)
            vips.append(vip_ref)

        for st in stickies:
            st['sf_id'] = sf_ref['id']
            try:
                st_ref = db_api.sticky_update(self.conf, st['id'], st)
            except exception.StickyNotFound:
                st_ref = db_api.sticky_create(self.conf, st)
            self.sf._sticky.append(st_ref)

        self.rs = self.sf._rservers
        self.probes = self.sf._probes
        self.vips = vips