From 4a2fba15da4f8fde5b3ed1273a7883f50fd4a14c Mon Sep 17 00:00:00 2001 From: Omar Sanchez Date: Fri, 17 Apr 2020 16:53:59 -0400 Subject: [PATCH 1/5] removed whitespaces and tabs of each file --- contrib/inventory/conf.sample.ini | 2 +- contrib/inventory/d42_ansible_dynamic_inventory.py | 2 +- contrib/inventory/d42_ansible_inventory_hostfile.py | 2 +- galaxy.yml | 2 +- plugins/inventory/d42.py | 5 +---- plugins/lookup/d42.py | 2 +- plugins/lookup/d42_prompt.py | 2 +- 7 files changed, 7 insertions(+), 10 deletions(-) diff --git a/contrib/inventory/conf.sample.ini b/contrib/inventory/conf.sample.ini index 5f11c8b..9671518 100644 --- a/contrib/inventory/conf.sample.ini +++ b/contrib/inventory/conf.sample.ini @@ -10,4 +10,4 @@ D42_SKIP_SSL_CHECK = True GROUP_BY_QUERY = select name, service_level from view_device_v1 GROUP_BY_FIELD = service_level GROUP_BY_REFERENCE_FIELD = name -SPLIT_GROUP_BY_COMMA = False +SPLIT_GROUP_BY_COMMA = False \ No newline at end of file diff --git a/contrib/inventory/d42_ansible_dynamic_inventory.py b/contrib/inventory/d42_ansible_dynamic_inventory.py index 7b80928..fb0fa45 100755 --- a/contrib/inventory/d42_ansible_dynamic_inventory.py +++ b/contrib/inventory/d42_ansible_dynamic_inventory.py @@ -39,4 +39,4 @@ def inventory(self): def empty_inventory(self): return {'_meta': {'hostvars': {}}} -Inventory() +Inventory() \ No newline at end of file diff --git a/contrib/inventory/d42_ansible_inventory_hostfile.py b/contrib/inventory/d42_ansible_inventory_hostfile.py index da68892..1357f57 100644 --- a/contrib/inventory/d42_ansible_inventory_hostfile.py +++ b/contrib/inventory/d42_ansible_inventory_hostfile.py @@ -15,4 +15,4 @@ else: print('[!] Can\'t write to file') - sys.exit() + sys.exit() \ No newline at end of file diff --git a/galaxy.yml b/galaxy.yml index 58643ac..349c0c4 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -12,4 +12,4 @@ tags: - device42 - integration - collection -repository: "https://github.com/device42/ansible_device42" +repository: "https://github.com/device42/ansible_device42" \ No newline at end of file diff --git a/plugins/inventory/d42.py b/plugins/inventory/d42.py index 9c47d95..2afaefb 100644 --- a/plugins/inventory/d42.py +++ b/plugins/inventory/d42.py @@ -116,7 +116,4 @@ def parse(self, inventory, loader, path, cache=False): self._add_host_to_composed_groups(self.get_option('groups'), dict(), host_name, strict) self._add_host_to_keyed_groups(self.get_option('keyed_groups'), dict(), host_name, strict) except Exception as e: - print(e) - - - + print(e) \ No newline at end of file diff --git a/plugins/lookup/d42.py b/plugins/lookup/d42.py index a7a267e..712b840 100644 --- a/plugins/lookup/d42.py +++ b/plugins/lookup/d42.py @@ -94,4 +94,4 @@ def runDoql(self, conf, query, output_type): elif output_type == 'list': return resp.text.split('\n') - return self.get_list_from_csv(resp.text) + return self.get_list_from_csv(resp.text) \ No newline at end of file diff --git a/plugins/lookup/d42_prompt.py b/plugins/lookup/d42_prompt.py index 9ff381f..184452e 100644 --- a/plugins/lookup/d42_prompt.py +++ b/plugins/lookup/d42_prompt.py @@ -81,4 +81,4 @@ def runDoql(self, conf, query, output_type): elif output_type == 'list': return resp.text.split('\n') - return self.get_list_from_csv(resp.text) + return self.get_list_from_csv(resp.text) \ No newline at end of file From 08126b9694a9a98a1f4a398f2df7b3a76052a1bf Mon Sep 17 00:00:00 2001 From: Omar Sanchez Date: Fri, 17 Apr 2020 17:31:39 -0400 Subject: [PATCH 2/5] bunch of linter fixes and missing imports added for ansible sanity check --- LICENSE | 2 +- .../d42_ansible_dynamic_inventory.py | 12 +++--- .../d42_ansible_inventory_hostfile.py | 7 ++-- contrib/inventory/lib.py | 9 +++-- plugins/inventory/d42.py | 32 +++++++-------- plugins/lookup/d42.py | 39 ++++++++++--------- plugins/lookup/d42_prompt.py | 33 +++++++++------- 7 files changed, 70 insertions(+), 64 deletions(-) diff --git a/LICENSE b/LICENSE index bef6deb..cabb7c6 100644 --- a/LICENSE +++ b/LICENSE @@ -4,7 +4,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 +http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, diff --git a/contrib/inventory/d42_ansible_dynamic_inventory.py b/contrib/inventory/d42_ansible_dynamic_inventory.py index fb0fa45..05b99c5 100755 --- a/contrib/inventory/d42_ansible_dynamic_inventory.py +++ b/contrib/inventory/d42_ansible_dynamic_inventory.py @@ -1,9 +1,5 @@ -#!/usr/bin/env python - +from __future__ import (absolute_import, division, print_function) import argparse -import sys -from lib import * - from contrib.inventory.lib import get_conf try: @@ -11,6 +7,9 @@ except ImportError: import simplejson as json +__metaclass__ = type + + class Inventory(object): def __init__(self): @@ -39,4 +38,5 @@ def inventory(self): def empty_inventory(self): return {'_meta': {'hostvars': {}}} -Inventory() \ No newline at end of file + +Inventory() diff --git a/contrib/inventory/d42_ansible_inventory_hostfile.py b/contrib/inventory/d42_ansible_inventory_hostfile.py index 1357f57..e91acf6 100644 --- a/contrib/inventory/d42_ansible_inventory_hostfile.py +++ b/contrib/inventory/d42_ansible_inventory_hostfile.py @@ -1,10 +1,9 @@ +from __future__ import (absolute_import, division, print_function) import sys -import json -import requests -from lib import * - from contrib.inventory.lib import get_conf +__metaclass__ = type + if __name__ == '__main__': conf = get_conf() ansible = Ansible(conf) diff --git a/contrib/inventory/lib.py b/contrib/inventory/lib.py index f274000..e213741 100644 --- a/contrib/inventory/lib.py +++ b/contrib/inventory/lib.py @@ -1,18 +1,19 @@ +from __future__ import (absolute_import, division, print_function) from io import StringIO import requests -import codecs import base64 import configparser import csv import sys import os - try: import json except ImportError: import simplejson as json +__metaclass__ = type + def get_conf(): @@ -77,6 +78,7 @@ def get_conf(): } return conf + class Device42: def __init__(self, conf): @@ -151,5 +153,6 @@ def write_inventory_file(groups): return True + if __name__ == "__main__": - print(get_conf()) \ No newline at end of file + print(get_conf()) diff --git a/plugins/inventory/d42.py b/plugins/inventory/d42.py index 2afaefb..d0b4eca 100644 --- a/plugins/inventory/d42.py +++ b/plugins/inventory/d42.py @@ -1,7 +1,6 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, print_function +from __future__ import (absolute_import, division, print_function) +from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable, to_safe_group_name +import requests __metaclass__ = type @@ -61,22 +60,20 @@ separator: '' ''' -from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable, to_safe_group_name -import requests - class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): NAME = 'device42.d42.d42' def verify_file(self, path): - valid = False - if super(InventoryModule, self).verify_file(path): - if path.endswith(('d42.yaml', 'd42.yml')): - valid = True - else: - self.display.vvv('Skipping due to inventory source not ending in "d42.yaml" nor "d42.yml"') - return valid + valid = False + + if super(InventoryModule, self).verify_file(path): + if path.endswith(('d42.yaml', 'd42.yml')): + valid = True + else: + self.display.vvv('Skipping due to inventory source not ending in "d42.yaml" nor "d42.yml"') + return valid def parse(self, inventory, loader, path, cache=False): super(InventoryModule, self).parse(inventory, loader, path) @@ -92,7 +89,8 @@ def parse(self, inventory, loader, path, cache=False): try: objects = [] - response = requests.get(base_url + "/api/1.0/devices/all", auth=(username, password), verify=ssl_check, timeout=10) + response = requests.get(base_url + "/api/1.0/devices/all", auth=(username, password), verify=ssl_check, + timeout=10) print('response code: ' + str(response.status_code)) json_response = response.json() @@ -105,7 +103,7 @@ def parse(self, inventory, loader, path, cache=False): for k in object_.keys(): self.inventory.set_variable(host_name, 'd42_' + k, object_[k]) - if object_['ip_addresses'] != []: + if object_['ip_addresses']: self.inventory.set_variable(host_name, 'ansible_host', object_['ip_addresses'][0]['ip']) self._set_composite_vars( @@ -116,4 +114,4 @@ def parse(self, inventory, loader, path, cache=False): self._add_host_to_composed_groups(self.get_option('groups'), dict(), host_name, strict) self._add_host_to_keyed_groups(self.get_option('keyed_groups'), dict(), host_name, strict) except Exception as e: - print(e) \ No newline at end of file + print(e) diff --git a/plugins/lookup/d42.py b/plugins/lookup/d42.py index 712b840..326cc24 100644 --- a/plugins/lookup/d42.py +++ b/plugins/lookup/d42.py @@ -1,6 +1,13 @@ from ansible.errors import AnsibleError from ansible.plugins.lookup import LookupBase -import json, requests, sys, csv, io, os +import json +import requests +import sys +import csv +import io +import os +from ansible.utils.display import Display +display = Display() if 'D42_SKIP_SSL_CHECK' in os.environ and os.environ['D42_SKIP_SSL_CHECK'] == 'True': requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) @@ -17,11 +24,8 @@ print('Please set D42_PWD environ.') sys.exit() -try: - from __main__ import display -except ImportError: - from ansible.utils.display import Display - display = Display() + +__metaclass__ = type class LookupModule(LookupBase): @@ -30,12 +34,13 @@ class LookupModule(LookupBase): def get_list_from_csv(text): f = io.StringIO(text) output_list = [] - dict_reader = csv.DictReader(f, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True, dialect='excel') + dict_reader = csv.DictReader(f, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True, + dialect='excel') for item in dict_reader: output_list.append(item) if len(output_list) == 1: - output_list = [output_list,] + output_list = [output_list, ] return output_list @@ -47,11 +52,11 @@ def run(self, terms, variables=None, **kwargs): } if terms[1] == "password": - return self.getUserPass(conf, terms[0], terms[2]) + return self.get_user_pass(conf, terms[0], terms[2]) elif terms[1] == "doql": - return self.runDoql(conf, terms[0], terms[2]) + return self.run_doql(conf, terms[0], terms[2]) - def getUserPass(self, conf, device, username): + def get_user_pass(self, conf, device, username): url = conf['D42_URL'] + "/api/1.0/passwords/?plain_text=yes&device=" + device + "&username=" + username resp = requests.request("GET", url, @@ -72,7 +77,7 @@ def getUserPass(self, conf, device, username): else: raise AnsibleError("No password found for user: %s and device: %s" % (username, device)) - def runDoql(self, conf, query, output_type): + def run_doql(self, conf, query, output_type): url = conf['D42_URL'] + "/services/data/v1.0/query/" post_data = { @@ -80,18 +85,14 @@ def runDoql(self, conf, query, output_type): "header": 'yes' if output_type == 'list_dicts' else 'no' } - resp = requests.request("POST", - url, - auth=(conf['D42_USER'], conf['D42_PWD']), - data=post_data, - verify=False) + resp = requests.request("POST", url, auth=(conf['D42_USER'], conf['D42_PWD']), data=post_data, verify=False) if resp.status_code != 200: raise AnsibleError("API Call failed with status code: " + str(resp.status_code)) if output_type == 'string': - return [resp.text.replace('\n', ''),] + return [resp.text.replace('\n', ''), ] elif output_type == 'list': return resp.text.split('\n') - return self.get_list_from_csv(resp.text) \ No newline at end of file + return self.get_list_from_csv(resp.text) diff --git a/plugins/lookup/d42_prompt.py b/plugins/lookup/d42_prompt.py index 184452e..1b0e48b 100644 --- a/plugins/lookup/d42_prompt.py +++ b/plugins/lookup/d42_prompt.py @@ -1,15 +1,19 @@ +from __future__ import (absolute_import, division, print_function) from ansible.errors import AnsibleError from ansible.plugins.lookup import LookupBase -import json, requests, imp, sys, csv, io, os +import json +import requests +import csv +import io +import os if 'D42_SKIP_SSL_CHECK' in os.environ and os.environ['D42_SKIP_SSL_CHECK'] == 'True': requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) -try: - from __main__ import display -except ImportError: - from ansible.utils.display import Display - display = Display() +from ansible.utils.display import Display +display = Display() + +__metaclass__ = type class LookupModule(LookupBase): @@ -18,12 +22,13 @@ class LookupModule(LookupBase): def get_list_from_csv(text): f = io.StringIO(text) output_list = [] - dict_reader = csv.DictReader(f, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True, dialect='excel') + dict_reader = csv.DictReader(f, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True, + dialect='excel') for item in dict_reader: output_list.append(item) if len(output_list) == 1: - output_list = [output_list,] + output_list = [output_list, ] return output_list @@ -34,11 +39,11 @@ def run(self, terms, variables=None, **kwargs): 'D42_PWD': terms[2], } if terms[4] == "password": - return self.getUserPass(conf, terms[3], terms[5]) + return self.get_user_pass(conf, terms[3], terms[5]) elif terms[4] == "doql": - return self.runDoql(conf, terms[3], terms[5]) + return self.run_doql(conf, terms[3], terms[5]) - def getUserPass(self, conf, device, username): + def get_user_pass(self, conf, device, username): url = conf['D42_URL'] + "/api/1.0/passwords/?plain_text=yes&device=" + device + "&username=" + username resp = requests.request("GET", url, @@ -59,7 +64,7 @@ def getUserPass(self, conf, device, username): else: raise AnsibleError("No password found for user: %s and device: %s" % (username, device)) - def runDoql(self, conf, query, output_type): + def run_doql(self, conf, query, output_type): url = conf['D42_URL'] + "/services/data/v1.0/query/" post_data = { @@ -77,8 +82,8 @@ def runDoql(self, conf, query, output_type): raise AnsibleError("API Call failed with status code: " + str(resp.status_code)) if output_type == 'string': - return [resp.text.replace('\n', ''),] + return [resp.text.replace('\n', ''), ] elif output_type == 'list': return resp.text.split('\n') - return self.get_list_from_csv(resp.text) \ No newline at end of file + return self.get_list_from_csv(resp.text) From 9a0eec4f190b1a000d29d666a0f51d2bc5783984 Mon Sep 17 00:00:00 2001 From: Omar Sanchez Date: Fri, 17 Apr 2020 17:49:39 -0400 Subject: [PATCH 3/5] more linter fixes --- contrib/inventory/d42_ansible_dynamic_inventory.py | 2 +- contrib/inventory/d42_ansible_inventory_hostfile.py | 4 ++-- plugins/lookup/d42.py | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/contrib/inventory/d42_ansible_dynamic_inventory.py b/contrib/inventory/d42_ansible_dynamic_inventory.py index 05b99c5..ae90ff1 100755 --- a/contrib/inventory/d42_ansible_dynamic_inventory.py +++ b/contrib/inventory/d42_ansible_dynamic_inventory.py @@ -1,6 +1,6 @@ from __future__ import (absolute_import, division, print_function) import argparse -from contrib.inventory.lib import get_conf +from contrib.inventory.lib import get_conf, Ansible, Device42 try: import json diff --git a/contrib/inventory/d42_ansible_inventory_hostfile.py b/contrib/inventory/d42_ansible_inventory_hostfile.py index e91acf6..c61e015 100644 --- a/contrib/inventory/d42_ansible_inventory_hostfile.py +++ b/contrib/inventory/d42_ansible_inventory_hostfile.py @@ -1,6 +1,6 @@ from __future__ import (absolute_import, division, print_function) import sys -from contrib.inventory.lib import get_conf +from contrib.inventory.lib import get_conf, Ansible, Device42 __metaclass__ = type @@ -14,4 +14,4 @@ else: print('[!] Can\'t write to file') - sys.exit() \ No newline at end of file + sys.exit() diff --git a/plugins/lookup/d42.py b/plugins/lookup/d42.py index 326cc24..f3e1e4e 100644 --- a/plugins/lookup/d42.py +++ b/plugins/lookup/d42.py @@ -1,3 +1,4 @@ +from __future__ import (absolute_import, division, print_function) from ansible.errors import AnsibleError from ansible.plugins.lookup import LookupBase import json From 4b66fe5457d6119b0c3d93c42324cf316aebffd7 Mon Sep 17 00:00:00 2001 From: Omar Sanchez Date: Fri, 17 Apr 2020 17:50:11 -0400 Subject: [PATCH 4/5] upped the version --- galaxy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/galaxy.yml b/galaxy.yml index 349c0c4..03d93b5 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,6 +1,6 @@ namespace: "device42" name: "d42" -version: "1.0.1" +version: "1.0.2" readme: "README.md" authors: - "Will Tome (@willtome)" From e864628fc74d7942407430a7137e722a2032a0b4 Mon Sep 17 00:00:00 2001 From: Omar Sanchez Date: Fri, 17 Apr 2020 18:00:01 -0400 Subject: [PATCH 5/5] moooore linter fixes --- LICENSE | 2 +- plugins/inventory/d42.py | 2 +- plugins/lookup/d42_prompt.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/LICENSE b/LICENSE index cabb7c6..bd64b90 100644 --- a/LICENSE +++ b/LICENSE @@ -10,4 +10,4 @@ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and -limitations under the License. \ No newline at end of file +limitations under the License. diff --git a/plugins/inventory/d42.py b/plugins/inventory/d42.py index d0b4eca..1f95e1c 100644 --- a/plugins/inventory/d42.py +++ b/plugins/inventory/d42.py @@ -75,7 +75,7 @@ def verify_file(self, path): return valid - def parse(self, inventory, loader, path, cache=False): + def parse(self, inventory, loader, path, cache=False): super(InventoryModule, self).parse(inventory, loader, path) self._read_config_data(path) diff --git a/plugins/lookup/d42_prompt.py b/plugins/lookup/d42_prompt.py index 1b0e48b..0847dd1 100644 --- a/plugins/lookup/d42_prompt.py +++ b/plugins/lookup/d42_prompt.py @@ -34,8 +34,8 @@ def get_list_from_csv(text): def run(self, terms, variables=None, **kwargs): conf = { - 'D42_URL': terms[0], - 'D42_USER': terms[1], + 'D42_URL': terms[0], + 'D42_USER': terms[1], 'D42_PWD': terms[2], } if terms[4] == "password":