def save_allowed_ip(interface_id, ip_address_id): allowed_ip = mappers.AllowedIp() update(allowed_ip, id=utils.generate_uuid(), interface_id=interface_id, ip_address_id=ip_address_id) save(allowed_ip)
def create(cls, **values): if 'id' not in values or values['id'] is None: values['id'] = utils.generate_uuid() values['created_at'] = utils.utcnow() instance = cls(**values).save() instance._notify_fields("create") return instance
def _generate_message(self, event_type, priority, payload): return { "message_id": str(utils.generate_uuid()), "publisher_id": socket.gethostname(), "event_type": event_type, "priority": priority, "payload": payload, "timestamp": str(utils.utcnow()), }
def _allocate_allocatable_address(ip_block, interface, requested_address=None): """Slowly migrate off the AllocatableIps Table.""" model = ipam.models.IpAddress db_session = session.get_session() db_session.begin() allocatable_qry = _query_by(ipam.models.AllocatableIp, db_session=db_session, ip_block_id=ip_block.id) if requested_address is not None: filter_kwargs = {'address': requested_address} allocatable_qry = allocatable_qry.filter(**filter_kwargs) allocatable_address = allocatable_qry.first() if not allocatable_address: db_session.commit() return ip = allocatable_address.address address = model(id=utils.generate_uuid(), created_at=utils.utcnow(), updated_at=utils.utcnow(), address=str(ip), ip_block_id=ip_block.id, interface_id=interface.id, used_by_tenant_id=interface.tenant_id, allocated=True) db_session.merge(address) try: db_session.flush() except sqlalchemy.exc.IntegrityError: db_session.rollback() db_session.begin() db_session.delete(allocatable_address) db_session.commit() LOG.debug("Allocatable ip %s in block %s was a dupe. Deleted" % (ip, ip_block.id)) return _allocate_allocatable_address(ip_block, interface, requested_address) db_session.delete(allocatable_address) db_session.commit() return address db_session.delete(allocatable_qry.address)
def setup_uuid_with(self, fake_uuid): self.mock.StubOutWithMock(utils, "generate_uuid") utils.generate_uuid().MultipleTimes().AndReturn(fake_uuid)
def save_nat_relationships(nat_relationships): for relationship in nat_relationships: ip_nat = mappers.IpNat() relationship['id'] = utils.generate_uuid() update(ip_nat, **relationship) save(ip_nat)
def create(cls, **values): values['id'] = utils.generate_uuid() values['created_at'] = utils.utcnow() instance = cls(**values).save() instance._notify_fields("create") return instance
def allocate_ipv4_address(ip_block, interface, requested_address=None): model = ipam.models.IpAddress address = _allocate_allocatable_address(ip_block, interface, requested_address) if address: return address db_session = session.get_session() with db_session.begin(): address_qry = _query_by(model, db_session=db_session, allocated=False, marked_for_deallocation=False, ip_block_id=ip_block.id) if requested_address is not None: address_qry = address_qry.filter(address=requested_address) address = address_qry.with_lockmode('update').first() if address: address.allocated = True address.interface_id = interface.id address.used_by_tenant_id = interface.tenant_id address.updated_at = utils.utcnow() db_session.merge(address) return address else: ips = netaddr.IPNetwork(ip_block.cidr) counter = (ip_block.allocatable_ip_counter or ips[0].value) if counter >= ips[-1].value: ip_block.is_full = True # NOTE(jkoelker) explicit save() to flush the session prior # to raising save(ip_block, db_session=db_session) raise exception.NoMoreAddressesError(_("IpBlock is full")) ip = netaddr.IPAddress(counter) # NOTE(jkoelker) HRM, this may need to be rethought order wise counter = counter + 1 if counter >= ips[-1].value: ip_block.is_full = True ip_block.allocatable_ip_counter = counter db_session.merge(ip_block) # NOTE(jkoelker) SQLAlchemy models, how do you work? ;) address = model(id=utils.generate_uuid(), created_at=utils.utcnow(), updated_at=utils.utcnow(), address=str(ip), ip_block_id=ip_block.id, interface_id=interface.id, used_by_tenant_id=interface.tenant_id, allocated=True) db_session.merge(address) return address
class InterfaceFactory(factory.Factory): FACTORY_FOR = models.Interface vif_id_on_device = factory.LazyAttribute(lambda a: utils.generate_uuid()) device_id = "instance_id" tenant_id = "RAX"