Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlm committed Dec 19, 2023
1 parent 50be381 commit 78a0505
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions botocore/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,14 @@ class CredentialBuiltinResolver:
'required',
)
CREDENTIAL_BUILTINS = {
'account_id': EndpointResolverBuiltins.AWS_ACCOUNT_ID,
'scope': EndpointResolverBuiltins.AWS_CREDENTIAL_SCOPE,
'account_id': {
'param_key': 'AccountId',
'builtin_key': EndpointResolverBuiltins.AWS_ACCOUNT_ID,
},
'scope': {
'param_key': 'CredentialScope',
'builtin_key': EndpointResolverBuiltins.AWS_CREDENTIAL_SCOPE,
},
}

def __init__(self, credentials, account_id_endpoint_mode=None):
Expand All @@ -493,6 +499,9 @@ def _validate_account_id_endpoint_mode(self):

def resolve(self, param_definitions, builtins):
"""Resolve endpoint builtins sourced from credentials."""
if self._credentials is None:
return

builtins_to_resolve = self._builtins_to_resolve(param_definitions)
if not builtins_to_resolve:
return
Expand All @@ -506,36 +515,30 @@ def resolve(self, param_definitions, builtins):
)

def _builtins_to_resolve(self, param_definitions):
builtins_to_resolve = {}
if self._credentials is None:
return builtins_to_resolve

for builtin_name, builtin_key in self.CREDENTIAL_BUILTINS.items():
if self._should_resolve_builtin(builtin_name, param_definitions):
builtins_to_resolve[builtin_name] = builtin_key

return builtins_to_resolve

def _should_resolve_builtin(self, builtin_name, param_definitions):
should_resolve = getattr(self, f'_should_resolve_{builtin_name}')
return should_resolve(param_definitions)
return {
builtin_name: config['builtin_key']
for builtin_name, config in self.CREDENTIAL_BUILTINS.items()
if self._should_resolve_builtin(
builtin_name, config['param_key'], param_definitions
)
}

def _should_resolve_account_id(self, param_definitions):
has_builtin = self._builtin_configured(param_definitions, 'AccountId')
mode_enabled = self._account_id_endpoint_mode != 'disabled'
return has_builtin and mode_enabled
def _should_resolve_builtin(
self, builtin_name, param_name, param_definitions
):
has_builtin = self._builtin_configured(param_definitions, param_name)
if not has_builtin:
return False

def _should_resolve_scope(self, param_definitions):
return self._builtin_configured(param_definitions, 'CredentialScope')
acct_id_ep_mode = self._account_id_endpoint_mode
return builtin_name != 'account_id' or acct_id_ep_mode != 'disabled'

def _builtin_configured(self, param_definitions, param_name):
param_def = param_definitions.get(param_name)
return param_def is not None and param_def.builtin is not None

def _validate_builtin(self, builtin_name, value):
def noop_validator(value):
return value

noop_validator = lambda value: value
validator = getattr(self, f'_validate_{builtin_name}', noop_validator)
return validator(value)

Expand Down

0 comments on commit 78a0505

Please sign in to comment.