Add support for rows

Signed-off-by: Paul Belanger <pabelanger@redhat.com>
This commit is contained in:
Paul Belanger 2015-05-06 15:15:29 -04:00
parent 628d7f5b7a
commit b81f524829
5 changed files with 90 additions and 2 deletions

View File

@ -42,5 +42,5 @@ class Builder(object):
def update_dashboard(self, path):
data = yaml.load(open(path))
schema = Dashboard()
schema.validate(data)
self.grafana.create_dashboard(data, overwrite=True)
result = schema.validate(data)
self.grafana.create_dashboard(result, overwrite=True)

View File

@ -14,6 +14,8 @@
import voluptuous as v
from grafana_dashboards.schema.row import Row
class Dashboard(object):
@ -22,6 +24,8 @@ class Dashboard(object):
v.Required('title'): v.All(str, v.Length(min=1)),
v.Optional('id'): int,
}
rows = Row().get_schema()
dashboard.update(rows.schema)
schema = v.Schema({
'dashboard': dashboard,
})

View File

@ -0,0 +1,37 @@
# 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 Row(object):
def get_schema(self):
row = {
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('title'): v.All(str, v.Length(min=1)),
}
schema = v.Schema({
v.Required('rows', default=[]): [row],
})
return schema
def validate(self, data):
schema = self.get_schema()
return schema(data)

View File

@ -0,0 +1,6 @@
rows:
- title: New row
height: 250px
editable: true
collapse: false
panels: []

41
tests/schema/test_row.py Normal file
View File

@ -0,0 +1,41 @@
# 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)