diff options
author | Yadnesh Kulkarni <ykulkarn@redhat.com> | 2023-04-04 10:51:06 +0530 |
---|---|---|
committer | Yadnesh Kulkarni <ykulkarn@redhat.com> | 2023-04-04 19:02:46 +0530 |
commit | 6e339d3e74df5460d372b3e2abce27664ddb1100 (patch) | |
tree | eac50f6786e342ab17a870d0faa6fd08cb6115ca /ceilometer/cache_utils.py | |
parent | 7dd58d6f75ffcfe9a9097e9f83be3087000ad355 (diff) | |
download | ceilometer-6e339d3e74df5460d372b3e2abce27664ddb1100.tar.gz |
Add vanity names to notification samples
This change adds "project_name" and "user_name" fields to the
polling samples created from notifications of "event_type".
Also move caching helper functions into "ceilometer/cache_utils.py"
to make them accessible throughout the project.
Change-Id: I68bd4ee096b28a2fd952e749d56a6b3eed9bfb94
Diffstat (limited to 'ceilometer/cache_utils.py')
-rw-r--r-- | ceilometer/cache_utils.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/ceilometer/cache_utils.py b/ceilometer/cache_utils.py index 31c1c0e9..f3fbd187 100644 --- a/ceilometer/cache_utils.py +++ b/ceilometer/cache_utils.py @@ -16,6 +16,8 @@ """Simple wrapper for oslo_cache.""" import uuid +from ceilometer import keystone_client +from keystoneauth1 import exceptions as ka_exceptions from oslo_cache import core as cache from oslo_cache import exception from oslo_log import log @@ -76,3 +78,29 @@ def cache_key_mangler(key): """Construct an opaque cache key.""" return uuid.uuid5(CACHE_NAMESPACE, key).hex + + +def resolve_uuid_from_cache(conf, attr, uuid): + # empty cache_client means either caching is not enabled or + # there was an error configuring cache + cache_client = get_client(conf) + if cache_client: + resource_name = cache_client.get(uuid) + if resource_name: + return resource_name + + # Retrieve project and user names from Keystone only + # if ceilometer doesn't have a caching backend + resource_name = resolve_uuid_from_keystone(conf, attr, uuid) + if cache_client: + cache_client.set(uuid, resource_name) + return resource_name + + +def resolve_uuid_from_keystone(conf, attr, uuid): + try: + return getattr(keystone_client.get_client(conf), attr).get(uuid).name + except AttributeError as e: + LOG.warning("Found '%s' while resolving uuid %s to name", e, uuid) + except ka_exceptions.NotFound as e: + LOG.warning(e.message) |