Skip to content

Commit

Permalink
Update hypervisor properties
Browse files Browse the repository at this point in the history
Catch missing key error instead of defaulting to None
Remove deprecated current workload property and unit tests
  • Loading branch information
gmatthews20 committed Dec 10, 2024
1 parent ffeb1e2 commit 55b32af
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 51 deletions.
56 changes: 28 additions & 28 deletions openstackquery/enums/props/hypervisor_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HypervisorProperties(PropEnum):
An enum class for all hypervisor properties
"""

HYPERVISOR_CURRENT_WORKLOAD = auto()
# HYPERVISOR_CURRENT_WORKLOAD = auto()
HYPERVISOR_DISK_FREE = auto()
HYPERVISOR_DISK_SIZE = auto()
HYPERVISOR_DISK_USED = auto()
Expand All @@ -35,10 +35,10 @@ def _get_aliases() -> Dict:
A method that returns all valid string alias mappings
"""
return {
HypervisorProperties.HYPERVISOR_CURRENT_WORKLOAD: [
"current_workload",
"workload",
],
# HypervisorProperties.HYPERVISOR_CURRENT_WORKLOAD: [
# "current_workload",
# "workload",
# ],
HypervisorProperties.HYPERVISOR_DISK_FREE: [
"local_disk_free",
"free_disk_gb",
Expand Down Expand Up @@ -74,39 +74,39 @@ def get_prop_mapping(prop) -> Optional[PropFunc]:
:param prop: A HypervisorProperty Enum for which a function may exist for
"""
mapping = {
HypervisorProperties.HYPERVISOR_CURRENT_WORKLOAD: lambda a: a[
"current_workload"
# HypervisorProperties.HYPERVISOR_CURRENT_WORKLOAD: lambda a: a[
# "current_workload"
# ],
HypervisorProperties.HYPERVISOR_DISK_FREE: lambda a: a.resources["DISK_GB"][
"free"
],
HypervisorProperties.HYPERVISOR_DISK_SIZE: lambda a: a.resources["DISK_GB"][
"total"
],
HypervisorProperties.HYPERVISOR_DISK_USED: lambda a: a.resources["DISK_GB"][
"usage"
],
HypervisorProperties.HYPERVISOR_DISK_FREE: lambda a: a.resources.get(
"DISK_GB"
).get("free", None),
HypervisorProperties.HYPERVISOR_DISK_SIZE: lambda a: a.resources.get(
"DISK_GB"
).get("total", None),
HypervisorProperties.HYPERVISOR_DISK_USED: lambda a: a.resources.get(
"DISK_GB"
).get("usage", None),
HypervisorProperties.HYPERVISOR_ID: lambda a: a["id"],
HypervisorProperties.HYPERVISOR_IP: lambda a: a["host_ip"],
HypervisorProperties.HYPERVISOR_MEMORY_FREE: lambda a: a.resources.get(
HypervisorProperties.HYPERVISOR_MEMORY_FREE: lambda a: a.resources[
"MEMORY_MB"
).get("free", None),
HypervisorProperties.HYPERVISOR_MEMORY_SIZE: lambda a: a.resources.get(
]["free"],
HypervisorProperties.HYPERVISOR_MEMORY_SIZE: lambda a: a.resources[
"MEMORY_MB"
).get("total", None),
HypervisorProperties.HYPERVISOR_MEMORY_USED: lambda a: a.resources.get(
]["total"],
HypervisorProperties.HYPERVISOR_MEMORY_USED: lambda a: a.resources[
"MEMORY_MB"
).get("usage", None),
]["usage"],
HypervisorProperties.HYPERVISOR_NAME: lambda a: a["name"],
# HypervisorProperties.HYPERVISOR_SERVER_COUNT: lambda a: a["runnning_vms"],
HypervisorProperties.HYPERVISOR_STATE: lambda a: a["state"],
HypervisorProperties.HYPERVISOR_STATUS: lambda a: a["status"],
HypervisorProperties.HYPERVISOR_VCPUS: lambda a: a.resources.get(
"VCPU"
).get("total", None),
HypervisorProperties.HYPERVISOR_VCPUS_USED: lambda a: a.resources.get(
"VCPU"
).get("usage", None),
HypervisorProperties.HYPERVISOR_VCPUS: lambda a: a.resources["VCPU"][
"total"
],
HypervisorProperties.HYPERVISOR_VCPUS_USED: lambda a: a.resources["VCPU"][
"usage"
],
HypervisorProperties.HYPERVISOR_DISABLED_REASON: lambda a: a["service"][
"disabled_reason"
],
Expand Down
2 changes: 1 addition & 1 deletion openstackquery/mappings/hypervisor_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def get_client_side_handlers() -> QueryClientSideHandlers:
HypervisorProperties.HYPERVISOR_VCPUS,
HypervisorProperties.HYPERVISOR_VCPUS_USED,
# HypervisorProperties.HYPERVISOR_SERVER_COUNT, # Deprecated, use server query
HypervisorProperties.HYPERVISOR_CURRENT_WORKLOAD,
# HypervisorProperties.HYPERVISOR_CURRENT_WORKLOAD,
]

return QueryClientSideHandlers(
Expand Down
2 changes: 1 addition & 1 deletion openstackquery/query_blocks/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_prop(self, prop: PropEnum) -> PropValue:
"""
try:
return self._prop_enum_cls.get_prop_mapping(prop)(self._obj_result)
except AttributeError:
except (AttributeError, KeyError):
return self._default_prop_value

def update_forwarded_properties(self, forwarded_props: Dict[str, PropValue]):
Expand Down
20 changes: 0 additions & 20 deletions tests/enums/props/test_hypervisor_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,6 @@ def test_get_marker_prop_func(mock_get_prop_mapping):
assert val == mock_get_prop_mapping.return_value


@pytest.mark.parametrize(
"val",
[
"hypervisor_current_workload",
"Hypervisor_Current_Workload",
"HyPeRvIsOr_CuRrEnT_wOrKlOaD",
"current_workload",
"workload",
],
)
def test_hypervisor_current_workload_serialization(val):
"""
Tests that variants of HYPERVISOR_CURRENT_WORKLOAD can be serialized
"""
assert (
HypervisorProperties.from_string(val)
is HypervisorProperties.HYPERVISOR_CURRENT_WORKLOAD
)


@pytest.mark.parametrize(
"val",
[
Expand Down
1 change: 0 additions & 1 deletion tests/mappings/test_hypervisor_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def test_client_side_handlers_integer(client_side_test_mappings):
HypervisorProperties.HYPERVISOR_MEMORY_FREE,
HypervisorProperties.HYPERVISOR_VCPUS,
HypervisorProperties.HYPERVISOR_VCPUS_USED,
HypervisorProperties.HYPERVISOR_CURRENT_WORKLOAD,
]
handler = HypervisorMapping.get_client_side_handlers().integer_handler
mappings = {
Expand Down

0 comments on commit 55b32af

Please sign in to comment.