summaryrefslogtreecommitdiff
path: root/grafana_dashboards/schema/links.py
diff options
context:
space:
mode:
Diffstat (limited to 'grafana_dashboards/schema/links.py')
-rw-r--r--grafana_dashboards/schema/links.py77
1 files changed, 77 insertions, 0 deletions
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