-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update HypervisorSate enum so it's easier to understand and use Refactor check valid sate into separate function Return HypervisorState instead of string from get_hypervisor_state Update tests Update to work with new version of query library
- Loading branch information
1 parent
fa551b2
commit 5363a77
Showing
5 changed files
with
78 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,42 @@ | ||
import re | ||
from typing import Dict | ||
from enums.hypervisor_states import HypervisorState | ||
|
||
|
||
def get_hypervisor_state(hypervisor: Dict, uptime_limit: int) -> str: | ||
def get_hypervisor_state(hypervisor: Dict, uptime_limit: int) -> HypervisorState: | ||
""" | ||
Returns a hypervisor state given a set of hypervisor variables | ||
:param hypervisor: Dictionary containing hypervisor: uptime, state, status and server count | ||
:param uptime_limit: Number of days of uptime before hypervisor requires maintenance | ||
:return: Hypervisor state | ||
""" | ||
uptime_pattern = re.compile(r"up\s+(\d+)\s+days,?\s+(\d+):(\d+)") | ||
hypervisor_uptime = hypervisor.get("hypervisor_uptime") | ||
if not valid_state(hypervisor): | ||
return HypervisorState.UNKNOWN | ||
|
||
if not ( | ||
isinstance(hypervisor_uptime, str) | ||
and (match := uptime_pattern.search(hypervisor_uptime)) | ||
): | ||
return "UNKNOWN" | ||
|
||
hypervisor_status = hypervisor.get("hypervisor_status") | ||
if hypervisor_status not in ["enabled", "disabled"]: | ||
return "UNKNOWN" | ||
|
||
hypervisor_state = hypervisor.get("hypervisor_state") | ||
if hypervisor_state not in ["up", "down"]: | ||
return "UNKNOWN" | ||
|
||
hypervisor_server_count = hypervisor.get("hypervisor_server_count") | ||
if not isinstance(hypervisor_server_count, int) or hypervisor_server_count < 0: | ||
return "UNKNOWN" | ||
|
||
days = int(match.group(1)) | ||
hv_state = { | ||
"uptime": days < uptime_limit, | ||
"enabled": hypervisor_status == "enabled", | ||
"state": hypervisor_state == "up", | ||
"servers": hypervisor_server_count > 0, | ||
"uptime": hypervisor["hypervisor_uptime_days"] < uptime_limit, | ||
"enabled": hypervisor["hypervisor_status"] == "enabled", | ||
"state": hypervisor["hypervisor_state"] == "up", | ||
"servers": hypervisor["hypervisor_server_count"] > 0, | ||
} | ||
|
||
for state in HypervisorState: | ||
if state.properties() == hv_state: | ||
return state.name | ||
return HypervisorState(hv_state) | ||
|
||
return "UNKNOWN" | ||
|
||
def valid_state(state) -> bool: | ||
""" | ||
Validates the hypervisor state | ||
:param state: Dictionary containing hypervisor state | ||
:return: True for valid state | ||
""" | ||
if not isinstance(state["hypervisor_uptime_days"], int): | ||
return False | ||
hypervisor_status = state["hypervisor_status"] | ||
if hypervisor_status not in ["enabled", "disabled"]: | ||
return False | ||
hypervisor_state = state["hypervisor_state"] | ||
if hypervisor_state not in ["up", "down"]: | ||
return False | ||
hypervisor_server_count = state["hypervisor_server_count"] | ||
if not isinstance(hypervisor_server_count, int) or hypervisor_server_count < 0: | ||
return False | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters