Skip to content

Commit

Permalink
lookup and use child api hostname if available when making AccountAdm…
Browse files Browse the repository at this point in the history
…in calls (#194)

Co-authored-by: Ben Carsten <[email protected]>
Co-authored-by: Aaron McConnell <[email protected]>
  • Loading branch information
3 people authored Oct 9, 2023
1 parent 5ede75c commit cdf7da6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 8 additions & 0 deletions duo_client/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from . import client

class Accounts(client.Client):
child_map = {}

def get_child_accounts(self):
"""
Return a list of all child accounts of the integration's account.
Expand All @@ -15,6 +17,12 @@ def get_child_accounts(self):
response = self.json_api_call('POST',
'/accounts/v1/account/list',
params)
if response and isinstance(response, list):
for account in response:
account_id = account.get('account_id', None)
api_hostname = account.get('api_hostname', None)
if account_id and api_hostname:
Accounts.child_map[account_id] = api_hostname
return response

def create_account(self, name):
Expand Down
13 changes: 12 additions & 1 deletion duo_client/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@

import six.moves.urllib

from . import client
from . import client, Accounts
from .logs.telephony import Telephony
import six
import warnings
Expand Down Expand Up @@ -3437,6 +3437,17 @@ def __init__(self, account_id, **kwargs):
"""Initializes an AccountAdmin for administering a child account.
account_id is the account id of the child account.
See the Client base class for other parameters."""
child_api_host = Accounts.child_map.get(account_id, None)
if child_api_host is None:
child_api_host = kwargs.get('host')
try:
accounts_api = Accounts(**kwargs)
accounts_api.get_child_accounts()
child_api_host = Accounts.child_map.get(account_id, kwargs['host'])
except RuntimeError:
pass
kwargs['host'] = child_api_host

super(AccountAdmin, self).__init__(**kwargs)
self.account_id = account_id

Expand Down

0 comments on commit cdf7da6

Please sign in to comment.