vultr: add api_endpoint param (#34850)

* vultr: add new param api_endpoint

* vultr: improve error handling for values read from ini file
This commit is contained in:
René Moser 2018-01-14 16:31:28 +01:00 committed by GitHub
parent 5a7a5b88c9
commit 698d5a524c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 102 additions and 8 deletions

View file

@ -9,7 +9,7 @@ import os
import time
import urllib
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
@ -22,6 +22,7 @@ def vultr_argument_spec():
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_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'),
)
@ -48,16 +49,24 @@ class Vultr:
except KeyError:
config = {}
self.api_config = {
'api_key': self.module.params.get('api_key') or config.get('key'),
'api_timeout': self.module.params.get('api_timeout') or config.get('timeout') or 60,
'api_retries': self.module.params.get('api_retries') or config.get('retries') or 5,
}
try:
self.api_config = {
'api_key': self.module.params.get('api_key') or config.get('key'),
'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
self.result['vultr_api'] = {
'api_account': self.module.params.get('api_account'),
'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
@ -70,7 +79,7 @@ class Vultr:
def read_ini_config(self):
ini_group = self.module.params.get('api_account')
keys = ['key', 'timeout', 'retries']
keys = ['key', 'timeout', 'retries', 'endpoint']
env_conf = {}
for key in keys:
if 'VULTR_API_%s' % key.upper() not in os.environ:
@ -122,7 +131,7 @@ class Vultr:
return "disable"
def api_query(self, path="/", method="GET", data=None):
url = VULTR_API_ENDPOINT + path
url = self.api_config['api_endpoint'] + path
if data:
data_encoded = dict()

View file

@ -49,6 +49,16 @@ vultr_api:
returned: success
type: int
sample: 60
api_retries:
description: Amount of max retries for the API requests
returned: success
type: int
sample: 5
api_endpoint:
description: Endpoint used for the API requests
returned: success
type: string
sample: "https://api.vultr.com"
vultr_account_facts:
description: Response from Vultr API
returned: success

View file

@ -69,6 +69,16 @@ vultr_api:
returned: success
type: int
sample: 60
api_retries:
description: Amount of max retries for the API requests
returned: success
type: int
sample: 5
api_endpoint:
description: Endpoint used for the API requests
returned: success
type: string
sample: "https://api.vultr.com"
vultr_dns_domain:
description: Response from Vultr API
returned: success

View file

@ -63,6 +63,16 @@ vultr_api:
returned: success
type: int
sample: 60
api_retries:
description: Amount of max retries for the API requests
returned: success
type: int
sample: 5
api_endpoint:
description: Endpoint used for the API requests
returned: success
type: string
sample: "https://api.vultr.com"
vultr_firewall_group:
description: Response from Vultr API
returned: success

View file

@ -111,6 +111,16 @@ vultr_api:
returned: success
type: int
sample: 60
api_retries:
description: Amount of max retries for the API requests
returned: success
type: int
sample: 5
api_endpoint:
description: Endpoint used for the API requests
returned: success
type: string
sample: "https://api.vultr.com"
vultr_firewall_rule:
description: Response from Vultr API
returned: success

View file

@ -155,6 +155,16 @@ vultr_api:
returned: success
type: int
sample: 60
api_retries:
description: Amount of max retries for the API requests
returned: success
type: int
sample: 5
api_endpoint:
description: Endpoint used for the API requests
returned: success
type: string
sample: "https://api.vultr.com"
vultr_server:
description: Response from Vultr API with a few additions/modification
returned: success

View file

@ -68,6 +68,16 @@ vultr_api:
returned: success
type: int
sample: 60
api_retries:
description: Amount of max retries for the API requests
returned: success
type: int
sample: 5
api_endpoint:
description: Endpoint used for the API requests
returned: success
type: string
sample: "https://api.vultr.com"
vultr_ssh_key:
description: Response from Vultr API
returned: success

View file

@ -80,6 +80,16 @@ vultr_api:
returned: success
type: int
sample: 60
api_retries:
description: Amount of max retries for the API requests
returned: success
type: int
sample: 5
api_endpoint:
description: Endpoint used for the API requests
returned: success
type: string
sample: "https://api.vultr.com"
vultr_startup_script:
description: Response from Vultr API
returned: success

View file

@ -102,6 +102,16 @@ vultr_api:
returned: success
type: int
sample: 60
api_retries:
description: Amount of max retries for the API requests
returned: success
type: int
sample: 5
api_endpoint:
description: Endpoint used for the API requests
returned: success
type: string
sample: "https://api.vultr.com"
vultr_user:
description: Response from Vultr API
returned: success

View file

@ -27,6 +27,11 @@ options:
- Name of the ini section in the C(vultr.ini) file.
- The ENV variable C(VULTR_API_ACCOUNT) is used as default, when defined.
default: default
api_endpoint:
description:
- URL to API endpint (without trailing slash).
- The ENV variable C(VULTR_API_ENDPOINT) is used as default, when defined.
default: "https://api.vultr.com"
validate_certs:
description:
- Validate SSL certs of the Vultr API.