From 56332c72304b0cdd4179464cf09296d84f6cca0b Mon Sep 17 00:00:00 2001 From: Dmitry Ratushnyy Date: Wed, 6 Sep 2023 12:52:14 +0000 Subject: [PATCH] Address review comments --- src/charm.py | 34 ++++++++++++++-------------------- tox.ini | 16 ++++++++-------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/charm.py b/src/charm.py index 69380c8df..116279219 100755 --- a/src/charm.py +++ b/src/charm.py @@ -596,7 +596,7 @@ def _on_secret_changed(self, event: SecretChangedEvent): f"Secret {event._id}:{event.secret.id} changed, but it's irrelevant for us" ) return - self._update_secrets_cache(scope) + self._update_juju_secrets_cache(scope) self._connect_mongodb_exporter() # END: charm event handlers @@ -1063,7 +1063,6 @@ def auth_enabled(self) -> bool: return any("MONGOD_ARGS" in line and "--auth" in line for line in env_vars) -<<<<<<< HEAD def has_backup_service(self): """Verifies the backup service is available.""" snap_cache = snap.SnapCache() @@ -1110,9 +1109,6 @@ def restart_backup_service(self) -> None: charmed_mongodb_snap.restart(services=["pbm-agent"]) -# pbm_snap.restart(services=["pbm-agent"]) -# END: helper functions -======= def _scope_obj(self, scope: Scopes): if scope == APP_SCOPE: return self.app @@ -1128,7 +1124,7 @@ def _peer_data(self, scope: Scopes): def _juju_secret_set(self, scope: Scopes, key: str, value: str) -> str: """Helper function setting Juju secret in Juju versions >3.0.""" peer_data = self._peer_data(scope) - self._juju_secrets_get(scope) + self._update_juju_secrets_cache(scope) secret = self.secrets[scope].get(Config.Secrets.SECRET_LABEL) @@ -1166,7 +1162,7 @@ def _juju_secret_set(self, scope: Scopes, key: str, value: str) -> str: return self.secrets[scope][Config.Secrets.SECRET_LABEL].id - def _juju_secrets_get(self, scope: Scopes) -> Optional[bool]: + def _update_juju_secrets_cache(self, scope: Scopes) -> None: """Helper function to get Juju secret.""" peer_data = self._peer_data(scope) @@ -1191,27 +1187,26 @@ def _juju_secrets_get(self, scope: Scopes) -> Optional[bool]: # We retrieve and cache actual secret data for the lifetime of the event scope self.secrets[scope][Config.Secrets.SECRET_CACHE_LABEL] = secret.get_content() - if self.secrets[scope].get(Config.Secrets.SECRET_CACHE_LABEL): - return True - return False + def _get_juju_secrets_cache(self, scope: Scopes): + return self.secrets[scope].get(Config.Secrets.SECRET_CACHE_LABEL) def _juju_secret_get(self, scope: Scopes, key: str) -> Optional[str]: """Helper function to get Juju secret.""" if not key: return - - if self._juju_secrets_get(scope): - secret_cache = self.secrets[scope].get(Config.Secrets.SECRET_CACHE_LABEL) - if secret_cache: - secret_data = secret_cache.get(key) - if secret_data and secret_data != Config.Secrets.SECRET_DELETED_LABEL: - logging.debug(f"Getting secret {scope}:{key}") - return secret_data + + self._update_juju_secrets_cache(scope) + secret_cache = self._get_juju_secrets_cache(scope) + if secret_cache: + secret_data = secret_cache.get(key) + if secret_data and secret_data != Config.Secrets.SECRET_DELETED_LABEL: + logging.debug(f"Getting secret {scope}:{key}") + return secret_data logging.debug(f"No value found for secret {scope}:{key}") def _juju_secret_remove(self, scope: Scopes, key: str) -> None: """Remove a Juju 3.x secret.""" - self._juju_secrets_get(scope) + self._update_juju_secrets_cache(scope) secret = self.secrets[scope].get(Config.Secrets.SECRET_LABEL) if not secret: @@ -1228,7 +1223,6 @@ def _juju_secret_remove(self, scope: Scopes, key: str) -> None: logging.debug(f"Secret {scope}:{key}") # END: helper functions ->>>>>>> 008665e (WIP: Fixing issue with charm) if __name__ == "__main__": diff --git a/tox.ini b/tox.ini index bd2955471..bfb2d5786 100644 --- a/tox.ini +++ b/tox.ini @@ -74,7 +74,7 @@ pass_env = CI_PACKED_CHARMS deps = pytest - juju==2.9.44.0 # The latest python-libjuju that supports both juju 2.9 and 3.0 + juju==3.2.0.1 pytest-operator pytest-mock -r {tox_root}/requirements.txt @@ -89,7 +89,7 @@ pass_env = CI_PACKED_CHARMS deps = pytest - juju==2.9.44.0 # The latest python-libjuju that supports both juju 2.9 and 3.0 + juju==3.2.0.1 pytest-operator -r {tox_root}/requirements.txt commands = @@ -103,7 +103,7 @@ pass_env = CI_PACKED_CHARMS deps = pytest - juju==2.9.44.0 # The latest python-libjuju that supports both juju 2.9 and 3.0 + juju==3.2.0.1 pytest-operator -r {tox_root}/requirements.txt commands = @@ -117,7 +117,7 @@ pass_env = CI_PACKED_CHARMS deps = pytest - juju==2.9.44.0 # The latest python-libjuju that supports both juju 2.9 and 3.0 + juju==3.2.0.1 pytest-operator -r {tox_root}/requirements.txt commands = @@ -131,7 +131,7 @@ pass_env = CI_PACKED_CHARMS deps = pytest - juju==2.9.44.0 # The latest python-libjuju that supports both juju 2.9 and 3.0 + juju==3.2.0.1 pytest-operator pytest-mock -r {tox_root}/requirements.txt @@ -151,7 +151,7 @@ pass_env = GCP_SECRET_KEY deps = pytest - juju==2.9.44.0 # The latest python-libjuju that supports both juju 2.9 and 3.0 + juju==3.2.0.1 pytest-operator pytest-mock -r {tox_root}/requirements.txt @@ -166,7 +166,7 @@ pass_env = CI_PACKED_CHARMS deps = pytest - juju==2.9.44.0 # The latest python-libjuju that supports both juju 2.9 and 3.0 + juju==3.2.0.1 pytest-operator -r {tox_root}/requirements.txt commands = @@ -181,7 +181,7 @@ pass_env = CI_PACKED_CHARMS deps = pytest - juju==2.9.44.0 # The latest python-libjuju that supports both juju 2.9 and 3.0 + juju==3.2.0.1 pytest-operator -r {tox_root}/requirements.txt commands =