From b81f5248297e3b3f89e73c7917139d66531c7af4 Mon Sep 17 00:00:00 2001 From: Paul Belanger Date: Wed, 6 May 2015 15:15:29 -0400 Subject: [PATCH] Add support for rows Signed-off-by: Paul Belanger --- grafana_dashboards/builder.py | 4 +-- grafana_dashboards/schema/dashboard.py | 4 +++ grafana_dashboards/schema/row.py | 37 +++++++++++++++++++++++ tests/schema/fixtures/row-001.yaml | 6 ++++ tests/schema/test_row.py | 41 ++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 grafana_dashboards/schema/row.py create mode 100644 tests/schema/fixtures/row-001.yaml create mode 100644 tests/schema/test_row.py diff --git a/grafana_dashboards/builder.py b/grafana_dashboards/builder.py index 2156678..8a79b0d 100644 --- a/grafana_dashboards/builder.py +++ b/grafana_dashboards/builder.py @@ -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) diff --git a/grafana_dashboards/schema/dashboard.py b/grafana_dashboards/schema/dashboard.py index 670c195..92bacac 100644 --- a/grafana_dashboards/schema/dashboard.py +++ b/grafana_dashboards/schema/dashboard.py @@ -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, }) diff --git a/grafana_dashboards/schema/row.py b/grafana_dashboards/schema/row.py new file mode 100644 index 0000000..be5c269 --- /dev/null +++ b/grafana_dashboards/schema/row.py @@ -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) diff --git a/tests/schema/fixtures/row-001.yaml b/tests/schema/fixtures/row-001.yaml new file mode 100644 index 0000000..369fc68 --- /dev/null +++ b/tests/schema/fixtures/row-001.yaml @@ -0,0 +1,6 @@ +rows: + - title: New row + height: 250px + editable: true + collapse: false + panels: [] diff --git a/tests/schema/test_row.py b/tests/schema/test_row.py new file mode 100644 index 0000000..b0d92e4 --- /dev/null +++ b/tests/schema/test_row.py @@ -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)