Merge ~jamesbeedy/interface-grafana-source:relation_base_to_endpoint into interface-grafana-source:master

Proposed by james beedy
Status: Work in progress
Proposed branch: ~jamesbeedy/interface-grafana-source:relation_base_to_endpoint
Merge into: interface-grafana-source:master
Diff against target: 91 lines (+40/-26)
1 file modified
requires.py (+40/-26)
Reviewer Review Type Date Requested Status
Prometheus Charmers Pending
Review via email: mp+393217@code.launchpad.net

Description of the change

Convert the problematic RelationBase requires endpoint to use reactive.Endpoint.

To post a comment you must log in.
Revision history for this message
Drew Freiberger (afreiberger) wrote :

There is a bug with the related update to charm-grafana. Setting this to WIP awaiting review and remediation of the issue in:
https://code.launchpad.net/%7Ejamesbeedy/charm-grafana/+git/charm-grafana/+merge/393218/+index?ss=1

Unmerged commits

ec02d32... by james beedy

only get data from the first unit

3954796... by james beedy

RelationBase -> Endpoint

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/requires.py b/requires.py
2index 862f46b..390e1bb 100644
3--- a/requires.py
4+++ b/requires.py
5@@ -1,28 +1,30 @@
6-from charms.reactive import hook
7-from charms.reactive import RelationBase
8-from charms.reactive import scopes
9+#!/usr/bin/python3
10+from charms.reactive import (
11+ Endpoint,
12+ clear_flag,
13+ set_flag,
14+ when_any,
15+)
16
17
18-class GrafanaRequires(RelationBase):
19- scope = scopes.SERVICE
20+class GrafanaRequires(Endpoint):
21+ """GrafanaRequires."""
22
23- @hook('{requires:grafana-source}-relation-{joined,changed}')
24+ @when_any('endpoint.{endpoint_name}.joined',
25+ 'endpoint.{endpoint_name}.changed')
26 def changed(self):
27- conv = self.conversation()
28- if conv.get_remote('type'):
29- conv.set_state('{relation_name}.available')
30+ if self.all_joined_units.received.get('type'):
31+ set_flag(f'{self.endpoint_name}.available')
32
33- @hook('{requires:grafana-source}-relation-departed')
34- def broken(self):
35- conv = self.conversation()
36- conv.remove_state('{relation_name}.available')
37+ @when_any('endpoint.{endpoint_name}.broken',
38+ 'endpoint.{endpoint_name}.departed')
39+ def departed(self):
40+ clear_flag(f'{self.endpoint_name}.available')
41
42 def datasources(self):
43 """
44 Returns a list of available datasources
45-
46 The return value is a list of dicts of the following form::
47-
48 [
49 {
50 'service_name': remote_service_name,
51@@ -37,17 +39,29 @@ class GrafanaRequires(RelationBase):
52 ]
53 """
54 datasources = []
55- for conv in self.conversations():
56- ds = {'service_name': conv.scope.split('/')[0],
57- 'type': conv.get_remote('type'),
58- 'url': conv.get_remote('url'),
59- 'description': conv.get_remote('description')
60- }
61- if conv.get_remote('username') and conv.get_remote('password'):
62- ds['username'] = conv.get_remote('username')
63- ds['password'] = conv.get_remote('password')
64- if conv.get_remote('database'):
65- ds['database'] = conv.get_remote('database')
66+
67+ for relation in self.relations:
68+
69+ # Only look at a single unit
70+ unit = relation.joined_units[0]
71+
72+ ds = {
73+ 'service_name': relation.application_name,
74+ 'type': unit.received.get('type'),
75+ 'url': unit.received.get('url'),
76+ 'description': unit.received.get('description'),
77+ }
78+
79+ username = unit.received.get('username')
80+ password = unit.received.get('password')
81+ if username and password:
82+ ds['username'] = username
83+ ds['password'] = password
84+
85+ database = unit.received.get('database')
86+ if database:
87+ ds['database'] = database
88+
89 datasources.append(ds)
90
91 return datasources

Subscribers

People subscribed via source and target branches