From 6473b1cea4cf01a59b3567017bfa1a65de8e530b Mon Sep 17 00:00:00 2001 From: Shivam Kalra Date: Thu, 13 Mar 2025 16:23:29 +0530 Subject: [PATCH 1/2] Added subaccount to add_admin() and created tests and example for add_admin() --- duo_client/admin.py | 4 +++- examples/Admin/create_admin.py | 30 ++++++++++++++++++++++++ tests/admin/test_admins.py | 42 ++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 examples/Admin/create_admin.py diff --git a/duo_client/admin.py b/duo_client/admin.py index d1b48553..491c60c4 100644 --- a/duo_client/admin.py +++ b/duo_client/admin.py @@ -2947,7 +2947,7 @@ def get_admin(self, admin_id): response = self.json_api_call('GET', path, {}) return response - def add_admin(self, name, email, phone, password, role=None): + def add_admin(self, name, email, phone, password, role=None, subaccount_role=None): """ Create an administrator and adds it to a customer. @@ -2970,6 +2970,8 @@ def add_admin(self, name, email, phone, password, role=None): params['phone'] = phone if role is not None: params['role'] = role + if subaccount_role is not None: + params['subaccount_role'] = subaccount_role response = self.json_api_call('POST', '/admin/v1/admins', params) return response diff --git a/examples/Admin/create_admin.py b/examples/Admin/create_admin.py new file mode 100644 index 00000000..a1f49e30 --- /dev/null +++ b/examples/Admin/create_admin.py @@ -0,0 +1,30 @@ +#!/usr/bin/python +import pprint +import sys + +import duo_client + +argv_iter = iter(sys.argv[1:]) +def get_next_arg(prompt): + try: + return next(argv_iter) + except StopIteration: + return input(prompt) + +# Configuration and information about objects to create. +admin_api = duo_client.Admin( + ikey=get_next_arg('Admin API integration key ("DI..."): '), + skey=get_next_arg('integration secret key: '), + host=get_next_arg('API hostname ("api-....duosecurity.com"): '), +) + +NAME = get_next_arg('Admin name: ') +EMAIL = get_next_arg('Email: ') +PHONE = get_next_arg('Phone number (e.g. 2154567890): ') +PASSWORD = get_next_arg('Password: ') +ROLE = get_next_arg('Administrative Role: ') +SUBACCOUNT_ROLE = get_next_arg('Subaccount Role: ') + +created_admin = admin_api.add_admin(NAME, EMAIL, PHONE, PASSWORD, ROLE, SUBACCOUNT_ROLE) +print('Created Admin: ') +pprint.pprint(created_admin) \ No newline at end of file diff --git a/tests/admin/test_admins.py b/tests/admin/test_admins.py index 60f50a41..df9a78a8 100644 --- a/tests/admin/test_admins.py +++ b/tests/admin/test_admins.py @@ -175,3 +175,45 @@ def test_update_admin_password_mgmt_status_set_password(self): 'has_external_password_mgmt': 'True', 'password': 'dolphins' }) + + def test_create_admin(self): + response = self.client.add_admin('test-name', 'test-email', 'test-phone', 'test-pswd') + self.assertEqual(response['method'], 'POST') + self.assertEqual(response['uri'], '/admin/v1/admins') + self.assertEqual( + json.loads(response['body']), + { + 'account_id': self.client.account_id, + 'name': 'test-name', + 'email': 'test-email', + 'phone': 'test-phone', + }) + + def test_create_admin_with_role(self): + response = self.client.add_admin('test-name', 'test-email', 'test-phone', 'test-pswd', 'test-role') + self.assertEqual(response['method'], 'POST') + self.assertEqual(response['uri'], '/admin/v1/admins') + self.assertEqual( + json.loads(response['body']), + { + 'account_id': self.client.account_id, + 'name': 'test-name', + 'email': 'test-email', + 'phone': 'test-phone', + 'role': 'test-role', + }) + + def test_create_admin_with_subaccount_role(self): + response = self.client.add_admin('test-name', 'test-email', 'test-phone', 'test-pswd', 'test-role', 'test-subaccount-role') + self.assertEqual(response['method'], 'POST') + self.assertEqual(response['uri'], '/admin/v1/admins') + self.assertEqual( + json.loads(response['body']), + { + 'account_id': self.client.account_id, + 'name': 'test-name', + 'email': 'test-email', + 'phone': 'test-phone', + 'role': 'test-role', + 'subaccount_role': 'test-subaccount-role', + }) From 45ff80763c41d0aa0484fcf9dc39110b2a010586 Mon Sep 17 00:00:00 2001 From: Shivam Kalra Date: Thu, 13 Mar 2025 16:36:09 +0530 Subject: [PATCH 2/2] added new line in the end of create_admin.py --- examples/Admin/create_admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Admin/create_admin.py b/examples/Admin/create_admin.py index a1f49e30..dd3aeca9 100644 --- a/examples/Admin/create_admin.py +++ b/examples/Admin/create_admin.py @@ -27,4 +27,4 @@ def get_next_arg(prompt): created_admin = admin_api.add_admin(NAME, EMAIL, PHONE, PASSWORD, ROLE, SUBACCOUNT_ROLE) print('Created Admin: ') -pprint.pprint(created_admin) \ No newline at end of file +pprint.pprint(created_admin)