Skip to content

Commit 5780418

Browse files
authored
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
1 parent e61300e commit 5780418

4 files changed

Lines changed: 36 additions & 0 deletions

File tree

docs/api.clients.load_balancers.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ LoadBalancerClient
2626
.. autoclass:: hcloud.load_balancers.domain.LoadBalancerTarget
2727
:members:
2828

29+
.. autoclass:: hcloud.load_balancers.domain.LoadBalancerTargetHealthStatus
30+
:members:
31+
2932
.. autoclass:: hcloud.load_balancers.domain.LoadBalancerTargetLabelSelector
3033
:members:
3134

hcloud/load_balancers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
LoadBalancerService,
1717
LoadBalancerServiceHttp,
1818
LoadBalancerTarget,
19+
LoadBalancerTargetHealthStatus,
1920
LoadBalancerTargetIP,
2021
LoadBalancerTargetLabelSelector,
2122
PrivateNet,

hcloud/load_balancers/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
LoadBalancerService,
2121
LoadBalancerServiceHttp,
2222
LoadBalancerTarget,
23+
LoadBalancerTargetHealthStatus,
2324
LoadBalancerTargetIP,
2425
LoadBalancerTargetLabelSelector,
2526
PrivateNet,
@@ -83,6 +84,17 @@ def __init__(self, client: LoadBalancersClient, data: dict, complete: bool = Tru
8384
tmp_target.use_private_ip = target["use_private_ip"]
8485
elif target["type"] == "ip":
8586
tmp_target.ip = LoadBalancerTargetIP(ip=target["ip"]["ip"])
87+
88+
target_health_status = target.get("health_status")
89+
if target_health_status is not None:
90+
tmp_target.health_status = [
91+
LoadBalancerTargetHealthStatus(
92+
listen_port=target_health_status_item["listen_port"],
93+
status=target_health_status_item["status"],
94+
)
95+
for target_health_status_item in target_health_status
96+
]
97+
8698
tmp_targets.append(tmp_target)
8799
data["targets"] = tmp_targets
88100

hcloud/load_balancers/domain.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ class LoadBalancerTarget(BaseDomain):
313313
Target IP
314314
:param use_private_ip: bool
315315
use the private IP instead of primary public IP
316+
:param health_status: list
317+
List of health statuses of the services on this target. Only present for target types "server" and "ip".
316318
"""
317319

318320
def __init__(
@@ -322,12 +324,14 @@ def __init__(
322324
label_selector: LoadBalancerTargetLabelSelector | None = None,
323325
ip: LoadBalancerTargetIP | None = None,
324326
use_private_ip: bool | None = None,
327+
health_status: list[LoadBalancerTargetHealthStatus] | None = None,
325328
):
326329
self.type = type
327330
self.server = server
328331
self.label_selector = label_selector
329332
self.ip = ip
330333
self.use_private_ip = use_private_ip
334+
self.health_status = health_status
331335

332336
def to_payload(self) -> dict[str, Any]:
333337
payload: dict[str, Any] = {
@@ -354,6 +358,22 @@ def to_payload(self) -> dict[str, Any]:
354358
return payload
355359

356360

361+
class LoadBalancerTargetHealthStatus(BaseDomain):
362+
"""LoadBalancerTargetHealthStatus Domain
363+
364+
:param listen_port: Load Balancer Target listen port
365+
:param status: Load Balancer Target status. Choices: healthy, unhealthy, unknown
366+
"""
367+
368+
def __init__(
369+
self,
370+
listen_port: int | None = None,
371+
status: str | None = None,
372+
):
373+
self.listen_port = listen_port
374+
self.status = status
375+
376+
357377
class LoadBalancerTargetLabelSelector(BaseDomain):
358378
"""LoadBalancerTargetLabelSelector Domain
359379

0 commit comments

Comments
 (0)