mirror of
https://github.com/ansible-collections/hetzner.hcloud
synced 2024-12-05 01:59:14 +00:00
fix(inventory): always use fresh cache on new cached session (#404)
##### SUMMARY The class scoped `cache` dict was being shared across all `cached_session`, we now make sure that the cache is instance scoped. Fixes #403 ##### ISSUE TYPE - Bugfix Pull Request
This commit is contained in:
parent
04f63d0679
commit
df7fa04149
4 changed files with 48 additions and 4 deletions
|
@ -5,6 +5,7 @@ exclude_paths:
|
|||
- .git/
|
||||
- .github/
|
||||
- changelogs/
|
||||
- examples/
|
||||
- tests/integration/targets/certificate
|
||||
- tests/integration/targets/firewall
|
||||
- tests/integration/targets/floating_ip
|
||||
|
|
2
changelogs/fragments/fix-inventory-fresh-cache.yaml
Normal file
2
changelogs/fragments/fix-inventory-fresh-cache.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- hcloud inventory - Ensure the API client use a new cache for every *cached session*.
|
35
examples/use-refresh-inventory.yml
Normal file
35
examples/use-refresh-inventory.yml
Normal file
|
@ -0,0 +1,35 @@
|
|||
---
|
||||
- name: Demonstrate the usage of 'refresh_inventory'
|
||||
hosts: localhost
|
||||
connection: local
|
||||
|
||||
tasks:
|
||||
- name: Print hostvars
|
||||
ansible.builtin.debug:
|
||||
var: hostvars
|
||||
|
||||
- name: Create new server
|
||||
hetzner.hcloud.server:
|
||||
name: my-server
|
||||
server_type: cx11
|
||||
image: debian-12
|
||||
|
||||
- name: Refresh inventory
|
||||
ansible.builtin.meta: refresh_inventory
|
||||
|
||||
- name: Run tests
|
||||
block:
|
||||
- name: Print updated inventory
|
||||
ansible.builtin.debug:
|
||||
var: hostvars
|
||||
|
||||
- name: Verify hostvars is not empty
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- hostvars != {}
|
||||
|
||||
always:
|
||||
- name: Cleanup server
|
||||
hetzner.hcloud.server:
|
||||
name: my-server
|
||||
state: absent
|
|
@ -68,7 +68,11 @@ def client_get_by_name_or_id(client: Client, resource: str, param: str | int):
|
|||
if HAS_REQUESTS:
|
||||
|
||||
class CachedSession(requests.Session):
|
||||
cache: dict[str, requests.Response] = {}
|
||||
cache: dict[str, requests.Response]
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.cache = {}
|
||||
|
||||
def send(self, request: requests.PreparedRequest, **kwargs) -> requests.Response: # type: ignore[no-untyped-def]
|
||||
"""
|
||||
|
@ -89,7 +93,7 @@ if HAS_REQUESTS:
|
|||
|
||||
class Client(ClientBase):
|
||||
@contextmanager
|
||||
def cached_session(self) -> None:
|
||||
def cached_session(self):
|
||||
"""
|
||||
Swap the client session during the scope of the context. The session will cache
|
||||
all GET requests.
|
||||
|
@ -98,5 +102,7 @@ class Client(ClientBase):
|
|||
for long living scopes.
|
||||
"""
|
||||
self._requests_session = CachedSession()
|
||||
yield
|
||||
self._requests_session = requests.Session()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
self._requests_session = requests.Session()
|
||||
|
|
Loading…
Reference in a new issue