Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
Handles Grafana backends that do not require authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-silvas committed Nov 4, 2021
1 parent 83c0ec3 commit 63b81d9
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions nautobot_plugin_chatops_grafana/grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ def timezone(self, new_timezone: str):
)
self.config = new_config

@property
def headers(self) -> dict:
"""Helper function to return the required headers for a Grafana requests.
Returns:
(dict): key, value pairs of header values.
"""
headers = {"Accept": "application/json"}
if self.config.grafana_api_key:
headers["Authorization"] = f"Bearer {self.config.grafana_api_key}"
return headers

def get_png(self, panel: Panel, panel_vars: List[PanelVariable]) -> Union[bytes, None]:
"""Using requests GET the generated URL and return the binary contents of the file.
Expand All @@ -171,10 +183,9 @@ def get_png(self, panel: Panel, panel_vars: List[PanelVariable]) -> Union[bytes,
Union[bytes, None]: The raw image from the renderer or None if there was an error.
"""
url, payload = self.get_png_url(panel, panel_vars)
headers = {"Authorization": f"Bearer {self.config.grafana_api_key}"}
try:
LOGGER.debug("Begin GET %s", url)
results = requests.get(url, headers=headers, stream=True, params=payload, timeout=REQUEST_TIMEOUT_SEC)
results = requests.get(url, headers=self.headers, stream=True, params=payload, timeout=REQUEST_TIMEOUT_SEC)
except RequestException as exc:
LOGGER.error("An error occurred while accessing the url: %s Exception: %s", url, exc)
return None
Expand Down Expand Up @@ -227,13 +238,12 @@ def get_dashboards(self) -> List[dict]:
Returns:
List[dict]: A list of the grafana dashboards.
"""
headers = {"Authorization": f"Bearer {self.config.grafana_api_key}"}
url = f"{self.config.grafana_url}/api/search"
try:
LOGGER.debug("Begin GET /api/search")
results = requests.get(
url=url,
headers=headers,
headers=self.headers,
params={"type": "dash-db"},
timeout=REQUEST_TIMEOUT_SEC,
)
Expand All @@ -254,13 +264,12 @@ def get_panels(self, dashboard_uid: str) -> List[dict]:
Returns:
List[dict]: A list of the grafana panels.
"""
headers = {"Authorization": f"Bearer {self.config.grafana_api_key}"}
url = f"{self.config.grafana_url}/api/dashboards/uid/{dashboard_uid}"
try:
LOGGER.debug("Begin GET /api/dashboards/uid/")
results = requests.get(
url=url,
headers=headers,
headers=self.headers,
timeout=REQUEST_TIMEOUT_SEC,
)
except RequestException as exc:
Expand Down Expand Up @@ -289,13 +298,12 @@ def get_variables(self, dashboard_uid: str) -> List[dict]:
Returns:
List[dict]: A list of the grafana variables.
"""
headers = {"Authorization": f"Bearer {self.config.grafana_api_key}"}
url = f"{self.config.grafana_url}/api/dashboards/uid/{dashboard_uid}"
try:
LOGGER.debug("Begin GET /api/dashboards/uid/")
results = requests.get(
url=url,
headers=headers,
headers=self.headers,
timeout=REQUEST_TIMEOUT_SEC,
)
except RequestException as exc:
Expand Down

0 comments on commit 63b81d9

Please sign in to comment.