Skip to content

Commit

Permalink
feat: closes #262, add support for ZIA convert admin to regular user …
Browse files Browse the repository at this point in the history
…endpoint

test: update test suite to add tests for converting admin to regular user

Signed-off-by: mkelly <[email protected]>
  • Loading branch information
mitchos committed May 5, 2024
1 parent fed3ab1 commit 014ede7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
34 changes: 31 additions & 3 deletions pyzscaler/zia/admin_and_role_management.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from box import Box, BoxList
from restfly.endpoint import APIEndpoint

from pyzscaler.utils import Iterator, snake_to_camel
from pyzscaler.utils import Iterator, convert_keys, snake_to_camel


class AdminAndRoleManagementAPI(APIEndpoint):
Expand Down Expand Up @@ -57,7 +57,7 @@ def add_user(self, name: str, login_name: str, email: str, password: str, **kwar
... name="Jane Bob",
... login_name="[email protected]",
... password="hunter3",
... email="[email protected],
... email="[email protected]",
... admin_scope="department",
... scope_ids = ['376542', '245688'])
Expand All @@ -66,7 +66,7 @@ def add_user(self, name: str, login_name: str, email: str, password: str, **kwar
... name="Head Bob",
... login_name="[email protected]",
... password="hunter4",
... email="[email protected],
... email="[email protected]",
... is_auditor=True)
"""
Expand Down Expand Up @@ -265,3 +265,31 @@ def update_user(self, user_id: str, **kwargs) -> dict:
payload[snake_to_camel(key)] = value

return self._put(f"adminUsers/{user_id}", json=payload)

def convert_admin_to_user(self, user_id: str, group_ids: list, **kwargs) -> Box:
"""
Convert an admin user to a standard user.
Args:
user_id (str): The unique id of the admin user to be converted.
group_ids (list): A list of group ids to assign to the user.
**kwargs: Optional keyword args.
Returns:
:obj:`Box`: The converted user resource record.
Examples:
Convert an admin user to a standard user:
>>> user = zia.admin_and_role_management.convert_admin_to_user(user_id='99695301',
... group_ids=['3846532', '3846541'])
"""
payload = {"groups": [{"id": group_id} for group_id in group_ids]}

# Update the payload with keyword arguments
payload.update({k: v for k, v in kwargs.items() if v is not None})

# Convert snake to camelcase if needed
payload = convert_keys(payload)

return self._post(f"adminUsers/{user_id}/convertToUser", json=payload)
22 changes: 22 additions & 0 deletions tests/zia/test_admin_and_role_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,25 @@ def test_admin_list_roles(admin_roles, zia):
resp = zia.admin_and_role_management.list_roles(include_auditor_role=True)
assert isinstance(resp, BoxList)
assert resp[0].id == 1


@responses.activate
def test_admin_users_convert_admin_to_user(zia):
responses.add(
method="POST",
url="https://zsapi.zscaler.net/api/v1/adminUsers/1/convertToUser",
json={"id": 1},
status=200,
match=[
matchers.json_params_matcher(
{
"groups": [{"id": "1"}, {"id": "2"}],
}
)
],
)

resp = zia.admin_and_role_management.convert_admin_to_user("1", group_ids=["1", "2"])

assert isinstance(resp, dict)
assert resp.id == 1

0 comments on commit 014ede7

Please sign in to comment.