Merge "Use library cloud_credentials v1.0" into main

This commit is contained in:
Zuul 2023-03-07 13:47:39 +00:00 committed by Gerrit Code Review
commit b19fcbc9fd
4 changed files with 39 additions and 16 deletions

View File

@ -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.nginx_ingress_integrator.v0.ingress
charmcraft fetch-lib charms.data_platform_libs.v0.database_requires 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.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.rabbitmq_k8s.v0.rabbitmq
charmcraft fetch-lib charms.ovn_central_k8s.v0.ovsdb charmcraft fetch-lib charms.ovn_central_k8s.v0.ovsdb
charmcraft fetch-lib charms.observability_libs.v0.kubernetes_service_patch charmcraft fetch-lib charms.observability_libs.v0.kubernetes_service_patch

View File

@ -977,7 +977,7 @@ class CloudCredentialsRequiresHandler(RelationHandler):
def setup_event_handler(self) -> ops.charm.Object: def setup_event_handler(self) -> ops.charm.Object:
"""Configure event handlers for cloud-credentials relation.""" """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") logger.debug("Setting up the cloud-credentials event handler")
credentials_service = cloud_credentials.CloudCredentialsRequires( credentials_service = cloud_credentials.CloudCredentialsRequires(

View File

@ -402,6 +402,9 @@ def add_cloud_credentials_relation_response(
harness: Harness, rel_id: str harness: Harness, rel_id: str
) -> None: ) -> None:
"""Add id service data to identity-service relation.""" """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( harness.update_relation_data(
rel_id, rel_id,
"keystone", "keystone",
@ -413,8 +416,7 @@ def add_cloud_credentials_relation_response(
"internal-host": "keystone.internal", "internal-host": "keystone.internal",
"internal-port": "5000", "internal-port": "5000",
"internal-protocol": "http", "internal-protocol": "http",
"username": "username", "credentials": credentials_id,
"password": "user-password",
"project-name": "user-project", "project-name": "user-project",
"project-id": "uproj-id", "project-id": "uproj-id",
"user-domain-name": "udomain-name", "user-domain-name": "udomain-name",

View File

@ -84,17 +84,20 @@ from ops.framework import (
EventSource, EventSource,
Object, Object,
) )
from ops.model import Relation from ops.model import (
Relation,
SecretNotFoundError,
)
# The unique Charmhub library identifier, never change it # The unique Charmhub library identifier, never change it
LIBID = "a5d96cc2686c47eea554ce2210c2d24e" LIBID = "a5d96cc2686c47eea554ce2210c2d24e"
# Increment this major API version when introducing breaking changes # Increment this major API version when introducing breaking changes
LIBAPI = 0 LIBAPI = 1
# Increment this PATCH version before using `charmcraft publish-lib` or reset # Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version # to 0 if you are raising the major API version
LIBPATCH = 1 LIBPATCH = 0
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -165,7 +168,7 @@ class CloudCredentialsRequires(Object):
logging.debug("CloudCredentials on_changed") logging.debug("CloudCredentials on_changed")
try: try:
self.on.ready.emit() self.on.ready.emit()
except AttributeError: except (AttributeError, KeyError):
logger.exception('Error when emitting event') logger.exception('Error when emitting event')
def _on_cloud_credentials_relation_broken(self, event): def _on_cloud_credentials_relation_broken(self, event):
@ -218,15 +221,35 @@ class CloudCredentialsRequires(Object):
"""Return the internal_protocol.""" """Return the internal_protocol."""
return self.get_remote_app_data('internal-protocol') return self.get_remote_app_data('internal-protocol')
@property
def credentials(self) -> str:
return self.get_remote_app_data('credentials')
@property @property
def username(self) -> str: def username(self) -> str:
"""Return the username.""" credentials_id = self.get_remote_app_data('credentials')
return self.get_remote_app_data('username') 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 @property
def password(self) -> str: def password(self) -> str:
"""Return the password.""" credentials_id = self.get_remote_app_data('credentials')
return self.get_remote_app_data('password') 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 @property
def project_name(self) -> str: def project_name(self) -> str:
@ -382,8 +405,7 @@ class CloudCredentialsProvides(Object):
internal_host: str, internal_host: str,
internal_port: str, internal_port: str,
internal_protocol: str, internal_protocol: str,
username: str, credentials: str,
password: str,
project_name: str, project_name: str,
project_id: str, project_id: str,
user_domain_name: str, user_domain_name: str,
@ -407,8 +429,7 @@ class CloudCredentialsProvides(Object):
app_data["internal-host"] = internal_host app_data["internal-host"] = internal_host
app_data["internal-port"] = str(internal_port) app_data["internal-port"] = str(internal_port)
app_data["internal-protocol"] = internal_protocol app_data["internal-protocol"] = internal_protocol
app_data["username"] = username app_data["credentials"] = credentials
app_data["password"] = password
app_data["project-name"] = project_name app_data["project-name"] = project_name
app_data["project-id"] = project_id app_data["project-id"] = project_id
app_data["user-domain-name"] = user_domain_name app_data["user-domain-name"] = user_domain_name