Interface for communicating db connection info/creds from application <-> application
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.md
interface.ayml
provides.py
requires.py

README.md

interface-db-info

This interface is used to help ease the process of getting database creds from a web to a worker.

Usage

Include the 'db-info' interface in your layer or charm's layer.yaml.

includes:
  - 'interface:db-info'

Provides Relation

If your layer or charm needs to provide database info, configure the provides stanza in your metadata.yaml to provide the interface.

provides:
  db-info:
    interface: db-info 

Set the database info on the provides side of the relation when the db-info.connected flag is set.

@when('pgsql.connected')
@when_not('myapp.postgresql.requested')
def request_application_database(pgsql):
    """Request application database
    """

    conf = config()
    status_set('maintenance', 'Requesting PostgreSQL database')

    pgsql.set_database(conf.get('db-name', 'myapp_db'))

    if conf.get('db-roles'):
        pgsql.set_roles(conf.get('db-roles'))
    if conf.get('db-extensions'):
        pgsql.set_extensions(conf.get('db-extensions'))

    status_set('active', 'Database requested')
    set_state('myapp.postgresql.requested')


@when_not('myapp.postgresql.available')
@when('pgsql.master.available', 'myapp.postgresql.requested')
def save_database_connection_info_to_unitdata(pgsql):
    """Save db-info to unitdata.kv()
    """

    status_set('maintenance',
               'Getting/Setting details for application database.')
    kv.set('dbname', pgsql.master.dbname)
    kv.set('dbuser', pgsql.master.user)
    kv.set('dbpass', pgsql.master.password)
    kv.set('dbhost', pgsql.master.host)
    kv.set('dbport', pgsql.master.port)

    status_set('active', 'Application database available')
    set_state('myapp.postgresql.available')


@when('myapp.postgresql.available', 'db-info.connected')
@when_not('myapp.db-info.available')
def set_db_info(db_info):
    db_info.set_db_info(
        dbhost=kv.get('dbhost'), dbport=kv.get('dbport'),
        dbuser=kv.get('dbuser'), dbpass=kv.get('dbpass'),
        dbname=kv.get('dbname'))
    set_state('myapp.db-info.available')

Requires Relation

If your layer or charm requires database info, configure the requires stanza in your metadata.yaml.

requires:
  db-info:
    interface: db-info 

Then access the db-info in your reactive code once it has been set by the provides side of the relation.

@when('db-info.available')
@when_not('myapp.db-info.available')
def get_save_db_info(db_info):
    status_set('maintenance',
               'Getting/Setting database connection info.')
    for key, val in db_info.get_db_info().items():
        kv.set(key, val)
    status_set('active', 'Application database available')
    set_state('myapp.db-info.available')


@when('myapp.db-info.available', 'codebase.available')
@when_not('myapp.database.settings.available')
def render_worker_database_settings():
    status_set('maintenance', 'Rendering database settings')
    postgres_config = {'dbname': kv.get('dbname'),
                       'dbuser': kv.get('dbuser'),
                       'dbpass': kv.get('dbpass'),
                       'dbhost': kv.get('dbhost'),
                       'dbport': kv.get('dbport')}
    render_settings_py(settings_filename="database.py",
                       secrets=postgres_config)
    status_set('active', 'Database settings available')
    set_state('myapp.database.settings.available')