From d26c57f9388a68b913507119a2a9030a4eeaf32c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Sat, 25 Feb 2017 10:32:39 +0100 Subject: [PATCH] exoscale: pep8 and python3 fixes (#21821) * exoscale: pep8 fixes * exo_dns_record: remove from pep8 test legacy * exoscale: fix python3 support --- lib/ansible/module_utils/exoscale.py | 58 +++++++++---------- .../network/exoscale/exo_dns_domain.py | 10 ++-- .../network/exoscale/exo_dns_record.py | 31 ++++++++-- test/sanity/pep8/legacy-files.txt | 1 - 4 files changed, 59 insertions(+), 41 deletions(-) diff --git a/lib/ansible/module_utils/exoscale.py b/lib/ansible/module_utils/exoscale.py index a2c21fffd07..b319396fcf4 100644 --- a/lib/ansible/module_utils/exoscale.py +++ b/lib/ansible/module_utils/exoscale.py @@ -27,23 +27,17 @@ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -try: - import json -except ImportError: - try: - import simplejson as json - except ImportError: - # Let snippet from module_utils/basic.py return a proper error in this case - pass - import os # import module snippets from ansible.module_utils.pycompat24 import get_exception from ansible.module_utils.six.moves import configparser +from ansible.module_utils.six import integer_types, string_types +from ansible.module_utils._text import to_text from ansible.module_utils.urls import fetch_url -EXO_DNS_BASEURL="https://api.exoscale.ch/dns/v1" +EXO_DNS_BASEURL = "https://api.exoscale.ch/dns/v1" + def exo_dns_argument_spec(): return dict( @@ -54,6 +48,7 @@ def exo_dns_argument_spec(): validate_certs=dict(default='yes', type='bool'), ) + def exo_dns_required_together(): return [['api_key', 'api_secret']] @@ -73,7 +68,7 @@ class ExoDns(object): self.api_secret = config['secret'] except Exception: e = get_exception() - self.module.fail_json(msg=str(e)) + self.module.fail_json(msg="Error while processing config: %s" % e) self.headers = { 'X-DNS-Token': "%s:%s" % (self.api_key, self.api_secret), @@ -95,10 +90,10 @@ class ExoDns(object): keys = ['key', 'secret'] env_conf = {} for key in keys: - if 'CLOUDSTACK_{0}'.format(key.upper()) not in os.environ: + if 'CLOUDSTACK_%s' % key.upper() not in os.environ: break else: - env_conf[key] = os.environ['CLOUDSTACK_{0}'.format(key.upper())] + env_conf[key] = os.environ['CLOUDSTACK_%s' % key.upper()] else: return env_conf @@ -112,8 +107,7 @@ class ExoDns(object): if 'CLOUDSTACK_CONFIG' in os.environ: paths += (os.path.expanduser(os.environ['CLOUDSTACK_CONFIG']),) if not any([os.path.exists(c) for c in paths]): - raise SystemExit("Config file not found. Tried {0}".format( - ", ".join(paths))) + self.module.fail_json(msg="Config file not found. Tried : %s" % ", ".join(paths)) conf = configparser.ConfigParser() conf.read(paths) @@ -122,24 +116,26 @@ class ExoDns(object): def api_query(self, resource="/domains", method="GET", data=None): url = EXO_DNS_BASEURL + resource if data: - data = json.dumps(data) + data = self.module.jsonify(data) response, info = fetch_url( - module = self.module, - url = url, - data = data, - method = method, - headers = self.headers, - timeout = self.module.params.get('api_timeout'), + module=self.module, + url=url, + data=data, + method=method, + headers=self.headers, + timeout=self.module.params.get('api_timeout'), ) if info['status'] not in (200, 201, 204): self.module.fail_json(msg="%s returned %s, with body: %s" % (url, info['status'], info['msg'])) try: - return json.load(response) + return self.module.from_json(to_text(response.read())) + except Exception: - return {} + e = get_exception() + self.module.fail_json(msg="Could not process response into json: %s" % e) def has_changed(self, want_dict, current_dict, only_keys=None): changed = False @@ -151,17 +147,19 @@ class ExoDns(object): if value is None: continue if key in current_dict: - if isinstance(current_dict[key], (int, long, float, complex)): + if isinstance(current_dict[key], integer_types): if value != current_dict[key]: self.result['diff']['before'][key] = current_dict[key] self.result['diff']['after'][key] = value changed = True - elif value.lower() != current_dict[key].encode('utf-8').lower(): - self.result['diff']['before'][key] = current_dict[key] - self.result['diff']['after'][key] = value - changed = True + elif isinstance(current_dict[key], string_types): + if value.lower() != current_dict[key].lower(): + self.result['diff']['before'][key] = current_dict[key] + self.result['diff']['after'][key] = value + changed = True + else: + self.module.fail_json(msg="Unable to determine comparison for key %s" % key) else: self.result['diff']['after'][key] = value changed = True return changed - diff --git a/lib/ansible/modules/network/exoscale/exo_dns_domain.py b/lib/ansible/modules/network/exoscale/exo_dns_domain.py index b0046c803dc..3dd4344f0b6 100644 --- a/lib/ansible/modules/network/exoscale/exo_dns_domain.py +++ b/lib/ansible/modules/network/exoscale/exo_dns_domain.py @@ -184,8 +184,12 @@ exo_dns_domain: sample: false ''' -# import exoscale common -from ansible.module_utils.exoscale import * +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.exoscale import ( + ExoDns, + exo_dns_argument_spec, + exo_dns_required_together +) class ExoDnsDomain(ExoDns): @@ -252,8 +256,6 @@ def main(): module.exit_json(**result) -# import module snippets -from ansible.module_utils.basic import * if __name__ == '__main__': main() diff --git a/lib/ansible/modules/network/exoscale/exo_dns_record.py b/lib/ansible/modules/network/exoscale/exo_dns_record.py index 960cbf871dc..affd1f5226f 100644 --- a/lib/ansible/modules/network/exoscale/exo_dns_record.py +++ b/lib/ansible/modules/network/exoscale/exo_dns_record.py @@ -244,8 +244,30 @@ exo_dns_record: sample: "2016-08-12T15:24:23.989Z" ''' -# import exoscale common -from ansible.module_utils.exoscale import * +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.exoscale import ( + ExoDns, + exo_dns_argument_spec, + exo_dns_required_together +) + +EXO_RECORD_TYPES = [ + 'A', + 'ALIAS', + 'CNAME', + 'MX', + 'SPF', + 'URL', + 'TXT', + 'NS', + 'SRV', + 'NAPTR', + 'PTR', + 'AAAA', + 'SSHFP', + 'HINFO', + 'POOL' +] class ExoDnsRecord(ExoDns): @@ -267,7 +289,6 @@ class ExoDnsRecord(ExoDns): if self.multiple and self.record_type != 'A': self.module.fail_json("Multiple is only usable with record_type A") - def _create_record(self, record): self.result['changed'] = True data = { @@ -357,7 +378,7 @@ def main(): argument_spec = exo_dns_argument_spec() argument_spec.update(dict( name=dict(default=""), - record_type=dict(choices=['A', 'ALIAS', 'CNAME', 'MX', 'SPF', 'URL', 'TXT', 'NS', 'SRV', 'NAPTR', 'PTR', 'AAAA', 'SSHFP', 'HINFO', 'POOL'], aliases=['rtype', 'type'], default='A'), + record_type=dict(choices=EXO_RECORD_TYPES, aliases=['rtype', 'type'], default='A'), content=dict(aliases=['value', 'address']), multiple=(dict(type='bool', default=False)), ttl=dict(type='int', default=3600), @@ -388,8 +409,6 @@ def main(): result = exo_dns_record.get_result(resource) module.exit_json(**result) -# import module snippets -from ansible.module_utils.basic import * if __name__ == '__main__': main() diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index c668c208259..4d8e57829e9 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -137,7 +137,6 @@ lib/ansible/modules/network/basics/slurp.py lib/ansible/modules/network/cloudflare_dns.py lib/ansible/modules/network/dnsimple.py lib/ansible/modules/network/dnsmadeeasy.py -lib/ansible/modules/network/exoscale/exo_dns_record.py lib/ansible/modules/network/f5/bigip_facts.py lib/ansible/modules/network/f5/bigip_monitor_tcp.py lib/ansible/modules/network/f5/bigip_node.py