Merge "Allow block devices without device_name"
This commit is contained in:
@@ -3451,32 +3451,35 @@ def block_device_mapping_update(context, bdm_id, values, legacy=True):
|
|||||||
|
|
||||||
def block_device_mapping_update_or_create(context, values, legacy=True):
|
def block_device_mapping_update_or_create(context, values, legacy=True):
|
||||||
_scrub_empty_str_values(values, ['volume_size'])
|
_scrub_empty_str_values(values, ['volume_size'])
|
||||||
|
values = _from_legacy_values(values, legacy, allow_updates=True)
|
||||||
|
|
||||||
session = get_session()
|
session = get_session()
|
||||||
with session.begin():
|
with session.begin():
|
||||||
result = _block_device_mapping_get_query(context, session=session).\
|
result = None
|
||||||
filter_by(instance_uuid=values['instance_uuid']).\
|
# NOTE(xqueralt): Only update a BDM when device_name was provided. We
|
||||||
filter_by(device_name=values['device_name']).\
|
# allow empty device names so they will be set later by the manager.
|
||||||
first()
|
if values['device_name']:
|
||||||
if not result:
|
query = _block_device_mapping_get_query(context, session=session)
|
||||||
values = _from_legacy_values(values, legacy)
|
result = query.filter_by(instance_uuid=values['instance_uuid'],
|
||||||
bdm_ref = models.BlockDeviceMapping()
|
device_name=values['device_name']).first()
|
||||||
bdm_ref.update(values)
|
|
||||||
bdm_ref.save(session=session)
|
|
||||||
result = bdm_ref
|
|
||||||
else:
|
|
||||||
values = _from_legacy_values(values, legacy, allow_updates=True)
|
|
||||||
result.update(values)
|
|
||||||
|
|
||||||
# NOTE(xqueralt): prevent from having multiple swap devices for the
|
if result:
|
||||||
# same instance. So delete the existing ones.
|
result.update(values)
|
||||||
|
else:
|
||||||
|
# Either the device_name doesn't exist in the database yet, or no
|
||||||
|
# device_name was provided. Both cases mean creating a new BDM.
|
||||||
|
result = models.BlockDeviceMapping(**values)
|
||||||
|
result.save(session=session)
|
||||||
|
|
||||||
|
# NOTE(xqueralt): Prevent from having multiple swap devices for the
|
||||||
|
# same instance. This will delete all the existing ones.
|
||||||
if block_device.new_format_is_swap(values):
|
if block_device.new_format_is_swap(values):
|
||||||
query = (_block_device_mapping_get_query(context, session=session).
|
query = _block_device_mapping_get_query(context, session=session)
|
||||||
filter_by(instance_uuid=values['instance_uuid']).
|
query = query.filter_by(instance_uuid=values['instance_uuid'],
|
||||||
filter_by(source_type='blank').
|
source_type='blank', guest_format='swap')
|
||||||
filter(models.BlockDeviceMapping.device_name !=
|
query = query.filter(models.BlockDeviceMapping.id != result.id)
|
||||||
values['device_name']).
|
|
||||||
filter_by(guest_format='swap'))
|
|
||||||
query.soft_delete()
|
query.soft_delete()
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user