summaryrefslogtreecommitdiff
path: root/grafana_dashboards/grafana/dashboard.py
blob: 68ca57f6cd4d523e1bb8e03dc0b97c7bb761b54a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Copyright 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import json

try:
    from urllib.parse import urljoin
except ImportError:
    from urlparse import urljoin

from requests import exceptions


class Dashboard(object):

    def __init__(self, url, session):
        self.url = url
        self.session = session

    def create(self, name, data, overwrite=False):
        """Create a new dashboard

        :param name: URL friendly title of the dashboard
        :type name: str
        :param data: Dashboard model
        :type data: dict
        :param overwrite: Overwrite existing dashboard with newer version or
                          with the same dashboard title
        :type overwrite: bool

        :raises Exception: if dashboard already exists

        """
        dashboard = {
            'dashboard': data,
            'overwrite': overwrite,
        }
        if not overwrite and self.is_dashboard(name):
            raise Exception('dashboard[%s] already exists' % name)

        res = self.session.post(
            self.url, data=json.dumps(dashboard))
        res.raise_for_status()
        if not self.is_dashboard(name):
            raise Exception('dashboard[%s] does not exist' % name)

    def delete(self, name):
        """Delete a dashboard

        :param name: URL friendly title of the dashboard
        :type name: str

        :raises Exception: if dashboard failed to delete

        """
        url = urljoin(self.url, name)
        self.session.delete(url)
        if self.is_dashboard(name):
            raise Exception('dashboard[%s] failed to delete' % name)

    def get(self, name):
        """Get a dashboard

        :param name: URL friendly title of the dashboard
        :type name: str

        :rtype: dict or None

        """
        url = urljoin(self.url, name)
        try:
            res = self.session.get(url)
            res.raise_for_status()
        except exceptions.HTTPError:
            return None

        return res.json()

    def is_dashboard(self, name):
        """Check if a dashboard exists

        :param name: URL friendly title of the dashboard
        :type name: str

        :returns: True if dashboard exists
        :rtype: bool

        """
        res = self.get(name)
        if res and res['meta']['slug'] == name:
            return True
        return False