Skip to content

Commit 57ab0d5

Browse files
committed
Auto-detecting opensearch version for grafana datasource since that is requirement now
1 parent 5d7c8f3 commit 57ab0d5

File tree

1 file changed

+42
-6
lines changed
  • psconfig/perfsonar-psconfig/psconfig/grafana

1 file changed

+42
-6
lines changed

psconfig/perfsonar-psconfig/psconfig/grafana/agent.py

+42-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class Agent(BaseAgent):
4242
"pplEnabled": True,
4343
"timeField": "pscheduler.start_time",
4444
"tlsAuth": False,
45-
"tlsSkipVerify": True,
45+
"tlsSkipVerify": True,
46+
"version": "AUTO"
4647
},
4748
"readOnly": False
4849
}
@@ -413,8 +414,14 @@ def _gf_http(self, url_path, action, method="get", data={}):
413414
General function for sending HTTP requests to Grafana and handling errors
414415
'''
415416
url = self._gf_build_url(url_path)
417+
return self._http(url, action, method, data, headers=self.grafana_header, auth=self.grafana_auth, log_prefix="grafana_")
418+
419+
def _http(self, url, action, method="get", data={}, headers={}, auth=None, log_prefix=""):
420+
'''
421+
General function for sending HTTP requests and handling errors
422+
'''
416423
local_context = {}
417-
local_context["grafana_url"] = url
424+
local_context["{}url".format(log_prefix)] = url
418425
local_context["action"] = "{}.start".format(action)
419426
if data:
420427
local_context["data"] = data
@@ -424,13 +431,13 @@ def _gf_http(self, url_path, action, method="get", data={}):
424431
try:
425432
r = None
426433
if method == "get":
427-
r = requests.get(url, headers=self.grafana_header, auth=self.grafana_auth, verify=False)
434+
r = requests.get(url, headers=headers, auth=auth, verify=False)
428435
elif method == "post":
429-
r = requests.post(url, json=data, headers=self.grafana_header, auth=self.grafana_auth, verify=False)
436+
r = requests.post(url, json=data, headers=headers, auth=auth, verify=False)
430437
elif method == "put":
431-
r = requests.put(url, json=data, headers=self.grafana_header, auth=self.grafana_auth, verify=False)
438+
r = requests.put(url, json=data, headers=headers, auth=auth, verify=False)
432439
elif method == "delete":
433-
r = requests.delete(url, headers=self.grafana_header, auth=self.grafana_auth, verify=False)
440+
r = requests.delete(url, headers=headers, auth=auth, verify=False)
434441
else:
435442
return None, "Invalid method specified."
436443
r.raise_for_status()
@@ -529,13 +536,41 @@ def _build_ds_name(self, ds_url):
529536
'''
530537
return self.grafana_datasource_name_format.format(ds_url)
531538

539+
def _ds_version(self, ds_body):
540+
'''
541+
Determine version of database for datasource. Required for Opensearch.
542+
'''
543+
#Detect database version if set to AUTO
544+
if ds_body.get("jsonData", {}).get("version","").upper() != "AUTO":
545+
#nothing to do
546+
return
547+
548+
#clear out AUTO in case something fails
549+
ds_body["jsonData"]["version"] = None
550+
if ds_body["type"] != "grafana-opensearch-datasource":
551+
#only know how to get version for opensearch
552+
self.logger.error(self.logf.format("Datasource version set to AUTO but setting version not supported for datasource type {}. Manually change jsonData.version in config with {}".format(ds_body["type"], self.PSCONFIG_KEY_GF_DS_SETTINGS)))
553+
return
554+
555+
#fetch version
556+
r, msg = self._http(ds_body["url"], "ds_version", log_prefix="ds_")
557+
if msg:
558+
#return if error making request
559+
return
560+
561+
#parse version
562+
ds_body["jsonData"]["version"] = r.json().get("version", {}).get("number", None)
563+
if ds_body["jsonData"]["version"] is None:
564+
self.logger.error(self.logf.format("Unable to detect version for {} of type {}. Manually set version in config with {}".format(ds_body["url"], ds_body["type"], self.PSCONFIG_KEY_GF_DS_SETTINGS)))
565+
532566
def _gf_create_datasource(self, ds_url, ds_settings):
533567
'''
534568
Creates a datasource using the Grafana API
535569
'''
536570
ds_body = ds_settings.copy()
537571
ds_body["url"] = ds_url
538572
ds_body["name"] = self._build_ds_name(ds_url)
573+
self._ds_version(ds_body)
539574
r, msg = self._gf_http("/api/datasources", "create_datasource", method="post", data=ds_body)
540575
if msg:
541576
self.logger.error(self.logf.format("Unable to create datasource for {}: {}".format(ds_url, msg)))
@@ -562,6 +597,7 @@ def _gf_update_datasource(self, ds_url, ds_settings, ds_name):
562597
ds_body["name"] = ds_name
563598
ds_body["uid"] = self.grafana_datasource_by_name.get(ds_name, {}).get("uid", None)
564599
ds_body["type"] = self.grafana_datasource_by_name.get(ds_name, {}).get("type", None)
600+
self._ds_version(ds_body)
565601
r, msg = self._gf_http(f'/api/datasources/uid/{ds_body["uid"]}', "update_datasource", method="put", data=ds_body)
566602
if msg:
567603
self.logger.error(self.logf.format("Unable to update datasource for {}: {}".format(ds_url, msg)))

0 commit comments

Comments
 (0)