1
0
Fork 0
mirror of https://github.com/ansible-collections/hetzner.hcloud.git synced 2026-02-04 08:01:49 +00:00

feat: compute load balancer targets status using a filter (#550)

##### SUMMARY

Allow to compute the status of a load balancer using a filter.

Closes #467 

##### ISSUE TYPE

- Feature Pull Request


##### COMPONENT NAME

hetzner.hcloud.loab_balancer_status
This commit is contained in:
Jonas L. 2024-08-14 14:18:39 +02:00 committed by GitHub
parent a85bd39738
commit fce8bc9bb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 199 additions and 10 deletions

49
plugins/filter/all.py Normal file
View file

@ -0,0 +1,49 @@
from __future__ import annotations
from typing import Literal
from ansible.errors import AnsibleFilterError
from ansible.module_utils.common.text.converters import to_native
# pylint: disable=unused-argument
def load_balancer_status(load_balancer: dict, *args, **kwargs) -> Literal["unknown", "unhealthy", "healthy"]:
"""
Return the status of a Load Balancer based on its targets.
"""
try:
result = "healthy"
for target in load_balancer["targets"]:
target_health_status = target.get("health_status")
# Report missing health status as unknown
if not target_health_status:
result = "unknown"
continue
for health_status in target_health_status:
status = health_status.get("status")
if status == "healthy":
continue
if status in (None, "unknown"):
result = "unknown"
continue
if status == "unhealthy":
return "unhealthy"
return result
except Exception as exc:
raise AnsibleFilterError(f"load_balancer_status - {to_native(exc)}", orig_exc=exc) from exc
class FilterModule:
"""
Hetzner Cloud filters.
"""
def filters(self):
return {
"load_balancer_status": load_balancer_status,
}