dashboard_data = f.read()\r
\r
dashboard_data = dashboard_data.replace('OSM_ID', uid).replace('OSM_NAME', name)\r
+ dashboard_json_data = json.loads(dashboard_data)
+ # get folder id
+ if project_name:
+ folder_name = project_name
+ else:
+ folder_name = name
+ response_folder_id = requests.request(
+ "GET", self.url + "/api/folders/{}".format(folder_name), headers=self.headers)
+ if response_folder_id.status_code == 200:
+ folder_id = json.loads(response_folder_id.text)["id"]
+ dashboard_json_data["folderId"] = folder_id
+ dashboard_json_data["overwrite"] = False
\r
response = requests.request(\r
- "POST", self.url + "/api/dashboards/db/", data=dashboard_data, headers=self.headers)\r
+ "POST", self.url + "/api/dashboards/db/", data=json.dumps(dashboard_json_data), headers=self.headers)
+
# get team id\r
if project_name is not None:\r
name = project_name\r
team_id = json.loads(response_team.text)["teams"][0]["id"]\r
permission_data = {"items": [{"teamId": team_id, "permission": 2}, ]}\r
# provide permission to dashboard\r
- dahboard_id = json.loads(response.text)["id"]\r
response = requests.request(\r
- "POST", self.url + "/api/dashboards/id/{}/permissions".format(dahboard_id), json=permission_data,\r
+ "POST", self.url + "/api/folders/{}/permissions".format(project_name), json=permission_data,
headers=self.headers)\r
+ log.info("Dashboard %s is created in Grafana", name)
return response\r
except Exception:\r
log.exception("Exception processing message: ")\r
\r
def delete_dashboard(self, uid):\r
response = requests.request("DELETE", self.url + "/api/dashboards/uid/" + uid, headers=self.headers)\r
- log.debug("Delete dashboard result: %s", response.text)\r
+ log.debug("Dashboard %s deleted from Grafana", uid)
return response\r
\r
def create_grafana_users(self, user):\r
json_data = json.loads(response_users.text)\r
url = "/api/org/users/{}/".format(json_data["id"])\r
permission_payload = {"role": "Editor", }\r
- requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers)\r
- log.info("Grafana user created: %s", response_users.text)\r
+ requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers)
+ log.info("New user %s created in Grafana", user)
return response_users\r
\r
# create grafana team with member\r
url = "/api/org/users/{}/".format(user_id)\r
permission_payload = {"role": "Admin", }\r
requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers)\r
- log.info("Member added to grafana team")\r
+ log.info("User %s is assigned Admin permission", user_name)
+ else:
+ url = "/api/org/users/{}/".format(user_id)
+ permission_payload = {"role": "Editor", }
+ requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers)
+ log.info("User %s is assigned Editor permission", user_name)
return response_team\r
\r
# create grafana team\r
def create_grafana_teams(self, team_name):\r
team_payload = {"name": team_name, }\r
- response = requests.request("POST", self.url + "/api/teams", json=team_payload, headers=self.headers)\r
- log.info("Grafana Team is created: %s", response.text)\r
+ requests.request("POST", self.url + "/api/teams", json=team_payload, headers=self.headers)
+ log.info("New team %s created in Grafana", team_name)
+
+ # create grafana folder
+ def create_grafana_folders(self, folder_name):
+ folder_payload = {"uid": folder_name, "title": folder_name}
+ requests.request("POST", self.url + "/api/folders", json=folder_payload, headers=self.headers)
+ log.info("Dashboard folder %s created", folder_name)
\r
def delete_grafana_users(self, user_name):\r
# find user id\r
log.exception("Exception processing message: ")\r
# delete user\r
response = requests.request("DELETE", self.url + "/api/admin/users/{}".format(user_id), headers=self.headers)\r
- log.info("Grafana user deleted: %s", response.text)\r
+ log.info("User %s deleted in Grafana", user_name)
return response\r
\r
def delete_grafana_team(self, project_name):\r
+ # delete grafana folder
+ requests.request("DELETE", self.url + "/api/folders/{}".format(project_name),
+ headers=self.headers)
+ # delete grafana team
team_obj = requests.request("GET", self.url + "/api/teams/search?name={}".format(project_name),\r
headers=self.headers)\r
team_id = json.loads(team_obj.text)["teams"][0]["id"]\r
response = requests.request("DELETE", self.url + "/api/teams/{}".format(team_id), headers=self.headers)\r
- log.info("Grafana team deleated: %s", response.text)\r
+ log.info("Team %s deleted in Grafana", project_name)
return response\r
\r
def update_grafana_teams(self, project_new_name, project_old_name):\r
data = {"name": project_new_name, }\r
response = requests.request("PUT", self.url + "/api/teams/{}".format(team_id), json=data, headers=self.headers)\r
log.info("Grafana team updated %s", response.text)\r
- return response\r
+ return response