Skip to content

Commit

Permalink
refactor: helper fun _get_env
Browse files Browse the repository at this point in the history
Somewhat frequently there is a lookup in the environment for the key that
matches a module parameter. This simple helper just encapsulates that
to make it a bit easier elsewhere-- lookup the same key in params,
credentials, env
  • Loading branch information
UnwashedMeme committed Aug 14, 2020
1 parent a3eea2f commit 53483f0
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions plugins/module_utils/azure_rm_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ def __init__(self, auth_source=None, profile=None, subscription_id=None, client_
# cert validation mode precedence: module-arg, credential profile, env, "validate"
self._cert_validation_mode = cert_validation_mode or \
self.credentials.get('cert_validation_mode') or \
os.environ.get('AZURE_CERT_VALIDATION_MODE') or \
self._get_env('cert_validation_mode') or \
'validate'

if self._cert_validation_mode not in ['validate', 'ignore']:
Expand Down Expand Up @@ -1368,6 +1368,10 @@ def fail(self, msg, exception=None, **kwargs):
def _default_fail_impl(self, msg, exception=None, **kwargs):
raise AzureRMAuthException(msg)

def _get_env(self, module_key, default=None):
"Read envvar matching module parameter"
return os.environ.get(AZURE_CREDENTIAL_ENV_MAPPING[module_key], default)

def _get_profile(self, profile="default"):
path = expanduser("~/.azure/credentials")
try:
Expand All @@ -1390,7 +1394,7 @@ def _get_profile(self, profile="default"):

def _get_msi_credentials(self, subscription_id=None, client_id=None, **kwargs):
credentials = MSIAuthentication(client_id=client_id)
subscription_id = subscription_id or os.environ.get(AZURE_CREDENTIAL_ENV_MAPPING['subscription_id'])
subscription_id = subscription_id or self._get_env('subscription_id')
if not subscription_id:
try:
# use the first subscription of the MSI
Expand All @@ -1408,7 +1412,7 @@ def _get_msi_credentials(self, subscription_id=None, client_id=None, **kwargs):
def _get_azure_cli_credentials(self, subscription_id=None, resource=None):
if self.is_ad_resource:
resource = 'https://graph.windows.net/'
subscription_id = subscription_id or os.environ.get(AZURE_CREDENTIAL_ENV_MAPPING['subscription_id'])
subscription_id = subscription_id or self._get_env('subscription_id')
profile = get_cli_profile()
credentials, subscription_id, tenant = profile.get_login_credentials(
subscription_id=subscription_id, resource=resource)
Expand Down

0 comments on commit 53483f0

Please sign in to comment.