forked from ijat/pritunl-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from nathanielvarona/improvement/organization-…
…user-query-more-dry Organization User Query More DRY
- Loading branch information
Showing
2 changed files
with
23 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters