0

I want to update the grafana dashboard through its API but i have found out 2 problems:

  1. i got an 412 HTTP error {'message': 'A dashboard with the same name in the folder already exists', 'status': 'name-exists'}
  2. when i change the name of the dash board it is getting moved to general folder

i got the json for updating the dashboard by:

  1. getting current state of dashboard by downloading it through API
  2. changing a few variables (right now i just changed those trying to make it stay in current folder)
response_json["meta"]["folderId"] = folder_id
response_json["meta"]["folderUid"] = folder_uid
response_json["meta"]["overwrite"] = True
response_json["dashboard"]["uid"] = dashboard_uid
response_json["dashboard"]["id"] = dashboard_id

I'm not changing anything else, i don't know therefore why I'm getting problems with name-exists; ofc it exists i want to update it not create a new one, so its expected that the dashboard exists.

I have tried changing the name of the dashboard.

1 Answers1

-1

try this:

def get_dashboard_by_uid(uid: str):
    # _headers contains Authorization with the bearer
    endpoint = f'{_url}/dashboards/uid/{uid}'
    return requests.get(endpoint, headers=_headers, verify=False).json()

def update_dashboard():
    dashboard = get_dashboard_by_uid('<your-dashboard-uid>')
    endpoint = f'<url>/dashboards/db'
    headers = _headers
    headers['Content-Type'] = 'application/json'
    headers['Host'] = '<your-host>'
    dashboard['dashboard']['title'] = 'Change from API update'
    dashboard['folderId'] = <your-folder-id>
    return requests.post(endpoint, data=json.dumps(dashboard), headers=headers, verify=False).json()

From the API doc:

You have to provide multiple fields in the JSON, the first one is the dashboard json model and the rest is the folder, a message for the commit version history and the overwrite property.

{ "dashboard": { json_dashboard_model }, "folderId": 0, "folderUid": "l3KqBxCMz", "message": "Made changes to xyz", "overwrite": false }

With this line in the code: dashboard['folderId'] = <your-folder-id> you can add the object to the folder in which you want your dashboard.

before

after code run

SIMULATAN
  • 833
  • 2
  • 17
RCoupy
  • 34
  • 5