diff --git a/docs/user_docs/query_docs/HYPERVISORS.md b/docs/user_docs/query_docs/HYPERVISORS.md index 411e396..3764001 100644 --- a/docs/user_docs/query_docs/HYPERVISORS.md +++ b/docs/user_docs/query_docs/HYPERVISORS.md @@ -43,7 +43,7 @@ from enums.query.props.hypervisor_properties import HypervisorProperties | HYPERVISOR_VCPUS | `int` | "vcpus" | The number of vCPUs on this hypervisor. | | HYPERVISOR_VCPUS_USED | `int` | "vcpus_used" | The number of vCPUs currently being used on this hypervisor. | | HYPERVISOR_DISABLED_REASON | `string` | "disabled_reason" | Comment of why the hypervisor is disabled, None if not disabled | -| HYPERVISOR_UPTIME | `string` | "uptime" | The total uptime of the hypervisor and info about average load | +| HYPERVISOR_UPTIME_DAYS | `string` | "uptime" | The number of days the hypervisor has been up | Any of these properties can be used for any of the API methods that takes a property - like `select`, `where`, `sort_by` etc diff --git a/openstackquery/enums/props/hypervisor_properties.py b/openstackquery/enums/props/hypervisor_properties.py index 0435def..cebfc47 100644 --- a/openstackquery/enums/props/hypervisor_properties.py +++ b/openstackquery/enums/props/hypervisor_properties.py @@ -1,10 +1,12 @@ from enum import auto +import re from typing import Dict, Optional from openstackquery.enums.props.prop_enum import PropEnum, PropFunc from openstackquery.exceptions.query_property_mapping_error import ( QueryPropertyMappingError, ) +from openstackquery.time_utils import TimeUtils class HypervisorProperties(PropEnum): @@ -28,7 +30,7 @@ class HypervisorProperties(PropEnum): HYPERVISOR_VCPUS = auto() HYPERVISOR_VCPUS_USED = auto() HYPERVISOR_DISABLED_REASON = auto() - HYPERVISOR_UPTIME = auto() + HYPERVISOR_UPTIME_DAYS = auto() @staticmethod def _get_aliases() -> Dict: @@ -64,7 +66,7 @@ def _get_aliases() -> Dict: HypervisorProperties.HYPERVISOR_VCPUS: ["vcpus"], HypervisorProperties.HYPERVISOR_VCPUS_USED: ["vcpus_used"], HypervisorProperties.HYPERVISOR_DISABLED_REASON: ["disabled_reason"], - HypervisorProperties.HYPERVISOR_UPTIME: ["uptime"], + HypervisorProperties.HYPERVISOR_UPTIME_DAYS: ["uptime"], } @staticmethod @@ -112,7 +114,9 @@ def get_prop_mapping(prop) -> Optional[PropFunc]: HypervisorProperties.HYPERVISOR_DISABLED_REASON: lambda a: a["service"][ "disabled_reason" ], - HypervisorProperties.HYPERVISOR_UPTIME: lambda a: a["uptime"], + HypervisorProperties.HYPERVISOR_UPTIME_DAYS: lambda a: int( + a["uptime"].strip().split(" ")[2] + ), } try: return mapping[prop] diff --git a/openstackquery/time_utils.py b/openstackquery/time_utils.py index 0e65813..df4904f 100644 --- a/openstackquery/time_utils.py +++ b/openstackquery/time_utils.py @@ -47,3 +47,10 @@ def convert_to_timestamp( return datetime.fromtimestamp(current_time - time_in_seconds).strftime( "%Y-%m-%dT%H:%M:%SZ" ) + + @staticmethod + def get_uptime(uptime_string: str): + time_uptime = uptime_string.split(",")[1] + days_uptime = uptime_string.split(" ")[3].strip() + + return f"{days_uptime}:{time_uptime}" diff --git a/tests/enums/props/test_hypervisor_properties.py b/tests/enums/props/test_hypervisor_properties.py index 21597bc..bb9afc7 100644 --- a/tests/enums/props/test_hypervisor_properties.py +++ b/tests/enums/props/test_hypervisor_properties.py @@ -285,15 +285,16 @@ def test_hypervisor_disabled_reason_serialization(val): @pytest.mark.parametrize( "val", [ - "hypervisor_uptime", - "Hypervisor_Uptime", - "HyPeRvIsOr_UpTiMe", + "HYPERVISOR_UPTIME_DAYS", + "HYPERVISOR_UPTIME_DAYS", + "HYPERVISOR_UPTIME_DAYS", ], ) -def test_hypervisor_uptime_serialization(val): +def test_HYPERVISOR_UPTIME_DAYS_serialization(val): """ - Tests that variants of HYPERVISOR_UPTIME can be serialized + Tests that variants of HYPERVISOR_UPTIME_DAYS can be serialized """ assert ( - HypervisorProperties.from_string(val) is HypervisorProperties.HYPERVISOR_UPTIME + HypervisorProperties.from_string(val) + is HypervisorProperties.HYPERVISOR_UPTIME_DAYS )