-
Notifications
You must be signed in to change notification settings - Fork 8
fix(linstorvolumemanager): don't allow InUse volumes to be deleted #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 3.2.12-8.3
Are you sure you want to change the base?
Conversation
Check for usage status before deleting a linstor volume and raise an appropriate error if this happens Signed-off-by: Antoine Bartuccio <[email protected]>
2e28b7a to
a86786c
Compare
| def is_volume_in_use(volume_info: Dict) -> bool: | ||
| for node in volume_info["nodes"].values(): | ||
| if node["in-use"]: | ||
| return True | ||
| return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could inline it, else it takes a lot of space for what it does.
def is_volume_in_use(volume_info: Dict) -> bool:
return any(node["in-use"] for node in volume_info["nodes"].values())| def get_volume_info(volume_uuid: str) -> Dict: | ||
| for volume in self.get_resources_info().values(): | ||
| if volume["uuid"] == volume_uuid: | ||
| return volume | ||
| raise LinstorVolumeManagerError( | ||
| f"Could not find info about volume `{volume_uuid}`", | ||
| LinstorVolumeManagerError.ERR_VOLUME_DESTROY | ||
| ) | ||
|
|
||
| def is_volume_in_use(volume_info: Dict) -> bool: | ||
| for node in volume_info["nodes"].values(): | ||
| if node["in-use"]: | ||
| return True | ||
| return False | ||
|
|
||
| self._ensure_volume_exists(volume_uuid) | ||
| self.ensure_volume_is_not_locked(volume_uuid) | ||
| if is_volume_in_use(get_volume_info(volume_uuid)): | ||
| raise LinstorVolumeManagerError( | ||
| f"Could not destroy volume `{volume_uuid}` as it is currently in use", | ||
| LinstorVolumeManagerError.ERR_VOLUME_DESTROY | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, having single-use nested functions introduces unnecessary noise IMHO.
Don’t take this suggestion at face value, it's mostly meant to illustrate the idea.
| def get_volume_info(volume_uuid: str) -> Dict: | |
| for volume in self.get_resources_info().values(): | |
| if volume["uuid"] == volume_uuid: | |
| return volume | |
| raise LinstorVolumeManagerError( | |
| f"Could not find info about volume `{volume_uuid}`", | |
| LinstorVolumeManagerError.ERR_VOLUME_DESTROY | |
| ) | |
| def is_volume_in_use(volume_info: Dict) -> bool: | |
| for node in volume_info["nodes"].values(): | |
| if node["in-use"]: | |
| return True | |
| return False | |
| self._ensure_volume_exists(volume_uuid) | |
| self.ensure_volume_is_not_locked(volume_uuid) | |
| if is_volume_in_use(get_volume_info(volume_uuid)): | |
| raise LinstorVolumeManagerError( | |
| f"Could not destroy volume `{volume_uuid}` as it is currently in use", | |
| LinstorVolumeManagerError.ERR_VOLUME_DESTROY | |
| ) | |
| self._ensure_volume_exists(volume_uuid) | |
| self.ensure_volume_is_not_locked(volume_uuid) | |
| volume_info = next( | |
| (volume for volume in self.get_resources_info().values() | |
| if volume["uuid"] == volume_uuid), None | |
| ) | |
| if not volume_info: | |
| raise LinstorVolumeManagerError( | |
| f"Could not find info about volume `{volume_uuid}`", | |
| LinstorVolumeManagerError.ERR_VOLUME_DESTROY | |
| ) | |
| if any(node["in-use"] for node in volume_info["nodes"].values()): | |
| raise LinstorVolumeManagerError( | |
| f"Could not destroy volume `{volume_uuid}` as it is currently in use", | |
| LinstorVolumeManagerError.ERR_VOLUME_DESTROY | |
| ) |
Check for usage status before deleting a linstor volume and raise an appropriate error if this happens