diff --git a/grafana_dashboards/schema/dashboard.py b/grafana_dashboards/schema/dashboard.py index bf396d8..47bd7ac 100644 --- a/grafana_dashboards/schema/dashboard.py +++ b/grafana_dashboards/schema/dashboard.py @@ -15,6 +15,7 @@ import voluptuous as v from grafana_dashboards.schema.row import Row +from grafana_dashboards.schema.template import Template class Dashboard(object): @@ -27,4 +28,7 @@ class Dashboard(object): } rows = Row().get_schema() dashboard.update(rows.schema) + templating = Template().get_schema() + dashboard.update(templating.schema) + return dashboard diff --git a/grafana_dashboards/schema/template/__init__.py b/grafana_dashboards/schema/template/__init__.py new file mode 100644 index 0000000..441ad3a --- /dev/null +++ b/grafana_dashboards/schema/template/__init__.py @@ -0,0 +1,59 @@ +# Copyright 2015 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import voluptuous as v + +from grafana_dashboards.schema.template.base import Base +from grafana_dashboards.schema.template.query import Query + + +class Template(object): + + def __init__(self): + # TODO(pabelanger): This is pretty ugly, there much be a better way to + # set default values. + self.defaults = { + 'enabled': False, + 'list': [], + } + + def _validate(self): + + def f(data): + res = self.defaults + if not isinstance(data, list): + raise v.Invalid('Should be a list') + + for template in data: + res['enabled'] = True + validate = Base().get_schema() + validate(template) + + if template['type'] == 'query': + schema = Query().get_schema() + + res['list'].append(schema(template)) + + return res + + return f + + def get_schema(self): + schema = v.Schema({ + v.Required( + 'templating', default=self.defaults): v.All( + self._validate()), + }) + + return schema diff --git a/grafana_dashboards/schema/template/base.py b/grafana_dashboards/schema/template/base.py new file mode 100644 index 0000000..4cb26e7 --- /dev/null +++ b/grafana_dashboards/schema/template/base.py @@ -0,0 +1,27 @@ +# Copyright 2015 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import voluptuous as v + + +class Base(object): + + def __init__(self): + self.base = { + v.Required('name'): v.All(str, v.Length(min=1)), + v.Required('type'): v.Any('query'), + } + + def get_schema(self): + return v.Schema(self.base, extra=True) diff --git a/grafana_dashboards/schema/template/query.py b/grafana_dashboards/schema/template/query.py new file mode 100644 index 0000000..b0573a0 --- /dev/null +++ b/grafana_dashboards/schema/template/query.py @@ -0,0 +1,30 @@ +# Copyright 2015 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import voluptuous as v + +from grafana_dashboards.schema.template.base import Base + + +class Query(Base): + + def get_schema(self): + query = { + v.Required('includeAll', default=False): v.All(bool), + v.Required('multi', default=False): v.All(bool), + v.Required('query', default=''): v.All(str), + v.Required('refresh', default=False): v.All(bool), + } + query.update(self.base) + return v.Schema(query) diff --git a/tests/schema/fixtures/dashboard-0001.json b/tests/schema/fixtures/dashboard-0001.json index 688f783..1e1e5b4 100644 --- a/tests/schema/fixtures/dashboard-0001.json +++ b/tests/schema/fixtures/dashboard-0001.json @@ -2,6 +2,10 @@ "dashboard": { "new-dashboard": { "rows": [], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0002.json b/tests/schema/fixtures/dashboard-0002.json index c6a0fce..43ab2dd 100644 --- a/tests/schema/fixtures/dashboard-0002.json +++ b/tests/schema/fixtures/dashboard-0002.json @@ -11,6 +11,10 @@ "title": "New row" } ], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0003.json b/tests/schema/fixtures/dashboard-0003.json index d5915d2..f25efcc 100644 --- a/tests/schema/fixtures/dashboard-0003.json +++ b/tests/schema/fixtures/dashboard-0003.json @@ -23,6 +23,10 @@ "title": "New row" } ], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0004.json b/tests/schema/fixtures/dashboard-0004.json index c1e67b9..ce65c27 100644 --- a/tests/schema/fixtures/dashboard-0004.json +++ b/tests/schema/fixtures/dashboard-0004.json @@ -21,6 +21,10 @@ "title": "New row" } ], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0005.json b/tests/schema/fixtures/dashboard-0005.json index 2c9d7b2..361d6f9 100644 --- a/tests/schema/fixtures/dashboard-0005.json +++ b/tests/schema/fixtures/dashboard-0005.json @@ -31,6 +31,10 @@ "title": "New row" } ], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0006.json b/tests/schema/fixtures/dashboard-0006.json index d72678c..4c9132f 100644 --- a/tests/schema/fixtures/dashboard-0006.json +++ b/tests/schema/fixtures/dashboard-0006.json @@ -36,6 +36,10 @@ "title": "New row" } ], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0007.json b/tests/schema/fixtures/dashboard-0007.json index 4f9e069..4cf9963 100644 --- a/tests/schema/fixtures/dashboard-0007.json +++ b/tests/schema/fixtures/dashboard-0007.json @@ -41,6 +41,10 @@ "title": "bar" } ], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0008.json b/tests/schema/fixtures/dashboard-0008.json index 77202f7..e87b484 100644 --- a/tests/schema/fixtures/dashboard-0008.json +++ b/tests/schema/fixtures/dashboard-0008.json @@ -2,6 +2,10 @@ "dashboard": { "new-dashboard": { "rows": [], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "browser", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0009.json b/tests/schema/fixtures/dashboard-0009.json index b52d3e7..0393f8d 100644 --- a/tests/schema/fixtures/dashboard-0009.json +++ b/tests/schema/fixtures/dashboard-0009.json @@ -33,6 +33,10 @@ "title": "New row" } ], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0010.json b/tests/schema/fixtures/dashboard-0010.json index 8944003..e183a2d 100644 --- a/tests/schema/fixtures/dashboard-0010.json +++ b/tests/schema/fixtures/dashboard-0010.json @@ -37,6 +37,10 @@ "title": "New row" } ], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0011.json b/tests/schema/fixtures/dashboard-0011.json index 41b417e..3232697 100644 --- a/tests/schema/fixtures/dashboard-0011.json +++ b/tests/schema/fixtures/dashboard-0011.json @@ -32,6 +32,10 @@ "title": "New row" } ], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0012.json b/tests/schema/fixtures/dashboard-0012.json index cde1484..22b7f5d 100644 --- a/tests/schema/fixtures/dashboard-0012.json +++ b/tests/schema/fixtures/dashboard-0012.json @@ -36,6 +36,10 @@ "title": "New row" } ], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0013.json b/tests/schema/fixtures/dashboard-0013.json index 8c95a81..da41c42 100644 --- a/tests/schema/fixtures/dashboard-0013.json +++ b/tests/schema/fixtures/dashboard-0013.json @@ -55,6 +55,10 @@ "title": "New row" } ], + "templating": { + "enabled": false, + "list": [] + }, "timezone": "utc", "title": "New dashboard" } diff --git a/tests/schema/fixtures/dashboard-0014.json b/tests/schema/fixtures/dashboard-0014.json new file mode 100644 index 0000000..819de9b --- /dev/null +++ b/tests/schema/fixtures/dashboard-0014.json @@ -0,0 +1,22 @@ +{ + "dashboard": { + "new-dashboard": { + "rows": [], + "templating": { + "enabled": true, + "list": [ + { + "includeAll": false, + "multi": false, + "name": "foobar", + "query": "foobar.*", + "refresh": false, + "type": "query" + } + ] + }, + "timezone": "utc", + "title": "New dashboard" + } + } +} diff --git a/tests/schema/fixtures/dashboard-0014.yaml b/tests/schema/fixtures/dashboard-0014.yaml new file mode 100644 index 0000000..b3c56c1 --- /dev/null +++ b/tests/schema/fixtures/dashboard-0014.yaml @@ -0,0 +1,6 @@ +dashboard: + templating: + - name: foobar + query: foobar.* + type: query + title: New dashboard diff --git a/tests/test_parser.py b/tests/test_parser.py index fcb9904..e5f4d8b 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -35,11 +35,19 @@ class TestCaseParser(TestCase): dashboard = { 'foobar': { 'rows': [], + 'templating': { + 'enabled': False, + 'list': [], + }, 'timezone': 'utc', 'title': 'foobar', }, 'new-dashboard': { 'rows': [], + 'templating': { + 'enabled': False, + 'list': [], + }, 'timezone': 'utc', 'title': 'New dashboard', }, @@ -71,6 +79,10 @@ class TestCaseParser(TestCase): dashboard = { 'new-dashboard': { 'rows': [], + 'templating': { + 'enabled': False, + 'list': [], + }, 'timezone': 'utc', 'title': 'New dashboard', },