Tools to make Grafana dashboards from templates
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

links.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. import voluptuous as v
  15. class Links(object):
  16. icons = [
  17. 'bolt',
  18. 'external link',
  19. 'cloud',
  20. 'dashboard',
  21. 'doc',
  22. 'info',
  23. 'question',
  24. ]
  25. link_base = {
  26. v.Required('type'): v.Any('dashboards', 'link'),
  27. v.Optional('asDropdown'): v.All(bool),
  28. v.Optional('icon', default='external link'): v.Any(*icons),
  29. v.Optional('includeVars', default=False): v.All(bool),
  30. v.Optional('keepTime', default=False): v.All(bool),
  31. v.Optional('tags'): v.All([str]),
  32. v.Optional('targetBlank', default=False): v.All(bool),
  33. v.Optional('title'): v.All(str),
  34. }
  35. def _validate(self):
  36. def f(data):
  37. res = []
  38. if not isinstance(data, list):
  39. raise v.Invalid('Should be a list')
  40. for link in data:
  41. validate = v.Schema(self.link_base, extra=True)
  42. validate(link)
  43. if link['type'] == 'dashboards':
  44. link_dashboards = {
  45. v.Optional('asDropdown'): v.All(bool),
  46. v.Optional('tags'): v.All([str]),
  47. }
  48. link_dashboards.update(self.link_base)
  49. schema = v.Schema(link_dashboards)
  50. elif link['type'] == 'link':
  51. link_link = {
  52. v.Optional('tooltip'): v.All(str),
  53. v.Required('url'): v.All(str),
  54. }
  55. link_link.update(self.link_base)
  56. schema = v.Schema(link_link)
  57. res.append(schema(link))
  58. return res
  59. return f
  60. def get_schema(self):
  61. schema = v.Schema({
  62. v.Optional('links'): v.All(self._validate()),
  63. })
  64. return schema