diff --git a/grafana_dashboards/schema/template/__init__.py b/grafana_dashboards/schema/template/__init__.py index 7e5dd2a..acc2bd8 100644 --- a/grafana_dashboards/schema/template/__init__.py +++ b/grafana_dashboards/schema/template/__init__.py @@ -16,6 +16,7 @@ import voluptuous as v from grafana_dashboards.schema.template.base import Base from grafana_dashboards.schema.template.custom import Custom +from grafana_dashboards.schema.template.datasource import Datasource from grafana_dashboards.schema.template.interval import Interval from grafana_dashboards.schema.template.query import Query @@ -48,6 +49,8 @@ class Template(object): schema = Interval().get_schema() if template['type'] == 'custom': schema = Custom().get_schema() + if template['type'] == 'datasource': + schema = Datasource().get_schema() res['list'].append(schema(template)) diff --git a/grafana_dashboards/schema/template/base.py b/grafana_dashboards/schema/template/base.py index 985f22f..620caa5 100644 --- a/grafana_dashboards/schema/template/base.py +++ b/grafana_dashboards/schema/template/base.py @@ -65,7 +65,8 @@ class Base(object): def __init__(self): self.base = { v.Required('name'): v.All(str, v.Length(min=1)), - v.Required('type'): v.Any('query', 'interval', 'custom'), + v.Required('type'): v.Any('query', 'interval', 'custom', + 'datasource'), } def get_schema(self): diff --git a/grafana_dashboards/schema/template/datasource.py b/grafana_dashboards/schema/template/datasource.py new file mode 100644 index 0000000..920b3fd --- /dev/null +++ b/grafana_dashboards/schema/template/datasource.py @@ -0,0 +1,34 @@ +# Copyright 2018 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 Datasource(Base): + + current = { + v.Required('text'): v.All(str, v.Length(min=1)), + v.Required('value'): v.All(str, v.Length(min=1)), + } + + def get_schema(self): + query = { + v.Required('query', default=''): v.All(str), + v.Required('current'): v.Any(self.current), + v.Optional('hide'): v.All(int, v.Range(min=0, max=2)), + } + query.update(self.base) + return v.Schema(query) diff --git a/tests/schema/fixtures/dashboard-0028.json b/tests/schema/fixtures/dashboard-0028.json new file mode 100644 index 0000000..4c874dd --- /dev/null +++ b/tests/schema/fixtures/dashboard-0028.json @@ -0,0 +1,23 @@ +{ + "dashboard": { + "new-dashboard": { + "rows": [], + "templating": { + "enabled": true, + "list": [ + { + "current": { + "text": "prometheus", + "value": "prometheus" + }, + "name": "foobar", + "query": "prometheus", + "type": "datasource" + } + ] + }, + "timezone": "utc", + "title": "New dashboard" + } + } +} diff --git a/tests/schema/fixtures/dashboard-0028.yaml b/tests/schema/fixtures/dashboard-0028.yaml new file mode 100644 index 0000000..c982f6e --- /dev/null +++ b/tests/schema/fixtures/dashboard-0028.yaml @@ -0,0 +1,9 @@ +dashboard: + templating: + - name: foobar + query: prometheus + type: datasource + current: + value: prometheus + text: prometheus + title: New dashboard