Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matthewsaum 62275 #62314

Merged
merged 15 commits into from
Jul 28, 2022
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
1 change: 1 addition & 0 deletions changelog/62275.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added "dig.PTR" function to resolve PTR records for IPs, as well as tests and documentation
32 changes: 32 additions & 0 deletions salt/modules/dig.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,37 @@ def A(host, nameserver=None):
return [x for x in cmd["stdout"].split("\n") if check_ip(x)]


def PTR(host, nameserver=None):
"""
.. versionadded:: 3006.0

Return the PTR record for ``host``.

Always returns a list.

CLI Example:

.. code-block:: bash

salt ns1 dig.PTR 1.2.3.4
"""
dig = ["dig", "+short", "-x", str(host)]

if nameserver is not None:
dig.append("@{}".format(nameserver))

cmd = __salt__["cmd.run_all"](dig, python_shell=False)
# In this case, 0 is not the same as False
if cmd["retcode"] != 0:
log.warning(
"dig returned exit code '%s'. Returning empty list as fallback.",
cmd["retcode"],
)
return []

return [i for i in cmd["stdout"].split("\n")]


def AAAA(host, nameserver=None):
"""
Return the AAAA record for ``host``.
Expand Down Expand Up @@ -319,6 +350,7 @@ def TXT(host, nameserver=None):

# Let lowercase work, since that is the convention for Salt functions
a = A
ptr = PTR
aaaa = AAAA
cname = CNAME
ns = NS
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/modules/test_dig.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ def test_a(self):
],
)

def test_ptr(self):
dig_mock = MagicMock(
return_value={
"pid": 3657,
"retcode": 0,
"stderr": "",
"stdout": ("dns.google."),
}
)
with patch.dict(dig.__salt__, {"cmd.run_all": dig_mock}):
self.assertEqual(
dig.ptr("8.8.8.8"),
[
"dns.google.",
],
)

def test_aaaa(self):
dig_mock = MagicMock(
return_value={
Expand Down