From 8bceab8e2c223f70acc163e437ef7226e4d9a48b Mon Sep 17 00:00:00 2001 From: lgarswood Date: Mon, 18 Apr 2016 00:26:57 +0400 Subject: [PATCH] Update zabbix_api.py Added join functionality --- zabbix/zabbix_api.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/zabbix/zabbix_api.py b/zabbix/zabbix_api.py index 87ccae9..8a087a0 100644 --- a/zabbix/zabbix_api.py +++ b/zabbix/zabbix_api.py @@ -318,6 +318,30 @@ def __init__(self, parent, data, **kwargs): setattr(self, key, val) self.debug(logging.WARNING, "Set %s:%s" % (repr(key), repr(val))) + # You can join on an already-retrieved list + # If there is a key conflict, the second result is favoured + def join(self, *args, **kwargs): + if "using" not in kwargs.keys(): + raise Exception("There was no join field supplied. Supply 'using='") + if "joinList" not in kwargs.keys(): + raise Exception("There was no list to join to supplied. Supply 'joinList='") + joinField = kwargs["using"] + joinList = kwargs["joinList"] + if "filter" not in args[0].keys(): + args[0]["filter"] = {} + args[0]["filter"][joinField] = [ x[joinField] for x in joinList ] + # Get the new result set, filtered by 'using' match + queryResult = self.universal("%s.%s" % (self.data["prefix"], "get"), args[0]) + joinedList = [] + # Create the grand dictionary of joined results + for fromRow in queryResult: + for toRow in joinList: + if fromRow[joinField] == toRow[joinField]: + keys = list(set(fromRow.keys() + toRow.keys())) + joinedRow = {key: (fromRow[key] if key in fromRow.keys() else toRow[key]) for key in keys} + joinedList.append(joinedRow) + return joinedList + def __getattr__(self, name): if self.data["prefix"] == "configuration" and name == "import_": # workaround for "import" method name = "import"