summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-02-16 20:09:40 +0000
committerGerrit Code Review <review@openstack.org>2018-02-16 20:09:40 +0000
commitca98fba97f6077dca0068d0d56c700f670dbe5d7 (patch)
tree9f2c1b97d412f771d2a3686c3993212c015678bd
parent8a525a916e56af8bf50354dac847776366105626 (diff)
parent89e71ad53db30f672473ff934df1aa1c65ec37a0 (diff)
Merge "Add links to GrafYaml"0.0.7
-rw-r--r--grafana_dashboards/schema/dashboard.py4
-rw-r--r--grafana_dashboards/schema/links.py77
-rw-r--r--tests/schema/fixtures/dashboard-0026.json55
-rw-r--r--tests/schema/fixtures/dashboard-0026.yaml23
4 files changed, 159 insertions, 0 deletions
diff --git a/grafana_dashboards/schema/dashboard.py b/grafana_dashboards/schema/dashboard.py
index 2997056..ddffcda 100644
--- a/grafana_dashboards/schema/dashboard.py
+++ b/grafana_dashboards/schema/dashboard.py
@@ -14,6 +14,7 @@
14 14
15import voluptuous as v 15import voluptuous as v
16 16
17from grafana_dashboards.schema.links import Links
17from grafana_dashboards.schema.row import Row 18from grafana_dashboards.schema.row import Row
18from grafana_dashboards.schema.template import Template 19from grafana_dashboards.schema.template import Template
19 20
@@ -21,6 +22,7 @@ from grafana_dashboards.schema.template import Template
21class Dashboard(object): 22class Dashboard(object):
22 23
23 def get_schema(self): 24 def get_schema(self):
25
24 dashboard = { 26 dashboard = {
25 v.Required('timezone', default='utc'): v.Any('browser', 'utc'), 27 v.Required('timezone', default='utc'): v.Any('browser', 'utc'),
26 v.Required('title'): v.All(str, v.Length(min=1)), 28 v.Required('title'): v.All(str, v.Length(min=1)),
@@ -31,6 +33,8 @@ class Dashboard(object):
31 v.Required('to'): v.Any(v.Datetime(), str), 33 v.Required('to'): v.Any(v.Datetime(), str),
32 }, 34 },
33 } 35 }
36 links = Links().get_schema()
37 dashboard.update(links.schema)
34 rows = Row().get_schema() 38 rows = Row().get_schema()
35 dashboard.update(rows.schema) 39 dashboard.update(rows.schema)
36 templating = Template().get_schema() 40 templating = Template().get_schema()
diff --git a/grafana_dashboards/schema/links.py b/grafana_dashboards/schema/links.py
new file mode 100644
index 0000000..74e7e57
--- /dev/null
+++ b/grafana_dashboards/schema/links.py
@@ -0,0 +1,77 @@
1# Copyright 2018 Red Hat, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import voluptuous as v
16
17
18class Links(object):
19 icons = [
20 'bolt',
21 'external link',
22 'cloud',
23 'dashboard',
24 'doc',
25 'info',
26 'question',
27 ]
28
29 link_base = {
30 v.Required('type'): v.Any('dashboards', 'link'),
31 v.Optional('asDropdown'): v.All(bool),
32 v.Optional('icon', default='external link'): v.Any(*icons),
33 v.Optional('includeVars', default=False): v.All(bool),
34 v.Optional('keepTime', default=False): v.All(bool),
35 v.Optional('tags'): v.All([str]),
36 v.Optional('targetBlank', default=False): v.All(bool),
37 v.Optional('title'): v.All(str),
38 }
39
40 def _validate(self):
41
42 def f(data):
43 res = []
44 if not isinstance(data, list):
45 raise v.Invalid('Should be a list')
46
47 for link in data:
48 validate = v.Schema(self.link_base, extra=True)
49 validate(link)
50
51 if link['type'] == 'dashboards':
52 link_dashboards = {
53 v.Optional('asDropdown'): v.All(bool),
54 v.Optional('tags'): v.All([str]),
55 }
56 link_dashboards.update(self.link_base)
57 schema = v.Schema(link_dashboards)
58 elif link['type'] == 'link':
59 link_link = {
60 v.Optional('tooltip'): v.All(str),
61 v.Required('url'): v.All(str),
62 }
63 link_link.update(self.link_base)
64 schema = v.Schema(link_link)
65
66 res.append(schema(link))
67
68 return res
69
70 return f
71
72 def get_schema(self):
73 schema = v.Schema({
74 v.Optional('links'): v.All(self._validate()),
75 })
76
77 return schema
diff --git a/tests/schema/fixtures/dashboard-0026.json b/tests/schema/fixtures/dashboard-0026.json
new file mode 100644
index 0000000..0fceb8c
--- /dev/null
+++ b/tests/schema/fixtures/dashboard-0026.json
@@ -0,0 +1,55 @@
1{
2 "dashboard": {
3 "dashboard-with-links": {
4 "links": [
5 {
6 "asDropdown": true,
7 "icon": "external link",
8 "includeVars": true,
9 "keepTime": true,
10 "tags": [
11 "test_tag"
12 ],
13 "targetBlank": true,
14 "title": "test_link_title",
15 "type": "dashboards"
16 },
17 {
18 "icon": "cloud",
19 "includeVars": false,
20 "keepTime": false,
21 "targetBlank": false,
22 "title": "test_link_title",
23 "type": "link",
24 "url": "http://grafana.com"
25 }
26 ],
27 "rows": [
28 {
29 "collapse": false,
30 "editable": true,
31 "height": "250px",
32 "panels": [
33 {
34 "content": "Some example text is required.",
35 "editable": true,
36 "error": false,
37 "mode": "markdown",
38 "span": 12,
39 "title": "no title (click here)",
40 "type": "text"
41 }
42 ],
43 "showTitle": false,
44 "title": "New row"
45 }
46 ],
47 "templating": {
48 "enabled": false,
49 "list": []
50 },
51 "timezone": "utc",
52 "title": "Dashboard with links"
53 }
54 }
55}
diff --git a/tests/schema/fixtures/dashboard-0026.yaml b/tests/schema/fixtures/dashboard-0026.yaml
new file mode 100644
index 0000000..3ea2d8a
--- /dev/null
+++ b/tests/schema/fixtures/dashboard-0026.yaml
@@ -0,0 +1,23 @@
1dashboard:
2 title: Dashboard with links
3 links:
4 - type: dashboards
5 title: test_link_title
6 asDropdown: true
7 tags:
8 - test_tag
9 includeVars: true
10 keepTime: true
11 targetBlank: true
12 - type: link
13 title: test_link_title
14 icon: cloud
15 url: http://grafana.com
16 rows:
17 - title: New row
18 height: 250px
19 panels:
20 - title: no title (click here)
21 type: text
22 mode: markdown
23 content: Some example text is required.