From 84c78d90c24ecf66d7a9bb0db0a3cdff8e081c8c Mon Sep 17 00:00:00 2001 From: Adrian Huber Date: Tue, 17 Aug 2021 14:18:11 +0200 Subject: [PATCH] Add support for LB DNS PTRs (#144) * Add support for dns ptr on load balancers * Update changelog --- CHANGELOG.rst | 4 +++ hcloud/load_balancers/client.py | 32 ++++++++++++++++++++++++ hcloud/load_balancers/domain.py | 14 +++++++++-- tests/unit/load_balancers/conftest.py | 16 ++++++++++++ tests/unit/load_balancers/test_client.py | 17 +++++++++++++ 5 files changed, 81 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e6a550b..52aecdb 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,10 @@ History ======= +v1.16.0 (2021-08-17) +--------------------- +* Feature: Add support for Load Balancer DNS PTRs + v1.15.0 (2021-08-16) --------------------- * Feature: Add support for Placement Groups diff --git a/hcloud/load_balancers/client.py b/hcloud/load_balancers/client.py index 22a8985..d23acf9 100644 --- a/hcloud/load_balancers/client.py +++ b/hcloud/load_balancers/client.py @@ -243,6 +243,18 @@ def change_algorithm(self, algorithm): """ return self._client.change_algorithm(self, algorithm) + def change_dns_ptr(self, ip, dns_ptr): + # type: (str, str) -> BoundAction + """Changes the hostname that will appear when getting the hostname belonging to the public IPs (IPv4 and IPv6) of this Load Balancer. + + :param ip: str + The IP address for which to set the reverse DNS entry + :param dns_ptr: str + Hostname to set as a reverse DNS PTR entry, will reset to original default value if `None` + :return: :class:`BoundAction ` + """ + return self._client.change_dns_ptr(self, ip, dns_ptr) + def change_protection(self, delete): # type: (LoadBalancerService) -> List[BoundAction] """Changes the protection configuration of a Load Balancer. @@ -759,6 +771,26 @@ def change_algorithm(self, load_balancer, algorithm): ) return BoundAction(self._client.actions, response["action"]) + def change_dns_ptr(self, load_balancer, ip, dns_ptr): + # type: (Union[LoadBalancer, BoundLoadBalancer], str, str) -> BoundAction + """Changes the hostname that will appear when getting the hostname belonging to the public IPs (IPv4 and IPv6) of this Load Balancer. + + :param ip: str + The IP address for which to set the reverse DNS entry + :param dns_ptr: str + Hostname to set as a reverse DNS PTR entry, will reset to original default value if `None` + :return: :class:`BoundAction ` + """ + + response = self._client.request( + url="/load_balancers/{load_balancer_id}/actions/change_dns_ptr".format( + load_balancer_id=load_balancer.id + ), + method="POST", + json={"ip": ip, "dns_ptr": dns_ptr}, + ) + return BoundAction(self._client.actions, response["action"]) + def change_protection(self, load_balancer, delete=None): # type: (Union[LoadBalancer, BoundLoadBalancer], Optional[bool]) -> BoundAction """Changes the protection configuration of a Load Balancer. diff --git a/hcloud/load_balancers/domain.py b/hcloud/load_balancers/domain.py index f932407..0430292 100644 --- a/hcloud/load_balancers/domain.py +++ b/hcloud/load_balancers/domain.py @@ -299,13 +299,18 @@ class IPv4Address(BaseDomain): The IPv4 Address """ - __slots__ = ("ip",) + __slots__ = ( + "ip", + "dns_ptr", + ) def __init__( self, ip, # type: str + dns_ptr, # type: str ): self.ip = ip + self.dns_ptr = dns_ptr class IPv6Network(BaseDomain): @@ -315,13 +320,18 @@ class IPv6Network(BaseDomain): The IPv6 Network as CIDR Notation """ - __slots__ = ("ip",) + __slots__ = ( + "ip", + "dns_ptr", + ) def __init__( self, ip, # type: str + dns_ptr, # type: str ): self.ip = ip + self.dns_ptr = dns_ptr class PrivateNet(BaseDomain): diff --git a/tests/unit/load_balancers/conftest.py b/tests/unit/load_balancers/conftest.py index 9a7286b..597bb91 100644 --- a/tests/unit/load_balancers/conftest.py +++ b/tests/unit/load_balancers/conftest.py @@ -538,6 +538,22 @@ def response_change_algorithm(): } +@pytest.fixture() +def response_change_reverse_dns_entry(): + return { + "action": { + "id": 13, + "command": "change_dns_ptr", + "status": "success", + "progress": 100, + "started": "2016-01-30T23:55:00+00:00", + "finished": "2016-01-30T23:56:00+00:00", + "resources": [{"id": 42, "type": "load_balancer"}], + "error": {"code": "action_failed", "message": "Action failed"}, + } + } + + @pytest.fixture() def response_change_protection(): return { diff --git a/tests/unit/load_balancers/test_client.py b/tests/unit/load_balancers/test_client.py index c38eb01..97a765a 100644 --- a/tests/unit/load_balancers/test_client.py +++ b/tests/unit/load_balancers/test_client.py @@ -246,6 +246,23 @@ def test_change_algorithm( assert action.progress == 100 assert action.command == "change_algorithm" + def test_change_dns_ptr( + self, hetzner_client, response_change_reverse_dns_entry, bound_load_balancer + ): + hetzner_client.request.return_value = response_change_reverse_dns_entry + action = bound_load_balancer.change_dns_ptr( + ip="1.2.3.4", dns_ptr="lb1.example.com" + ) + hetzner_client.request.assert_called_with( + json={"dns_ptr": "lb1.example.com", "ip": "1.2.3.4"}, + url="/load_balancers/14/actions/change_dns_ptr", + method="POST", + ) + + assert action.id == 13 + assert action.progress == 100 + assert action.command == "change_dns_ptr" + def test_change_protection( self, hetzner_client, response_change_protection, bound_load_balancer ):