Skip to content

Commit

Permalink
chore: use StatusStr for known status values
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaqq committed Nov 22, 2024
1 parent 99d8fc1 commit 8cf92b3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
62 changes: 45 additions & 17 deletions juju/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,70 @@
from __future__ import annotations

import logging
import sys
import warnings

if sys.version_info >= (3, 11):
from enum import StrEnum
else:
from backports.strenum import StrEnum

from .client import client

log = logging.getLogger(__name__)


def derive_status(statuses: list[str]):
class StatusStr(StrEnum):
"""Recognised status values.
Please keep this set exact same as the severity map below.
"""

error = "error"
blocked = "blocked"
waiting = "waiting"
maintenance = "maintenance"
active = "active"
terminated = "terminated"
unknown = "unknown"


""" severity_map holds status values with a severity measure.
Status values with higher severity are used in preference to others.
"""
severity_map: dict[StatusStr, int] = {
# FIXME: Juju defines a lot more status values #1204
StatusStr.error: 100,
StatusStr.blocked: 90,
StatusStr.waiting: 80,
StatusStr.maintenance: 70,
StatusStr.active: 60,
StatusStr.terminated: 50,
StatusStr.unknown: 40,
}


def derive_status(statuses: list[str | StatusStr]) -> StatusStr:
"""Derive status from a set.
derive_status is used to determine the application status from a set of unit
status values.
:param statuses: list of known unit workload statuses
"""
current = "unknown"
current: StatusStr = StatusStr.unknown
for status in statuses:
if status in severity_map and severity_map[status] > severity_map[current]:
try:
status = StatusStr(status)
except ValueError:
continue
if (new_level := severity_map.get(status)) and new_level > severity_map[
current
]:
current = status
return current


""" severity_map holds status values with a severity measure.
Status values with higher severity are used in preference to others.
"""
severity_map = {
"error": 100,
"blocked": 90,
"waiting": 80,
"maintenance": 70,
"active": 60,
"terminated": 50,
"unknown": 40,
}


async def formatted_status(model, target=None, raw=False, filters=None):
"""Returns a string that mimics the content of the information
returned in the juju status command. If the raw parameter is
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "Python library for Juju"
readme = "docs/readme.rst"
license = { file = "LICENSE" }
maintainers = [{name = "Juju Ecosystem Engineering", email = "[email protected]"}]
requires-python = ">=3.8"
requires-python = ">=3.8.6"
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
Expand Down Expand Up @@ -33,7 +33,8 @@ dependencies = [
"kubernetes>=12.0.1,<31.0.0",
"hvac",
"packaging",
"typing-extensions>=4.5.0"
"typing-extensions>=4.5.0",
"backports.strenum>=1.3.1",
]

[project.urls]
Expand Down Expand Up @@ -212,7 +213,7 @@ ignore = [
[tool.pyright]
# These are tentative
include = ["**/*.py"]
pythonVersion = "3.8"
pythonVersion = "3.8.6"
typeCheckingMode = "strict"
useLibraryCodeForTypes = true
reportGeneralTypeIssues = true
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"hvac",
"packaging",
"typing-extensions>=4.5.0",
"backports.strenum",
],
include_package_data=True,
maintainer="Juju Ecosystem Engineering",
Expand Down

0 comments on commit 8cf92b3

Please sign in to comment.