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:
Jonas L 2023-11-24 20:50:43 +01:00 committed by GitHub
parent 04f63d0679
commit df7fa04149
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 4 deletions

View file

@ -5,6 +5,7 @@ exclude_paths:
- .git/
- .github/
- changelogs/
- examples/
- tests/integration/targets/certificate
- tests/integration/targets/firewall
- tests/integration/targets/floating_ip

View file

@ -0,0 +1,2 @@
bugfixes:
- hcloud inventory - Ensure the API client use a new cache for every *cached session*.

View 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

View file

@ -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()
try:
yield
finally:
self._requests_session = requests.Session()