Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit e7674ad

Browse files
committed
datasource API: add list and find by name functions
1 parent a550de4 commit e7674ad

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

datasource.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,32 @@ func (c *Client) DataSourceByUID(uid string) (*DataSource, error) {
351351
return result, err
352352
}
353353

354+
// DataSourceIdByName returns the Grafana data source id by name.
355+
func (c *Client) DataSourceIdByName(name string) (int64, error) {
356+
path := fmt.Sprintf("/api/datasources/id/%s", name)
357+
358+
result := struct {
359+
ID int64 `json:"id"`
360+
}{}
361+
err := c.request("GET", path, nil, nil, &result)
362+
if err != nil {
363+
return result.ID, err
364+
}
365+
366+
return result.ID, err
367+
}
368+
369+
// DataSources returns all data sources as defined in Grafana.
370+
func (c *Client) DataSources() ([]*DataSource, error) {
371+
result := make([]*DataSource, 0)
372+
err := c.request("GET", "/api/datasources", nil, nil, &result)
373+
if err != nil {
374+
return nil, err
375+
}
376+
377+
return result, nil
378+
}
379+
354380
// DeleteDataSource deletes the Grafana data source whose ID it's passed.
355381
func (c *Client) DeleteDataSource(id int64) error {
356382
path := fmt.Sprintf("/api/datasources/%d", id)

datasource_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
const (
1010
createdDataSourceJSON = `{"id":1,"uid":"myuid0001","message":"Datasource added", "name": "test_datasource"}`
11+
getDataSourceJSON = `{"id":1}`
12+
getDataSourcesJSON = `[{"id":1,"name":"foo","type":"cloudwatch","url":"http://some-url.com","access":"access","isDefault":true}]`
1113
)
1214

1315
func TestNewDataSource(t *testing.T) {
@@ -235,3 +237,36 @@ func TestNewAzureDataSource(t *testing.T) {
235237
t.Error("datasource creation response should return the created datasource ID")
236238
}
237239
}
240+
241+
func TestDataSources(t *testing.T) {
242+
server, client := gapiTestTools(t, 200, getDataSourcesJSON)
243+
defer server.Close()
244+
245+
datasources, err := client.DataSources()
246+
if err != nil {
247+
t.Fatal(err)
248+
}
249+
250+
t.Log(pretty.PrettyFormat(datasources))
251+
252+
if len(datasources) != 1 {
253+
t.Error("Length of returned datasources should be 1")
254+
}
255+
if datasources[0].ID != 1 || datasources[0].Name != "foo" {
256+
t.Error("Not correctly parsing returned datasources.")
257+
}
258+
}
259+
260+
func TestDataSourceIdByName(t *testing.T) {
261+
server, client := gapiTestTools(t, 200, getDataSourceJSON)
262+
defer server.Close()
263+
264+
datasourceId, err := client.DataSourceIdByName("foo")
265+
if err != nil {
266+
t.Fatal(err)
267+
}
268+
269+
if datasourceId != 1 {
270+
t.Error("Not correctly parsing returned datasources.")
271+
}
272+
}

0 commit comments

Comments
 (0)