|
| 1 | +from collections.abc import Iterable |
| 2 | + |
| 3 | +from ._types import Status |
| 4 | + |
| 5 | +# TODO: think further about how these should handle subordinate applications; |
| 6 | +# they should probably automatically include subordinate status, but per |
| 7 | +# Dima that might come from a different place in the status object, so not |
| 8 | +# be handled automatically with the code below. See also: |
| 9 | +# https://github.com/juju/python-libjuju/blob/138d8f9058e1023e01a01d587b81949433da61ce/tests/unit/data/subordinate-fullstatus.json |
| 10 | + |
| 11 | + |
| 12 | +def all_active(status: Status, apps: Iterable[str] | None = None) -> bool: |
| 13 | + """Report whether all applications or units in *status* are in "active" status. |
| 14 | +
|
| 15 | + Args: |
| 16 | + status: The :class:`Status` object being tested. |
| 17 | + apps: An optional list of application names. If provided, only these applications |
| 18 | + (and their units) are tested. |
| 19 | + """ |
| 20 | + return _all_statuses_are('active', status, apps) |
| 21 | + |
| 22 | + |
| 23 | +def all_error(status: Status, apps: Iterable[str] | None = None) -> bool: |
| 24 | + """Report whether all applications or units in *status* are in "error" status. |
| 25 | +
|
| 26 | + TODO: Args |
| 27 | + """ |
| 28 | + return _all_statuses_are('error', status, apps) |
| 29 | + |
| 30 | + |
| 31 | +def all_blocked(status: Status, apps: Iterable[str] | None = None) -> bool: |
| 32 | + """Report whether all applications or units in *status* are in "blocked" status. |
| 33 | +
|
| 34 | + TODO: Args |
| 35 | + """ |
| 36 | + return _all_statuses_are('blocked', status, apps) |
| 37 | + |
| 38 | + |
| 39 | +def all_maintenance(status: Status, apps: Iterable[str] | None = None) -> bool: |
| 40 | + """Report whether all applications or units in *status* are in "maintenance" status. |
| 41 | +
|
| 42 | + TODO: Args |
| 43 | + """ |
| 44 | + return _all_statuses_are('maintenance', status, apps) |
| 45 | + |
| 46 | + |
| 47 | +def all_waiting(status: Status, apps: Iterable[str] | None = None) -> bool: |
| 48 | + """Report whether all applications or units in *status* are in "waiting" status. |
| 49 | +
|
| 50 | + TODO: Args |
| 51 | + """ |
| 52 | + return _all_statuses_are('waiting', status, apps) |
| 53 | + |
| 54 | + |
| 55 | +def any_active(status: Status, apps: Iterable[str] | None = None) -> bool: |
| 56 | + """Report whether any application or unit in *status* is in "active" status. |
| 57 | +
|
| 58 | + Args: |
| 59 | + status: The :class:`Status` object being tested. |
| 60 | + apps: An optional list of application names. If provided, only these applications |
| 61 | + (and their units) are tested. |
| 62 | + """ |
| 63 | + return _any_status_is('active', status, apps) |
| 64 | + |
| 65 | + |
| 66 | +def any_error(status: Status, apps: Iterable[str] | None = None) -> bool: |
| 67 | + """Report whether any application or unit in *status* is in "error" status. |
| 68 | +
|
| 69 | + TODO: Args |
| 70 | + """ |
| 71 | + return _any_status_is('error', status, apps) |
| 72 | + |
| 73 | + |
| 74 | +def any_blocked(status: Status, apps: Iterable[str] | None = None) -> bool: |
| 75 | + """Report whether any application or unit in *status* is in "blocked" status. |
| 76 | +
|
| 77 | + TODO: Args |
| 78 | + """ |
| 79 | + return _any_status_is('blocked', status, apps) |
| 80 | + |
| 81 | + |
| 82 | +def any_maintenance(status: Status, apps: Iterable[str] | None = None) -> bool: |
| 83 | + """Report whether any application or unit in *status* is in "maintenance" status. |
| 84 | +
|
| 85 | + TODO: Args |
| 86 | + """ |
| 87 | + return _any_status_is('maintenance', status, apps) |
| 88 | + |
| 89 | + |
| 90 | +def any_waiting(status: Status, apps: Iterable[str] | None = None) -> bool: |
| 91 | + """Report whether any application or unit in *status* is in "waiting" status. |
| 92 | +
|
| 93 | + TODO: Args |
| 94 | + """ |
| 95 | + return _any_status_is('waiting', status, apps) |
| 96 | + |
| 97 | + |
| 98 | +def _all_statuses_are(expected: str, status: Status, apps: Iterable[str] | None) -> bool: |
| 99 | + if isinstance(apps, str | bytes): |
| 100 | + raise TypeError('"apps" must be an iterable of names (like a list), not a string') |
| 101 | + |
| 102 | + if apps is None: |
| 103 | + apps = status.apps.keys() |
| 104 | + |
| 105 | + for app in apps: |
| 106 | + app_info = status.apps.get(app) |
| 107 | + if app_info is None: |
| 108 | + return False |
| 109 | + if app_info.app_status.current != expected: |
| 110 | + return False |
| 111 | + for unit_info in app_info.units.values(): |
| 112 | + if unit_info.workload_status.current != expected: |
| 113 | + return False |
| 114 | + return True |
| 115 | + |
| 116 | + |
| 117 | +def _any_status_is(expected: str, status: Status, apps: Iterable[str] | None) -> bool: |
| 118 | + if isinstance(apps, str | bytes): |
| 119 | + raise TypeError('"apps" must be an iterable of names (like a list), not a string') |
| 120 | + |
| 121 | + if apps is None: |
| 122 | + apps = status.apps.keys() |
| 123 | + |
| 124 | + for app in apps: |
| 125 | + app_info = status.apps.get(app) |
| 126 | + if app_info is None: |
| 127 | + continue |
| 128 | + if app_info.app_status.current == expected: |
| 129 | + return True |
| 130 | + for unit_info in app_info.units.values(): |
| 131 | + if unit_info.workload_status.current == expected: |
| 132 | + return True |
| 133 | + return False |
0 commit comments