Skip to content

Commit 9d43a7c

Browse files
author
Aaron Sierra
committed
dns: Add hostname helpers to Zone class
Add .hostname(), .fqdn(), and .prefix() helpers to the Zone class. Each of these functions accepts a host prefix, complete name (unrooted hostname), or FQDN (rooted hostname) associated with the Zone object and returns the format implied by the function name.
1 parent 181e6a0 commit 9d43a7c

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

libcloud/dns/base.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,70 @@ def __repr__(self):
102102
self.driver.name,
103103
)
104104

105+
def prefix(self, subname):
106+
"""
107+
Accept subordinate (or identity) names in multiple convenience formats.
108+
109+
In the following examples, "www" is returned:
110+
111+
| Format | subname | self.domain |
112+
|-----------------|--------------------|---------------|
113+
| Bare host name | "www" | "example.com" |
114+
| FQDN (unrooted) | "www.example.com" | "example.com" |
115+
| FQDN (rooted) | "www.example.com." | "example.com" |
116+
117+
:param subname: Hostname or FQDN.
118+
:type subname: ``str``
119+
120+
:return: Bare record name, without domain part.
121+
:rtype: ``str``
122+
"""
123+
124+
return subname.rstrip(".").removesuffix(self.domain).rstrip(".")
125+
126+
def hostname(self, subname):
127+
"""
128+
Accept subordinate (or identity) names in multiple convenience formats.
129+
130+
In the following examples, "www.example.com" is returned:
131+
132+
| Format | subname | self.domain |
133+
|-----------------|--------------------|---------------|
134+
| Bare host name | "www" | "example.com" |
135+
| FQDN (unrooted) | "www.example.com" | "example.com" |
136+
| FQDN (rooted) | "www.example.com." | "example.com" |
137+
138+
:param subname: Hostname or FQDN.
139+
:type subname: ``str``
140+
141+
:return: Complete hostname, including domain part.
142+
:rtype: ``str``
143+
"""
144+
145+
prefix = self.prefix(subname)
146+
return f"{prefix}.{self.domain}" if prefix else self.domain
147+
148+
def fqdn(self, subname):
149+
"""
150+
Accept subordinate (or identity) names in multiple convenience formats.
151+
152+
In the following examples, "www.example.com." is returned:
153+
154+
| Format | subname | self.domain |
155+
|-----------------|--------------------|---------------|
156+
| Bare host name | "www" | "example.com" |
157+
| FQDN (unrooted) | "www.example.com" | "example.com" |
158+
| FQDN (rooted) | "www.example.com." | "example.com" |
159+
160+
:param subname: Hostname or FQDN.
161+
:type subname: ``str``
162+
163+
:return: Complete hostname, including domain part.
164+
:rtype: ``str``
165+
"""
166+
167+
return f"{self.hostname(subname)}."
168+
105169

106170
class Record:
107171
"""

libcloud/test/dns/test_base.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ def setUp(self):
6363
self.driver = DNSDriver("none", "none")
6464
self.tmp_file = tempfile.mkstemp()
6565
self.tmp_path = self.tmp_file[1]
66+
self.master_zone = Zone(
67+
id=1, domain="example.com", type="master", ttl=900, driver=self.driver
68+
)
69+
70+
def test_zone_helpers(self):
71+
zone = self.master_zone
72+
73+
for func in (zone.prefix, zone.hostname, zone.fqdn):
74+
with self.assertRaises(AttributeError):
75+
self.assertEqual(func(None))
76+
77+
for apex in ("", "example.com", "example.com."):
78+
self.assertEqual(zone.prefix(apex), "")
79+
self.assertEqual(zone.hostname(apex), "example.com")
80+
self.assertEqual(zone.fqdn(apex), "example.com.")
81+
82+
for sub in ("sub", "sub.example.com", "sub.example.com."):
83+
self.assertEqual(zone.prefix(sub), "sub")
84+
self.assertEqual(zone.hostname(sub), "sub.example.com")
85+
self.assertEqual(zone.fqdn(sub), "sub.example.com.")
6686

6787
def test_export_zone_to_bind_format_slave_should_throw(self):
6888
zone = Zone(id=1, domain="example.com", type="slave", ttl=900, driver=self.driver)

0 commit comments

Comments
 (0)