From a93154c57f7b9196e967cf86005d53c1deb6ebca Mon Sep 17 00:00:00 2001 From: Sloane Hertel Date: Tue, 26 Mar 2019 08:15:12 -0500 Subject: [PATCH] Update inventory caching to remove deprecation warnings (#53976) --- lib/ansible/plugins/inventory/aws_ec2.py | 4 ++-- lib/ansible/plugins/inventory/aws_rds.py | 4 ++-- lib/ansible/plugins/inventory/gcp_compute.py | 4 ++-- lib/ansible/plugins/inventory/openstack.py | 13 +++++++++---- lib/ansible/plugins/inventory/virtualbox.py | 4 ++-- .../plugins/inventory/vmware_vm_inventory.py | 4 ++-- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/ansible/plugins/inventory/aws_ec2.py b/lib/ansible/plugins/inventory/aws_ec2.py index 9f877c86b85..b8008830f4c 100644 --- a/lib/ansible/plugins/inventory/aws_ec2.py +++ b/lib/ansible/plugins/inventory/aws_ec2.py @@ -583,7 +583,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): cache_needs_update = False if cache: try: - results = self.cache.get(cache_key) + results = self._cache[cache_key] except KeyError: # if cache expires or cache file doesn't exist cache_needs_update = True @@ -596,7 +596,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): # If the cache has expired/doesn't exist or if refresh_inventory/flush cache is used # when the user is using caching, update the cached inventory if cache_needs_update or (not cache and self.get_option('cache')): - self.cache.set(cache_key, results) + self._cache[cache_key] = results @staticmethod def _legacy_script_compatible_group_sanitization(name): diff --git a/lib/ansible/plugins/inventory/aws_rds.py b/lib/ansible/plugins/inventory/aws_rds.py index 517bed520e7..6b890324731 100644 --- a/lib/ansible/plugins/inventory/aws_rds.py +++ b/lib/ansible/plugins/inventory/aws_rds.py @@ -308,7 +308,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): cache_needs_update = False if cache: try: - results = self.cache.get(cache_key) + results = self._cache[cache_key] except KeyError: # if cache expires or cache file doesn't exist cache_needs_update = True @@ -323,4 +323,4 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): # If the cache has expired/doesn't exist or if refresh_inventory/flush cache is used # when the user is using caching, update the cached inventory if cache_needs_update or (not cache and self.get_option('cache')): - self.cache.set(cache_key, formatted_inventory) + self._cache[cache_key] = formatted_inventory diff --git a/lib/ansible/plugins/inventory/gcp_compute.py b/lib/ansible/plugins/inventory/gcp_compute.py index b947e4e1001..7e319aca38f 100644 --- a/lib/ansible/plugins/inventory/gcp_compute.py +++ b/lib/ansible/plugins/inventory/gcp_compute.py @@ -370,7 +370,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): cache_needs_update = False if cache: try: - results = self.cache.get(cache_key) + results = self._cache[cache_key] for project in results: for zone in results[project]: self._add_hosts(results[project][zone], config_data, False) @@ -392,7 +392,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): cached_data[project][zone] = resp.get('items') if cache_needs_update: - self.cache.set(cache_key, cached_data) + self._cache[cache_key] = cached_data @staticmethod def _legacy_script_compatible_group_sanitization(name): diff --git a/lib/ansible/plugins/inventory/openstack.py b/lib/ansible/plugins/inventory/openstack.py index 671ffd3c68d..a6917f8fb4e 100644 --- a/lib/ansible/plugins/inventory/openstack.py +++ b/lib/ansible/plugins/inventory/openstack.py @@ -156,14 +156,19 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): if 'clouds' in self._config_data: self._config_data = {} + # update cache if the user has caching enabled and the cache is being refreshed + # will update variable below in the case of an expired cache + cache_needs_update = not cache and self.get_option('cache') + if cache: cache = self.get_option('cache') source_data = None if cache: try: - source_data = self.cache.get(cache_key) + source_data = self._cache[cache_key] except KeyError: - pass + # cache expired or doesn't exist yet + cache_needs_update = True if not source_data: clouds_yaml_path = self._config_data.get('clouds_yaml_path') @@ -199,8 +204,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): source_data = cloud_inventory.list_hosts( expand=expand_hostvars, fail_on_cloud_config=fail_on_errors) - if self.cache is not None: - self.cache.set(cache_key, source_data) + if cache_needs_update: + self._cache[cache_key] = source_data self._populate_from_source(source_data) diff --git a/lib/ansible/plugins/inventory/virtualbox.py b/lib/ansible/plugins/inventory/virtualbox.py index 68932d66b89..816252d457a 100644 --- a/lib/ansible/plugins/inventory/virtualbox.py +++ b/lib/ansible/plugins/inventory/virtualbox.py @@ -244,7 +244,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): update_cache = False if cache: try: - source_data = self.cache.get(cache_key) + source_data = self._cache[cache_key] except KeyError: update_cache = True @@ -274,4 +274,4 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): cacheable_results = self._populate_from_source(source_data, using_current_cache) if update_cache: - self.cache.set(cache_key, cacheable_results) + self._cache[cache_key] = cacheable_results diff --git a/lib/ansible/plugins/inventory/vmware_vm_inventory.py b/lib/ansible/plugins/inventory/vmware_vm_inventory.py index ed5984a16c1..1c004d54677 100644 --- a/lib/ansible/plugins/inventory/vmware_vm_inventory.py +++ b/lib/ansible/plugins/inventory/vmware_vm_inventory.py @@ -356,7 +356,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): update_cache = False if cache: try: - source_data = self.cache.get(cache_key) + source_data = self._cache[cache_key] except KeyError: update_cache = True @@ -364,7 +364,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): cacheable_results = self._populate_from_source(source_data, using_current_cache) if update_cache: - self.cache.set(cache_key, cacheable_results) + self._cache[cache_key] = cacheable_results def _populate_from_cache(self, source_data): """ Populate cache using source data """