Skip to content

Commit

Permalink
Add support for LB DNS PTRs (#144)
Browse files Browse the repository at this point in the history
* Add support for dns ptr on load balancers

* Update changelog
  • Loading branch information
Adi146 authored Aug 17, 2021
1 parent f20ee33 commit 84c78d9
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions hcloud/load_balancers/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <hcloud.actions.client.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.
Expand Down Expand Up @@ -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 <hcloud.actions.client.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.
Expand Down
14 changes: 12 additions & 2 deletions hcloud/load_balancers/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/load_balancers/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/load_balancers/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down

0 comments on commit 84c78d9

Please sign in to comment.