From aa0c20d563434a097eb035943b102410183b00f8 Mon Sep 17 00:00:00 2001 From: Hemanth Nakkina Date: Tue, 7 Mar 2023 16:04:10 +0530 Subject: [PATCH] Use library cloud_credentials v1.0 Update cloud_credentials library to v1.0 Change-Id: Ia63aa2989497555f0c39699d99c4090f3e5866f5 --- ops-sunbeam/fetch-libs.sh | 2 +- ops-sunbeam/ops_sunbeam/relation_handlers.py | 2 +- ops-sunbeam/ops_sunbeam/test_utils.py | 6 ++- .../{v0 => v1}/cloud_credentials.py | 45 ++++++++++++++----- 4 files changed, 39 insertions(+), 16 deletions(-) rename ops-sunbeam/unit_tests/lib/charms/keystone_k8s/{v0 => v1}/cloud_credentials.py (92%) diff --git a/ops-sunbeam/fetch-libs.sh b/ops-sunbeam/fetch-libs.sh index d38a620a..05950abd 100755 --- a/ops-sunbeam/fetch-libs.sh +++ b/ops-sunbeam/fetch-libs.sh @@ -7,7 +7,7 @@ echo "WARNING: Charm interface libs are excluded from ASO python package." charmcraft fetch-lib charms.nginx_ingress_integrator.v0.ingress charmcraft fetch-lib charms.data_platform_libs.v0.database_requires charmcraft fetch-lib charms.keystone_k8s.v1.identity_service -charmcraft fetch-lib charms.keystone_k8s.v0.cloud_credentials +charmcraft fetch-lib charms.keystone_k8s.v1.cloud_credentials charmcraft fetch-lib charms.rabbitmq_k8s.v0.rabbitmq charmcraft fetch-lib charms.ovn_central_k8s.v0.ovsdb charmcraft fetch-lib charms.observability_libs.v0.kubernetes_service_patch diff --git a/ops-sunbeam/ops_sunbeam/relation_handlers.py b/ops-sunbeam/ops_sunbeam/relation_handlers.py index 994fc04f..1e7970d8 100644 --- a/ops-sunbeam/ops_sunbeam/relation_handlers.py +++ b/ops-sunbeam/ops_sunbeam/relation_handlers.py @@ -955,7 +955,7 @@ class CloudCredentialsRequiresHandler(RelationHandler): def setup_event_handler(self) -> ops.charm.Object: """Configure event handlers for cloud-credentials relation.""" - import charms.keystone_k8s.v0.cloud_credentials as cloud_credentials + import charms.keystone_k8s.v1.cloud_credentials as cloud_credentials logger.debug("Setting up the cloud-credentials event handler") credentials_service = cloud_credentials.CloudCredentialsRequires( diff --git a/ops-sunbeam/ops_sunbeam/test_utils.py b/ops-sunbeam/ops_sunbeam/test_utils.py index 5a2301f4..ec5628fb 100644 --- a/ops-sunbeam/ops_sunbeam/test_utils.py +++ b/ops-sunbeam/ops_sunbeam/test_utils.py @@ -402,6 +402,9 @@ def add_cloud_credentials_relation_response( harness: Harness, rel_id: str ) -> None: """Add id service data to identity-service relation.""" + credentials_content = {"username": "username", "password": "user-password"} + credentials_id = harness.add_model_secret("keystone", credentials_content) + harness.grant_secret(credentials_id, harness.charm.app.name) harness.update_relation_data( rel_id, "keystone", @@ -413,8 +416,7 @@ def add_cloud_credentials_relation_response( "internal-host": "keystone.internal", "internal-port": "5000", "internal-protocol": "http", - "username": "username", - "password": "user-password", + "credentials": credentials_id, "project-name": "user-project", "project-id": "uproj-id", "user-domain-name": "udomain-name", diff --git a/ops-sunbeam/unit_tests/lib/charms/keystone_k8s/v0/cloud_credentials.py b/ops-sunbeam/unit_tests/lib/charms/keystone_k8s/v1/cloud_credentials.py similarity index 92% rename from ops-sunbeam/unit_tests/lib/charms/keystone_k8s/v0/cloud_credentials.py rename to ops-sunbeam/unit_tests/lib/charms/keystone_k8s/v1/cloud_credentials.py index 0384119a..9ff0a8d3 100644 --- a/ops-sunbeam/unit_tests/lib/charms/keystone_k8s/v0/cloud_credentials.py +++ b/ops-sunbeam/unit_tests/lib/charms/keystone_k8s/v1/cloud_credentials.py @@ -84,17 +84,20 @@ from ops.framework import ( EventSource, Object, ) -from ops.model import Relation +from ops.model import ( + Relation, + SecretNotFoundError, +) # The unique Charmhub library identifier, never change it LIBID = "a5d96cc2686c47eea554ce2210c2d24e" # Increment this major API version when introducing breaking changes -LIBAPI = 0 +LIBAPI = 1 # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 1 +LIBPATCH = 0 logger = logging.getLogger(__name__) @@ -165,7 +168,7 @@ class CloudCredentialsRequires(Object): logging.debug("CloudCredentials on_changed") try: self.on.ready.emit() - except AttributeError: + except (AttributeError, KeyError): logger.exception('Error when emitting event') def _on_cloud_credentials_relation_broken(self, event): @@ -218,15 +221,35 @@ class CloudCredentialsRequires(Object): """Return the internal_protocol.""" return self.get_remote_app_data('internal-protocol') + @property + def credentials(self) -> str: + return self.get_remote_app_data('credentials') + @property def username(self) -> str: - """Return the username.""" - return self.get_remote_app_data('username') + credentials_id = self.get_remote_app_data('credentials') + if not credentials_id: + return None + + try: + credentials = self.charm.model.get_secret(id=credentials_id) + return credentials.get_content().get("username") + except SecretNotFoundError: + logger.warning(f"Secret {credentials_id} not found") + return None @property def password(self) -> str: - """Return the password.""" - return self.get_remote_app_data('password') + credentials_id = self.get_remote_app_data('credentials') + if not credentials_id: + return None + + try: + credentials = self.charm.model.get_secret(id=credentials_id) + return credentials.get_content().get("password") + except SecretNotFoundError: + logger.warning(f"Secret {credentials_id} not found") + return None @property def project_name(self) -> str: @@ -382,8 +405,7 @@ class CloudCredentialsProvides(Object): internal_host: str, internal_port: str, internal_protocol: str, - username: str, - password: str, + credentials: str, project_name: str, project_id: str, user_domain_name: str, @@ -407,8 +429,7 @@ class CloudCredentialsProvides(Object): app_data["internal-host"] = internal_host app_data["internal-port"] = str(internal_port) app_data["internal-protocol"] = internal_protocol - app_data["username"] = username - app_data["password"] = password + app_data["credentials"] = credentials app_data["project-name"] = project_name app_data["project-id"] = project_id app_data["user-domain-name"] = user_domain_name