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 b741f06 commit a179166
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 @@ -306,7 +305,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 @@ -335,6 +340,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 @@ -504,7 +510,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 @@ -687,7 +699,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 @@ -1266,14 +1284,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 @@ -1283,6 +1300,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 @@ -3787,7 +3787,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 @@ -3827,7 +3826,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 @@ -3854,7 +3852,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 a179166

Please sign in to comment.