X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fdashboarder%2Fbackends%2Fgrafana.py;h=acc602e602a3e40a397b94f082234fd2cea995cb;hb=396648b6eab42dcabdec1cef131618971dc64fbe;hp=4f1fe5274b0f49aa8837441bdf067c9c12e4d030;hpb=3402cbbd480429f72b3501704fa147d157dc99a3;p=osm%2FMON.git diff --git a/osm_mon/dashboarder/backends/grafana.py b/osm_mon/dashboarder/backends/grafana.py index 4f1fe52..acc602e 100644 --- a/osm_mon/dashboarder/backends/grafana.py +++ b/osm_mon/dashboarder/backends/grafana.py @@ -18,50 +18,55 @@ # License for the specific language governing permissions and limitations # under the License. # For those usages not covered by the Apache License, Version 2.0 please -# contact: glavado@whitestack.com +# contact: glavado@whitestack.com or fbravo@whitestack.com ## import logging import requests +import base64 +from osm_mon.core.config import Config log = logging.getLogger(__name__) -# TODO (lavado): migrate to Class, import config variables to get token -url = "http://grafana:3000/api/" -headers = { - 'content-type': "application/json", - 'authorization': "Basic YWRtaW46YWRtaW4=" - } - -def get_all_dashboard_uids(): - # Gets only dashboards that were automated by OSM (with tag 'osm_automated') - response = requests.request("GET", url + "search?tag=osm_automated", headers=headers) - dashboards = response.json() - dashboard_uids = [] - for dashboard in dashboards: - dashboard_uids.append(dashboard['uid']) - log.debug("Searching for all dashboard uids: %s", dashboard_uids) - return dashboard_uids - - -def get_dashboard_status(uid): - response = requests.request("GET", url + "dashboards/uid/" + uid, headers=headers) - log.debug("Searching for dashboard result: %s", response.text) - return response - - -def create_dashboard(uid, name, json_file): - with open(json_file) as f: - dashboard_data = f.read() - - dashboard_data = dashboard_data.replace('OSM_ID', uid).replace('OSM_NAME', name) - - response = requests.request("POST", url + "dashboards/db/", data=dashboard_data, headers=headers) - log.debug("Creating dashboard result: %s", response.text) - return response - - -def delete_dashboard(uid): - response = requests.request("DELETE", url + "dashboards/uid/" + uid, headers=headers) - log.debug("Delete dashboard result: %s", response.text) - return response +class GrafanaBackend: + def __init__(self, config: Config): + self.conf = config + self.url = config.get('grafana', 'url') + grafana_user = config.get("grafana", "user") + grafana_password = config.get("grafana", "password") + self.headers = { + 'content-type': "application/json", + 'authorization': "Basic %s" % base64.b64encode( + (grafana_user + ":" + grafana_password).encode("utf-8")).decode() + } + + def get_all_dashboard_uids(self): + # Gets only dashboards that were automated by OSM (with tag 'osm_automated') + response = requests.request("GET", self.url + "/api/search?tag=osm_automated", headers=self.headers) + dashboards = response.json() + dashboard_uids = [] + for dashboard in dashboards: + dashboard_uids.append(dashboard['uid']) + log.debug("Searching for all dashboard uids: %s", dashboard_uids) + return dashboard_uids + + def get_dashboard_status(self, uid): + response = requests.request("GET", self.url + "/api/dashboards/uid/" + uid, headers=self.headers) + log.debug("Searching for dashboard result: %s", response.text) + return response + + def create_dashboard(self, uid, name, json_file): + with open(json_file) as f: + dashboard_data = f.read() + + dashboard_data = dashboard_data.replace('OSM_ID', uid).replace('OSM_NAME', name) + + response = requests.request( + "POST", self.url + "/api/dashboards/db/", data=dashboard_data, headers=self.headers) + log.debug("Creating dashboard result: %s", response.text) + return response + + def delete_dashboard(self, uid): + response = requests.request("DELETE", self.url + "/api/dashboards/uid/" + uid, headers=self.headers) + log.debug("Delete dashboard result: %s", response.text) + return response