From 5780418f00a42e20cccacec6e030e464105807ba Mon Sep 17 00:00:00 2001 From: Jonas L Date: Thu, 17 Aug 2023 14:39:58 +0200 Subject: [PATCH] feat: add load balancer target health status field (#288) Related to ansible-collections/hetzner.hcloud#221 Docs: https://docs.hetzner.cloud/#load-balancers-get-a-load-balancer --- docs/api.clients.load_balancers.rst | 3 +++ hcloud/load_balancers/__init__.py | 1 + hcloud/load_balancers/client.py | 12 ++++++++++++ hcloud/load_balancers/domain.py | 20 ++++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/docs/api.clients.load_balancers.rst b/docs/api.clients.load_balancers.rst index f407f20..771eadf 100644 --- a/docs/api.clients.load_balancers.rst +++ b/docs/api.clients.load_balancers.rst @@ -26,6 +26,9 @@ LoadBalancerClient .. autoclass:: hcloud.load_balancers.domain.LoadBalancerTarget :members: +.. autoclass:: hcloud.load_balancers.domain.LoadBalancerTargetHealthStatus + :members: + .. autoclass:: hcloud.load_balancers.domain.LoadBalancerTargetLabelSelector :members: diff --git a/hcloud/load_balancers/__init__.py b/hcloud/load_balancers/__init__.py index 76106a5..4ac79ce 100644 --- a/hcloud/load_balancers/__init__.py +++ b/hcloud/load_balancers/__init__.py @@ -16,6 +16,7 @@ LoadBalancerService, LoadBalancerServiceHttp, LoadBalancerTarget, + LoadBalancerTargetHealthStatus, LoadBalancerTargetIP, LoadBalancerTargetLabelSelector, PrivateNet, diff --git a/hcloud/load_balancers/client.py b/hcloud/load_balancers/client.py index b999ddc..72325f1 100644 --- a/hcloud/load_balancers/client.py +++ b/hcloud/load_balancers/client.py @@ -20,6 +20,7 @@ LoadBalancerService, LoadBalancerServiceHttp, LoadBalancerTarget, + LoadBalancerTargetHealthStatus, LoadBalancerTargetIP, LoadBalancerTargetLabelSelector, PrivateNet, @@ -83,6 +84,17 @@ def __init__(self, client: LoadBalancersClient, data: dict, complete: bool = Tru tmp_target.use_private_ip = target["use_private_ip"] elif target["type"] == "ip": tmp_target.ip = LoadBalancerTargetIP(ip=target["ip"]["ip"]) + + target_health_status = target.get("health_status") + if target_health_status is not None: + tmp_target.health_status = [ + LoadBalancerTargetHealthStatus( + listen_port=target_health_status_item["listen_port"], + status=target_health_status_item["status"], + ) + for target_health_status_item in target_health_status + ] + tmp_targets.append(tmp_target) data["targets"] = tmp_targets diff --git a/hcloud/load_balancers/domain.py b/hcloud/load_balancers/domain.py index c22aa27..c5810d6 100644 --- a/hcloud/load_balancers/domain.py +++ b/hcloud/load_balancers/domain.py @@ -313,6 +313,8 @@ class LoadBalancerTarget(BaseDomain): Target IP :param use_private_ip: bool use the private IP instead of primary public IP + :param health_status: list + List of health statuses of the services on this target. Only present for target types "server" and "ip". """ def __init__( @@ -322,12 +324,14 @@ def __init__( label_selector: LoadBalancerTargetLabelSelector | None = None, ip: LoadBalancerTargetIP | None = None, use_private_ip: bool | None = None, + health_status: list[LoadBalancerTargetHealthStatus] | None = None, ): self.type = type self.server = server self.label_selector = label_selector self.ip = ip self.use_private_ip = use_private_ip + self.health_status = health_status def to_payload(self) -> dict[str, Any]: payload: dict[str, Any] = { @@ -354,6 +358,22 @@ def to_payload(self) -> dict[str, Any]: return payload +class LoadBalancerTargetHealthStatus(BaseDomain): + """LoadBalancerTargetHealthStatus Domain + + :param listen_port: Load Balancer Target listen port + :param status: Load Balancer Target status. Choices: healthy, unhealthy, unknown + """ + + def __init__( + self, + listen_port: int | None = None, + status: str | None = None, + ): + self.listen_port = listen_port + self.status = status + + class LoadBalancerTargetLabelSelector(BaseDomain): """LoadBalancerTargetLabelSelector Domain