I am working on a charm for the BI tool called apache-superset … initially I wrote it to accept db relations because it should be able to relate to a postgresql DB … BUT I now want to add a 2nd “DB” of sorts… Trino!
my question is… how to I most eloquently add a 2nd DB over the same relation… but with perhaps a different action or interface?
name: apache-superset
summary: This charm brings up apache superset
maintainers:
- emcp <emcp@whichdegree.co>
description: |
LXD tested only charm
tags:
- business intelligence
subordinate: false
series:
- focal
- jammy
requires:
db:
interface: pgsql
optional: true
db-relation-joined hook
#!/bin/bash
juju-log -l 'WARNING' 'db-relation-created'
status-set maintenance "Preparing superset to create connection to new DB $(date +"%H:%M")"
relation-set database="mysupersetdb"
juju-log -l 'WARNING' "database is set to mysupersetdb on joined"
if I using trino… I need to get info from TRINO … like IP Address… connection info… but will I create a whole new hook if I reuse the db for the relation?
just want to know if I need to relate different DBs with the same db … and if so how do I pull this info out in the bash hook in order to reaction correctly?
there are two aspects to relationing and I was in search of the following
http-relation-changed.sh
...
juju-log -l 'WARNING' “This is the relation id: $JUJU_RELATION_ID”
juju-log -l 'WARNING' “This is the remote unit: $JUJU_REMOTE_UNIT”
juju-log -l 'WARNING' “This is the local unit: $JUJU_UNIT_NAME”
if [[ $JUJU_REMOTE_UNIT == trino* ]]
trino_server_address=`relation-get private-address`
juju-log -l "WARNING" "(http-changed) We got a Trino server address $trino_server_address"
So the interface is http, but the remote UNIT is what I can pivot to different unit types upon… therein know that I have a TRINO server relating to me and to wire it in appropriately
Glad you figured out how you can access the remote unit’s name, but I suspect things would be easier and less confusing if you gave the two relation endpoints different names. Not sure what the two DBs are for, but presumably they have different purposes.
Even calling them ‘pgql-db’ and ‘trino-db’ would make things a lot clearer, imho.
Come to think of it, the metadata you pasted above:
is in fact invalid: you can’t map the same string (‘db’) to multiple values. So you need to give different names to the endpoints, and that means you can tell which remote you’re talking about by the name of the hook (pgql-db-relation-changed vs trino-db-relation-changed, for example).
If you really care about the remote unit name, however, you will need to use the envvar you already found out about.
I actually reworked the trino charm entirely… because in fact it is NOT a DB… but a sort of middleware over REST (http) … but this is very helpful to learn how best to interface with the ecosystem at large.