Repository: StackStorm/st2

Issue:
Add query type to linux.dig action
## SUMMARY

I would like the ability to query TXT records and noticed there is no way to specify a query type to the dig action. 

### STACKSTORM VERSION

`st2 3.6.0, on Python 3.6.8`

## Steps to reproduce the problem

I attempted a few ways to add "TXT" to the query by adding to queryopts or try appending to the string hostname. Upon looking at the code I realized nothing like that would work.

## Expected Results

Get a list returned of TXT records

## Some sample code to add it

```
class DigAction(Action):
    def run(self, rand, count, nameserver, hostname, queryopts, querytype): # Add querytype parameter
        opt_list = []
        output = []

        cmd_args = ["dig"]
        if nameserver:
            nameserver = "@" + nameserver
            cmd_args.append(nameserver)

        if isinstance(queryopts, str) and "," in queryopts:
            opt_list = queryopts.split(",")
        else:
            opt_list.append(queryopts)

        cmd_args.extend(["+" + option for option in opt_list])

        cmd_args.append(hostname)
        cmd_args.append(querytype) # append query type (Default is set to "A" in dig.yaml)

        try:
            raw_result = subprocess.Popen(
                cmd_args, stderr=subprocess.PIPE, stdout=subprocess.PIPE
            ).communicate()[0]

            if sys.version_info >= (3,):
                # This function might call getpreferred encoding unless we pass
                # do_setlocale=False.
                encoding = locale.getpreferredencoding(do_setlocale=False)
                result_list_str = raw_result.decode(encoding)
            else:
                result_list_str = str(raw_result)

            if querytype.lower() == "txt":   # improve the output formatting result of TXT records
                result_list_str = result_list_str.replace('"', '')   # strip quotes so we don't see \" wrapped around output
            result_list = list(filter(None, result_list_str.split("\n")))
```

I only spent a few minutes on this code to test making it work for me. It could be improved on to make sure works for other types as well. I added inline comments to show the only lines I added


Discussion:
Good stuff, thanks for sharing!
How about opening a PR with that addition? 

Resolving patch:
diff --git a/contrib/linux/actions/dig.py b/contrib/linux/actions/dig.py
--- a/contrib/linux/actions/dig.py
+++ b/contrib/linux/actions/dig.py
@@ -25,7 +25,7 @@
 
 
 class DigAction(Action):
-    def run(self, rand, count, nameserver, hostname, queryopts):
+    def run(self, rand, count, nameserver, hostname, queryopts, querytype):
         opt_list = []
         output = []
 
@@ -42,6 +42,7 @@ def run(self, rand, count, nameserver, hostname, queryopts):
         cmd_args.extend(["+" + option for option in opt_list])
 
         cmd_args.append(hostname)
+        cmd_args.append(querytype)
 
         try:
             raw_result = subprocess.Popen(
@@ -56,6 +57,10 @@ def run(self, rand, count, nameserver, hostname, queryopts):
             else:
                 result_list_str = str(raw_result)
 
+            # Better format the output when the type is TXT
+            if querytype.lower() == "txt":
+                result_list_str = result_list_str.replace('"', "")
+
             result_list = list(filter(None, result_list_str.split("\n")))
 
         # NOTE: Python3 supports the FileNotFoundError, the errono.ENOENT is for py2 compat


Test patch:
diff --git a/contrib/linux/tests/test_action_dig.py b/contrib/linux/tests/test_action_dig.py
--- a/contrib/linux/tests/test_action_dig.py
+++ b/contrib/linux/tests/test_action_dig.py
@@ -45,6 +45,23 @@ def test_run_with_empty_queryopts(self):
             self.assertIsInstance(result, str)
             self.assertGreater(len(result), 0)
 
+    def test_run_with_empty_querytype(self):
+        action = self.get_action_instance()
+
+        results = action.run(
+            rand=False,
+            count=0,
+            nameserver=None,
+            hostname="google.com",
+            queryopts="short",
+            querytype="",
+        )
+        self.assertIsInstance(results, list)
+
+        for result in results:
+            self.assertIsInstance(result, str)
+            self.assertGreater(len(result), 0)
+
     def test_run(self):
         action = self.get_action_instance()
 
@@ -54,6 +71,7 @@ def test_run(self):
             nameserver=None,
             hostname="google.com",
             queryopts="short",
+            querytype="A",
         )
         self.assertIsInstance(results, list)
         self.assertGreater(len(results), 0)