From 72080f7136fee7713ad0dbcaa466137cfad268d2 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Fri, 11 Mar 2022 11:28:48 +1100 Subject: [PATCH] Strip id/uid from .json input As noted inline, a default export from Grafana includes the uid/id which breaks when you re-import it into a different site. Strip them. Change-Id: I318614b33aed2ece93d44e832bad7907724cb1bc --- grafana_dashboards/parser.py | 7 +++++++ tests/fixtures/parser/json-dashboard-0001.json | 3 ++- tests/test_parser.py | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/grafana_dashboards/parser.py b/grafana_dashboards/parser.py index 4e6e3c2..96dc204 100644 --- a/grafana_dashboards/parser.py +++ b/grafana_dashboards/parser.py @@ -56,6 +56,13 @@ class YamlParser(object): slug = slugify(data['title']) if not self.data.get('dashboard'): self.data['dashboard'] = {} + # The idea is that people work interactively locally using + # the GUI editor, then export the .json using the sharing + # panel. That mostly works, but it can leave a id & uid + # that isn't valid when importing to another grafana + # instance. Remove them. + data.pop('id', None) + data.pop('uid', None) self.data['dashboard'][slug] = data else: result = self.validate(data) diff --git a/tests/fixtures/parser/json-dashboard-0001.json b/tests/fixtures/parser/json-dashboard-0001.json index 2279700..e03593b 100644 --- a/tests/fixtures/parser/json-dashboard-0001.json +++ b/tests/fixtures/parser/json-dashboard-0001.json @@ -137,5 +137,6 @@ "timezone": "", "title": "test json", "uid": "M-GEcyWMk", - "version": 1 + "version": 1, + "id": 1234 } diff --git a/tests/test_parser.py b/tests/test_parser.py index 221cc54..f20920c 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -109,3 +109,5 @@ class TestCaseParser(TestCase): # Get parsed dashboard res, md5 = self.parser.get_dashboard('test-json') self.assertEqual(res['title'], 'test json') + self.assertNotIn('id', res.keys()) + self.assertNotIn('uid', res.keys())