Rework Panel object
Add custom validtor to deal with panel schema. Signed-off-by: Paul Belanger <pabelanger@redhat.com>
This commit is contained in:
parent
14f812543a
commit
353cf6b3e3
grafana_dashboards/schema
tests/schema
@ -15,37 +15,58 @@
|
||||
import voluptuous as v
|
||||
|
||||
|
||||
class BasePanel(object):
|
||||
class Panel(object):
|
||||
|
||||
def validate(self, data):
|
||||
panel = {
|
||||
def __init__(self):
|
||||
self.base = {
|
||||
v.Required('editable', default=True): v.All(bool),
|
||||
v.Required('error', default=False): v.All(bool),
|
||||
v.Required('span', default=12): v.All(int, v.Range(min=0, max=12)),
|
||||
v.Required('title'): v.All(str, v.Length(min=1)),
|
||||
v.Required('type'): v.All(str),
|
||||
v.Required('type'): v.Any('dashlist', 'text'),
|
||||
v.Optional('id'): int,
|
||||
}
|
||||
panel.update(self.fields)
|
||||
|
||||
self.dashlist = {
|
||||
v.Required('limit', default=10): v.All(int),
|
||||
v.Required('mode', default='starred'): v.Any('search', 'starred'),
|
||||
v.Required('tag', default=''): v.All(str),
|
||||
v.Required('query', default=''): v.All(str),
|
||||
}
|
||||
self.dashlist.update(self.base)
|
||||
|
||||
self.text = {
|
||||
v.Required('content'): v.All(str),
|
||||
v.Required('mode', default='markdown'): v.Any(
|
||||
'html', 'markdown', 'text'),
|
||||
v.Optional('style'): dict(),
|
||||
}
|
||||
self.text.update(self.base)
|
||||
|
||||
def _validate(self):
|
||||
res = []
|
||||
|
||||
def f(data):
|
||||
if not isinstance(data, list):
|
||||
raise v.Invalid('Should be a list')
|
||||
|
||||
for x in data:
|
||||
schema = v.Schema(self.base, extra=True)
|
||||
schema(x)
|
||||
if x['type'] == 'text':
|
||||
panel = v.Schema(self.text)
|
||||
elif x['type'] == 'dashlist':
|
||||
panel = v.Schema(self.dashlist)
|
||||
|
||||
res.append(panel(x))
|
||||
|
||||
return res
|
||||
|
||||
return f
|
||||
|
||||
def get_schema(self):
|
||||
schema = v.Schema({
|
||||
'panels': [panel]
|
||||
v.Required('panels', default=[]): v.All(self._validate()),
|
||||
})
|
||||
|
||||
return schema(data)
|
||||
|
||||
|
||||
class Dashlist(BasePanel):
|
||||
fields = {
|
||||
v.Required('limit', default=10): v.All(int),
|
||||
v.Required('mode'): v.All(str),
|
||||
v.Required('tag', default=''): v.All(str),
|
||||
v.Required('query', default=''): v.All(str),
|
||||
}
|
||||
|
||||
|
||||
class Text(BasePanel):
|
||||
fields = {
|
||||
v.Required('content'): v.All(str),
|
||||
v.Required('mode'): v.All(str),
|
||||
v.Optional('style'): dict(),
|
||||
}
|
||||
return schema
|
||||
|
@ -14,6 +14,8 @@
|
||||
|
||||
import voluptuous as v
|
||||
|
||||
from grafana_dashboards.schema.panel import Panel
|
||||
|
||||
|
||||
class Row(object):
|
||||
|
||||
@ -22,17 +24,13 @@ class Row(object):
|
||||
v.Required('collapse', default=False): v.All(bool),
|
||||
v.Required('editable', default=True): v.All(bool),
|
||||
v.Required('height'): v.All(str),
|
||||
v.Required('panels', default=[]): list,
|
||||
v.Required('showTitle', default=False): v.All(bool),
|
||||
v.Required('title'): v.All(str, v.Length(min=1)),
|
||||
}
|
||||
panels = Panel().get_schema()
|
||||
row.update(panels.schema)
|
||||
schema = v.Schema({
|
||||
v.Required('rows', default=[]): [row],
|
||||
})
|
||||
|
||||
return schema
|
||||
|
||||
def validate(self, data):
|
||||
schema = self.get_schema()
|
||||
|
||||
return schema(data)
|
||||
|
@ -3,3 +3,20 @@ dashboard:
|
||||
rows:
|
||||
- title: New row
|
||||
height: 250px
|
||||
panels:
|
||||
- type: text
|
||||
title: no title (click here)
|
||||
error: false
|
||||
editable: true
|
||||
span: 12
|
||||
mode: markdown
|
||||
content: ''
|
||||
style: {}
|
||||
- type: dashlist
|
||||
title: Starred Dashboards
|
||||
error: false
|
||||
editable: true
|
||||
span: 12
|
||||
mode: starred
|
||||
query: ''
|
||||
limit: 1
|
||||
|
@ -1,11 +0,0 @@
|
||||
panels:
|
||||
- type: dashlist
|
||||
title: Starred Dashboards
|
||||
error: false
|
||||
editable: true
|
||||
span: 12
|
||||
id: 1
|
||||
mode: starred
|
||||
query: ''
|
||||
limit: 10
|
||||
tag: ''
|
@ -1,21 +0,0 @@
|
||||
panels:
|
||||
- type: dashlist
|
||||
title: Starred Dashboards
|
||||
error: false
|
||||
editable: true
|
||||
span: 12
|
||||
id: 1
|
||||
mode: starred
|
||||
query: ''
|
||||
limit: 10
|
||||
tag: ''
|
||||
- type: dashlist
|
||||
title: FooBar
|
||||
error: false
|
||||
editable: true
|
||||
span: 6
|
||||
id: 2
|
||||
mode: starred
|
||||
query: ''
|
||||
limit: 3
|
||||
tag: ''
|
@ -1,7 +0,0 @@
|
||||
rows:
|
||||
- title: New row
|
||||
height: 250px
|
||||
editable: true
|
||||
collapse: false
|
||||
panels: []
|
||||
showTitle: false
|
@ -1,10 +0,0 @@
|
||||
panels:
|
||||
- type: text
|
||||
title: no title (click here)
|
||||
error: false
|
||||
editable: true
|
||||
span: 12
|
||||
id: 1
|
||||
mode: markdown
|
||||
content: ''
|
||||
style: {}
|
@ -1,19 +0,0 @@
|
||||
panels:
|
||||
- type: text
|
||||
title: no title (click here)
|
||||
error: false
|
||||
editable: true
|
||||
span: 12
|
||||
id: 1
|
||||
mode: markdown
|
||||
content: ''
|
||||
style: {}
|
||||
- type: text
|
||||
title: FooBar
|
||||
error: false
|
||||
editable: true
|
||||
span: 6
|
||||
id: 3
|
||||
mode: markdown
|
||||
content: ''
|
||||
style: {}
|
@ -1,43 +0,0 @@
|
||||
# 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 os
|
||||
import re
|
||||
import yaml
|
||||
|
||||
from testtools import TestCase
|
||||
|
||||
from grafana_dashboards.schema import panel
|
||||
|
||||
FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
|
||||
'fixtures')
|
||||
LAYOUT_RE = re.compile(r'^(dashlist|text)-.*\.yaml$')
|
||||
|
||||
|
||||
class TestCaseSchemaPanel(TestCase):
|
||||
def test_layouts(self):
|
||||
for fn in os.listdir(os.path.join(FIXTURE_DIR)):
|
||||
schema = None
|
||||
m = LAYOUT_RE.match(fn)
|
||||
if not m:
|
||||
continue
|
||||
layout = os.path.join(FIXTURE_DIR, fn)
|
||||
data = yaml.load(open(layout))
|
||||
|
||||
if m.group(1) == 'dashlist':
|
||||
schema = panel.Dashlist()
|
||||
elif m.group(1) == 'text':
|
||||
schema = panel.Text()
|
||||
|
||||
schema.validate(data)
|
@ -1,41 +0,0 @@
|
||||
# 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 os
|
||||
import re
|
||||
import yaml
|
||||
|
||||
from testtools import TestCase
|
||||
|
||||
from grafana_dashboards.schema import row
|
||||
|
||||
FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
|
||||
'fixtures')
|
||||
LAYOUT_RE = re.compile(r'^(row)-.*\.yaml$')
|
||||
|
||||
|
||||
class TestCaseSchemaRow(TestCase):
|
||||
def test_layouts(self):
|
||||
for fn in os.listdir(os.path.join(FIXTURE_DIR)):
|
||||
schema = None
|
||||
m = LAYOUT_RE.match(fn)
|
||||
if not m:
|
||||
continue
|
||||
layout = os.path.join(FIXTURE_DIR, fn)
|
||||
data = yaml.load(open(layout))
|
||||
|
||||
if m.group(1) == 'row':
|
||||
schema = row.Row()
|
||||
|
||||
schema.validate(data)
|
Loading…
x
Reference in New Issue
Block a user