From 10842cbd0829e37eaf8d6a0e73c567d52d6eab55 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Tue, 23 Aug 2022 13:40:04 +0200 Subject: [PATCH] Fix indent-errors in Python Plugins - skip files with python2 compatibility --- .pylintrc | 2 +- plugins/action/icinga2_object.py | 269 +++++++++--------- plugins/filter/prefix.py | 1 + plugins/lookup/icinga2_parser.py | 3 +- plugins/module_utils/parse.py | 1 + .../modules/icinga2_elasticsearchwriter.py | 52 ++-- plugins/modules/icinga2_gelfwriter.py | 56 ++-- plugins/modules/icinga2_icingadb.py | 62 ++-- plugins/modules/icinga2_influxdb2writer.py | 40 ++- plugins/modules/icinga2_opentsdbwriter.py | 48 ++-- plugins/modules/icinga2_perfdatawriter.py | 52 ++-- 11 files changed, 289 insertions(+), 297 deletions(-) diff --git a/.pylintrc b/.pylintrc index 34f88908..8b4acf8d 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,6 +1,6 @@ # pylint config [FORMAT] -good-names=r +good-names=n,x,s,op,i,d,l,r,__metaclass__ [MESSAGES CONTROL] disable=fixme, duplicate-code, diff --git a/plugins/action/icinga2_object.py b/plugins/action/icinga2_object.py index e36568e0..6e7a0293 100644 --- a/plugins/action/icinga2_object.py +++ b/plugins/action/icinga2_object.py @@ -1,6 +1,7 @@ +# pylint: disable=consider-using-f-string,super-with-arguments import re -from ansible.errors import AnsibleError, AnsibleFileNotFound +from ansible.errors import AnsibleError from ansible.plugins.action import ActionBase from ansible.utils.vars import merge_hash from ansible_collections.icinga.icinga.plugins.module_utils.parse import Icinga2Parser @@ -8,136 +9,136 @@ class ActionModule(ActionBase): - def run(self, tmp=None, task_vars=None): - - result = super(ActionModule, self).run(tmp, task_vars) - - args = dict() - args = self._task.args.copy() - args = merge_hash(args.pop('args', {}), args) - object_type = args.pop('type', None) - - if object_type not in task_vars['icinga2_object_types']: - raise AnsibleError('unknown Icinga object type: %s' % object_type) - - # - # distribute to object type as module (name: icinga2_type) - # - obj = dict() - obj = self._execute_module( - module_name='icinga2_'+object_type.lower(), - module_args=args, - task_vars=task_vars, - tmp=tmp - ) - - if 'failed' in obj: - raise AnsibleError('Call to module failed: %s' % obj['msg']) - elif 'skipped' in obj and obj['skipped']: - raise AnsibleError('Call to module was skipped: %s' % obj['msg']) - - # - # file path handling for assemble - # - path = task_vars['icinga2_fragments_path'] + '/' + obj['file'] + '/' - file_fragment = path + obj['order'] + '_' + object_type.lower() + '-' + obj['name'] - - file_args = dict() - file_args['state'] = 'directory' - file_args['path'] = path - file_module = self._execute_module( - module_name='file', - module_args=file_args, - task_vars=task_vars, - tmp=tmp - ) - result = merge_hash(result, file_module) - - if obj['state'] != 'absent': - varlist = list() # list of variables from 'apply for' - - # - # quoting of object name? - # - if obj['name'] not in task_vars['icinga2_combined_constants']: - object_name = '"' + obj['name'] + '"' - else: - object_name = obj['name'] - - # - # apply rule? - # - if 'apply' in obj and obj['apply'] and not obj['args']['assign']: - raise AnsibleError('Apply rule %s is missing the assign rule.' % obj['name']) - if 'apply' in obj and obj['apply']: - object_content = 'apply ' + object_type - if 'apply_target' in obj and obj['apply_target']: - object_content += ' ' + object_name + ' to ' + obj['apply_target'] - elif 'apply_for' in obj and obj['apply_for']: - object_content += ' for (' + obj['apply_for'] + ') ' - r = re.search(r'^(.+)\s+in\s+', obj['apply_for']) - if r: - tmp = r.group(1).strip() - r = re.search(r'^(.+)=>(.+)$', tmp) - if r: - varlist.extend([r.group(1).strip(), r.group(2).strip()]) - else: - varlist.append(tmp) - else: - object_content += ' ' + object_name - # - # template? - # - elif 'template' in obj and obj['template']: - object_content = 'template ' + object_type + ' ' + object_name - # - # object - # - else: - object_content = 'object ' + object_type + ' ' + object_name - object_content += ' {\n' - - # - # imports? - # - if 'imports' in obj: - for item in obj['imports']: - object_content += ' import "' + str(item) + '"\n' - object_content += '\n' - - # - # parser - # - object_content += Icinga2Parser().parse(obj['args'], list(task_vars['icinga2_combined_constants'].keys())+task_vars['icinga2_reserved']+varlist+list(obj['args'].keys()), 2) + '}\n' - copy_action = self._task.copy() - copy_action.args = dict() - copy_action.args['dest'] = file_fragment - copy_action.args['content'] = object_content - - copy_action = self._shared_loader_obj.action_loader.get( - 'copy', - task=copy_action, - connection=self._connection, - play_context=self._play_context, - loader=self._loader, - templar=self._templar, - shared_loader_obj=self._shared_loader_obj - ) - - result = merge_hash(result, copy_action.run(task_vars=task_vars)) - else: - # remove file if does not belong to a feature - if 'features-available' not in path: - file_args = dict() - file_args['state'] = 'absent' - file_args['path'] = file_fragment - file_module = self._execute_module( - module_name='file', - module_args=file_args, - task_vars=task_vars, - tmp=tmp - ) - result = merge_hash(result, file_module) - result['dest'] = file_fragment - - return result + def run(self, tmp=None, task_vars=None): + + result = super(ActionModule, self).run(tmp, task_vars) + + args = dict() + args = self._task.args.copy() + args = merge_hash(args.pop('args', {}), args) + object_type = args.pop('type', None) + + if object_type not in task_vars['icinga2_object_types']: + raise AnsibleError('unknown Icinga object type: %s' % object_type) + + # + # distribute to object type as module (name: icinga2_type) + # + obj = dict() + obj = self._execute_module( + module_name='icinga2_'+object_type.lower(), + module_args=args, + task_vars=task_vars, + tmp=tmp + ) + + if 'failed' in obj: + raise AnsibleError('Call to module failed: %s' % obj['msg']) + if 'skipped' in obj and obj['skipped']: + raise AnsibleError('Call to module was skipped: %s' % obj['msg']) + + # + # file path handling for assemble + # + path = task_vars['icinga2_fragments_path'] + '/' + obj['file'] + '/' + file_fragment = path + obj['order'] + '_' + object_type.lower() + '-' + obj['name'] + + file_args = dict() + file_args['state'] = 'directory' + file_args['path'] = path + file_module = self._execute_module( + module_name='file', + module_args=file_args, + task_vars=task_vars, + tmp=tmp + ) + result = merge_hash(result, file_module) + + if obj['state'] != 'absent': + varlist = list() # list of variables from 'apply for' + + # + # quoting of object name? + # + if obj['name'] not in task_vars['icinga2_combined_constants']: + object_name = '"' + obj['name'] + '"' + else: + object_name = obj['name'] + + # + # apply rule? + # + if 'apply' in obj and obj['apply'] and not obj['args']['assign']: + raise AnsibleError('Apply rule %s is missing the assign rule.' % obj['name']) + if 'apply' in obj and obj['apply']: + object_content = 'apply ' + object_type + if 'apply_target' in obj and obj['apply_target']: + object_content += ' ' + object_name + ' to ' + obj['apply_target'] + elif 'apply_for' in obj and obj['apply_for']: + object_content += ' for (' + obj['apply_for'] + ') ' + r = re.search(r'^(.+)\s+in\s+', obj['apply_for']) + if r: + tmp = r.group(1).strip() + r = re.search(r'^(.+)=>(.+)$', tmp) + if r: + varlist.extend([r.group(1).strip(), r.group(2).strip()]) + else: + varlist.append(tmp) + else: + object_content += ' ' + object_name + # + # template? + # + elif 'template' in obj and obj['template']: + object_content = 'template ' + object_type + ' ' + object_name + # + # object + # + else: + object_content = 'object ' + object_type + ' ' + object_name + object_content += ' {\n' + + # + # imports? + # + if 'imports' in obj: + for item in obj['imports']: + object_content += ' import "' + str(item) + '"\n' + object_content += '\n' + + # + # parser + # + object_content += Icinga2Parser().parse(obj['args'], list(task_vars['icinga2_combined_constants'].keys())+task_vars['icinga2_reserved']+varlist+list(obj['args'].keys()), 2) + '}\n' + copy_action = self._task.copy() + copy_action.args = dict() + copy_action.args['dest'] = file_fragment + copy_action.args['content'] = object_content + + copy_action = self._shared_loader_obj.action_loader.get( + 'copy', + task=copy_action, + connection=self._connection, + play_context=self._play_context, + loader=self._loader, + templar=self._templar, + shared_loader_obj=self._shared_loader_obj + ) + + result = merge_hash(result, copy_action.run(task_vars=task_vars)) + else: + # remove file if does not belong to a feature + if 'features-available' not in path: + file_args = dict() + file_args['state'] = 'absent' + file_args['path'] = file_fragment + file_module = self._execute_module( + module_name='file', + module_args=file_args, + task_vars=task_vars, + tmp=tmp + ) + result = merge_hash(result, file_module) + result['dest'] = file_fragment + + return result diff --git a/plugins/filter/prefix.py b/plugins/filter/prefix.py index f832293a..9a6f7d7a 100644 --- a/plugins/filter/prefix.py +++ b/plugins/filter/prefix.py @@ -1,3 +1,4 @@ +# pylint: skip-file from __future__ import (absolute_import, division, print_function) __metaclass__ = type diff --git a/plugins/lookup/icinga2_parser.py b/plugins/lookup/icinga2_parser.py index 1c77d28c..abff6c2b 100644 --- a/plugins/lookup/icinga2_parser.py +++ b/plugins/lookup/icinga2_parser.py @@ -45,7 +45,8 @@ class LookupModule(LookupBase): - def run(self, terms, variables=None, **kwargs): + # TODO can the variables parameter be removed? + def run(self, terms, variables=None, **kwargs): # pylint: disable=unused-argument config = Icinga2Parser() ret = [] constants = list(kwargs.get('constants', {}).keys()) diff --git a/plugins/module_utils/parse.py b/plugins/module_utils/parse.py index cf3e5a5a..18f34654 100644 --- a/plugins/module_utils/parse.py +++ b/plugins/module_utils/parse.py @@ -1,3 +1,4 @@ +# pylint: skip-file import re diff --git a/plugins/modules/icinga2_elasticsearchwriter.py b/plugins/modules/icinga2_elasticsearchwriter.py index a1fc7f62..e25372b4 100644 --- a/plugins/modules/icinga2_elasticsearchwriter.py +++ b/plugins/modules/icinga2_elasticsearchwriter.py @@ -1,40 +1,38 @@ -#!/usr/bin/python - - from ansible.module_utils.basic import AnsibleModule + def main(): - module = AnsibleModule( - supports_check_mode=True, - argument_spec = dict( - state = dict(default='present', choices=['present', 'absent']), - name = dict(required=True), - order = dict(default=10, type='int'), - file = dict(default='features-available/elasticsearch.conf', type='str'), - host = dict(type='str'), - port = dict(type='int'), + module = AnsibleModule( + supports_check_mode=True, + argument_spec = dict( + state = dict(default='present', choices=['present', 'absent']), + name = dict(required=True), + order = dict(default=10, type='int'), + file = dict(default='features-available/elasticsearch.conf', type='str'), + host = dict(type='str'), + port = dict(type='int'), index = dict(type='str'), - enable_send_perfdata = dict(type='bool'), + enable_send_perfdata = dict(type='bool'), flush_interval = dict(type='str'), flush_threshold = dict(type='int'), username = dict(type='str'), password = dict(type='str'), - enable_tls = dict(type='bool'), - insecure_noverify = dict(type='bool'), - ca_path = dict(type='str'), - cert_path = dict(type='str'), - key_path = dict(type='str'), + enable_tls = dict(type='bool'), + insecure_noverify = dict(type='bool'), + ca_path = dict(type='str'), + cert_path = dict(type='str'), + key_path = dict(type='str'), enable_ha = dict(type='bool'), - ) - ) + ) + ) - args = module.params - name = args.pop('name') - order = args.pop('order') - state = args.pop('state') - file = args.pop('file') + args = module.params + name = args.pop('name') + order = args.pop('order') + state = args.pop('state') + file = args.pop('file') - module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) + module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) if __name__ == '__main__': - main() + main() diff --git a/plugins/modules/icinga2_gelfwriter.py b/plugins/modules/icinga2_gelfwriter.py index 6b69b4d3..78751039 100644 --- a/plugins/modules/icinga2_gelfwriter.py +++ b/plugins/modules/icinga2_gelfwriter.py @@ -1,36 +1,34 @@ -#!/usr/bin/python - - from ansible.module_utils.basic import AnsibleModule + def main(): - module = AnsibleModule( - supports_check_mode=True, - argument_spec = dict( - state = dict(default='present', choices=['present', 'absent']), - name = dict(required=True), - order = dict(default=10, type='int'), - file = dict(default='features-available/gelf.conf', type='str'), - host = dict(type='str'), - port = dict(type='int'), - source = dict(type='str'), - enable_send_perfdata = dict(type='bool'), - enable_ha = dict(type='bool'), - enable_tls = dict(type='bool'), - insecure_noverify = dict(type='bool'), - ca_path = dict(type='str'), - cert_path = dict(type='str'), - key_path = dict(type='str'), - ) - ) + module = AnsibleModule( + supports_check_mode=True, + argument_spec = dict( + state = dict(default='present', choices=['present', 'absent']), + name = dict(required=True), + order = dict(default=10, type='int'), + file = dict(default='features-available/gelf.conf', type='str'), + host = dict(type='str'), + port = dict(type='int'), + source = dict(type='str'), + enable_send_perfdata = dict(type='bool'), + enable_ha = dict(type='bool'), + enable_tls = dict(type='bool'), + insecure_noverify = dict(type='bool'), + ca_path = dict(type='str'), + cert_path = dict(type='str'), + key_path = dict(type='str'), + ) + ) - args = module.params - name = args.pop('name') - order = args.pop('order') - state = args.pop('state') - file = args.pop('file') + args = module.params + name = args.pop('name') + order = args.pop('order') + state = args.pop('state') + file = args.pop('file') - module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) + module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) if __name__ == '__main__': - main() + main() diff --git a/plugins/modules/icinga2_icingadb.py b/plugins/modules/icinga2_icingadb.py index 54ed9666..d4074681 100644 --- a/plugins/modules/icinga2_icingadb.py +++ b/plugins/modules/icinga2_icingadb.py @@ -1,39 +1,37 @@ -#!/usr/bin/python - - from ansible.module_utils.basic import AnsibleModule + def main(): - module = AnsibleModule( - supports_check_mode=True, - argument_spec = dict( - state = dict(default='present', choices=['present', 'absent']), - name = dict(required=True), - order = dict(default=10, type='int'), - file = dict(default='features-available/icingadb.conf', type='str'), - host = dict(type='str'), - port = dict(type='int'), - path = dict(type='str'), - password = dict(type='str'), - enable_tls = dict(type='bool'), - cert_path = dict(type='str'), - key_path = dict(type='str'), - ca_path = dict(type='str'), - crl_path = dict(type='str'), - cipher_list = dict(type='str'), - tls_protocolmin = dict(type='str'), - insecure_noverify = dict(type='bool'), - connect_timeout = dict(type='int'), - ) - ) + module = AnsibleModule( + supports_check_mode=True, + argument_spec = dict( + state = dict(default='present', choices=['present', 'absent']), + name = dict(required=True), + order = dict(default=10, type='int'), + file = dict(default='features-available/icingadb.conf', type='str'), + host = dict(type='str'), + port = dict(type='int'), + path = dict(type='str'), + password = dict(type='str'), + enable_tls = dict(type='bool'), + cert_path = dict(type='str'), + key_path = dict(type='str'), + ca_path = dict(type='str'), + crl_path = dict(type='str'), + cipher_list = dict(type='str'), + tls_protocolmin = dict(type='str'), + insecure_noverify = dict(type='bool'), + connect_timeout = dict(type='int'), + ) + ) - args = module.params - name = args.pop('name') - order = args.pop('order') - state = args.pop('state') - file = args.pop('file') + args = module.params + name = args.pop('name') + order = args.pop('order') + state = args.pop('state') + file = args.pop('file') - module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) + module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) if __name__ == '__main__': - main() + main() diff --git a/plugins/modules/icinga2_influxdb2writer.py b/plugins/modules/icinga2_influxdb2writer.py index 687e722a..8caa7909 100644 --- a/plugins/modules/icinga2_influxdb2writer.py +++ b/plugins/modules/icinga2_influxdb2writer.py @@ -1,18 +1,16 @@ -#!/usr/bin/python - - from ansible.module_utils.basic import AnsibleModule + def main(): - module = AnsibleModule( - supports_check_mode=True, - argument_spec = dict( - state = dict(default='present', choices=['present', 'absent']), - name = dict(required=True), - order = dict(default=10, type='int'), - file = dict(default='features-available/influxdb2.conf', type='str'), - host = dict(type='str', required=True), - port = dict(type='int', required=True), + module = AnsibleModule( + supports_check_mode=True, + argument_spec = dict( + state = dict(default='present', choices=['present', 'absent']), + name = dict(required=True), + order = dict(default=10, type='int'), + file = dict(default='features-available/influxdb2.conf', type='str'), + host = dict(type='str', required=True), + port = dict(type='int', required=True), organization = dict(type='str', required=True), bucket = dict(type='str', required=True), auth_token = dict(type='str', required=True), @@ -28,16 +26,16 @@ def main(): flush_interval = dict(type='str'), flush_threshold = dict(type='int'), enable_ha = dict(type='bool'), - ) - ) + ) + ) - args = module.params - name = args.pop('name') - order = args.pop('order') - state = args.pop('state') - file = args.pop('file') + args = module.params + name = args.pop('name') + order = args.pop('order') + state = args.pop('state') + file = args.pop('file') - module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) + module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) if __name__ == '__main__': - main() + main() diff --git a/plugins/modules/icinga2_opentsdbwriter.py b/plugins/modules/icinga2_opentsdbwriter.py index cee8e5cc..95317c16 100644 --- a/plugins/modules/icinga2_opentsdbwriter.py +++ b/plugins/modules/icinga2_opentsdbwriter.py @@ -1,32 +1,30 @@ -#!/usr/bin/python - - from ansible.module_utils.basic import AnsibleModule + def main(): - module = AnsibleModule( - supports_check_mode=True, - argument_spec = dict( - state = dict(default='present', choices=['present', 'absent']), - name = dict(required=True), - order = dict(default=10, type='int'), - file = dict(default='features-available/opentsdb.conf', type='str'), - host = dict(type='str'), - port = dict(type='int'), - enable_ha = dict(type='bool'), - enable_generic_metrics = dict(type='bool'), - host_template = dict(type='dict'), - service_template = dict(type='dict'), - ) - ) + module = AnsibleModule( + supports_check_mode=True, + argument_spec = dict( + state = dict(default='present', choices=['present', 'absent']), + name = dict(required=True), + order = dict(default=10, type='int'), + file = dict(default='features-available/opentsdb.conf', type='str'), + host = dict(type='str'), + port = dict(type='int'), + enable_ha = dict(type='bool'), + enable_generic_metrics = dict(type='bool'), + host_template = dict(type='dict'), + service_template = dict(type='dict'), + ) + ) - args = module.params - name = args.pop('name') - order = args.pop('order') - state = args.pop('state') - file = args.pop('file') + args = module.params + name = args.pop('name') + order = args.pop('order') + state = args.pop('state') + file = args.pop('file') - module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) + module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) if __name__ == '__main__': - main() + main() diff --git a/plugins/modules/icinga2_perfdatawriter.py b/plugins/modules/icinga2_perfdatawriter.py index 52b9a98e..39f7f580 100644 --- a/plugins/modules/icinga2_perfdatawriter.py +++ b/plugins/modules/icinga2_perfdatawriter.py @@ -1,34 +1,32 @@ -#!/usr/bin/python - - from ansible.module_utils.basic import AnsibleModule + def main(): - module = AnsibleModule( - supports_check_mode=True, - argument_spec = dict( - state = dict(default='present', choices=['present', 'absent']), - name = dict(required=True), - order = dict(default=10, type='int'), - file = dict(default='features-available/perfdata.conf', type='str'), - host_perfdata_path = dict(type='str'), - service_perfdata_path = dict(type='str'), - host_temp_path = dict(type='str'), - service_temp_path = dict(type='str'), - host_format_template = dict(type='str'), - service_format_template = dict(type='str'), - rotation_interval = dict(type='str'), - enable_ha = dict(type='bool'), - ) - ) + module = AnsibleModule( + supports_check_mode=True, + argument_spec = dict( + state = dict(default='present', choices=['present', 'absent']), + name = dict(required=True), + order = dict(default=10, type='int'), + file = dict(default='features-available/perfdata.conf', type='str'), + host_perfdata_path = dict(type='str'), + service_perfdata_path = dict(type='str'), + host_temp_path = dict(type='str'), + service_temp_path = dict(type='str'), + host_format_template = dict(type='str'), + service_format_template = dict(type='str'), + rotation_interval = dict(type='str'), + enable_ha = dict(type='bool'), + ) + ) - args = module.params - name = args.pop('name') - order = args.pop('order') - state = args.pop('state') - file = args.pop('file') + args = module.params + name = args.pop('name') + order = args.pop('order') + state = args.pop('state') + file = args.pop('file') - module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) + module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) if __name__ == '__main__': - main() + main()