Browse Source

Merge "Add links to GrafYaml"

Zuul 1 year ago
parent
commit
ca98fba97f

+ 4
- 0
grafana_dashboards/schema/dashboard.py View File

@@ -14,6 +14,7 @@
14 14
 
15 15
 import voluptuous as v
16 16
 
17
+from grafana_dashboards.schema.links import Links
17 18
 from grafana_dashboards.schema.row import Row
18 19
 from grafana_dashboards.schema.template import Template
19 20
 
@@ -21,6 +22,7 @@ from grafana_dashboards.schema.template import Template
21 22
 class Dashboard(object):
22 23
 
23 24
     def get_schema(self):
25
+
24 26
         dashboard = {
25 27
             v.Required('timezone', default='utc'): v.Any('browser', 'utc'),
26 28
             v.Required('title'): v.All(str, v.Length(min=1)),
@@ -31,6 +33,8 @@ class Dashboard(object):
31 33
                 v.Required('to'): v.Any(v.Datetime(), str),
32 34
             },
33 35
         }
36
+        links = Links().get_schema()
37
+        dashboard.update(links.schema)
34 38
         rows = Row().get_schema()
35 39
         dashboard.update(rows.schema)
36 40
         templating = Template().get_schema()

+ 77
- 0
grafana_dashboards/schema/links.py View File

@@ -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
+
15
+import voluptuous as v
16
+
17
+
18
+class 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

+ 55
- 0
tests/schema/fixtures/dashboard-0026.json View File

@@ -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
+}

+ 23
- 0
tests/schema/fixtures/dashboard-0026.yaml View File

@@ -0,0 +1,23 @@
1
+dashboard:
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.

Loading…
Cancel
Save