Skip to content

Commit

Permalink
proxmox inventory: proposal for #9710 (caching) (#9760)
Browse files Browse the repository at this point in the history
* Proposal for #9710

* Fixed comments

* Fixed trailing whitespace

* Fixed changelog fragment
  • Loading branch information
iqt4 authored Feb 17, 2025
1 parent 94e1511 commit d696bb7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/9760-proxmox-inventory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "proxmox inventory plugin - plugin did not update cache correctly after ``meta: refresh_inventory`` (https://github.com/ansible-collections/community.general/issues/9710, https://github.com/ansible-collections/community.general/pull/9760)."
22 changes: 15 additions & 7 deletions plugins/inventory/proxmox.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,17 @@ def _get_auth(self):

def _get_json(self, url, ignore_errors=None):

if not self.use_cache or url not in self._cache.get(self.cache_key, {}):
data = []
has_data = False

if self.cache_key not in self._cache:
self._cache[self.cache_key] = {'url': ''}
if self.use_cache:
try:
data = self._cache[self.cache_key][url]
has_data = True
except KeyError:
self.update_cache = True

data = []
if not has_data:
s = self._get_session()
while True:
ret = s.get(url, headers=self.headers)
Expand All @@ -339,9 +344,8 @@ def _get_json(self, url, ignore_errors=None):
data = data + json['data']
break

self._cache[self.cache_key][url] = data

return make_unsafe(self._cache[self.cache_key][url])
self._results[url] = data
return make_unsafe(data)

def _get_nodes(self):
return self._get_json(f"{self.proxmox_url}/api2/json/nodes")
Expand Down Expand Up @@ -680,10 +684,14 @@ def parse(self, inventory, loader, path, cache=True):
self.exclude_nodes = self.get_option('exclude_nodes')
self.cache_key = self.get_cache_key(path)
self.use_cache = cache and self.get_option('cache')
self.update_cache = not cache and self.get_option('cache')
self.host_filters = self.get_option('filters')
self.group_prefix = self.get_option('group_prefix')
self.facts_prefix = self.get_option('facts_prefix')
self.strict = self.get_option('strict')

# actually populate inventory
self._results = {}
self._populate()
if self.update_cache:
self._cache[self.cache_key] = self._results

0 comments on commit d696bb7

Please sign in to comment.