Skip to content

Commit

Permalink
feat: add load balancer target health status field (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola authored Aug 17, 2023
1 parent e61300e commit 5780418
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/api.clients.load_balancers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions hcloud/load_balancers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
LoadBalancerService,
LoadBalancerServiceHttp,
LoadBalancerTarget,
LoadBalancerTargetHealthStatus,
LoadBalancerTargetIP,
LoadBalancerTargetLabelSelector,
PrivateNet,
Expand Down
12 changes: 12 additions & 0 deletions hcloud/load_balancers/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
LoadBalancerService,
LoadBalancerServiceHttp,
LoadBalancerTarget,
LoadBalancerTargetHealthStatus,
LoadBalancerTargetIP,
LoadBalancerTargetLabelSelector,
PrivateNet,
Expand Down Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions hcloud/load_balancers/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand All @@ -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] = {
Expand All @@ -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
Expand Down

0 comments on commit 5780418

Please sign in to comment.