Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions hcloud/load_balancers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
IPv6Network,
LoadBalancer,
LoadBalancerAlgorithm,
LoadBalancerCreatePublicNetwork,
LoadBalancerHealtCheckHttp,
LoadBalancerHealthCheck,
LoadBalancerHealthCheckHttp,
Expand All @@ -36,6 +37,7 @@
"LoadBalancer",
"LoadBalancerProtection",
"LoadBalancerAlgorithm",
"LoadBalancerCreatePublicNetwork",
"LoadBalancerHealtCheckHttp",
"LoadBalancerHealthCheckHttp",
"LoadBalancerHealthCheck",
Expand Down
47 changes: 34 additions & 13 deletions hcloud/load_balancers/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
from ..locations import BoundLocation
from ..metrics import Metrics
from ..networks import BoundNetwork
from ..primary_ips import BoundPrimaryIP
from ..servers import BoundServer
from .domain import (
CreateLoadBalancerResponse,
DeleteLoadBalancerResponse,
GetMetricsResponse,
IPv4Address,
IPv6Network,
LoadBalancer,
LoadBalancerAlgorithm,
LoadBalancerCreatePublicNetwork,
LoadBalancerHealthCheck,
LoadBalancerHealthCheckHttp,
LoadBalancerService,
Expand Down Expand Up @@ -72,10 +75,23 @@ def __init__(

public_net = data.get("public_net")
if public_net:
ipv4_address = IPv4Address.from_dict(public_net["ipv4"])
ipv6_network = IPv6Network.from_dict(public_net["ipv6"])
if public_net_ipv4_id := public_net["ipv4"].get("id"):
public_net["ipv4"]["primary_ip"] = BoundPrimaryIP(
client._parent.primary_ips,
{"id": public_net_ipv4_id},
complete=False,
)
if public_net_ipv6_id := public_net["ipv6"].get("id"):
public_net["ipv6"]["primary_ip"] = BoundPrimaryIP(
client._parent.primary_ips,
{"id": public_net_ipv6_id},
complete=False,
)

data["public_net"] = PublicNetwork(
ipv4=ipv4_address, ipv6=ipv6_network, enabled=public_net["enabled"]
ipv4=IPv4Address.from_dict(public_net["ipv4"]),
ipv6=IPv6Network.from_dict(public_net["ipv6"]),
enabled=public_net["enabled"],
)

private_nets = data.get("private_net")
Expand Down Expand Up @@ -213,11 +229,8 @@ def update(
"""
return self._client.update(self, name=name, labels=labels)

def delete(self) -> bool:
"""Deletes a Load Balancer.

:return: boolean
"""
def delete(self) -> DeleteLoadBalancerResponse:
"""Deletes a Load Balancer."""
return self._client.delete(self)

def get_metrics(
Expand Down Expand Up @@ -514,6 +527,7 @@ def create(
location: Location | BoundLocation | None = None,
network_zone: str | None = None,
public_interface: bool | None = None,
public_net: LoadBalancerCreatePublicNetwork | None = None,
network: Network | BoundNetwork | None = None,
) -> CreateLoadBalancerResponse:
"""Creates a Load Balancer .
Expand All @@ -536,6 +550,8 @@ def create(
The targets the Load Balancer is currently serving
:param public_interface: bool
Enable or disable the public interface of the Load Balancer
:param public_net:
Public network configuration for the Load Balancer.
:param network: Network
Adds the Load Balancer to a Network
:return: :class:`CreateLoadBalancerResponse <hcloud.load_balancers.domain.CreateLoadBalancerResponse>`
Expand All @@ -548,6 +564,8 @@ def create(
data["network"] = network.id
if public_interface is not None:
data["public_interface"] = public_interface
if public_net is not None:
data["public_net"] = public_net.to_payload()
if labels is not None:
data["labels"] = labels
if algorithm is not None:
Expand Down Expand Up @@ -595,17 +613,20 @@ def update(
)
return BoundLoadBalancer(self, response["load_balancer"])

def delete(self, load_balancer: LoadBalancer | BoundLoadBalancer) -> bool:
def delete(
self, load_balancer: LoadBalancer | BoundLoadBalancer
) -> DeleteLoadBalancerResponse:
"""Deletes a Load Balancer.

:param load_balancer: :class:`BoundLoadBalancer <hcloud.load_balancers.client.BoundLoadBalancer>` or :class:`LoadBalancer <hcloud.load_balancers.domain.LoadBalancer>`
:return: boolean
"""
self._client.request(
url=f"{self._base_url}/{load_balancer.id}",
response = self._client.request(
method="DELETE",
url=f"{self._base_url}/{load_balancer.id}",
)
return DeleteLoadBalancerResponse(
action=BoundAction(self._parent.actions, response["action"])
)
return True

def get_metrics(
self,
Expand Down
75 changes: 73 additions & 2 deletions hcloud/load_balancers/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ..locations import BoundLocation
from ..metrics import Metrics
from ..networks import BoundNetwork, Network
from ..primary_ips import BoundPrimaryIP, PrimaryIP
from ..servers import BoundServer
from .client import BoundLoadBalancer

Expand Down Expand Up @@ -578,22 +579,67 @@ def __init__(
self.enabled = enabled


class LoadBalancerCreatePublicNetwork(BaseDomain):
"""Load Balancer Create Public Network Domain

:param ipv4: ID of an existing Primary IP of type `ipv4` to assign to the Load Balancer.
:param ipv6: ID of an existing Primary IP of type `ipv6` to assign to the Load Balancer.
"""

__api_properties__ = (
"ipv4",
"ipv6",
)
__slots__ = __api_properties__

def __init__(
self,
ipv4: PrimaryIP | BoundPrimaryIP | None = None,
ipv6: PrimaryIP | BoundPrimaryIP | None = None,
):
self.ipv4 = ipv4
self.ipv6 = ipv6

def to_payload(self) -> dict[str, Any]:
"""
Generates the request payload from this domain object.
"""
payload: dict[str, Any] = {}

if self.ipv4 is not None:
payload["ipv4"] = self.ipv4.id
if self.ipv6 is not None:
payload["ipv6"] = self.ipv6.id

return payload


class IPv4Address(BaseDomain):
"""IPv4 Address Domain

:param ip: str
The IPv4 Address
"""

__api_properties__ = ("ip", "dns_ptr")
__api_properties__ = (
"primary_ip",
"ip",
"blocked",
"dns_ptr",
)
__slots__ = __api_properties__

def __init__(
self,
ip: str,
dns_ptr: str,
*,
primary_ip: PrimaryIP | BoundPrimaryIP | None = None,
blocked: bool | None = None,
):
self.primary_ip = primary_ip
self.ip = ip
self.blocked = blocked
self.dns_ptr = dns_ptr


Expand All @@ -604,15 +650,25 @@ class IPv6Network(BaseDomain):
The IPv6 Network as CIDR Notation
"""

__api_properties__ = ("ip", "dns_ptr")
__api_properties__ = (
"primary_ip",
"ip",
"blocked",
"dns_ptr",
)
__slots__ = __api_properties__

def __init__(
self,
ip: str,
dns_ptr: str,
*,
primary_ip: PrimaryIP | BoundPrimaryIP | None = None,
blocked: bool | None = None,
):
self.primary_ip = primary_ip
self.ip = ip
self.blocked = blocked
self.dns_ptr = dns_ptr


Expand Down Expand Up @@ -658,6 +714,21 @@ def __init__(
self.action = action


class DeleteLoadBalancerResponse(BaseDomain):
"""
Delete Load Balancer Response Domain.
"""

__api_properties__ = ("action",)
__slots__ = __api_properties__

def __init__(
self,
action: BoundAction,
):
self.action = action


MetricsType = Literal[
"open_connections",
"connections_per_second",
Expand Down
18 changes: 16 additions & 2 deletions tests/unit/load_balancers/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,22 @@ def response_load_balancer():
"load_balancer": {
"id": 4711,
"name": "Web Frontend",
"ipv4": "131.232.99.1",
"ipv6": "2001:db8::1",
"public_net": {
"enabled": True,
"ipv4": {
"id": 4712,
"ip": "131.232.99.1",
"blocked": False,
"dns_ptr": "lb1.example.com",
},
"ipv6": {
"id": 4713,
"ip": "2001:db8::1",
"blocked": False,
"dns_ptr": "lb1.example.com",
},
},
"private_net": [{"network": 4711, "ip": "10.0.255.1"}],
"location": {
"id": 1,
"name": "fsn1",
Expand Down
Loading
Loading