diff --git a/duo_client/admin.py b/duo_client/admin.py index d1b4855..491c60c 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 0000000..dd3aeca --- /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) diff --git a/tests/admin/test_admins.py b/tests/admin/test_admins.py index 60f50a4..df9a78a 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', + })