From 61717e7a8d2e0ab9cbfd28c685cc67b7be1ad968 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Fri, 24 Jul 2015 12:43:06 +0200 Subject: [PATCH] notifier: stop using global conf object Change-Id: I99b1eb1b7fedbe86320f6d843213418b3c38c530 --- aodh/notifier/__init__.py | 7 ++++++- aodh/notifier/rest.py | 15 +++++++++------ aodh/notifier/test.py | 5 +++-- aodh/notifier/trust.py | 8 +++----- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/aodh/notifier/__init__.py b/aodh/notifier/__init__.py index 7172fba1..6afbebf6 100644 --- a/aodh/notifier/__init__.py +++ b/aodh/notifier/__init__.py @@ -31,6 +31,10 @@ LOG = logging.getLogger(__name__) class AlarmNotifier(object): """Base class for alarm notifier plugins.""" + @staticmethod + def __init__(conf): + pass + @abc.abstractmethod def notify(self, action, alarm_id, alarm_name, severity, previous, current, reason, reason_data): @@ -58,7 +62,8 @@ class AlarmNotifierService(os_service.Service): self.notifiers = extension.ExtensionManager( self.NOTIFIER_EXTENSIONS_NAMESPACE, - invoke_on_load=True) + invoke_on_load=True, + invoke_args=(conf,)) def start(self): super(AlarmNotifierService, self).start() diff --git a/aodh/notifier/rest.py b/aodh/notifier/rest.py index e1c81cf3..7c8ee90d 100644 --- a/aodh/notifier/rest.py +++ b/aodh/notifier/rest.py @@ -58,8 +58,11 @@ cfg.CONF.register_opts(OPTS) class RestAlarmNotifier(notifier.AlarmNotifier): """Rest alarm notifier.""" - @staticmethod - def notify(action, alarm_id, alarm_name, severity, previous, + def __init__(self, conf): + super(RestAlarmNotifier, self).__init__(conf) + self.conf = conf + + def notify(self, action, alarm_id, alarm_name, severity, previous, current, reason, reason_data, headers=None): headers = headers or {} if not headers.get('x-openstack-request-id'): @@ -82,14 +85,14 @@ class RestAlarmNotifier(notifier.AlarmNotifier): 'headers': headers} if action.scheme == 'https': - default_verify = int(cfg.CONF.rest_notifier_ssl_verify) + default_verify = int(self.conf.rest_notifier_ssl_verify) options = urlparse.parse_qs(action.query) verify = bool(int(options.get('aodh-alarm-ssl-verify', [default_verify])[-1])) kwargs['verify'] = verify - cert = cfg.CONF.rest_notifier_certificate_file - key = cfg.CONF.rest_notifier_certificate_key + cert = self.conf.rest_notifier_certificate_file + key = self.conf.rest_notifier_certificate_key if cert: kwargs['cert'] = (cert, key) if key else cert @@ -97,7 +100,7 @@ class RestAlarmNotifier(notifier.AlarmNotifier): # library. However, there's no interval between retries in urllib3 # implementation. It will be better to put some interval between # retries (future work). - max_retries = cfg.CONF.rest_notifier_max_retries + max_retries = self.conf.rest_notifier_max_retries session = requests.Session() session.mount(action.geturl(), requests.adapters.HTTPAdapter(max_retries=max_retries)) diff --git a/aodh/notifier/test.py b/aodh/notifier/test.py index 1c5f79c4..0a316fd4 100644 --- a/aodh/notifier/test.py +++ b/aodh/notifier/test.py @@ -1,5 +1,5 @@ # -# Copyright 2013 eNovance +# Copyright 2013-2015 eNovance # # 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 @@ -20,7 +20,8 @@ from aodh import notifier class TestAlarmNotifier(notifier.AlarmNotifier): "Test alarm notifier.""" - def __init__(self): + def __init__(self, conf): + super(TestAlarmNotifier, self).__init__(conf) self.notifications = [] def notify(self, action, alarm_id, alarm_name, severity, diff --git a/aodh/notifier/trust.py b/aodh/notifier/trust.py index 1527964b..040ab61d 100644 --- a/aodh/notifier/trust.py +++ b/aodh/notifier/trust.py @@ -14,7 +14,6 @@ # under the License. """Rest alarm notifier with trusted authentication.""" -from oslo_config import cfg from six.moves.urllib import parse from aodh import keystone_client @@ -31,12 +30,11 @@ class TrustRestAlarmNotifier(rest.RestAlarmNotifier): The URL must be in the form trust+http://trust-id@host/action. """ - @staticmethod - def notify(action, alarm_id, alarm_name, severity, previous, current, + def notify(self, action, alarm_id, alarm_name, severity, previous, current, reason, reason_data): trust_id = action.username - client = keystone_client.get_v3_client(cfg.CONF, trust_id) + client = keystone_client.get_v3_client(self.conf, trust_id) # Remove the fake user netloc = action.netloc.split("@")[1] @@ -47,6 +45,6 @@ class TrustRestAlarmNotifier(rest.RestAlarmNotifier): action.fragment) headers = {'X-Auth-Token': client.auth_token} - rest.RestAlarmNotifier.notify( + super(TrustRestAlarmNotifier, self).notify( action, alarm_id, alarm_name, severity, previous, current, reason, reason_data, headers)