Add a "dig" action
authorAdam Israel <adam.israel@canonical.com>
Wed, 5 Oct 2016 23:27:19 +0000 (16:27 -0700)
committerAdam Israel <adam.israel@canonical.com>
Wed, 5 Oct 2016 23:27:19 +0000 (16:27 -0700)
Signed-off-by: Adam Israel <adam.israel@canonical.com>
layers/netutils/actions.yaml
layers/netutils/actions/dig [new file with mode: 0755]
layers/netutils/reactive/layer_netutils.py

index 9995f77..5c99e21 100644 (file)
@@ -30,3 +30,18 @@ traceroute:
       type: string
   required:
     - destination
+dig:
+  description: "DNS lookup"
+  params:
+    nsserver:
+      description: "The nameserver to lookup against."
+      type: string
+    host:
+      description: "The host to lookup"
+      type: string
+    type:
+      description: "The DNS record type to lookup"
+      type: string
+  required:
+    - host
+
diff --git a/layers/netutils/actions/dig b/layers/netutils/actions/dig
new file mode 100755 (executable)
index 0000000..736a406
--- /dev/null
@@ -0,0 +1,18 @@
+#!/usr/bin/env python3
+import sys
+sys.path.append('lib')
+
+from charms.reactive import main
+from charms.reactive import set_state
+from charmhelpers.core.hookenv import action_fail
+
+"""
+`set_state` only works here because it's flushed to disk inside the `main()`
+loop. remove_state will need to be called inside the action method.
+"""
+set_state('actions.dig')
+
+try:
+    main()
+except Exception as e:
+    action_fail(repr(e))
index 8f8db26..7043f19 100644 (file)
@@ -22,6 +22,31 @@ def ready():
     status_set('active', 'Ready!')
     set_flag('netutils.ready')
 
+@when('actions.dig')
+def dig():
+    err = ''
+    try:
+        nsserver = action_get('nsserver')
+        host = action_get('host')
+        nstype = action_get('type')
+        cmd = "dig"
+
+        if nsserver:
+            cmd += " @{}".format(nsserver)
+        if host:
+            cmd += " {}".format(host)
+        else:
+            action_fail('Hostname required.')
+        if nstype:
+            cmd += " -t {}".format(nstype)
+
+        result, err = _run(cmd)
+    except:
+        action_fail('dig command failed:' + err)
+    else:
+        action_set({'outout': result})
+    finally:
+        remove_flag('actions.dig')
 
 @when('actions.nmap')
 def nmap():