The dpcharmlibs.interfaces package.
Project description
dpcharmlibs.interfaces: Library to manage the relation for the data-platform products
The interfaces library.
To install, add dpcharmlibs-interfaces to your Python dependencies. Then in your Python code, import as:
from dpcharmlibs import interfaces
Abstract
This V1 has been specified in https://docs.google.com/document/d/1lnuonWnoQb36RWYwfHOBwU0VClLbawpTISXIC_yNKYo, and should be backward compatible with v0 clients.
This library contains the Requires and Provides classes for handling the relation between an application and multiple managed application supported by the data-team: MySQL, Postgresql, MongoDB, Redis, Kafka, and Karapace.
Components
Models
This library exposes basic default models that can be used in most cases. If you need more complex models, you can subclass them.
from dpcharmlibs.interfaces import RequirerCommonModel, ExtraSecretStr
class ExtendedCommonModel(RequirerCommonModel):
operator_password: ExtraSecretStr
Secret groups are handled using annotated types.
If you wish to add extra secret groups, please follow the following model.
The string metadata represents the secret group name, and OptionalSecretStr is a TypeAlias for
SecretStr | None. Finally, SecretStr represents a field validating the URI pattern secret:.*
MyGroupSecretStr = Annotated[OptionalSecretStr, Field(exclude=True, default=None), "mygroup"]
Fields not specified as OptionalSecretStr and extended with a group name in the metadata will NOT get serialised.
Requirer Charm
This library is a uniform interface to a selection of common database metadata, with added custom events that add convenience to database management, and methods to consume the application related data.
from dpcharmlibs.interfaces import (
RequirerCommonModel,
RequirerDataContractV1,
ResourceCreatedEvent,
ResourceEntityCreatedEvent,
ResourceProviderModel,
ResourceRequirerEventHandler,
)
class ClientCharm(CharmBase):
# Database charm that accepts connections from application charms.
def __init__(self, *args) -> None:
super().__init__(*args)
requests = [
RequirerCommonModel(
resource="clientdb",
),
RequirerCommonModel(
resource="clientbis",
),
RequirerCommonModel(
entity_type="USER",
)
]
self.database = ResourceRequirerEventHandler(
self,"database", requests, response_model=ResourceProviderModel
)
self.framework.observe(self.database.on.resource_created, self._on_resource_created)
self.framework.observe(self.database.on.resource_entity_created, self._on_entity_created)
def _on_resource_created(self, event: ResourceCreatedEvent) -> None:
# Event triggered when a new database is created.
relation_id = event.relation.id
response = event.response # This is the response model
username = event.response.username
password = event.response.password
...
def _on_entity_created(self, event: ResourceCreatedEvent) -> None:
# Event triggered when a new entity is created.
...
Compared to V0, this library makes heavy use of pydantic models, and allows for multiple requests, specified as a list. On the Requirer side, each response will trigger one custom event for that response. This way, it allows for more strategic events to be emitted according to the request.
As show above, the library provides some custom events to handle specific situations, which are listed below:
- resource_created: event emitted when the requested database is created.
- resource_entity_created: event emitted when the requested entity is created.
- endpoints_changed: event emitted when the read/write endpoints of the database have changed.
- read_only_endpoints_changed: event emitted when the read-only endpoints of the database have changed. Event is not triggered if read/write endpoints changed too.
If it is needed to connect multiple database clusters to the same relation endpoint the application charm can implement the same code as if it would connect to only one database cluster (like the above code example).
To differentiate multiple clusters connected to the same relation endpoint the application charm can use the name of the remote application:
def _on_resource_created(self, event: ResourceCreatedEvent) -> None:
# Get the remote app name of the cluster that triggered this event
cluster = event.relation.app.name
It is also possible to provide an alias for each different database cluster/relation.
So, it is possible to differentiate the clusters in two ways.
The first is to use the remote application name, with event.relation.app.name.
The second way is to use different event handlers to handle each cluster events.
The implementation would be something like the following code:
from dpcharmlibs.interfaces import (
RequirerCommonModel,
RequirerDataContractV1,
ResourceCreatedEvent,
ResourceEntityCreatedEvent,
ResourceProviderModel,
ResourceRequirerEventHandler,
)
class ApplicationCharm(CharmBase):
# Application charm that connects to database charms.
def __init__(self, *args):
super().__init__(*args)
requests = [
RequirerCommonModel(
resource="clientdb",
),
RequirerCommonModel(
resource="clientbis",
),
]
# Define the cluster aliases and one handler for each cluster database
# created event.
self.database = ResourceRequirerEventHandler(
self,
relation_name="database"
relations_aliases = ["cluster1", "cluster2"],
requests=
)
self.framework.observe(
self.database.on.cluster1_resource_created, self._on_cluster1_resource_created
)
self.framework.observe(
self.database.on.cluster2_resource_created, self._on_cluster2_resource_created
)
def _on_cluster1_resource_created(self, event: ResourceCreatedEvent) -> None:
# Handle the created database on the cluster named cluster1
# Create configuration file for app
config_file = self._render_app_config_file(
event.response.username,
event.response.password,
event.response.endpoints,
)
...
def _on_cluster2_resource_created(self, event: ResourceCreatedEvent) -> None:
# Handle the created database on the cluster named cluster2
# Create configuration file for app
config_file = self._render_app_config_file(
event.response.username,
event.response.password,
event.response.endpoints,
)
...
Provider Charm
Following an example of using the ResourceRequestedEvent, in the context of the database charm code:
from dpcharmlibs.interfaces import (
ResourceProviderEventHandler,
ResourceProviderModel,
ResourceRequestedEvent,
RequirerCommonModel,
)
class SampleCharm(CharmBase):
def __init__(self, *args):
super().__init__(*args)
# Charm events defined in the database provides charm library.
self.provided_database = ResourceProviderEventHandler(
self, "database", RequirerCommonModel,
)
self.framework.observe(self.provided_database.on.resource_requested,
self._on_resource_requested)
# Database generic helper
self.database = DatabaseHelper()
def _on_resource_requested(self, event: ResourceRequestedEvent) -> None:
# Handle the event triggered by a new database requested in the relation
# Retrieve the database name using the charm library.
db_name = event.request.resource
# generate a new user credential
username = self.database.generate_user(event.request.request_id)
password = self.database.generate_password(event.request.request_id)
# set the credentials for the relation
response = ResourceProviderModel(
salt=event.request.salt,
request_id=event.request.request_id,
resource=db_name,
username=username,
password=password,
...
)
self.provided_database.set_response(event.relation.id, response)
As shown above, the library provides a custom event (resource_requested) to handle the situation when an application charm requests a new database to be created. It's preferred to subscribe to this event instead of relation changed event to avoid creating a new database when other information other than a database name is exchanged in the relation databag.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dpcharmlibs_interfaces-1.0.1.tar.gz.
File metadata
- Download URL: dpcharmlibs_interfaces-1.0.1.tar.gz
- Upload date:
- Size: 203.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8334262d233fc97729b1367522b657624de82a176cfe721f4a196969fbd3fdef
|
|
| MD5 |
3b2aefbb8a4cc06f89311ba730172478
|
|
| BLAKE2b-256 |
61ab326c68d8e8991b8c510ac1ab9a882d6c6cc43ea13609f0befd10929ae41a
|
Provenance
The following attestation bundles were made for dpcharmlibs_interfaces-1.0.1.tar.gz:
Publisher:
publish.yaml on canonical/data-platform-charmlibs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dpcharmlibs_interfaces-1.0.1.tar.gz -
Subject digest:
8334262d233fc97729b1367522b657624de82a176cfe721f4a196969fbd3fdef - Sigstore transparency entry: 1203469814
- Sigstore integration time:
-
Permalink:
canonical/data-platform-charmlibs@59307ef7fd30b3a1250f940b9dee1f828850dd89 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@59307ef7fd30b3a1250f940b9dee1f828850dd89 -
Trigger Event:
push
-
Statement type:
File details
Details for the file dpcharmlibs_interfaces-1.0.1-py3-none-any.whl.
File metadata
- Download URL: dpcharmlibs_interfaces-1.0.1-py3-none-any.whl
- Upload date:
- Size: 37.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad238c4b4350b13ef8d7818340a96e3a98d64110119e0d1b1f98f845021b7555
|
|
| MD5 |
dc70ff0c5c5041ebf593a4a76c5d4351
|
|
| BLAKE2b-256 |
ba62f46e48d50827153ceff5e1e4216a54e37f26a1c1d87721069cbb91a62e27
|
Provenance
The following attestation bundles were made for dpcharmlibs_interfaces-1.0.1-py3-none-any.whl:
Publisher:
publish.yaml on canonical/data-platform-charmlibs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dpcharmlibs_interfaces-1.0.1-py3-none-any.whl -
Subject digest:
ad238c4b4350b13ef8d7818340a96e3a98d64110119e0d1b1f98f845021b7555 - Sigstore transparency entry: 1203469815
- Sigstore integration time:
-
Permalink:
canonical/data-platform-charmlibs@59307ef7fd30b3a1250f940b9dee1f828850dd89 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@59307ef7fd30b3a1250f940b9dee1f828850dd89 -
Trigger Event:
push
-
Statement type: