0

I'm learning both Grafana, AND how to interact with APIs in Powershell. I was able to use the Grafana HTTP API to create a dashboard, however I cannot get the same API to create a Datasource. My code is as follows:

$header = @{"Authorization" = "Bearer apikey="}
$createDatasourceUri = "http://localhost:3000/api/datasources"
$createDatasourcejson = @'
{
    "datasource": {
        "name": "prometheusApiTest",
        "type": "prometheus",
        "url": "http://localhost:9090",
        "access": "proxy",
        "basicAuth": false,
        "isDefault": true
    }
}
'@

$datasourceParameters = @{
    Method = "POST"
    URI = $createDatasourceUri
    Body = $createDatasourcejson
    Headers = $header
    ContentType = "application/json"
}

Invoke-RestMethod @datasourceParameters 

I am presented with the following error:

Invoke-RestMethod : [{"fieldNames":["Name"],"classification":"RequiredError","message":"Required"},{"fieldNames":["Type"],"classification":"RequiredError","message":"Required"},{"fieldNames":["Access"],"classificat
ion":"RequiredError","message":"Required"}]

I do not know what's going on. Anything I can find about this error says I need to specify the ContentType as "application/json", but I've very clearly done so. I do receive data when I do a "GET" on that API endpoint, and even copying the data that gets returned still results in the above error. I'm at a complete loss, as this same code worked to create a dashboard (albeit with the right json payload for a dashboard). Any ideas?

nightsurfer
  • 113
  • 7

1 Answers1

0

The Grafana docs for the Datasource API only has the datasource element in the response, not the initial call. Line 5 of my code, and the subsequent {} for that element, are not needed, and the script functions with those lines removed.

Matt
  • 12,848
  • 2
  • 31
  • 53
nightsurfer
  • 113
  • 7