[nft PATCH v3 09/14] py: Add JSON support to nftables Class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Phil Sutter <phil@xxxxxx>
---
 py/nftables.py | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/py/nftables.py b/py/nftables.py
index eb81f5b2fdb9c..47ff14afc9741 100644
--- a/py/nftables.py
+++ b/py/nftables.py
@@ -68,6 +68,13 @@ class Nftables:
         self.nft_ctx_output_set_stateless = lib.nft_ctx_output_set_stateless
         self.nft_ctx_output_set_stateless.argtypes = [c_void_p, c_bool]
 
+        self.nft_ctx_output_get_json = lib.nft_ctx_output_get_json
+        self.nft_ctx_output_get_json.restype = c_bool
+        self.nft_ctx_output_get_json.argtypes = [c_void_p]
+
+        self.nft_ctx_output_set_json = lib.nft_ctx_output_set_json
+        self.nft_ctx_output_set_json.argtypes = [c_void_p, c_bool]
+
         self.nft_ctx_output_get_debug = lib.nft_ctx_output_get_debug
         self.nft_ctx_output_get_debug.restype = c_int
         self.nft_ctx_output_get_debug.argtypes = [c_void_p]
@@ -169,7 +176,7 @@ class Nftables:
         return self.nft_ctx_output_get_stateless(self.__ctx)
 
     def set_stateless_output(self, val):
-        """Enable or Disable stateless output.
+        """Enable or disable stateless output.
 
         Accepts a boolean turning stateless output either on or off.
 
@@ -179,6 +186,24 @@ class Nftables:
         self.nft_ctx_output_set_stateless(self.__ctx, val)
         return old
 
+    def get_json_output(self):
+        """Get the current state of JSON output.
+
+        Returns a boolean indicating whether JSON output is active or not.
+        """
+        return self.nft_ctx_output_get_json(self.__ctx)
+
+    def set_json_output(self, val):
+        """Enable or disable JSON output.
+
+        Accepts a boolean turning JSON output either on or off.
+
+        Returns the previous value.
+        """
+        old = self.get_json_output()
+        self.nft_ctx_output_set_json(self.__ctx, val)
+        return old
+
     def get_debug(self):
         """Get currently active debug flags.
 
@@ -247,3 +272,21 @@ class Nftables:
         error = self.nft_ctx_get_error_buffer(self.__ctx)
 
         return (rc, output, error)
+
+    def json_cmd(self, json_root):
+        """Run an nftables command in JSON syntax via libnftables.
+
+        Accepts a hash object as input.
+
+        Returns a tuple (rc, output, error):
+        rc     -- reutrn code as returned by nft_run_cmd_from_buffer() function
+        output -- a hash object containing library standard output
+        error  -- a string containing output written to stderr
+        """
+        json_out_old = self.set_json_output(True)
+        rc, output, error = self.cmd(json.dumps(json_root))
+        if not json_out_old:
+            self.set_json_output(json_out_old)
+        if len(output):
+            output = json.loads(output)
+        return (rc, output, error)
-- 
2.17.0

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux