response = requests.request(
"POST", self.url + "/api/dashboards/db/", data=json.dumps(dashboard_json_data), headers=self.headers)
+ # Don't create the dashboard if already exists.
+ if json.loads(response.text).get("status") == "name-exists":
+ return
+
# Get team id
if project_name is not None:
name = project_name
response_team = requests.request(
"GET", self.url + "/api/teams/search?name={}".format(name), headers=self.headers)
- # Check if team informaiton is available
- if len(json.loads(response_team.text)["teams"]) > 0:
- # As team information is available so it is a non-admin user
- team_id = json.loads(response_team.text)["teams"][0]["id"]
- permission_data = {"items": [{"teamId": team_id, "permission": 2}, ]}
- # Give required permission to the team's folder
- response = requests.request(
- "POST", self.url + "/api/folders/{}/permissions".format(project_name), json=permission_data,
- headers=self.headers)
- else:
+ # Remove default permissions of admin user's dashboard so that it is not visible to non-admin users
+ if len(json.loads(response_team.text)["teams"]) == 0:
# As team information is not available so it is admin user
dahboard_id = json.loads(response.text)["id"]
- # Remove dashboard's default permissions so that it is only visible to admin user
requests.request(
"POST", self.url + "/api/dashboards/id/{}/permissions".format(dahboard_id),
headers=self.headers)
requests.request("POST", self.url + "/api/folders", json=folder_payload, headers=self.headers)
log.info("Dashboard folder %s created", folder_name)
+ response_team = requests.request("GET", self.url + "/api/teams/search?name={}".format(folder_name),
+ headers=self.headers)
+ # Create team if it doesn't already exists
+ if len(json.loads(response_team.text)["teams"]) == 0:
+ self.create_grafana_teams(folder_name)
+ response_team = requests.request("GET", self.url + "/api/teams/search?name={}".format(folder_name),
+ headers=self.headers)
+ # Assign required permission to the team's folder
+ team_id = json.loads(response_team.text)["teams"][0]["id"]
+ permission_data = {"items": [{"teamId": team_id, "permission": 2}, ]}
+ requests.request("POST", self.url + "/api/folders/{}/permissions".format(folder_name),
+ json=permission_data, headers=self.headers)
+
def delete_grafana_users(self, user_name):
# Get user id
response_id = requests.request("GET", self.url + "/api/users/lookup?loginOrEmail={}".format(user_name),