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

Commit 9b129a2

Browse files
authored
Merge pull request #83 from sancyx/datasources
datasource API: add list and find by name functions
2 parents a550de4 + 7905615 commit 9b129a2

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

datasource.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,33 @@ 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+
362+
err := c.request("GET", path, nil, nil, &result)
363+
if err != nil {
364+
return 0, err
365+
}
366+
367+
return result.ID, nil
368+
}
369+
370+
// DataSources returns all data sources as defined in Grafana.
371+
func (c *Client) DataSources() ([]*DataSource, error) {
372+
result := make([]*DataSource, 0)
373+
err := c.request("GET", "/api/datasources", nil, nil, &result)
374+
if err != nil {
375+
return nil, err
376+
}
377+
378+
return result, nil
379+
}
380+
354381
// DeleteDataSource deletes the Grafana data source whose ID it's passed.
355382
func (c *Client) DeleteDataSource(id int64) error {
356383
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)