PostgreSQL database relation for Juju Operator Framework Charms
Project description
Juju Operator Framework Charm Interface for PostgreSQL Relations
To use this interface in your
Juju Operator Framework charm,
instruct charmcraft to embed
it into your built Operator Framework charm by adding ops-lib-pgsql to
your requirements.txt
file::
ops
ops-lib-pgsql
Your charm needs to declare its use of the interface in its metadata.yaml
file:
requires:
db:
interface: pgsql
limit: 1 # Most charms only handle a single PostgreSQL Application.
Your charm needs to bootstrap it and handle events:
pgsql = ops.lib.use("pgsql", 1, "postgresql-charmers@lists.launchpad.net")
class MyCharm(ops.charm.CharmBase):
_state = ops.framework.StoredState()
def __init__(self, *args):
super().__init__(*args)
self._state.set_default(db_conn_str=None, db_uri=None, db_ro_uris=[])
self.db = pgsql.PostgreSQLClient(self, 'db') # 'db' relation in metadata.yaml
self.framework.observe(self.db.on.database_relation_joined, self._on_database_relation_joined)
self.framework.observe(self.db.on.master_changed, self._on_master_changed)
self.framework.observe(self.db.on.standby_changed, self._on_standby_changed)
def _on_database_relation_joined(self, event: pgsql.DatabaseRelationJoinedEvent):
if self.model.unit.is_leader():
# Provide requirements to the PostgreSQL server.
event.database = 'mydbname' # Request database named mydbname
event.extensions = ['citext'] # Request the citext extension installed
elif event.database != 'mydbname':
# Leader has not yet set requirements. Defer, incase this unit
# becomes leader and needs to perform that operation.
event.defer()
return
def _on_master_changed(self, event: pgsql.MasterChangedEvent):
if event.database != 'mydbname':
# Leader has not yet set requirements. Wait until next event,
# or risk connecting to an incorrect database.
return
# The connection to the primary database has been created,
# changed or removed. More specific events are available, but
# most charms will find it easier to just handle the Changed
# events. event.master is None if the master database is not
# available, or a pgsql.ConnectionString instance.
self._state.db_conn_str = None if event.master is None else event.master.conn_str
self._state.db_uri = None if event.master is None else event.master.uri
# You probably want to emit an event here or call a setup routine to
# do something useful with the libpq connection string or URI now they
# are available.
def _on_standby_changed(self, event: pgsql.StandbyChangedEvent):
if event.database != 'mydbname':
# Leader has not yet set requirements. Wait until next event,
# or risk connecting to an incorrect database.
return
# Charms needing access to the hot standby databases can get
# their connection details here. Applications can scale out
# horizontally if they can make use of the read only hot
# standby replica databases, rather than only use the single
# master. event.stanbys will be an empty list if no hot standby
# databases are available.
self._state.db_ro_uris = [c.uri for c in event.standbys]
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ops-lib-pgsql-1.4.zip
(25.0 kB
view details)
Built Distribution
File details
Details for the file ops-lib-pgsql-1.4.zip
.
File metadata
- Download URL: ops-lib-pgsql-1.4.zip
- Upload date:
- Size: 25.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ea802a1a9e6be4457dbe49959a76c50d63b689abc1b803c127bd24e5bd98654 |
|
MD5 | 2da14a07f9b939ffa5ef0c85512cf0be |
|
BLAKE2b-256 | 4c9a9ee6cdd38308847df589f2e7e885784d5e450d5a7d8fdd8112bb5d343a15 |
File details
Details for the file ops_lib_pgsql-1.4-py2.py3-none-any.whl
.
File metadata
- Download URL: ops_lib_pgsql-1.4-py2.py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85b07a364339da2423c2631680f03aab8f9b4d8a6493efc66303224ff9ab34af |
|
MD5 | 021c8028136f4572bb8f041a969c6a72 |
|
BLAKE2b-256 | 72cfd6ad2887b77b403a01f640861dfb657de51aefcd928b68bbc46ea0b7c13f |