diff options
| author | Mehdi Abaakouk <sileht@redhat.com> | 2016-01-06 09:44:09 +0100 |
|---|---|---|
| committer | Mehdi Abaakouk <sileht@redhat.com> | 2016-01-07 16:30:14 +0100 |
| commit | 71cb6b3d0ee1f393c2bafee13aa2f013cc82f77d (patch) | |
| tree | 00661af188dfdf3c44889a74b0de3dd5782d7ffe /ceilometerclient/v2 | |
| parent | 6097b0ce2ed80651f2e49c47adf7c0b5a11f5431 (diff) | |
| download | python-ceilometerclient-71cb6b3d0ee1f393c2bafee13aa2f013cc82f77d.tar.gz | |
Don't copy the auth_plugin for aodh
We must not copy a keystone session object or auth_plugin object.
This change simplifies the aodh redirection code by:
* sharing the keystone session object between aodh and ceilometer client
* creating a new AuthPlugin() for the aodh client instead of copying the ceilometer one
and then change its content.
Closes-bug: #1531452
Change-Id: I2b2195e32c5dd74136237f7166c9c0d325434611
Diffstat (limited to 'ceilometerclient/v2')
| -rw-r--r-- | ceilometerclient/v2/client.py | 78 |
1 files changed, 40 insertions, 38 deletions
diff --git a/ceilometerclient/v2/client.py b/ceilometerclient/v2/client.py index e6a6075..f613cec 100644 --- a/ceilometerclient/v2/client.py +++ b/ceilometerclient/v2/client.py @@ -57,13 +57,17 @@ class Client(object): def __init__(self, *args, **kwargs): """Initialize a new client for the Ceilometer v2 API.""" - if not kwargs.get('auth_plugin'): + if not kwargs.get('auth_plugin') and not kwargs.get('session'): kwargs['auth_plugin'] = ceiloclient.get_auth_plugin(*args, **kwargs) + self.auth_plugin = kwargs.get('auth_plugin') self.http_client = ceiloclient._construct_http_client(**kwargs) - self.alarm_client, aodh_enabled = self._get_alarm_client(**kwargs) + self.alarm_client = self._get_alarm_client(**kwargs) + aodh_enabled = self.alarm_client is not None + if not aodh_enabled: + self.alarm_client = self.http_client self.meters = meters.MeterManager(self.http_client) self.samples = samples.OldSampleManager(self.http_client) self.new_samples = samples.SampleManager(self.http_client) @@ -83,43 +87,41 @@ class Client(object): self.alarm_client) self.capabilities = capabilities.CapabilitiesManager(self.http_client) - def _get_alarm_client(self, **kwargs): + @staticmethod + def _get_alarm_client(**ceilo_kwargs): """Get client for alarm manager that redirect to aodh.""" - kwargs = copy.deepcopy(kwargs) - self.alarm_auth_plugin = kwargs.get('auth_plugin') - aodh_endpoint = kwargs.get('aodh_endpoint') - if kwargs.get('session') is not None: + # NOTE(sileht): the auth_plugin/keystone session cannot be copied + # because they rely on threading module. + auth_plugin = ceilo_kwargs.pop('auth_plugin', None) + session = ceilo_kwargs.pop('session', None) + + kwargs = copy.deepcopy(ceilo_kwargs) + kwargs["service_type"] = "alarming" + aodh_endpoint = ceilo_kwargs.get('aodh_endpoint') + + if session: + # keystone session can be shared between client + ceilo_kwargs['session'] = kwargs['session'] = session if aodh_endpoint: kwargs['endpoint_override'] = aodh_endpoint - else: - kwargs["service_type"] = "alarming" - try: - c = ceiloclient._construct_http_client(**kwargs) - # NOTE(sileht): when a keystoneauth1 session object is used - # endpoint looking is done on first request, so do it. - c.get("/") - return c, True - except ka_exc.EndpointNotFound: - return self.http_client, False - except kc_exc.EndpointNotFound: - return self.http_client, False + elif auth_plugin and kwargs.get('auth_url'): + ceilo_kwargs['auth_plugin'] = auth_plugin + kwargs.pop('endpoint', None) + kwargs['auth_plugin'] = ceiloclient.get_auth_plugin( + aodh_endpoint, **kwargs) else: - if aodh_endpoint: - kwargs["auth_plugin"].opts['endpoint'] = aodh_endpoint - elif not kwargs.get('auth_url'): - # Users may just provided ceilometer endpoint and token, and no - # auth_url, in this case, we need 'aodh_endpoint' also - # provided, otherwise we cannot get aodh endpoint from - # keystone, and assume aodh is unavailable. - return self.http_client, False - else: - try: - # NOTE(liusheng): Getting the aodh's endpoint to rewrite - # the endpoint of alarm auth_plugin. - kwargs["auth_plugin"].redirect_to_aodh_endpoint( - kwargs.get('timeout')) - except ka_exc.EndpointNotFound: - return self.http_client, False - except kc_exc.EndpointNotFound: - return self.http_client, False - return ceiloclient._construct_http_client(**kwargs), True + # Users may just provided ceilometer endpoint and token, and no + # auth_url, in this case, we need 'aodh_endpoint' also + # provided, otherwise we cannot get aodh endpoint from + # keystone, and assume aodh is unavailable. + return None + + try: + # NOTE(sileht): try to use aodh + c = ceiloclient._construct_http_client(**kwargs) + c.get("/") + return c + except ka_exc.EndpointNotFound: + return None + except kc_exc.EndpointNotFound: + return None |
