Ejemplo n.º 1
0
def create_locks(context, network_ids, addresses):
    """Creates locks for each IP address that is null-routed.

    The function creates the IP address if it is not present in the database.

    """

    for address in addresses:
        address_model = None
        try:
            address_model = _find_or_create_address(context, network_ids,
                                                    address)
            lock_holder = None
            if address_model.lock_id:
                lock_holder = db_api.lock_holder_find(
                    context,
                    lock_id=address_model.lock_id,
                    name=LOCK_NAME,
                    scope=db_api.ONE)

            if not lock_holder:
                LOG.info("Creating lock holder on IPAddress %s with id %s",
                         address_model.address_readable, address_model.id)
                db_api.lock_holder_create(context,
                                          address_model,
                                          name=LOCK_NAME,
                                          type="ip_address")
        except Exception:
            LOG.exception("Failed to create lock holder on IPAddress %s",
                          address_model)
            continue
    context.session.flush()
Ejemplo n.º 2
0
    def _stubs(self, network, subnet, address, lock=False):
        self.ipam = quark.ipam.QuarkIpamANY()
        with self.context.session.begin():
            next_ip = subnet.pop("next_auto_assign_ip", 0)
            net_mod = db_api.network_create(self.context, **network)
            subnet["network"] = net_mod
            sub_mod = db_api.subnet_create(self.context, **subnet)

            address["network_id"] = net_mod["id"]
            address["subnet_id"] = sub_mod["id"]
            ip = db_api.ip_address_create(self.context, **address)
            address.pop("address")
            ip = db_api.ip_address_update(self.context, ip, **address)

            # NOTE(asadoughi): update after cidr constructor has been invoked
            db_api.subnet_update(self.context,
                                 sub_mod,
                                 next_auto_assign_ip=next_ip)

        if lock:
            db_api.lock_holder_create(self.context,
                                      ip,
                                      name="testlock",
                                      type="ip_address")
        yield net_mod
Ejemplo n.º 3
0
def create_locks(context, network_ids, addresses):
    """Creates locks for each IP address that is null-routed.

    The function creates the IP address if it is not present in the database.

    """

    for address in addresses:
        address_model = None
        try:
            address_model = _find_or_create_address(
                context, network_ids, address)
            lock_holder = None
            if address_model.lock_id:
                lock_holder = db_api.lock_holder_find(
                    context,
                    lock_id=address_model.lock_id, name=LOCK_NAME,
                    scope=db_api.ONE)

            if not lock_holder:
                LOG.info("Creating lock holder on IPAddress %s with id %s",
                         address_model.address_readable,
                         address_model.id)
                db_api.lock_holder_create(
                    context, address_model, name=LOCK_NAME, type="ip_address")
        except Exception:
            LOG.exception("Failed to create lock holder on IPAddress %s",
                          address_model)
            continue
    context.session.flush()
Ejemplo n.º 4
0
    def test_delete_locks_doesnt_have_lock(self):
        network = db_api.network_create(self.context)
        address_model = db_api.ip_address_create(
            self.context,
            address=netaddr.IPAddress("192.168.10.1"),
            network=network)
        db_api.lock_holder_create(
            self.context, address_model,
            name="not-null-routes", type="ip_address")
        self.context.session.flush()

        null_routes.delete_locks(self.context, [network.id], [])
        self.context.session.refresh(address_model)
        self.assertIsNotNone(address_model.lock_id)
Ejemplo n.º 5
0
    def test_create_lock_holder(self):
        kwargs = {"address": netaddr.IPAddress("192.168.2.1")}
        ip_address = db_api.ip_address_create(self.context, **kwargs)
        kwargs = {"type": "ip_address", "name": "because i said so"}
        lock_holder = db_api.lock_holder_create(
            self.context, ip_address, **kwargs)

        self.context.session.refresh(ip_address)
        self.assertEqual(ip_address.lock_id, lock_holder.lock_id)
Ejemplo n.º 6
0
    def test_create_locks_lock_holder_exists(self):
        network = db_api.network_create(self.context)
        address_model = db_api.ip_address_create(
            self.context,
            address=netaddr.IPAddress("192.168.10.1"),
            network=network)
        db_api.lock_holder_create(
            self.context, address_model,
            name=null_routes.LOCK_NAME, type="ip_address")
        self.context.session.flush()

        addresses = netaddr.IPSet(netaddr.IPNetwork(self.sub_cidr))
        null_routes.create_locks(self.context, [network.id], addresses)

        lock_holders = db_api.lock_holder_find(
            self.context,
            lock_id=address_model.lock_id,
            name=null_routes.LOCK_NAME,
            scope=db_api.ALL)
        self.assertEqual(len(lock_holders), 1)
Ejemplo n.º 7
0
    def test_create_lock_holder_already_locked(self):
        kwargs = {"address": netaddr.IPAddress("192.168.2.1")}
        ip_address = db_api.ip_address_create(self.context, **kwargs)
        name = "because i said so"
        kwargs_1 = {"type": "ip_address", "name": name + "1"}
        lock_holder_1 = db_api.lock_holder_create(
            self.context, ip_address, **kwargs_1)
        self.context.session.flush()

        kwargs_2 = {"type": "ip_address", "name": name + "2"}
        self.context.session.refresh(ip_address)
        lock_holder_2 = db_api.lock_holder_create(
            self.context, ip_address, **kwargs_2)
        self.context.session.flush()

        self.context.session.refresh(ip_address)
        self.assertNotEqual(lock_holder_1.id, lock_holder_2.id)
        self.assertEqual(lock_holder_1.name, name + "1")
        self.assertEqual(lock_holder_2.name, name + "2")
        self.assertEqual(lock_holder_1.lock_id, lock_holder_2.lock_id)
        self.assertEqual(ip_address.lock_id, lock_holder_1.lock_id)
Ejemplo n.º 8
0
    def _stubs(self, network, subnet, address, lock=False):
        self.ipam = quark.ipam.QuarkIpamANY()
        with self.context.session.begin():
            next_ip = subnet.pop("next_auto_assign_ip", 0)
            net_mod = db_api.network_create(self.context, **network)
            subnet["network"] = net_mod
            sub_mod = db_api.subnet_create(self.context, **subnet)

            address["network_id"] = net_mod["id"]
            address["subnet_id"] = sub_mod["id"]
            ip = db_api.ip_address_create(self.context, **address)
            address.pop("address")
            ip = db_api.ip_address_update(self.context, ip, **address)

            # NOTE(asadoughi): update after cidr constructor has been invoked
            db_api.subnet_update(self.context,
                                 sub_mod,
                                 next_auto_assign_ip=next_ip)

        if lock:
            db_api.lock_holder_create(self.context, ip,
                                      name="testlock", type="ip_address")
        yield net_mod
Ejemplo n.º 9
0
    def test_find_lock_holder(self):
        kwargs = {"address": netaddr.IPAddress("192.168.2.1")}
        ip_address = db_api.ip_address_create(self.context, **kwargs)
        kwargs = {"type": "ip_address", "name": "because i said so"}
        lock_holder = db_api.lock_holder_create(
            self.context, ip_address, **kwargs)

        self.context.session.refresh(ip_address)
        self.assertEqual(ip_address.lock_id, lock_holder.lock_id)

        lock_holders = db_api.lock_holder_find(
            self.context,
            lock_id=ip_address.lock_id, name=kwargs["name"],
            scope=db_api.ALL)
        self.assertEqual(len(lock_holders), 1)
        self.assertEqual(lock_holders[0]["lock_id"], ip_address.lock_id)
        self.assertEqual(lock_holders[0]["name"], kwargs["name"])