Initial commit of schema

Here we are using voluptuous to help validate our yaml files.  Right now we we
just have support for text panels, but it is a start.

Signed-off-by: Paul Belanger <pabelanger@redhat.com>
This commit is contained in:
Paul Belanger 2015-05-05 10:52:01 -04:00
parent f65385a578
commit 71e14b74f2
9 changed files with 87 additions and 28 deletions

View File

@ -0,0 +1,42 @@
# 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 BasePanel(object):
def validate(self, data):
panel = {
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.Optional('id'): int,
}
panel.update(self.fields)
schema = v.Schema({
'panels': [panel]
})
return schema(data)
class Text(BasePanel):
fields = {
v.Required('content'): v.All(str),
v.Required('mode'): v.All(str),
v.Optional('style'): dict(),
}

View File

@ -1,28 +0,0 @@
# -*- coding: utf-8 -*-
# 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.
"""
test_grafana_dashboards
----------------------------------
Tests for `grafana_dashboards` module.
"""
from grafana_dashboards.tests import base
class TestGrafana_dashboards(base.TestCase):
def test_something(self):
pass

View File

@ -4,3 +4,6 @@
pbr>=0.6,!=0.7,<1.0 pbr>=0.6,!=0.7,<1.0
Babel>=1.3 Babel>=1.3
PyYAML>=3.1.0
voluptuous>=0.7

0
tests/__init__.py Normal file
View File

0
tests/schema/__init__.py Normal file
View File

View File

@ -0,0 +1,10 @@
panels:
- type: text
title: no title (click here)
error: false
editable: true
span: 12
id: 1
mode: markdown
content: ''
style: {}

View File

@ -0,0 +1,32 @@
# 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 yaml
from testtools import TestCase
from grafana_dashboards.schema import panel
FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
'fixtures')
class TestCaseSchemaPanel(TestCase):
def test_layouts(self):
for fn in os.listdir(os.path.join(FIXTURE_DIR)):
layout = os.path.join(FIXTURE_DIR, fn)
data = yaml.load(open(layout))
schema = panel.Text()
schema.validate(data)