Add adhoc template variables

Change-Id: I884d9d54dedaaab586fa3c2e8228f40a2bd39d79
This commit is contained in:
Quique Llorente 2018-07-10 15:56:29 +02:00
parent 0969e906f3
commit e36f4ef99e
5 changed files with 62 additions and 1 deletions

View File

@ -14,6 +14,7 @@
import voluptuous as v import voluptuous as v
from grafana_dashboards.schema.template.adhoc import Adhoc
from grafana_dashboards.schema.template.base import Base from grafana_dashboards.schema.template.base import Base
from grafana_dashboards.schema.template.custom import Custom from grafana_dashboards.schema.template.custom import Custom
from grafana_dashboards.schema.template.datasource import Datasource from grafana_dashboards.schema.template.datasource import Datasource
@ -51,6 +52,8 @@ class Template(object):
schema = Custom().get_schema() schema = Custom().get_schema()
if template['type'] == 'datasource': if template['type'] == 'datasource':
schema = Datasource().get_schema() schema = Datasource().get_schema()
if template['type'] == 'adhoc':
schema = Adhoc().get_schema()
res['list'].append(schema(template)) res['list'].append(schema(template))

View File

@ -0,0 +1,29 @@
# 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 Adhoc(Base):
def get_schema(self):
query = {
v.Required('datasource'): v.All(str),
v.Optional('hide'): v.All(int, v.Range(min=0, max=2)),
v.Optional('label', default=''): v.All(str),
}
query.update(self.base)
return v.Schema(query)

View File

@ -66,7 +66,7 @@ class Base(object):
self.base = { self.base = {
v.Required('name'): v.All(str, v.Length(min=1)), 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'), 'datasource', 'adhoc'),
} }
def get_schema(self): def get_schema(self):

View File

@ -0,0 +1,21 @@
{
"dashboard": {
"new-dashboard": {
"rows": [],
"templating": {
"enabled": true,
"list": [
{
"datasource": "telegraf",
"hide": 0,
"label": "Influxdb filter",
"name": "foobar",
"type": "adhoc"
}
]
},
"timezone": "utc",
"title": "New dashboard"
}
}
}

View File

@ -0,0 +1,8 @@
dashboard:
templating:
- name: foobar
datasource: telegraf
hide: 0
label: "Influxdb filter"
type: adhoc
title: New dashboard