Add a "dig" action
Signed-off-by: Adam Israel <adam.israel@canonical.com>
diff --git a/layers/netutils/actions.yaml b/layers/netutils/actions.yaml
index 9995f77..5c99e21 100644
--- a/layers/netutils/actions.yaml
+++ b/layers/netutils/actions.yaml
@@ -30,3 +30,18 @@
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
index 0000000..736a406
--- /dev/null
+++ b/layers/netutils/actions/dig
@@ -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))
diff --git a/layers/netutils/reactive/layer_netutils.py b/layers/netutils/reactive/layer_netutils.py
index 8f8db26..7043f19 100644
--- a/layers/netutils/reactive/layer_netutils.py
+++ b/layers/netutils/reactive/layer_netutils.py
@@ -22,6 +22,31 @@
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():