| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2018 Whitestack, LLC |
| 4 | # ************************************************************* |
| 5 | |
| 6 | # This file is part of OSM Monitoring module |
| 7 | # All Rights Reserved to Whitestack, LLC |
| 8 | |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | # not use this file except in compliance with the License. You may obtain |
| 11 | # a copy of the License at |
| 12 | |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | # License for the specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # For those usages not covered by the Apache License, Version 2.0 please |
| 21 | # contact: glavado@whitestack.com |
| 22 | ## |
| 23 | import logging |
| 24 | import requests |
| 25 | |
| 26 | log = logging.getLogger(__name__) |
| 27 | |
| lavado | cee3b86 | 2019-11-29 12:40:30 -0500 | [diff] [blame] | 28 | # TODO (lavado): migrate to Class, import config variables to get token |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 29 | url = "http://grafana:3000/api/" |
| 30 | headers = { |
| 31 | 'content-type': "application/json", |
| 32 | 'authorization': "Basic YWRtaW46YWRtaW4=" |
| 33 | } |
| 34 | |
| 35 | |
| 36 | def get_all_dashboard_uids(): |
| 37 | response = requests.request("GET", url + "search?query=%", headers=headers) |
| 38 | dashboards = response.json() |
| 39 | dashboard_uids = [] |
| 40 | for dashboard in dashboards: |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 41 | dashboard_uids.append(dashboard['uid']) |
| 42 | log.debug("Searching for all dashboard uids: %s", dashboard_uids) |
| 43 | return dashboard_uids |
| 44 | |
| 45 | |
| 46 | def get_dashboard_status(uid): |
| 47 | response = requests.request("GET", url + "dashboards/uid/" + uid, headers=headers) |
| 48 | log.debug("Searching for dashboard result: %s", response.text) |
| 49 | return response |
| 50 | |
| 51 | |
| 52 | def create_dashboard(uid, name, json_file): |
| 53 | with open(json_file) as f: |
| 54 | dashboard_data = f.read() |
| 55 | |
| 56 | dashboard_data = dashboard_data.replace('OSM_ID', uid).replace('OSM_NAME', name) |
| 57 | |
| 58 | response = requests.request("POST", url + "dashboards/db/", data=dashboard_data, headers=headers) |
| 59 | log.debug("Creating dashboard result: %s", response.text) |
| 60 | return response |
| 61 | |
| 62 | |
| 63 | def delete_dashboard(uid): |
| 64 | response = requests.request("DELETE", url + "dashboards/uid/" + uid, headers=headers) |
| 65 | log.debug("Delete dashboard result: %s", response.text) |
| 66 | return response |