Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
fix: intent comparison name changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeff07 committed Dec 20, 2021
1 parent 7439482 commit 1059227
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
16 changes: 13 additions & 3 deletions ipfabric/settings/user_mgmt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from typing import Any, Optional

import pytz
from pydantic import Field, BaseModel

from ipfabric.tools.helpers import create_regex
Expand All @@ -18,6 +19,7 @@ class User(BaseModel):
domains: Optional[Any] = Field(alias='domainSuffixes')
custom_scope: bool = Field(alias='customScope')
ldap_id: Any = Field(alias='ldapId')
timezone: Optional[str] = None


class UserMgmt:
Expand All @@ -35,6 +37,8 @@ def get_users(self, username: str = None):
"columns": ["id", "isLocal", "username", "ssoProvider", "ldapId",
"domainSuffixes", "email", "customScope", "scope"]
}
if int(self.client.version) >= 4.2:
payload['columns'].append('timezone')
if username:
payload['filters'] = {"username": ["reg", create_regex(username)]}
users = self.client._ipf_pager('tables/users', payload)
Expand All @@ -50,21 +54,27 @@ def get_user_by_id(self, user_id: str):
resp.raise_for_status()
return User(**resp.json())

def add_user(self, username: str, email: str, password: str, scope: list):
def add_user(self, username: str, email: str, password: str, scope: list, timezone: str = 'UTC'):
"""
Adds a user
:param username: str: Username
:param email: str: Email
:param password: str: Must be 8 characters
:param scope: list: Accepted values: ['read', 'write', 'settings', 'team']
:param timezone: str: v4.2 and above, Defaults UTC. See pytz.all_timezones for correct syntax
:return: User
"""
if len(password) < 8:
raise SyntaxError("Password must be 8 characters.")
if not all(x in ['read', 'write', 'settings', 'team'] for x in scope):
raise SyntaxError("Only accepted scopes are ['read', 'write', 'settings', 'team']")
resp = self.client.post('users',
json={"username": username, "email": email, "password": password, "scope": scope})
payload = {"username": username, "email": email, "password": password, "scope": scope}
if int(self.client.version) >= 4.2:
if timezone not in pytz.all_timezones:
raise ValueError(f"Timezone {timezone} is not located. "
f"This is case sensitive please see pytz.all_timezones.")
payload['timezone'] = timezone
resp = self.client.post('users', json=payload)
resp.raise_for_status()
user_id = resp.json()['id']
return self.get_user_by_id(user_id)
Expand Down
14 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ python = "^3.6.2"
httpx = "^0.21.1"
python-dateutil = "^2.8.2"
pydantic = "^1.8.2"
pytz = "^2021.3"

[tool.poetry.dev-dependencies]
pytest = "^6.2.5"
Expand Down

0 comments on commit 1059227

Please sign in to comment.