Replace usage of LegacyEngineFacade

Switch to using oslo_db.sqlalchemy.enginefacade instead, as this is
required for SQLAlchemy 2.x support.

Change-Id: I055fed2ef2f4953cdd7b215ccafaa27767e915ae
This commit is contained in:
Matt Crees 2024-02-29 12:46:01 +00:00
parent 9aa976ae13
commit b91c0f691e
4 changed files with 249 additions and 283 deletions

View File

@ -17,9 +17,7 @@
import sys
from oslo_config import cfg
from oslo_db import exception as common_db_exc
from oslo_db.sqlalchemy import session as db_session
from oslo_log import log as logging
import sqlalchemy as sa
from sqlalchemy.sql.expression import asc
@ -35,9 +33,6 @@ RESOURCE_PROPERTY_MODELS = {
LOG = logging.getLogger(__name__)
get_engine = facade_wrapper.get_engine
get_session = facade_wrapper.get_session
def get_backend():
"""The backend is this module itself."""
@ -49,16 +44,18 @@ def model_query(model, session=None):
:param model: base model to query
"""
session = session or get_session()
if session is None:
with facade_wrapper.session_for_read() as session:
return session.query(model)
return session.query(model)
def setup_db():
try:
engine = db_session.EngineFacade(cfg.CONF.database.connection,
sqlite_fk=True).get_engine()
models.Lease.metadata.create_all(engine)
with facade_wrapper.session_for_write(sqlite_fk=True) as session:
engine = session.get_bind()
models.Lease.metadata.create_all(engine)
facade_wrapper._clear_engine()
except sa.exc.OperationalError as e:
LOG.error("Database registration exception: %s", e)
return False
@ -67,9 +64,10 @@ def setup_db():
def drop_db():
try:
engine = db_session.EngineFacade(cfg.CONF.database.connection,
sqlite_fk=True).get_engine()
models.Lease.metadata.drop_all(engine)
with facade_wrapper.session_for_write(sqlite_fk=True) as session:
engine = session.get_bind()
models.Lease.metadata.drop_all(engine)
facade_wrapper._clear_engine()
except Exception as e:
LOG.error("Database shutdown exception: %s", e)
return False
@ -125,29 +123,32 @@ def _reservation_get(session, reservation_id):
def reservation_get(reservation_id):
return _reservation_get(get_session(), reservation_id)
with facade_wrapper.session_for_read() as session:
return _reservation_get(session, reservation_id)
def reservation_get_all():
query = model_query(models.Reservation, get_session())
return query.all()
with facade_wrapper.session_for_read() as session:
query = model_query(models.Reservation, session)
return query.all()
def reservation_get_all_by_lease_id(lease_id):
reservations = (model_query(models.Reservation,
get_session()).filter_by(lease_id=lease_id))
return reservations.all()
with facade_wrapper.session_for_read() as session:
reservations = (model_query(models.Reservation,
session).filter_by(lease_id=lease_id))
return reservations.all()
def reservation_get_all_by_values(**kwargs):
"""Returns all entries filtered by col=value."""
reservation_query = model_query(models.Reservation, get_session())
for name, value in kwargs.items():
column = getattr(models.Reservation, name, None)
if column:
reservation_query = reservation_query.filter(column == value)
return reservation_query.all()
with facade_wrapper.session_for_read() as session:
reservation_query = model_query(models.Reservation, session)
for name, value in kwargs.items():
column = getattr(models.Reservation, name, None)
if column:
reservation_query = reservation_query.filter(column == value)
return reservation_query.all()
def reservation_create(values):
@ -155,8 +156,7 @@ def reservation_create(values):
reservation = models.Reservation()
reservation.update(values)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
reservation.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -168,9 +168,7 @@ def reservation_create(values):
def reservation_update(reservation_id, values):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
reservation = _reservation_get(session, reservation_id)
reservation.update(values)
reservation.save(session=session)
@ -179,8 +177,7 @@ def reservation_update(reservation_id, values):
def reservation_destroy(reservation_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
reservation = _reservation_get(session, reservation_id)
if not reservation:
@ -198,12 +195,14 @@ def _lease_get(session, lease_id):
def lease_get(lease_id):
return _lease_get(get_session(), lease_id)
with facade_wrapper.session_for_read() as session:
return _lease_get(session, lease_id)
def lease_get_all():
query = model_query(models.Lease, get_session())
return query.all()
with facade_wrapper.session_for_read() as session:
query = model_query(models.Lease, session)
return query.all()
def lease_get_all_by_project(project_id):
@ -215,10 +214,11 @@ def lease_get_all_by_user(user_id):
def lease_list(project_id=None):
query = model_query(models.Lease, get_session())
if project_id is not None:
query = query.filter_by(project_id=project_id)
return query.all()
with facade_wrapper.session_for_read() as session:
query = model_query(models.Lease, session)
if project_id is not None:
query = query.filter_by(project_id=project_id)
return query.all()
def lease_create(values):
@ -228,8 +228,7 @@ def lease_create(values):
events = values.pop("events", [])
lease.update(values)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
lease.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -263,9 +262,7 @@ def lease_create(values):
def lease_update(lease_id, values):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
lease = _lease_get(session, lease_id)
lease.update(values)
lease.save(session=session)
@ -274,8 +271,7 @@ def lease_update(lease_id, values):
def lease_destroy(lease_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
lease = _lease_get(session, lease_id)
if not lease:
@ -297,11 +293,13 @@ def _event_get_all(session):
def event_get(event_id):
return _event_get(get_session(), event_id)
with facade_wrapper.session_for_read() as session:
return _event_get(session, event_id)
def event_get_all():
return _event_get_all(get_session()).all()
with facade_wrapper.session_for_read() as session:
return _event_get_all(session).all()
def _event_get_sorted_by_filters(sort_key, sort_dir, filters):
@ -309,7 +307,8 @@ def _event_get_sorted_by_filters(sort_key, sort_dir, filters):
sort_fn = {'desc': desc, 'asc': asc}
events_query = _event_get_all(get_session())
with facade_wrapper.session_for_read() as session:
events_query = _event_get_all(session)
if 'status' in filters:
events_query = (
@ -361,8 +360,7 @@ def event_create(values):
event = models.Event()
event.update(values)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
event.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -374,9 +372,7 @@ def event_create(values):
def event_update(event_id, values):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
event = _event_get(session, event_id)
event.update(values)
event.save(session=session)
@ -385,8 +381,7 @@ def event_update(event_id, values):
def event_destroy(event_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
event = _event_get(session, event_id)
if not event:
@ -403,13 +398,14 @@ def _host_reservation_get(session, host_reservation_id):
def host_reservation_get(host_reservation_id):
return _host_reservation_get(get_session(),
host_reservation_id)
with facade_wrapper.session_for_read() as session:
return _host_reservation_get(session, host_reservation_id)
def host_reservation_get_all():
query = model_query(models.ComputeHostReservation, get_session())
return query.all()
with facade_wrapper.session_for_read() as session:
query = model_query(models.ComputeHostReservation, session)
return query.all()
def _host_reservation_get_by_reservation_id(session, reservation_id):
@ -418,8 +414,8 @@ def _host_reservation_get_by_reservation_id(session, reservation_id):
def host_reservation_get_by_reservation_id(reservation_id):
return _host_reservation_get_by_reservation_id(get_session(),
reservation_id)
with facade_wrapper.session_for_read() as session:
return _host_reservation_get_by_reservation_id(session, reservation_id)
def host_reservation_create(values):
@ -427,8 +423,7 @@ def host_reservation_create(values):
host_reservation = models.ComputeHostReservation()
host_reservation.update(values)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
host_reservation.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -440,11 +435,8 @@ def host_reservation_create(values):
def host_reservation_update(host_reservation_id, values):
session = get_session()
with session.begin():
host_reservation = _host_reservation_get(session,
host_reservation_id)
with facade_wrapper.session_for_write() as session:
host_reservation = _host_reservation_get(session, host_reservation_id)
host_reservation.update(values)
host_reservation.save(session=session)
@ -452,10 +444,8 @@ def host_reservation_update(host_reservation_id, values):
def host_reservation_destroy(host_reservation_id):
session = get_session()
with session.begin():
host_reservation = _host_reservation_get(session,
host_reservation_id)
with facade_wrapper.session_for_write() as session:
host_reservation = _host_reservation_get(session, host_reservation_id)
if not host_reservation:
# raise not found error
@ -471,8 +461,7 @@ def instance_reservation_create(values):
instance_reservation = models.InstanceReservations()
instance_reservation.update(value)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
instance_reservation.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -486,15 +475,15 @@ def instance_reservation_create(values):
def instance_reservation_get(instance_reservation_id, session=None):
if not session:
session = get_session()
with facade_wrapper.session_for_write() as session:
query = model_query(models.InstanceReservations, session)
return query.filter_by(id=instance_reservation_id).first()
query = model_query(models.InstanceReservations, session)
return query.filter_by(id=instance_reservation_id).first()
def instance_reservation_update(instance_reservation_id, values):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
instance_reservation = instance_reservation_get(
instance_reservation_id, session)
@ -509,8 +498,7 @@ def instance_reservation_update(instance_reservation_id, values):
def instance_reservation_destroy(instance_reservation_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
instance = instance_reservation_get(instance_reservation_id)
if not instance:
@ -526,23 +514,25 @@ def _host_allocation_get(session, host_allocation_id):
def host_allocation_get(host_allocation_id):
return _host_allocation_get(get_session(),
host_allocation_id)
with facade_wrapper.session_for_read() as session:
return _host_allocation_get(session, host_allocation_id)
def host_allocation_get_all():
query = model_query(models.ComputeHostAllocation, get_session())
return query.all()
with facade_wrapper.session_for_read() as session:
query = model_query(models.ComputeHostAllocation, session)
return query.all()
def host_allocation_get_all_by_values(**kwargs):
"""Returns all entries filtered by col=value."""
allocation_query = model_query(models.ComputeHostAllocation, get_session())
for name, value in kwargs.items():
column = getattr(models.ComputeHostAllocation, name, None)
if column:
allocation_query = allocation_query.filter(column == value)
return allocation_query.all()
with facade_wrapper.session_for_read() as session:
allocation_query = model_query(models.ComputeHostAllocation, session)
for name, value in kwargs.items():
column = getattr(models.ComputeHostAllocation, name, None)
if column:
allocation_query = allocation_query.filter(column == value)
return allocation_query.all()
def host_allocation_create(values):
@ -550,8 +540,7 @@ def host_allocation_create(values):
host_allocation = models.ComputeHostAllocation()
host_allocation.update(values)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
host_allocation.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -563,9 +552,7 @@ def host_allocation_create(values):
def host_allocation_update(host_allocation_id, values):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
host_allocation = _host_allocation_get(session,
host_allocation_id)
host_allocation.update(values)
@ -575,8 +562,7 @@ def host_allocation_update(host_allocation_id, values):
def host_allocation_destroy(host_allocation_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
host_allocation = _host_allocation_get(session,
host_allocation_id)
@ -600,17 +586,20 @@ def _host_get_all(session):
def host_get(host_id):
return _host_get(get_session(), host_id)
with facade_wrapper.session_for_read() as session:
return _host_get(session, host_id)
def host_list():
return model_query(models.ComputeHost, get_session()).all()
with facade_wrapper.session_for_read() as session:
return model_query(models.ComputeHost, session).all()
def host_get_all_by_filters(filters):
"""Returns hosts filtered by name of the field."""
hosts_query = _host_get_all(get_session())
with facade_wrapper.session_for_read() as session:
hosts_query = _host_get_all(session)
if 'status' in filters:
hosts_query = hosts_query.filter(
@ -627,7 +616,8 @@ def host_get_all_by_queries(queries):
#sqlalchemy.sql.operators.ColumnOperators
"""
hosts_query = model_query(models.ComputeHost, get_session())
with facade_wrapper.session_for_read() as session:
hosts_query = model_query(models.ComputeHost, session)
oper = {
'<': ['lt', lambda a, b: a >= b],
@ -666,11 +656,12 @@ def host_get_all_by_queries(queries):
hosts_query = hosts_query.filter(filt)
else:
# looking for resource properties matches
extra_filter = (
_host_resource_property_query(get_session())
.filter(models.ResourceProperty.property_name == key)
).all()
with facade_wrapper.session_for_read() as session:
# looking for resource properties matches
extra_filter = (
_host_resource_property_query(session)
.filter(models.ResourceProperty.property_name == key)
).all()
if not extra_filter:
raise db_exc.BlazarDBNotFound(
@ -724,8 +715,7 @@ def host_create(values):
host = models.ComputeHost()
host.update(values)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
host.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -737,9 +727,7 @@ def host_create(values):
def host_update(host_id, values):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
host = _host_get(session, host_id)
host.update(values)
host.save(session=session)
@ -748,8 +736,7 @@ def host_update(host_id, values):
def host_destroy(host_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
host = _host_get(session, host_id)
if not host:
@ -776,8 +763,8 @@ def _host_extra_capability_get(session, host_extra_capability_id):
def host_extra_capability_get(host_extra_capability_id):
return _host_extra_capability_get(get_session(),
host_extra_capability_id)
with facade_wrapper.session_for_read() as session:
return _host_extra_capability_get(session, host_extra_capability_id)
def _host_extra_capability_get_all_per_host(session, host_id):
@ -788,8 +775,8 @@ def _host_extra_capability_get_all_per_host(session, host_id):
def host_extra_capability_get_all_per_host(host_id):
return _host_extra_capability_get_all_per_host(get_session(),
host_id).all()
with facade_wrapper.session_for_read() as session:
return _host_extra_capability_get_all_per_host(session, host_id).all()
def host_extra_capability_create(values):
@ -804,8 +791,7 @@ def host_extra_capability_create(values):
host_extra_capability = models.ComputeHostExtraCapability()
host_extra_capability.update(values)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
host_extra_capability.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -818,9 +804,7 @@ def host_extra_capability_create(values):
def host_extra_capability_update(host_extra_capability_id, values):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
host_extra_capability, _ = (
_host_extra_capability_get(session,
host_extra_capability_id))
@ -831,8 +815,7 @@ def host_extra_capability_update(host_extra_capability_id, values):
def host_extra_capability_destroy(host_extra_capability_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
host_extra_capability = _host_extra_capability_get(
session, host_extra_capability_id)
@ -846,9 +829,7 @@ def host_extra_capability_destroy(host_extra_capability_id):
def host_extra_capability_get_all_per_name(host_id, property_name):
session = get_session()
with session.begin():
with facade_wrapper.session_for_read() as session:
query = _host_extra_capability_get_all_per_host(session, host_id)
return query.filter(
models.ResourceProperty.property_name == property_name).all()
@ -861,8 +842,7 @@ def fip_reservation_create(fip_reservation_values):
fip_reservation = models.FloatingIPReservation()
fip_reservation.update(values)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
fip_reservation.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -879,13 +859,12 @@ def _fip_reservation_get(session, fip_reservation_id):
def fip_reservation_get(fip_reservation_id):
return _fip_reservation_get(get_session(), fip_reservation_id)
with facade_wrapper.session_for_read() as session:
return _fip_reservation_get(session, fip_reservation_id)
def fip_reservation_update(fip_reservation_id, fip_reservation_values):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
fip_reservation = _fip_reservation_get(session, fip_reservation_id)
fip_reservation.update(fip_reservation_values)
fip_reservation.save(session=session)
@ -894,8 +873,7 @@ def fip_reservation_update(fip_reservation_id, fip_reservation_values):
def fip_reservation_destroy(fip_reservation_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
fip_reservation = _fip_reservation_get(session, fip_reservation_id)
if not fip_reservation:
@ -913,8 +891,7 @@ def required_fip_create(required_fip_values):
required_fip = models.RequiredFloatingIP()
required_fip.update(values)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
required_fip.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -931,13 +908,12 @@ def _required_fip_get(session, required_fip_id):
def required_fip_get(required_fip_id):
return _required_fip_get(get_session(), required_fip_id)
with facade_wrapper.session_for_read() as session:
return _required_fip_get(session, required_fip_id)
def required_fip_update(required_fip_id, required_fip_values):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
required_fip = _required_fip_get(session, required_fip_id)
required_fip.update(required_fip_values)
required_fip.save(session=session)
@ -946,8 +922,7 @@ def required_fip_update(required_fip_id, required_fip_values):
def required_fip_destroy(required_fip_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
required_fip = _required_fip_get(session, required_fip_id)
if not required_fip:
@ -959,8 +934,7 @@ def required_fip_destroy(required_fip_id):
def required_fip_destroy_by_fip_reservation_id(fip_reservation_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
required_fips = model_query(
models.RequiredFloatingIP, session).filter_by(
floatingip_reservation_id=fip_reservation_id)
@ -976,7 +950,8 @@ def _fip_allocation_get(session, fip_allocation_id):
def fip_allocation_get(fip_allocation_id):
return _fip_allocation_get(get_session(), fip_allocation_id)
with facade_wrapper.session_for_read() as session:
return _fip_allocation_get(session, fip_allocation_id)
def fip_allocation_create(allocation_values):
@ -984,8 +959,7 @@ def fip_allocation_create(allocation_values):
fip_allocation = models.FloatingIPAllocation()
fip_allocation.update(values)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
fip_allocation.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -998,17 +972,17 @@ def fip_allocation_create(allocation_values):
def fip_allocation_get_all_by_values(**kwargs):
"""Returns all entries filtered by col=value."""
allocation_query = model_query(models.FloatingIPAllocation, get_session())
for name, value in kwargs.items():
column = getattr(models.FloatingIPAllocation, name, None)
if column:
allocation_query = allocation_query.filter(column == value)
return allocation_query.all()
with facade_wrapper.session_for_read() as session:
allocation_query = model_query(models.FloatingIPAllocation, session)
for name, value in kwargs.items():
column = getattr(models.FloatingIPAllocation, name, None)
if column:
allocation_query = allocation_query.filter(column == value)
return allocation_query.all()
def fip_allocation_destroy(allocation_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
fip_allocation = _fip_allocation_get(session, allocation_id)
if not fip_allocation:
@ -1020,9 +994,7 @@ def fip_allocation_destroy(allocation_id):
def fip_allocation_update(allocation_id, allocation_values):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
fip_allocation = _fip_allocation_get(session, allocation_id)
fip_allocation.update(allocation_values)
fip_allocation.save(session=session)
@ -1049,7 +1021,8 @@ def fip_get_all_by_queries(queries):
#sqlalchemy.sql.operators.ColumnOperators
"""
fips_query = model_query(models.FloatingIP, get_session())
with facade_wrapper.session_for_read() as session:
fips_query = model_query(models.FloatingIP, session)
oper = {
'<': ['lt', lambda a, b: a >= b],
@ -1105,11 +1078,13 @@ def reservable_fip_get_all_by_queries(queries):
def floatingip_get(floatingip_id):
return _floatingip_get(get_session(), floatingip_id)
with facade_wrapper.session_for_read() as session:
return _floatingip_get(session, floatingip_id)
def floatingip_list():
return model_query(models.FloatingIP, get_session()).all()
with facade_wrapper.session_for_read() as session:
return model_query(models.FloatingIP, session).all()
def floatingip_create(values):
@ -1117,8 +1092,7 @@ def floatingip_create(values):
floatingip = models.FloatingIP()
floatingip.update(values)
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
try:
floatingip.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
@ -1130,8 +1104,7 @@ def floatingip_create(values):
def floatingip_destroy(floatingip_id):
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
floatingip = _floatingip_get(session, floatingip_id)
if not floatingip:
@ -1153,7 +1126,8 @@ def _resource_property_get(session, resource_type, property_name):
def resource_property_get(resource_type, property_name):
return _resource_property_get(get_session(), resource_type, property_name)
with facade_wrapper.session_for_read() as session:
return _resource_property_get(session, resource_type, property_name)
def resource_properties_list(resource_type):
@ -1161,10 +1135,7 @@ def resource_properties_list(resource_type):
raise db_exc.BlazarDBResourcePropertiesNotEnabled(
resource_type=resource_type)
session = get_session()
with session.begin():
with facade_wrapper.session_for_read() as session:
resource_model = RESOURCE_PROPERTY_MODELS[resource_type]
query = session.query(
models.ResourceProperty.property_name,
@ -1180,21 +1151,21 @@ def _resource_property_create(session, values):
resource_property = models.ResourceProperty()
resource_property.update(values)
with session.begin():
try:
resource_property.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
# raise exception about duplicated columns (e.columns)
raise db_exc.BlazarDBDuplicateEntry(
model=resource_property.__class__.__name__,
columns=e.columns)
try:
resource_property.save(session=session)
except common_db_exc.DBDuplicateEntry as e:
# raise exception about duplicated columns (e.columns)
raise db_exc.BlazarDBDuplicateEntry(
model=resource_property.__class__.__name__,
columns=e.columns)
return resource_property_get(values.get('resource_type'),
values.get('property_name'))
def resource_property_create(values):
return _resource_property_create(get_session(), values)
with facade_wrapper.session_for_write() as session:
return _resource_property_create(session, values)
def resource_property_update(resource_type, property_name, values):
@ -1203,9 +1174,7 @@ def resource_property_update(resource_type, property_name, values):
resource_type=resource_type)
values = values.copy()
session = get_session()
with session.begin():
with facade_wrapper.session_for_write() as session:
resource_property = _resource_property_get(
session, resource_type, property_name)
@ -1235,5 +1204,6 @@ def _resource_property_get_or_create(session, resource_type, property_name):
def resource_property_get_or_create(resource_type, property_name):
return _resource_property_get_or_create(
get_session(), resource_type, property_name)
with facade_wrapper.session_for_write() as session:
return _resource_property_get_or_create(
session, resource_type, property_name)

View File

@ -12,22 +12,21 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import threading
from oslo_config import cfg
from oslo_db.sqlalchemy import session as db_session
from oslo_db.sqlalchemy import enginefacade
CONF = cfg.CONF
_CONTEXT = threading.local()
_engine_facade = None
def get_session():
return _get_facade().get_session()
def session_for_read():
return _get_facade().reader.using(_CONTEXT)
def get_engine():
return _get_facade().get_engine()
def session_for_write(sqlite_fk=False):
return _get_facade(sqlite_fk=sqlite_fk).writer.using(_CONTEXT)
def _clear_engine():
@ -35,13 +34,11 @@ def _clear_engine():
_engine_facade = None
def _get_facade():
def _get_facade(sqlite_fk=False):
global _engine_facade
if not _engine_facade:
# FIXME(priteau): Remove autocommit=True (and ideally use of
# LegacyEngineFacade) asap since it's not compatible with SQLAlchemy
# 2.0.
_engine_facade = db_session.EngineFacade.from_config(CONF,
autocommit=True)
ctx = enginefacade.transaction_context()
ctx.configure(sqlite_fk=sqlite_fk)
_engine_facade = ctx
return _engine_facade

View File

@ -26,8 +26,6 @@ from blazar.manager import exceptions as mgr_exceptions
from blazar.plugins import instances as instance_plugin
from blazar.plugins import oshosts as host_plugin
get_session = facade_wrapper.get_session
def get_backend():
"""The backend is this module itself."""
@ -35,63 +33,65 @@ def get_backend():
def _get_leases_from_resource_id(resource_id, start_date, end_date):
session = get_session()
border0 = start_date <= models.Lease.end_date
border1 = models.Lease.start_date <= end_date
query = (session.query(models.Lease).join(models.Reservation)
.filter(models.Reservation.resource_id == resource_id)
.filter(sa.and_(border0, border1)))
for lease in query:
yield lease
with facade_wrapper.session_for_read() as session:
border0 = start_date <= models.Lease.end_date
border1 = models.Lease.start_date <= end_date
query = (session.query(models.Lease).join(models.Reservation)
.filter(models.Reservation.resource_id == resource_id)
.filter(sa.and_(border0, border1)))
for lease in query:
yield lease
def _get_leases_from_host_id(host_id, start_date, end_date):
session = get_session()
border0 = start_date <= models.Lease.end_date
border1 = models.Lease.start_date <= end_date
query = (session.query(models.Lease).join(models.Reservation)
.join(models.ComputeHostAllocation)
.filter(models.ComputeHostAllocation.compute_host_id == host_id)
.filter(sa.and_(border0, border1)))
for lease in query:
yield lease
with facade_wrapper.session_for_read() as session:
border0 = start_date <= models.Lease.end_date
border1 = models.Lease.start_date <= end_date
query = (session.query(models.Lease).join(models.Reservation)
.join(models.ComputeHostAllocation)
.filter(models.ComputeHostAllocation.compute_host_id ==
host_id)
.filter(sa.and_(border0, border1)))
for lease in query:
yield lease
def _get_leases_from_fip_id(fip_id, start_date, end_date):
session = get_session()
border0 = sa.and_(models.Lease.start_date < start_date,
models.Lease.end_date < start_date)
border1 = sa.and_(models.Lease.start_date > end_date,
models.Lease.end_date > end_date)
query = (session.query(models.Lease).join(models.Reservation)
.join(models.FloatingIPAllocation)
.filter(models.FloatingIPAllocation.floatingip_id == fip_id)
.filter(~sa.or_(border0, border1)))
for lease in query:
yield lease
with facade_wrapper.session_for_read() as session:
border0 = sa.and_(models.Lease.start_date < start_date,
models.Lease.end_date < start_date)
border1 = sa.and_(models.Lease.start_date > end_date,
models.Lease.end_date > end_date)
query = (session.query(models.Lease).join(models.Reservation)
.join(models.FloatingIPAllocation)
.filter(models.FloatingIPAllocation.floatingip_id == fip_id)
.filter(~sa.or_(border0, border1)))
for lease in query:
yield lease
def get_reservations_by_host_id(host_id, start_date, end_date):
session = get_session()
border0 = start_date <= models.Lease.end_date
border1 = models.Lease.start_date <= end_date
query = (session.query(models.Reservation).join(models.Lease)
.join(models.ComputeHostAllocation)
.filter(models.ComputeHostAllocation.compute_host_id == host_id)
.filter(sa.and_(border0, border1)))
return query.all()
with facade_wrapper.session_for_read() as session:
border0 = start_date <= models.Lease.end_date
border1 = models.Lease.start_date <= end_date
query = (session.query(models.Reservation).join(models.Lease)
.join(models.ComputeHostAllocation)
.filter(models.ComputeHostAllocation.compute_host_id ==
host_id)
.filter(sa.and_(border0, border1)))
return query.all()
def get_reservations_by_host_ids(host_ids, start_date, end_date):
session = get_session()
border0 = start_date <= models.Lease.end_date
border1 = models.Lease.start_date <= end_date
query = (session.query(models.Reservation).join(models.Lease)
.join(models.ComputeHostAllocation)
.filter(models.ComputeHostAllocation.compute_host_id
.in_(host_ids))
.filter(sa.and_(border0, border1)))
return query.all()
with facade_wrapper.session_for_read() as session:
border0 = start_date <= models.Lease.end_date
border1 = models.Lease.start_date <= end_date
query = (session.query(models.Reservation).join(models.Lease)
.join(models.ComputeHostAllocation)
.filter(models.ComputeHostAllocation.compute_host_id
.in_(host_ids))
.filter(sa.and_(border0, border1)))
return query.all()
def get_reservations_for_allocations(session, start_date, end_date,
@ -130,52 +130,52 @@ def get_reservations_for_allocations(session, start_date, end_date,
def get_reservation_allocations_by_host_ids(host_ids, start_date, end_date,
lease_id=None,
reservation_id=None):
session = get_session()
# Get all reservations applicable
reservations = get_reservations_for_allocations(
session, start_date, end_date, lease_id, reservation_id)
with facade_wrapper.session_for_read() as session:
# Get all reservations applicable
reservations = get_reservations_for_allocations(
session, start_date, end_date, lease_id, reservation_id)
# Select (reservation_id, host_id) for all reservations
allocations_query = (session.query(
models.ComputeHostAllocation.reservation_id,
models.ComputeHostAllocation.compute_host_id)
.filter(models.ComputeHostAllocation.compute_host_id.in_(host_ids))
.filter(models.ComputeHostAllocation.reservation_id.in_(
list(set([x['id'] for x in reservations])))))
# Select (reservation_id, host_id) for all reservations
allocations_query = (session.query(
models.ComputeHostAllocation.reservation_id,
models.ComputeHostAllocation.compute_host_id)
.filter(models.ComputeHostAllocation.compute_host_id.in_(host_ids))
.filter(models.ComputeHostAllocation.reservation_id.in_(
list(set([x['id'] for x in reservations])))))
# Create a mapping of reservation_id to list of host_ids
allocations = defaultdict(list)
for row in allocations_query.all():
allocations[row[0]].append(row[1])
# Create a mapping of reservation_id to list of host_ids
allocations = defaultdict(list)
for row in allocations_query.all():
allocations[row[0]].append(row[1])
# Copy the host id lists to the corresponding reservation
for r in reservations:
r['host_ids'] = allocations[r['id']]
return reservations
# Copy the host id lists to the corresponding reservation
for r in reservations:
r['host_ids'] = allocations[r['id']]
return reservations
def get_reservation_allocations_by_fip_ids(fip_ids, start_date, end_date,
lease_id=None, reservation_id=None):
session = get_session()
reservations = get_reservations_for_allocations(
session, start_date, end_date, lease_id, reservation_id)
with facade_wrapper.session_for_read() as session:
reservations = get_reservations_for_allocations(
session, start_date, end_date, lease_id, reservation_id)
allocations_query = (session.query(
models.FloatingIPAllocation.reservation_id,
models.FloatingIPAllocation.floatingip_id)
.filter(models.FloatingIPAllocation.floatingip_id.in_(fip_ids))
.filter(models.FloatingIPAllocation.reservation_id.in_(
list(set([x['id'] for x in reservations])))))
allocations_query = (session.query(
models.FloatingIPAllocation.reservation_id,
models.FloatingIPAllocation.floatingip_id)
.filter(models.FloatingIPAllocation.floatingip_id.in_(fip_ids))
.filter(models.FloatingIPAllocation.reservation_id.in_(
list(set([x['id'] for x in reservations])))))
allocations = defaultdict(list)
allocations = defaultdict(list)
for row in allocations_query.all():
allocations[row[0]].append(row[1])
for row in allocations_query.all():
allocations[row[0]].append(row[1])
for r in reservations:
r['floatingip_ids'] = allocations[r['id']]
for r in reservations:
r['floatingip_ids'] = allocations[r['id']]
return reservations
return reservations
def get_plugin_reservation(resource_type, resource_id):

View File

@ -42,7 +42,6 @@ class Database(fixtures.Fixture):
cfg.CONF.set_override('connection', str(database_connection),
group='database')
facade_wrapper._clear_engine()
self.engine = facade_wrapper.get_engine()
db_api.setup_db()
self.addCleanup(db_api.drop_db)