Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added grafana uid #364

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions lib/charms/grafana_k8s/v0/grafana_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ def __init__(self, *args):
)
from ops.model import Relation

from charms.observability_libs.v0.juju_topology import JujuTopology
PietroPasotti marked this conversation as resolved.
Show resolved Hide resolved

# The unique Charmhub library identifier, never change it
LIBID = "974705adb86f40228298156e34b460dc"

Expand All @@ -162,7 +164,7 @@ def __init__(self, *args):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 23
LIBPATCH = 24

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -432,13 +434,22 @@ def update_source(self, source_url: Optional[str] = ""):
def get_source_uids(self) -> Dict[str, Dict[str, str]]:
"""Get the datasource UID(s) assigned by the remote end(s) to this datasource.

Returns a mapping from remote application names to unit names to datasource uids.
Returns a mapping from remote application UIDs to unit names to datasource uids.
"""
uids = {}
for rel in self._charm.model.relations.get(self._relation_name, []):
if not rel:
continue
uids[rel.app.name] = json.loads(rel.data[rel.app].get("datasource_uids", "{}"))
app_databag = rel.data[rel.app]
grafana_uid = app_databag.get("grafana_uid")
if not grafana_uid:
logger.warning(
"remote end is using an old grafana_datasource interface: "
"`grafana_uid` field not found."
)
continue

uids[grafana_uid] = json.loads(app_databag.get("datasource_uids", "{}"))
return uids

def _set_sources_from_event(self, event: RelationJoinedEvent) -> None:
Expand Down Expand Up @@ -568,6 +579,16 @@ def _publish_source_uids(self, rel: Relation, uids: Dict[str, str]):

Assumes only leader unit will call this method
"""
juju_topology = JujuTopology.from_charm(self._charm)
unique_grafana_name = "juju_{}_{}_{}_{}".format(
juju_topology.model,
juju_topology.model_uuid,
juju_topology.application,
# we inited it from_charm(), so .unit is guaranteed to be set.
juju_topology.unit.split("/")[1], # type: ignore
)

rel.data[self._charm.app]["grafana_uid"] = unique_grafana_name
rel.data[self._charm.app]["datasource_uids"] = json.dumps(uids)

def _get_source_config(self, rel: Relation):
Expand Down
11 changes: 8 additions & 3 deletions tests/scenario/test_datasources.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_datasource_sharing(ctx):
"remote_host/0": "juju_foo_bar_baz_0",
"remote_host/1": "juju_foo_bar_baz_1",
}
assert local_app_data["grafana_uid"]


def test_datasource_get():
Expand All @@ -59,7 +60,10 @@ def test_datasource_get():
{"model": "foo", "model_uuid": "bar", "application": "baz", "type": "tempo"}
)
},
remote_app_data={"datasource_uids": json.dumps(local_ds_uids)},
remote_app_data={
"grafana_uid": "foo-grafana-1",
"datasource_uids": json.dumps(local_ds_uids),
},
)
state = State(leader=True, relations={datasource})

Expand All @@ -85,6 +89,7 @@ def __init__(self, framework: Framework):

def test_datasource_get_nodata():
# GIVEN a datasource relation with two remote units, but which hasn't shared any datasource uids
# for example because the remote end is using an older charm lib
datasource = Relation(
"grafana-source",
remote_app_name="remote_host",
Expand Down Expand Up @@ -115,5 +120,5 @@ def __init__(self, framework: Framework):
ctx = Context(MyProviderCharm, MyProviderCharm.META)
with ctx(ctx.on.relation_changed(datasource), state) as mgr:
charm = mgr.charm
# THEN we can see our datasource uids via the provider
assert list(charm.source_provider.get_source_uids().values())[0] == {}
# THEN we can see no datasource uids via the provider
assert not charm.source_provider.get_source_uids()
Loading