Skip to content

Commit

Permalink
Move the cache to the _ModelCache.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Sep 18, 2024
1 parent 3752955 commit c9be2ed
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
30 changes: 24 additions & 6 deletions ops/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
Any,
BinaryIO,
Callable,
ClassVar,
Dict,
Generator,
Iterable,
Expand Down Expand Up @@ -300,7 +299,13 @@ def get_secret(self, *, id: Optional[str] = None, label: Optional[str] = None) -
# Canonicalize to "secret:<id>" form for consistency in backend calls.
id = Secret._canonicalize_id(id, self.uuid)
content = self._backend.secret_get(id=id, label=label)
return Secret(self._backend, id=id, label=label, content=content)
return Secret(
self._backend,
id=id,
label=label,
content=content,
_secret_set_cache=self._cache._secret_set_cache,
)

def get_cloud_spec(self) -> 'CloudSpec':
"""Get details of the cloud in which the model is deployed.
Expand Down Expand Up @@ -329,6 +334,7 @@ class _ModelCache:
def __init__(self, meta: 'ops.charm.CharmMeta', backend: '_ModelBackend'):
self._meta = meta
self._backend = backend
self._secret_set_cache: Dict[str, Dict[str, Any]] = collections.defaultdict(dict)
self._weakrefs: _WeakCacheType = weakref.WeakValueDictionary()

@typing.overload
Expand Down Expand Up @@ -495,7 +501,13 @@ def add_secret(
rotate=rotate,
owner='application',
)
return Secret(self._backend, id=id, label=label, content=content)
return Secret(
self._backend,
id=id,
label=label,
content=content,
_secret_set_cache=self._cache._secret_set_cache,
)


def _calculate_expiry(
Expand Down Expand Up @@ -675,7 +687,13 @@ def add_secret(
rotate=rotate,
owner='unit',
)
return Secret(self._backend, id=id, label=label, content=content)
return Secret(
self._backend,
id=id,
label=label,
content=content,
_secret_set_cache=self._cache._secret_set_cache,
)

def open_port(
self, protocol: typing.Literal['tcp', 'udp', 'icmp'], port: Optional[int] = None
Expand Down Expand Up @@ -1254,14 +1272,13 @@ class Secret:

_key_re = re.compile(r'^([a-z](?:-?[a-z0-9]){2,})$') # copied from Juju code

_secret_set_cache: ClassVar[Dict[str, Dict[str, Any]]] = collections.defaultdict(dict)

def __init__(
self,
backend: '_ModelBackend',
id: Optional[str] = None,
label: Optional[str] = None,
content: Optional[Dict[str, str]] = None,
_secret_set_cache: Optional[Dict[str, Dict[str, Any]]] = None,
):
if not (id or label):
raise TypeError('Must provide an id or label, or both')
Expand All @@ -1271,6 +1288,7 @@ def __init__(
self._id = id
self._label = label
self._content = content
self._secret_set_cache = _secret_set_cache or collections.defaultdict(dict)

def __repr__(self):
fields: List[str] = []
Expand Down
3 changes: 0 additions & 3 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3780,7 +3780,6 @@ def test_set_content(self, model: ops.Model, fake_script: FakeScript):
def test_set_info(self, model: ops.Model, fake_script: FakeScript):
fake_script.write('secret-set', """exit 0""")
fake_script.write('secret-info-get', """echo '{"z": {"label": "y", "revision": 7}}'""")
ops.Secret._secret_set_cache.clear()

secret = self.make_secret(model, id='x')
expire = datetime.datetime(2022, 12, 9, 16, 59, 0)
Expand Down Expand Up @@ -3820,7 +3819,6 @@ def test_set_info(self, model: ops.Model, fake_script: FakeScript):
def test_set_content_then_info(self, model: ops.Model, fake_script: FakeScript):
fake_script.write('secret-set', """exit 0""")
fake_script.write('secret-get', """echo '{"foo": "bar"}'""")
ops.Secret._secret_set_cache.clear()

secret = self.make_secret(model, id='q')
secret.set_content({'foo': 'bar'})
Expand All @@ -3847,7 +3845,6 @@ def test_set_content_then_info(self, model: ops.Model, fake_script: FakeScript):

def test_set_info_then_content(self, model: ops.Model, fake_script: FakeScript):
fake_script.write('secret-set', """exit 0""")
ops.Secret._secret_set_cache.clear()

secret = self.make_secret(model, id='q')
description = 'desc'
Expand Down

0 comments on commit c9be2ed

Please sign in to comment.