Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added join functionality #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions zabbix/zabbix_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down