Fix edge case for middleware hostname override in asset.py #1733
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request addresses a subtle yet important issue in the asset management API, specifically in the
operate_assets
method ofcare/facility/api/viewsets/asset.py
and thecheck_asset_status
function incare/facility/tasks/asset_monitor.py
. The modification ensures a more reliable fallback mechanism for retrieving the middleware hostname associated with assets.Previously, the code utilized the
asset.meta.get("middleware_hostname", asset.current_location.middleware_address)
pattern. This approach, however, did not account for scenarios whereasset.meta['middleware_hostname']
could be an empty string. In such cases, the intended fallback toasset.current_location.middleware_address
was bypassed, inadvertently defaulting toasset.current_location.facility.middleware_address
due to the outeror
condition.The current update addresses this issue by repositioning the fallback logic. Instead of relying on the
get
method's default value, the code now explicitly checks for the truthiness of themiddleware_hostname
retrieved fromasset.meta
. This change ensures that ifmiddleware_hostname
is an empty string, the code will correctly fall back toasset.current_location.middleware_address
, and subsequently toasset.current_location.facility.middleware_address
if needed.@coronasafe/care-backend-maintainers @coronasafe/care-backend-admins