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.

dashboard.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright 2015 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 json
  15. try:
  16. from urllib.parse import urljoin
  17. except ImportError:
  18. from urlparse import urljoin
  19. from requests import exceptions
  20. class Dashboard(object):
  21. def __init__(self, url, session):
  22. self.url = url
  23. self.session = session
  24. def create(self, name, data, overwrite=False):
  25. """Create a new dashboard
  26. :param name: URL friendly title of the dashboard
  27. :type name: str
  28. :param data: Dashboard model
  29. :type data: dict
  30. :param overwrite: Overwrite existing dashboard with newer version or
  31. with the same dashboard title
  32. :type overwrite: bool
  33. :raises Exception: if dashboard already exists
  34. """
  35. dashboard = {
  36. 'dashboard': data,
  37. 'overwrite': overwrite,
  38. }
  39. if not overwrite and self.is_dashboard(name):
  40. raise Exception('dashboard[%s] already exists' % name)
  41. res = self.session.post(
  42. self.url, data=json.dumps(dashboard))
  43. res.raise_for_status()
  44. if not self.is_dashboard(name):
  45. raise Exception('dashboard[%s] does not exist' % name)
  46. def delete(self, name):
  47. """Delete a dashboard
  48. :param name: URL friendly title of the dashboard
  49. :type name: str
  50. :raises Exception: if dashboard failed to delete
  51. """
  52. url = urljoin(self.url, name)
  53. self.session.delete(url)
  54. if self.is_dashboard(name):
  55. raise Exception('dashboard[%s] failed to delete' % name)
  56. def get(self, name):
  57. """Get a dashboard
  58. :param name: URL friendly title of the dashboard
  59. :type name: str
  60. :rtype: dict or None
  61. """
  62. url = urljoin(self.url, name)
  63. try:
  64. res = self.session.get(url)
  65. res.raise_for_status()
  66. except exceptions.HTTPError:
  67. return None
  68. return res.json()
  69. def is_dashboard(self, name):
  70. """Check if a dashboard exists
  71. :param name: URL friendly title of the dashboard
  72. :type name: str
  73. :returns: True if dashboard exists
  74. :rtype: bool
  75. """
  76. res = self.get(name)
  77. if res and res['meta']['slug'] == name:
  78. return True
  79. return False