Skip to content

Commit

Permalink
Change uptime property to uptime days
Browse files Browse the repository at this point in the history
Change hypervisor uptime property to return uptime in days instead of returning the entire uptime string
  • Loading branch information
gmatthews20 committed Dec 11, 2024
1 parent 7994dc1 commit 6368280
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/user_docs/query_docs/HYPERVISORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions openstackquery/enums/props/hypervisor_properties.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions openstackquery/time_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check warning on line 54 in openstackquery/time_utils.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/time_utils.py#L53-L54

Added lines #L53 - L54 were not covered by tests

return f"{days_uptime}:{time_uptime}"

Check warning on line 56 in openstackquery/time_utils.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/time_utils.py#L56

Added line #L56 was not covered by tests
13 changes: 7 additions & 6 deletions tests/enums/props/test_hypervisor_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 6368280

Please sign in to comment.