mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
FactCache changes
* Fix FactCache to conform to the dict API * update needs to take a dict rather than a key and a value * __init__ needs to allow for setting the intial dictionary * Remove unneeded _display and _cache attributes * Move ansible.plugins.cache.FactCache to ansible.vars.fact_cache.FactCache because this isn't part of the cache plugin API. * Add backwards compatibility when calling update on the new FactCache * Remove code for calling old FactCache. There's no way to call the old FactCache so there's no need for backwards compatible code for calling code. Backwards compatibility is handling things which are calling the new FactCache. * Port our code to the new FactCache location.
This commit is contained in:
77
lib/ansible/plugins/cache/__init__.py
vendored
77
lib/ansible/plugins/cache/__init__.py
vendored
@@ -1,4 +1,5 @@
|
||||
# (c) 2014, Michael DeHaan <michael.dehaan@gmail.com>
|
||||
# (c) 2018, Ansible Project
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
@@ -29,10 +30,27 @@ from ansible.module_utils._text import to_bytes
|
||||
from ansible.module_utils.common._collections_compat import MutableMapping
|
||||
from ansible.plugins.loader import cache_loader
|
||||
from ansible.utils.display import Display
|
||||
from ansible.vars.fact_cache import FactCache as RealFactCache
|
||||
|
||||
display = Display()
|
||||
|
||||
|
||||
class FactCache(RealFactCache):
|
||||
"""
|
||||
This is for backwards compatibility. Will be removed after deprecation. It was removed as it
|
||||
wasn't actually part of the cache plugin API. It's actually the code to make use of cache
|
||||
plugins, not the cache plugin itself. Subclassing it wouldn't yield a usable Cache Plugin and
|
||||
there was no facility to use it as anything else.
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
display.deprecated('ansible.plugins.cache.FactCache has been moved to'
|
||||
' ansible.vars.fact_cache.FactCache. If you are looking for the class'
|
||||
' to subclass for a cache plugin, you want'
|
||||
' ansible.plugins.cache.BaseCacheModule or one of its subclasses.',
|
||||
version='2.12')
|
||||
super(FactCache, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class BaseCacheModule(with_metaclass(ABCMeta, object)):
|
||||
|
||||
# Backwards compat only. Just import the global display instead
|
||||
@@ -244,65 +262,6 @@ class BaseFileCacheModule(BaseCacheModule):
|
||||
pass
|
||||
|
||||
|
||||
class FactCache(MutableMapping):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
self._plugin = cache_loader.get(C.CACHE_PLUGIN)
|
||||
if not self._plugin:
|
||||
raise AnsibleError('Unable to load the facts cache plugin (%s).' % (C.CACHE_PLUGIN))
|
||||
|
||||
# Backwards compat: self._display isn't really needed, just import the global display and use that.
|
||||
self._display = display
|
||||
|
||||
# in memory cache so plugins don't expire keys mid run
|
||||
self._cache = {}
|
||||
|
||||
def __getitem__(self, key):
|
||||
if not self._plugin.contains(key):
|
||||
raise KeyError
|
||||
return self._plugin.get(key)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self._plugin.set(key, value)
|
||||
|
||||
def __delitem__(self, key):
|
||||
self._plugin.delete(key)
|
||||
|
||||
def __contains__(self, key):
|
||||
return self._plugin.contains(key)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._plugin.keys())
|
||||
|
||||
def __len__(self):
|
||||
return len(self._plugin.keys())
|
||||
|
||||
def copy(self):
|
||||
""" Return a primitive copy of the keys and values from the cache. """
|
||||
return dict(self)
|
||||
|
||||
def keys(self):
|
||||
return self._plugin.keys()
|
||||
|
||||
def flush(self):
|
||||
""" Flush the fact cache of all keys. """
|
||||
self._plugin.flush()
|
||||
|
||||
def update(self, host_facts):
|
||||
""" We override the normal update to ensure we always use the 'setter' method """
|
||||
for key in host_facts:
|
||||
try:
|
||||
host_cache = self._plugin.get(key)
|
||||
if host_cache:
|
||||
host_cache.update(host_facts[key])
|
||||
else:
|
||||
host_cache = host_facts[key]
|
||||
self._plugin.set(key, host_cache)
|
||||
except KeyError:
|
||||
self._plugin.set(key, host_facts[key])
|
||||
|
||||
|
||||
class InventoryFileCacheModule(BaseFileCacheModule):
|
||||
"""
|
||||
A caching module backed by file based storage.
|
||||
|
||||
@@ -63,10 +63,10 @@ import os
|
||||
from ansible import constants as C
|
||||
from ansible.errors import AnsibleParserError
|
||||
from ansible.inventory.helpers import get_group_vars
|
||||
from ansible.plugins.cache import FactCache
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.utils.vars import combine_vars
|
||||
from ansible.vars.fact_cache import FactCache
|
||||
|
||||
|
||||
class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
|
||||
@@ -73,13 +73,12 @@ EXAMPLES = '''
|
||||
|
||||
import os
|
||||
|
||||
from itertools import product
|
||||
|
||||
from ansible import constants as C
|
||||
from ansible.errors import AnsibleParserError
|
||||
from ansible.plugins.cache import FactCache
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin
|
||||
|
||||
from itertools import product
|
||||
|
||||
|
||||
class InventoryModule(BaseInventoryPlugin):
|
||||
""" constructs groups and vars using Jinja2 template expressions """
|
||||
@@ -90,8 +89,6 @@ class InventoryModule(BaseInventoryPlugin):
|
||||
|
||||
super(InventoryModule, self).__init__()
|
||||
|
||||
self._cache = FactCache()
|
||||
|
||||
def verify_file(self, path):
|
||||
|
||||
valid = False
|
||||
|
||||
Reference in New Issue
Block a user