|
|
@ -9,7 +9,7 @@ import os
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
import urllib
|
|
|
|
import urllib
|
|
|
|
from ansible.module_utils.six.moves import configparser
|
|
|
|
from ansible.module_utils.six.moves import configparser
|
|
|
|
from ansible.module_utils._text import to_text
|
|
|
|
from ansible.module_utils._text import to_text, to_native
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -22,6 +22,7 @@ def vultr_argument_spec():
|
|
|
|
api_timeout=dict(type='int', default=os.environ.get('VULTR_API_TIMEOUT')),
|
|
|
|
api_timeout=dict(type='int', default=os.environ.get('VULTR_API_TIMEOUT')),
|
|
|
|
api_retries=dict(type='int', default=os.environ.get('VULTR_API_RETRIES')),
|
|
|
|
api_retries=dict(type='int', default=os.environ.get('VULTR_API_RETRIES')),
|
|
|
|
api_account=dict(default=os.environ.get('VULTR_API_ACCOUNT') or 'default'),
|
|
|
|
api_account=dict(default=os.environ.get('VULTR_API_ACCOUNT') or 'default'),
|
|
|
|
|
|
|
|
api_endpoint=dict(default=os.environ.get('VULTR_API_ENDPOINT')),
|
|
|
|
validate_certs=dict(default=True, type='bool'),
|
|
|
|
validate_certs=dict(default=True, type='bool'),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -48,16 +49,24 @@ class Vultr:
|
|
|
|
except KeyError:
|
|
|
|
except KeyError:
|
|
|
|
config = {}
|
|
|
|
config = {}
|
|
|
|
|
|
|
|
|
|
|
|
self.api_config = {
|
|
|
|
try:
|
|
|
|
'api_key': self.module.params.get('api_key') or config.get('key'),
|
|
|
|
self.api_config = {
|
|
|
|
'api_timeout': self.module.params.get('api_timeout') or config.get('timeout') or 60,
|
|
|
|
'api_key': self.module.params.get('api_key') or config.get('key'),
|
|
|
|
'api_retries': self.module.params.get('api_retries') or config.get('retries') or 5,
|
|
|
|
'api_timeout': self.module.params.get('api_timeout') or int(config.get('timeout') or 60),
|
|
|
|
}
|
|
|
|
'api_retries': self.module.params.get('api_retries') or int(config.get('retries') or 5),
|
|
|
|
|
|
|
|
'api_endpoint': self.module.params.get('api_endpoint') or config.get('endpoint') or VULTR_API_ENDPOINT,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
|
|
self.fail_json(msg="One of the following settings, "
|
|
|
|
|
|
|
|
"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)))
|
|
|
|
|
|
|
|
|
|
|
|
# Common vultr returns
|
|
|
|
# Common vultr returns
|
|
|
|
self.result['vultr_api'] = {
|
|
|
|
self.result['vultr_api'] = {
|
|
|
|
'api_account': self.module.params.get('api_account'),
|
|
|
|
'api_account': self.module.params.get('api_account'),
|
|
|
|
'api_timeout': self.api_config['api_timeout'],
|
|
|
|
'api_timeout': self.api_config['api_timeout'],
|
|
|
|
|
|
|
|
'api_retries': self.api_config['api_retries'],
|
|
|
|
|
|
|
|
'api_endpoint': self.api_config['api_endpoint'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Headers to be passed to the API
|
|
|
|
# Headers to be passed to the API
|
|
|
@ -70,7 +79,7 @@ class Vultr:
|
|
|
|
def read_ini_config(self):
|
|
|
|
def read_ini_config(self):
|
|
|
|
ini_group = self.module.params.get('api_account')
|
|
|
|
ini_group = self.module.params.get('api_account')
|
|
|
|
|
|
|
|
|
|
|
|
keys = ['key', 'timeout', 'retries']
|
|
|
|
keys = ['key', 'timeout', 'retries', 'endpoint']
|
|
|
|
env_conf = {}
|
|
|
|
env_conf = {}
|
|
|
|
for key in keys:
|
|
|
|
for key in keys:
|
|
|
|
if 'VULTR_API_%s' % key.upper() not in os.environ:
|
|
|
|
if 'VULTR_API_%s' % key.upper() not in os.environ:
|
|
|
@ -122,7 +131,7 @@ class Vultr:
|
|
|
|
return "disable"
|
|
|
|
return "disable"
|
|
|
|
|
|
|
|
|
|
|
|
def api_query(self, path="/", method="GET", data=None):
|
|
|
|
def api_query(self, path="/", method="GET", data=None):
|
|
|
|
url = VULTR_API_ENDPOINT + path
|
|
|
|
url = self.api_config['api_endpoint'] + path
|
|
|
|
|
|
|
|
|
|
|
|
if data:
|
|
|
|
if data:
|
|
|
|
data_encoded = dict()
|
|
|
|
data_encoded = dict()
|
|
|
|