From 698d5a524cd1d4dcd9a0f97bca75d1120c6d2599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Sun, 14 Jan 2018 16:31:28 +0100 Subject: [PATCH] vultr: add api_endpoint param (#34850) * vultr: add new param api_endpoint * vultr: improve error handling for values read from ini file --- lib/ansible/module_utils/vultr.py | 25 +++++++++++++------ .../modules/cloud/vultr/vr_account_facts.py | 10 ++++++++ .../modules/cloud/vultr/vr_dns_domain.py | 10 ++++++++ .../modules/cloud/vultr/vr_firewall_group.py | 10 ++++++++ .../modules/cloud/vultr/vr_firewall_rule.py | 10 ++++++++ lib/ansible/modules/cloud/vultr/vr_server.py | 10 ++++++++ lib/ansible/modules/cloud/vultr/vr_ssh_key.py | 10 ++++++++ .../modules/cloud/vultr/vr_startup_script.py | 10 ++++++++ lib/ansible/modules/cloud/vultr/vr_user.py | 10 ++++++++ .../utils/module_docs_fragments/vultr.py | 5 ++++ 10 files changed, 102 insertions(+), 8 deletions(-) diff --git a/lib/ansible/module_utils/vultr.py b/lib/ansible/module_utils/vultr.py index 9085ed2b26..7beb4fa3c7 100644 --- a/lib/ansible/module_utils/vultr.py +++ b/lib/ansible/module_utils/vultr.py @@ -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() diff --git a/lib/ansible/modules/cloud/vultr/vr_account_facts.py b/lib/ansible/modules/cloud/vultr/vr_account_facts.py index d853f522e4..e05aa19dc6 100644 --- a/lib/ansible/modules/cloud/vultr/vr_account_facts.py +++ b/lib/ansible/modules/cloud/vultr/vr_account_facts.py @@ -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 diff --git a/lib/ansible/modules/cloud/vultr/vr_dns_domain.py b/lib/ansible/modules/cloud/vultr/vr_dns_domain.py index aa57149329..8e584f855b 100644 --- a/lib/ansible/modules/cloud/vultr/vr_dns_domain.py +++ b/lib/ansible/modules/cloud/vultr/vr_dns_domain.py @@ -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 diff --git a/lib/ansible/modules/cloud/vultr/vr_firewall_group.py b/lib/ansible/modules/cloud/vultr/vr_firewall_group.py index 92cdaaf251..503e38cc3f 100644 --- a/lib/ansible/modules/cloud/vultr/vr_firewall_group.py +++ b/lib/ansible/modules/cloud/vultr/vr_firewall_group.py @@ -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 diff --git a/lib/ansible/modules/cloud/vultr/vr_firewall_rule.py b/lib/ansible/modules/cloud/vultr/vr_firewall_rule.py index fa6fad7337..d3052b7491 100644 --- a/lib/ansible/modules/cloud/vultr/vr_firewall_rule.py +++ b/lib/ansible/modules/cloud/vultr/vr_firewall_rule.py @@ -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 diff --git a/lib/ansible/modules/cloud/vultr/vr_server.py b/lib/ansible/modules/cloud/vultr/vr_server.py index 7332c20243..7e0df4dd8b 100644 --- a/lib/ansible/modules/cloud/vultr/vr_server.py +++ b/lib/ansible/modules/cloud/vultr/vr_server.py @@ -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 diff --git a/lib/ansible/modules/cloud/vultr/vr_ssh_key.py b/lib/ansible/modules/cloud/vultr/vr_ssh_key.py index 460683dac0..583a76af42 100644 --- a/lib/ansible/modules/cloud/vultr/vr_ssh_key.py +++ b/lib/ansible/modules/cloud/vultr/vr_ssh_key.py @@ -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 diff --git a/lib/ansible/modules/cloud/vultr/vr_startup_script.py b/lib/ansible/modules/cloud/vultr/vr_startup_script.py index 3e5c7c060e..4ad8a8710b 100644 --- a/lib/ansible/modules/cloud/vultr/vr_startup_script.py +++ b/lib/ansible/modules/cloud/vultr/vr_startup_script.py @@ -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 diff --git a/lib/ansible/modules/cloud/vultr/vr_user.py b/lib/ansible/modules/cloud/vultr/vr_user.py index 1aea4d5c0a..6f10a17b7a 100644 --- a/lib/ansible/modules/cloud/vultr/vr_user.py +++ b/lib/ansible/modules/cloud/vultr/vr_user.py @@ -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 diff --git a/lib/ansible/utils/module_docs_fragments/vultr.py b/lib/ansible/utils/module_docs_fragments/vultr.py index 5e1a6f4722..1b8cd8845d 100644 --- a/lib/ansible/utils/module_docs_fragments/vultr.py +++ b/lib/ansible/utils/module_docs_fragments/vultr.py @@ -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.