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

Organization User Query More DRY #40

Merged
Merged
Show file tree
Hide file tree
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
37 changes: 22 additions & 15 deletions pritunl_api/utils/query.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
def org_user(pritunl, org_name, user_name=None):
def __get_org_by_name(orgs_obj, org_name):
for org in orgs_obj:
if org['name'] == org_name:
return org
return None
"""
Retrieve an organization and optionally a user from Pritunl.

def __get_user_by_name(users_obj, user_name):
for user in users_obj:
if user["name"] == user_name:
return user
return None
Args:
pritunl (object): Pritunl API client
org_name (str): Name of the organization
user_name (str, optional): Name of the user. If not provided, returns all users in the organization.

org = __get_org_by_name(pritunl.organization.get(), org_name)
user = pritunl.user.get(org_id=org['id'])
Returns:
tuple: (org, user) where org is the organization object and user is the user object or a list of user objects

if user_name:
user = __get_user_by_name(pritunl.user.get(org_id=org['id']), user_name)
return org, user
Notes:
If user_name is provided, returns the user object if found, otherwise returns None.
If user_name is not provided, returns a list of all user objects in the organization.
"""
def __get_by_name(objs, name):
for obj in objs:
if obj['name'] == name:
return obj
return None

org = next((org for org in pritunl.organization.get() if org['name'] == org_name), None)
if user_name:
user = next((user for user in pritunl.user.get(org_id=org['id']) if user['name'] == user_name), None)
else:
user = pritunl.user.get(org_id=org['id']) # Return all users
return org, user
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pritunl-api"
version = "1.1.11"
version = "1.1.12"
description = "Pritunl API Client for Python"
authors = ["Nathaniel Varona <nathaniel.varona+pypi@gmail.com>"]
license = "MIT"
Expand Down