Merge charm-grafana:dev/fix-default-data-source-values into charm-grafana:master

Proposed by David O Neill
Status: Superseded
Proposed branch: charm-grafana:dev/fix-default-data-source-values
Merge into: charm-grafana:master
Diff against target: 179 lines (+44/-14)
4 files modified
src/lib/charms/layer/grafana.py (+4/-1)
src/reactive/grafana.py (+28/-9)
src/tests/functional/tests/test_grafana.py (+9/-3)
src/tests/unit/test_grafana.py (+3/-1)
Reviewer Review Type Date Requested Status
Adam Dyess (community) Approve
Alvaro Uria (community) Needs Fixing
Review via email: mp+391521@code.launchpad.net

This proposal has been superseded by a proposal from 2020-09-30.

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

3909788... by David O Neill

fix missing default

Revision history for this message
Alvaro Uria (aluria) wrote :

"make test" fails due to linting (seems unrelated to this change, but it would be good to fix):
"""
lint run-test-pre: PYTHONHASHSEED='1443983391'
lint run-test: commands[0] | flake8
./reactive/grafana.py:987:60: W291 trailing whitespace
ERROR: InvocationError for command /home/circleci/project/charm-grafana/src/.tox/lint/bin/flake8 (exited with code 1)
___________________________________ summary ____________________________________
ERROR: lint: commands failed
Makefile:57: recipe for target 'lint' failed
make: *** [lint] Error 1
"""

Have we tested if the change works on other versions of the grafana snap? 6/stable and latest/stable point to v6.7.4, while 7/stable points to v7.0.3. The APT package is on v7, too, and this change should be tested on both v6 (only available via snap) and v7.
Note: Func tests already test 6/stable from the snap, as well as changes to 7/stable; Non-snap func tests (aka via apt) already test v7 directly.

Marked as "Needs fixing" so "make test" can pass.

review: Needs Fixing
faee20b... by David O Neill

Rework values for version 7 of snap and fix linting issues

Revision history for this message
Adam Dyess (addyess) wrote :

Thanks for fixing the linting + black here. LGTM although this .append(...) stuff isn't super clear

review: Approve

Unmerged commits

faee20b... by David O Neill

Rework values for version 7 of snap and fix linting issues

3909788... by David O Neill

fix missing default

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/lib/charms/layer/grafana.py b/src/lib/charms/layer/grafana.py
2index d48c5bd..ecd8c69 100644
3--- a/src/lib/charms/layer/grafana.py
4+++ b/src/lib/charms/layer/grafana.py
5@@ -118,7 +118,10 @@ def post_dashboard(name, dashboard):
6 return (False, "Unable to retrieve grafana password.")
7 api_auth = ("admin", passwd)
8 r = requests.post(
9- import_url, auth=api_auth, headers=headers, data=json.dumps(dashboard),
10+ import_url,
11+ auth=api_auth,
12+ headers=headers,
13+ data=json.dumps(dashboard),
14 )
15 if r.status_code == 200:
16 return (True, None)
17diff --git a/src/reactive/grafana.py b/src/reactive/grafana.py
18index ff26b3e..88e7eea 100644
19--- a/src/reactive/grafana.py
20+++ b/src/reactive/grafana.py
21@@ -188,7 +188,8 @@ def install_packages():
22
23 if config.changed("install_file") and config.get("install_file"):
24 hookenv.status_set(
25- "maintenance", "Installing deb pkgs since install_file changed",
26+ "maintenance",
27+ "Installing deb pkgs since install_file changed",
28 )
29
30 url = config.get("install_file")
31@@ -286,7 +287,8 @@ def install_plugins():
32 def upgrade_charm():
33 """Run upgrade charm hook."""
34 hookenv.status_set(
35- "maintenance", "Forcing package update and reconfiguration on upgrade-charm",
36+ "maintenance",
37+ "Forcing package update and reconfiguration on upgrade-charm",
38 )
39 remove_state("grafana.installed")
40 remove_state("grafana.backup.configured")
41@@ -318,7 +320,9 @@ def config_changed():
42 else:
43 # Install from requested channel
44 snap.install(
45- SNAP_NAME, channel=config["snap_channel"], force_dangerous=False,
46+ SNAP_NAME,
47+ channel=config["snap_channel"],
48+ force_dangerous=False,
49 )
50 remove_state("grafana.configured")
51 if config.changed("dashboards_backup_schedule") or config.changed(
52@@ -401,7 +405,8 @@ def add_backup_api_keys():
53 for i in select_query("SELECT id FROM org"):
54 org_id = i[0]
55 if select_query(
56- "SELECT id FROM api_key WHERE org_id=? AND name=?", [org_id, name],
57+ "SELECT id FROM api_key WHERE org_id=? AND name=?",
58+ [org_id, name],
59 ):
60 hookenv.log(
61 "API key {} in org {} already exists, skipping".format(name, org_id)
62@@ -475,7 +480,8 @@ def setup_backup_shedule():
63 hookenv.status_set("maintenance", "Configuring grafana dashboard backup")
64 hookenv.log("Setting up dashboards backup job...")
65 host.rsync(
66- "files/dashboards_backup", "/usr/local/bin/dashboards_backup",
67+ "files/dashboards_backup",
68+ "/usr/local/bin/dashboards_backup",
69 )
70 host.mkdir(config.get("dashboards_backup_dir"))
71 settings = {
72@@ -604,7 +610,8 @@ def sources_gone(relation):
73 conn = sqlite3.connect("{}/grafana.db".format(data_dir), timeout=30)
74 cur = conn.cursor()
75 cur.execute(
76- "DELETE FROM DATA_SOURCE WHERE type=? AND url=?", (ds["type"], ds["url"]),
77+ "DELETE FROM DATA_SOURCE WHERE type=? AND url=?",
78+ (ds["type"], ds["url"]),
79 )
80 conn.commit()
81 cur.close()
82@@ -784,7 +791,9 @@ def check_and_add_dashboard(
83 hookenv.log(
84 "Skipping Dashboard Template: {} missing {} metrics."
85 "Missing: {}".format(
86- filename, len(missing_metrics), ", ".join(missing_metrics),
87+ filename,
88+ len(missing_metrics),
89+ ", ".join(missing_metrics),
90 ),
91 hookenv.DEBUG,
92 )
93@@ -820,7 +829,8 @@ def check_and_add_dashboard(
94
95 hookenv.log(
96 "dashboard differences[{}], dashboard title[{}]".format(
97- dashboard_changed, dashboard_json["dashboard"]["title"],
98+ dashboard_changed,
99+ dashboard_json["dashboard"]["title"],
100 ),
101 hookenv.DEBUG,
102 )
103@@ -955,6 +965,14 @@ def generate_query(ds, is_default, id=None, org_id=1):
104 # The 32 bit mask is necessary because Python's 64 bit ints are too
105 # large for SQLite3.
106 values.append(hash(ds["url"]) & 0xFFFFFFFF)
107+ # latest grafana will fail to accept the datasource
108+ # unless these fields are default as follows
109+ fields.append("json_data")
110+ values.append("{}")
111+ fields.append("secure_json_data")
112+ values.append("{}")
113+ fields.append("read_only")
114+ values.append(0)
115
116 # Used in connecting a mysql source
117 if "database" in ds:
118@@ -1020,7 +1038,8 @@ def get_orgs(port, passwd):
119 https://grafana.com/docs/grafana/latest/http_api/org/
120 """
121 req = requests.get(
122- "http://127.0.0.1:{}/api/orgs".format(port), auth=("admin", passwd),
123+ "http://127.0.0.1:{}/api/orgs".format(port),
124+ auth=("admin", passwd),
125 )
126 return req.json() if req.status_code == 200 else []
127
128diff --git a/src/tests/functional/tests/test_grafana.py b/src/tests/functional/tests/test_grafana.py
129index f848077..beeddd2 100644
130--- a/src/tests/functional/tests/test_grafana.py
131+++ b/src/tests/functional/tests/test_grafana.py
132@@ -41,7 +41,9 @@ class BaseGrafanaTest(unittest.TestCase):
133 """Get grafana admin password."""
134 if self._admin_pass is None:
135 action = model.run_action(
136- self.lead_unit_name, "get-admin-password", raise_on_failure=True,
137+ self.lead_unit_name,
138+ "get-admin-password",
139+ raise_on_failure=True,
140 )
141 self._admin_pass = action.data["results"]["password"]
142 return self._admin_pass
143@@ -84,7 +86,9 @@ class CharmOperationTest(BaseGrafanaTest):
144 We'll retry until the TEST_TIMEOUT.
145 """
146 test_command = "{} -I 127.0.0.1 -p {} -u {}".format(
147- "/usr/lib/nagios/plugins/check_http", DEFAULT_API_PORT, DEFAULT_API_URL,
148+ "/usr/lib/nagios/plugins/check_http",
149+ DEFAULT_API_PORT,
150+ DEFAULT_API_URL,
151 )
152 timeout = time.time() + TEST_TIMEOUT
153 while time.time() < timeout:
154@@ -174,7 +178,9 @@ class CharmOperationTest(BaseGrafanaTest):
155 "role": "Viewer",
156 }
157 action = model.run_action(
158- self.lead_unit_name, "create-user", action_params=params,
159+ self.lead_unit_name,
160+ "create-user",
161+ action_params=params,
162 )
163 self.assertTrue(action.data["results"]["Code"] == "0")
164 time.sleep(30) # Dirty hack to overcome race condition
165diff --git a/src/tests/unit/test_grafana.py b/src/tests/unit/test_grafana.py
166index b9c75a9..c2386e4 100644
167--- a/src/tests/unit/test_grafana.py
168+++ b/src/tests/unit/test_grafana.py
169@@ -109,7 +109,9 @@ class GrafanaTestCase(unittest.TestCase):
170
171 @mock.patch.object(grafana, "model_name")
172 @mock.patch.object(
173- grafana, "get_folders", return_value=[{"id": 7, "title": "[juju-foo-model]"}],
174+ grafana,
175+ "get_folders",
176+ return_value=[{"id": 7, "title": "[juju-foo-model]"}],
177 )
178 @mock.patch.object(grafana, "create_folder")
179 def test_ensure_and_get_dash_folder_folder_exists(

Subscribers

No one subscribed via source and target branches