From 3b509b1095a5c65fd16e46110240941712633014 Mon Sep 17 00:00:00 2001 From: Fabio Alessandro Locati Date: Fri, 9 Dec 2016 22:31:07 +0000 Subject: [PATCH] Avoid extending a class if it does not exists (#19059) --- lib/ansible/modules/monitoring/zabbix_host.py | 24 +++++++++---------- .../modules/monitoring/zabbix_hostmacro.py | 13 +++++----- .../modules/monitoring/zabbix_screen.py | 23 +++++++++--------- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/lib/ansible/modules/monitoring/zabbix_host.py b/lib/ansible/modules/monitoring/zabbix_host.py index aa113efe50..08e34c0e6b 100644 --- a/lib/ansible/modules/monitoring/zabbix_host.py +++ b/lib/ansible/modules/monitoring/zabbix_host.py @@ -168,23 +168,21 @@ import copy try: from zabbix_api import ZabbixAPI, ZabbixAPISubClass + # Extend the ZabbixAPI + # Since the zabbix-api python module too old (version 1.0, no higher version so far), + # it does not support the 'hostinterface' api calls, + # so we have to inherit the ZabbixAPI class to add 'hostinterface' support. + class ZabbixAPIExtends(ZabbixAPI): + hostinterface = None + + def __init__(self, server, timeout, user, passwd, **kwargs): + ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd) + self.hostinterface = ZabbixAPISubClass(self, dict({"prefix": "hostinterface"}, **kwargs)) + HAS_ZABBIX_API = True except ImportError: HAS_ZABBIX_API = False - -# Extend the ZabbixAPI -# Since the zabbix-api python module too old (version 1.0, no higher version so far), -# it does not support the 'hostinterface' api calls, -# so we have to inherit the ZabbixAPI class to add 'hostinterface' support. -class ZabbixAPIExtends(ZabbixAPI): - hostinterface = None - - def __init__(self, server, timeout, user, passwd, **kwargs): - ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd) - self.hostinterface = ZabbixAPISubClass(self, dict({"prefix": "hostinterface"}, **kwargs)) - - class Host(object): def __init__(self, module, zbx): self._module = module diff --git a/lib/ansible/modules/monitoring/zabbix_hostmacro.py b/lib/ansible/modules/monitoring/zabbix_hostmacro.py index 75c552cf22..54ce0a960f 100644 --- a/lib/ansible/modules/monitoring/zabbix_hostmacro.py +++ b/lib/ansible/modules/monitoring/zabbix_hostmacro.py @@ -107,18 +107,17 @@ import copy try: from zabbix_api import ZabbixAPI, ZabbixAPISubClass + # Extend the ZabbixAPI + # Since the zabbix-api python module too old (version 1.0, no higher version so far). + class ZabbixAPIExtends(ZabbixAPI): + def __init__(self, server, timeout, user, passwd, **kwargs): + ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd) + HAS_ZABBIX_API = True except ImportError: HAS_ZABBIX_API = False -# Extend the ZabbixAPI -# Since the zabbix-api python module too old (version 1.0, no higher version so far). -class ZabbixAPIExtends(ZabbixAPI): - def __init__(self, server, timeout, user, passwd, **kwargs): - ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd) - - class HostMacro(object): def __init__(self, module, zbx): self._module = module diff --git a/lib/ansible/modules/monitoring/zabbix_screen.py b/lib/ansible/modules/monitoring/zabbix_screen.py index 7e0ade2abe..2a390e9a1b 100644 --- a/lib/ansible/modules/monitoring/zabbix_screen.py +++ b/lib/ansible/modules/monitoring/zabbix_screen.py @@ -147,22 +147,21 @@ try: from zabbix_api import ZabbixAPI, ZabbixAPISubClass from zabbix_api import ZabbixAPIException from zabbix_api import Already_Exists + + # Extend the ZabbixAPI + # Since the zabbix-api python module too old (version 1.0, and there's no higher version so far), it doesn't support the 'screenitem' api call, + # we have to inherit the ZabbixAPI class to add 'screenitem' support. + class ZabbixAPIExtends(ZabbixAPI): + screenitem = None + + def __init__(self, server, timeout, user, passwd, **kwargs): + ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd) + self.screenitem = ZabbixAPISubClass(self, dict({"prefix": "screenitem"}, **kwargs)) + HAS_ZABBIX_API = True except ImportError: HAS_ZABBIX_API = False - -# Extend the ZabbixAPI -# Since the zabbix-api python module too old (version 1.0, and there's no higher version so far), it doesn't support the 'screenitem' api call, -# we have to inherit the ZabbixAPI class to add 'screenitem' support. -class ZabbixAPIExtends(ZabbixAPI): - screenitem = None - - def __init__(self, server, timeout, user, passwd, **kwargs): - ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd) - self.screenitem = ZabbixAPISubClass(self, dict({"prefix": "screenitem"}, **kwargs)) - - class Screen(object): def __init__(self, module, zbx): self._module = module