mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
vultr.py: fix env var handling (#42659)
This commit is contained in:
parent
cf4e7fd70a
commit
08acc74056
1 changed files with 17 additions and 12 deletions
|
@ -43,9 +43,9 @@ class Vultr:
|
|||
# For caching HTTP API responses
|
||||
self.api_cache = dict()
|
||||
|
||||
# Reads the config from vultr.ini
|
||||
try:
|
||||
config = self.read_ini_config()
|
||||
config = self.read_env_variables()
|
||||
config.update(self.read_ini_config())
|
||||
except KeyError:
|
||||
config = {}
|
||||
|
||||
|
@ -61,6 +61,9 @@ class Vultr:
|
|||
"in section '%s' in the ini config file has not an int value: timeout, retries. "
|
||||
"Error was %s" % (self.module.params.get('api_account'), to_native(e)))
|
||||
|
||||
if not self.api_config.get('api_key'):
|
||||
self.module.fail_json(msg="The API key is not speicied. Please refer to the documentation.")
|
||||
|
||||
# Common vultr returns
|
||||
self.result['vultr_api'] = {
|
||||
'api_account': self.module.params.get('api_account'),
|
||||
|
@ -76,18 +79,18 @@ class Vultr:
|
|||
'Accept': 'application/json',
|
||||
}
|
||||
|
||||
def read_ini_config(self):
|
||||
ini_group = self.module.params.get('api_account')
|
||||
|
||||
def read_env_variables(self):
|
||||
keys = ['key', 'timeout', 'retries', 'endpoint']
|
||||
env_conf = {}
|
||||
for key in keys:
|
||||
if 'VULTR_API_%s' % key.upper() not in os.environ:
|
||||
break
|
||||
else:
|
||||
env_conf[key] = os.environ['VULTR_API_%s' % key.upper()]
|
||||
else:
|
||||
return env_conf
|
||||
continue
|
||||
env_conf[key] = os.environ['VULTR_API_%s' % key.upper()]
|
||||
|
||||
return env_conf
|
||||
|
||||
def read_ini_config(self):
|
||||
ini_group = self.module.params.get('api_account')
|
||||
|
||||
paths = (
|
||||
os.path.join(os.path.expanduser('~'), '.vultr.ini'),
|
||||
|
@ -95,11 +98,13 @@ class Vultr:
|
|||
)
|
||||
if 'VULTR_API_CONFIG' in os.environ:
|
||||
paths += (os.path.expanduser(os.environ['VULTR_API_CONFIG']),)
|
||||
if not any((os.path.exists(c) for c in paths)):
|
||||
self.module.fail_json(msg="Config file not found. Tried : %s" % ", ".join(paths))
|
||||
|
||||
conf = configparser.ConfigParser()
|
||||
conf.read(paths)
|
||||
|
||||
if not conf._sections.get(ini_group):
|
||||
return dict()
|
||||
|
||||
return dict(conf.items(ini_group))
|
||||
|
||||
def fail_json(self, **kwargs):
|
||||
|
|
Loading…
Reference in a new issue