db: Migrate "share metadata" APIs to enginefacade
Signed-off-by: Stephen Finucane <stephenfin@redhat.com> Change-Id: Ied3fb602aa690ee16c7e4a9744aa053e89dd9ae8
This commit is contained in:
parent
60c64d4d95
commit
e53a461c51
@ -3938,19 +3938,34 @@ def share_snapshot_instance_export_locations_update(
|
|||||||
#################################
|
#################################
|
||||||
|
|
||||||
|
|
||||||
|
def _share_metadata_get_query(context, share_id):
|
||||||
|
return model_query(
|
||||||
|
context, models.ShareMetadata, read_deleted="no",
|
||||||
|
).filter_by(share_id=share_id).options(joinedload('share'))
|
||||||
|
|
||||||
|
|
||||||
@require_context
|
@require_context
|
||||||
@require_share_exists
|
@require_share_exists
|
||||||
|
@context_manager.reader
|
||||||
def share_metadata_get(context, share_id):
|
def share_metadata_get(context, share_id):
|
||||||
return _share_metadata_get(context, share_id)
|
return _share_metadata_get(context, share_id)
|
||||||
|
|
||||||
|
|
||||||
|
def _share_metadata_get(context, share_id):
|
||||||
|
rows = _share_metadata_get_query(context, share_id).all()
|
||||||
|
result = {}
|
||||||
|
for row in rows:
|
||||||
|
result[row['key']] = row['value']
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
@require_context
|
@require_context
|
||||||
@require_share_exists
|
@require_share_exists
|
||||||
def share_metadata_get_item(context, share_id, key, session=None):
|
@context_manager.reader
|
||||||
session = session or get_session()
|
def share_metadata_get_item(context, share_id, key):
|
||||||
try:
|
try:
|
||||||
row = _share_metadata_get_item(context, share_id, key,
|
row = _share_metadata_get_item(context, share_id, key)
|
||||||
session=session)
|
|
||||||
except exception.MetadataItemNotFound:
|
except exception.MetadataItemNotFound:
|
||||||
raise exception.MetadataItemNotFound()
|
raise exception.MetadataItemNotFound()
|
||||||
|
|
||||||
@ -3962,85 +3977,67 @@ def share_metadata_get_item(context, share_id, key, session=None):
|
|||||||
|
|
||||||
@require_context
|
@require_context
|
||||||
@require_share_exists
|
@require_share_exists
|
||||||
|
@context_manager.writer
|
||||||
def share_metadata_delete(context, share_id, key):
|
def share_metadata_delete(context, share_id, key):
|
||||||
(_share_metadata_get_query(context, share_id).
|
_share_metadata_get_query(
|
||||||
filter_by(key=key).soft_delete())
|
context, share_id,
|
||||||
|
).filter_by(key=key).soft_delete()
|
||||||
|
|
||||||
|
|
||||||
@require_context
|
@require_context
|
||||||
@require_share_exists
|
@require_share_exists
|
||||||
|
@context_manager.writer
|
||||||
def share_metadata_update(context, share_id, metadata, delete):
|
def share_metadata_update(context, share_id, metadata, delete):
|
||||||
return _share_metadata_update(context, share_id, metadata, delete)
|
return _share_metadata_update(context, share_id, metadata, delete)
|
||||||
|
|
||||||
|
|
||||||
@require_context
|
@require_context
|
||||||
@require_share_exists
|
@require_share_exists
|
||||||
def share_metadata_update_item(context, share_id, item, session=None):
|
@context_manager.writer
|
||||||
|
def share_metadata_update_item(context, share_id, item):
|
||||||
return _share_metadata_update(context, share_id, item, delete=False)
|
return _share_metadata_update(context, share_id, item, delete=False)
|
||||||
|
|
||||||
|
|
||||||
def _share_metadata_get_query(context, share_id, session=None):
|
|
||||||
return (model_query(context, models.ShareMetadata, session=session,
|
|
||||||
read_deleted="no").
|
|
||||||
filter_by(share_id=share_id).
|
|
||||||
options(joinedload('share')))
|
|
||||||
|
|
||||||
|
|
||||||
def _share_metadata_get(context, share_id, session=None):
|
|
||||||
rows = _share_metadata_get_query(context, share_id,
|
|
||||||
session=session).all()
|
|
||||||
result = {}
|
|
||||||
for row in rows:
|
|
||||||
result[row['key']] = row['value']
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
|
@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
|
||||||
def _share_metadata_update(context, share_id, metadata, delete, session=None):
|
def _share_metadata_update(context, share_id, metadata, delete):
|
||||||
if not session:
|
# Set existing metadata to deleted if delete argument is True
|
||||||
session = get_session()
|
delete = strutils.bool_from_string(delete)
|
||||||
|
if delete:
|
||||||
|
original_metadata = _share_metadata_get(context, share_id)
|
||||||
|
for meta_key, meta_value in original_metadata.items():
|
||||||
|
if meta_key not in metadata:
|
||||||
|
meta_ref = _share_metadata_get_item(
|
||||||
|
context, share_id, meta_key,
|
||||||
|
)
|
||||||
|
meta_ref.soft_delete(session=context.session)
|
||||||
|
|
||||||
with session.begin():
|
meta_ref = None
|
||||||
# Set existing metadata to deleted if delete argument is True
|
|
||||||
delete = strutils.bool_from_string(delete)
|
|
||||||
if delete:
|
|
||||||
original_metadata = _share_metadata_get(context, share_id,
|
|
||||||
session=session)
|
|
||||||
for meta_key, meta_value in original_metadata.items():
|
|
||||||
if meta_key not in metadata:
|
|
||||||
meta_ref = _share_metadata_get_item(context, share_id,
|
|
||||||
meta_key,
|
|
||||||
session=session)
|
|
||||||
meta_ref.soft_delete(session=session)
|
|
||||||
|
|
||||||
meta_ref = None
|
# Now update all existing items with new values, or create new meta
|
||||||
|
# objects
|
||||||
|
for meta_key, meta_value in metadata.items():
|
||||||
|
|
||||||
# Now update all existing items with new values, or create new meta
|
# update the value whether it exists or not
|
||||||
# objects
|
item = {"value": meta_value}
|
||||||
for meta_key, meta_value in metadata.items():
|
|
||||||
|
|
||||||
# update the value whether it exists or not
|
try:
|
||||||
item = {"value": meta_value}
|
meta_ref = _share_metadata_get_item(
|
||||||
|
context, share_id, meta_key,
|
||||||
|
)
|
||||||
|
except exception.MetadataItemNotFound:
|
||||||
|
meta_ref = models.ShareMetadata()
|
||||||
|
item.update({"key": meta_key, "share_id": share_id})
|
||||||
|
|
||||||
try:
|
meta_ref.update(item)
|
||||||
meta_ref = _share_metadata_get_item(context, share_id,
|
meta_ref.save(session=context.session)
|
||||||
meta_key,
|
|
||||||
session=session)
|
|
||||||
except exception.MetadataItemNotFound:
|
|
||||||
meta_ref = models.ShareMetadata()
|
|
||||||
item.update({"key": meta_key, "share_id": share_id})
|
|
||||||
|
|
||||||
meta_ref.update(item)
|
return metadata
|
||||||
meta_ref.save(session=session)
|
|
||||||
|
|
||||||
return metadata
|
|
||||||
|
|
||||||
|
|
||||||
def _share_metadata_get_item(context, share_id, key, session=None):
|
def _share_metadata_get_item(context, share_id, key):
|
||||||
result = (_share_metadata_get_query(context, share_id, session=session).
|
result = _share_metadata_get_query(
|
||||||
filter_by(key=key).
|
context, share_id,
|
||||||
first())
|
).filter_by(key=key).first()
|
||||||
|
|
||||||
if not result:
|
if not result:
|
||||||
raise exception.MetadataItemNotFound()
|
raise exception.MetadataItemNotFound()
|
||||||
|
Loading…
Reference in New Issue
Block a user