From 29cfebe3320ec8f3392e00c4dba38cfe391f637a Mon Sep 17 00:00:00 2001 From: Yanis Guenane Date: Tue, 14 Aug 2018 23:05:57 +0200 Subject: [PATCH] scaleway: Create a base class for all the modules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Purpose of this commit is to create a base class for the upcoming scaleway module to use. Co-authored-by: Rémy Leone --- lib/ansible/module_utils/scaleway.py | 85 +++++++++++++++++++ .../utils/module_docs_fragments/scaleway.py | 18 ++-- 2 files changed, 98 insertions(+), 5 deletions(-) diff --git a/lib/ansible/module_utils/scaleway.py b/lib/ansible/module_utils/scaleway.py index caf6886f26..2ecb7c94e8 100644 --- a/lib/ansible/module_utils/scaleway.py +++ b/lib/ansible/module_utils/scaleway.py @@ -1,9 +1,26 @@ import json import sys +from ansible.module_utils.basic import env_fallback from ansible.module_utils.urls import fetch_url +def scaleway_argument_spec(): + return dict( + api_token=dict(required=True, fallback=(env_fallback, ['SCW_TOKEN', 'SCW_API_KEY', 'SCW_OAUTH_TOKEN', 'SCW_API_TOKEN']), + no_log=True, aliases=['oauth_token']), + api_url=dict(fallback=(env_fallback, ['SCW_API_URL']), default='https://api.scaleway.com', aliases=['base_url']), + api_timeout=dict(type='int', default=30, aliases=['timeout']), + validate_certs=dict(default=True, type='bool'), + ) + + +class ScalewayException(Exception): + + def __init__(self, message): + self.message = message + + class Response(object): def __init__(self, resp, info): @@ -32,6 +49,74 @@ class Response(object): return self.status_code in (200, 201, 202, 204) +class Scaleway(object): + + def __init__(self, module): + self.module = module + self.headers = { + 'X-Auth-Token': self.module.params.get('api_token'), + 'User-Agent': self.get_user_agent_string(module), + 'Content-type': 'application/json', + } + self.name = None + + def get_resources(self): + results = self.get('/%s' % self.name) + + if not results.ok: + raise ScalewayException('Error fetching {0} ({1}) [{2}: {3}]'.format( + self.name, '%s/%s' % (self.module.params.get('api_url'), self.name), + results.status_code, results.json['message'] + )) + + return results.json.get(self.name) + + def _url_builder(self, path): + if path[0] == '/': + path = path[1:] + return '%s/%s' % (self.module.params.get('api_url'), path) + + def send(self, method, path, data=None, headers=None): + url = self._url_builder(path) + data = self.module.jsonify(data) + + if headers is not None: + self.headers.update(headers) + + resp, info = fetch_url( + self.module, url, data=data, headers=self.headers, method=method, + timeout=self.module.params.get('api_timeout') + ) + + # Exceptions in fetch_url may result in a status -1, the ensures a proper error to the user in all cases + if info['status'] == -1: + self.module.fail_json(msg=info['msg']) + + return Response(resp, info) + + @staticmethod + def get_user_agent_string(module): + return "ansible %s Python %s" % (module.ansible_version, sys.version.split(' ')[0]) + + def get(self, path, data=None, headers=None): + return self.send('GET', path, data, headers) + + def put(self, path, data=None, headers=None): + return self.send('PUT', path, data, headers) + + def post(self, path, data=None, headers=None): + return self.send('POST', path, data, headers) + + def delete(self, path, data=None, headers=None): + return self.send('DELETE', path, data, headers) + + def patch(self, path, data=None, headers=None): + return self.send("PATCH", path, data, headers) + + def update(self, path, data=None, headers=None): + return self.send("UPDATE", path, data, headers) + + class ScalewayAPI(object): def __init__(self, module, base_url, headers=None): diff --git a/lib/ansible/utils/module_docs_fragments/scaleway.py b/lib/ansible/utils/module_docs_fragments/scaleway.py index bc34cc1927..69b1369529 100644 --- a/lib/ansible/utils/module_docs_fragments/scaleway.py +++ b/lib/ansible/utils/module_docs_fragments/scaleway.py @@ -8,14 +8,20 @@ class ModuleDocFragment(object): # Standard documentation fragment DOCUMENTATION = ''' options: - oauth_token: + api_token: description: - Scaleway OAuth token. - aliases: ['api_token'] - timeout: + aliases: ['oauth_token'] + api_url: + description: + - Scaleway API URL + default: 'https://api.scaleway.com' + aliases: ['base_url'] + api_timeout: description: - HTTP timeout to Scaleway API in seconds. default: 30 + aliases: ['timeout'] validate_certs: description: - Validate SSL certs of the Scaleway API. @@ -23,7 +29,9 @@ options: type: bool notes: - Also see the API documentation on U(https://developer.scaleway.com/) - - If parameters are not set within the module, the following + - If C(api_token) is not set within the module, the following environment variables can be used in decreasing order of precedence - C(SCW_TOKEN), C(SCW_API_KEY) or C(SCW_OAUTH_TOKEN). + C(SCW_TOKEN), C(SCW_API_KEY), C(SCW_OAUTH_TOKEN) or C(SCW_API_TOKEN). + - If one wants to use a different C(api_url) one can also set the C(SCW_API_URL) + environment variable. '''