Skip to content

Commit 84c78d9

Browse files
authored
Add support for LB DNS PTRs (#144)
* Add support for dns ptr on load balancers * Update changelog
1 parent f20ee33 commit 84c78d9

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
History
33
=======
44

5+
v1.16.0 (2021-08-17)
6+
---------------------
7+
* Feature: Add support for Load Balancer DNS PTRs
8+
59
v1.15.0 (2021-08-16)
610
---------------------
711
* Feature: Add support for Placement Groups

hcloud/load_balancers/client.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,18 @@ def change_algorithm(self, algorithm):
243243
"""
244244
return self._client.change_algorithm(self, algorithm)
245245

246+
def change_dns_ptr(self, ip, dns_ptr):
247+
# type: (str, str) -> BoundAction
248+
"""Changes the hostname that will appear when getting the hostname belonging to the public IPs (IPv4 and IPv6) of this Load Balancer.
249+
250+
:param ip: str
251+
The IP address for which to set the reverse DNS entry
252+
:param dns_ptr: str
253+
Hostname to set as a reverse DNS PTR entry, will reset to original default value if `None`
254+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
255+
"""
256+
return self._client.change_dns_ptr(self, ip, dns_ptr)
257+
246258
def change_protection(self, delete):
247259
# type: (LoadBalancerService) -> List[BoundAction]
248260
"""Changes the protection configuration of a Load Balancer.
@@ -759,6 +771,26 @@ def change_algorithm(self, load_balancer, algorithm):
759771
)
760772
return BoundAction(self._client.actions, response["action"])
761773

774+
def change_dns_ptr(self, load_balancer, ip, dns_ptr):
775+
# type: (Union[LoadBalancer, BoundLoadBalancer], str, str) -> BoundAction
776+
"""Changes the hostname that will appear when getting the hostname belonging to the public IPs (IPv4 and IPv6) of this Load Balancer.
777+
778+
:param ip: str
779+
The IP address for which to set the reverse DNS entry
780+
:param dns_ptr: str
781+
Hostname to set as a reverse DNS PTR entry, will reset to original default value if `None`
782+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
783+
"""
784+
785+
response = self._client.request(
786+
url="/load_balancers/{load_balancer_id}/actions/change_dns_ptr".format(
787+
load_balancer_id=load_balancer.id
788+
),
789+
method="POST",
790+
json={"ip": ip, "dns_ptr": dns_ptr},
791+
)
792+
return BoundAction(self._client.actions, response["action"])
793+
762794
def change_protection(self, load_balancer, delete=None):
763795
# type: (Union[LoadBalancer, BoundLoadBalancer], Optional[bool]) -> BoundAction
764796
"""Changes the protection configuration of a Load Balancer.

hcloud/load_balancers/domain.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,18 @@ class IPv4Address(BaseDomain):
299299
The IPv4 Address
300300
"""
301301

302-
__slots__ = ("ip",)
302+
__slots__ = (
303+
"ip",
304+
"dns_ptr",
305+
)
303306

304307
def __init__(
305308
self,
306309
ip, # type: str
310+
dns_ptr, # type: str
307311
):
308312
self.ip = ip
313+
self.dns_ptr = dns_ptr
309314

310315

311316
class IPv6Network(BaseDomain):
@@ -315,13 +320,18 @@ class IPv6Network(BaseDomain):
315320
The IPv6 Network as CIDR Notation
316321
"""
317322

318-
__slots__ = ("ip",)
323+
__slots__ = (
324+
"ip",
325+
"dns_ptr",
326+
)
319327

320328
def __init__(
321329
self,
322330
ip, # type: str
331+
dns_ptr, # type: str
323332
):
324333
self.ip = ip
334+
self.dns_ptr = dns_ptr
325335

326336

327337
class PrivateNet(BaseDomain):

tests/unit/load_balancers/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,22 @@ def response_change_algorithm():
538538
}
539539

540540

541+
@pytest.fixture()
542+
def response_change_reverse_dns_entry():
543+
return {
544+
"action": {
545+
"id": 13,
546+
"command": "change_dns_ptr",
547+
"status": "success",
548+
"progress": 100,
549+
"started": "2016-01-30T23:55:00+00:00",
550+
"finished": "2016-01-30T23:56:00+00:00",
551+
"resources": [{"id": 42, "type": "load_balancer"}],
552+
"error": {"code": "action_failed", "message": "Action failed"},
553+
}
554+
}
555+
556+
541557
@pytest.fixture()
542558
def response_change_protection():
543559
return {

tests/unit/load_balancers/test_client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,23 @@ def test_change_algorithm(
246246
assert action.progress == 100
247247
assert action.command == "change_algorithm"
248248

249+
def test_change_dns_ptr(
250+
self, hetzner_client, response_change_reverse_dns_entry, bound_load_balancer
251+
):
252+
hetzner_client.request.return_value = response_change_reverse_dns_entry
253+
action = bound_load_balancer.change_dns_ptr(
254+
ip="1.2.3.4", dns_ptr="lb1.example.com"
255+
)
256+
hetzner_client.request.assert_called_with(
257+
json={"dns_ptr": "lb1.example.com", "ip": "1.2.3.4"},
258+
url="/load_balancers/14/actions/change_dns_ptr",
259+
method="POST",
260+
)
261+
262+
assert action.id == 13
263+
assert action.progress == 100
264+
assert action.command == "change_dns_ptr"
265+
249266
def test_change_protection(
250267
self, hetzner_client, response_change_protection, bound_load_balancer
251268
):

0 commit comments

Comments
 (0)