Skip to content

Commit

Permalink
Refactor the REST client. Add some documentation for the basic types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgelbg authored Nov 14, 2019
1 parent 4982691 commit fc98fdd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
3 changes: 3 additions & 0 deletions pkg/grafana/client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package grafana

// ClientConfig is the configuration for a supported Grafana client
type ClientConfig struct {
Host string
Key string
}

// Client contains the supported operations over a Grafana instance
type Client interface {
GetAllDatasources() ([]*DataSource, error)
}

// NewClientFn is a convenient type offered for instantiating new clients
type NewClientFn = func(ClientConfig) (Client, error)
28 changes: 13 additions & 15 deletions pkg/grafana/rest-client.go → pkg/grafana/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ func (r *RestClient) GetAllDatasources() ([]*DataSource, error) {

existedEnv := make(map[string]bool)
for idx, ds := range datasources {
var newDs DataSource
if newDs, err = r.GetDatasource(ds.Id); err != nil {
newDs, err := r.getDatasource(ds.Id)
if err != nil {
return nil, err
}

// assign the current datasource with the new datasource with json data field
ds = &newDs
ds = newDs
datasources[idx] = ds

ds.SecureJsonData = make(map[string]string)
Expand All @@ -105,23 +105,21 @@ func (r *RestClient) GetAllDatasources() ([]*DataSource, error) {
return datasources, err
}

func (r *RestClient) GetDatasource(id int64) (DataSource, error) {
var (
raw []byte
ds DataSource
code int
err error
)

func (r *RestClient) getDatasource(id int64) (*DataSource, error) {
datasourcePath := fmt.Sprintf("/api/datasources/%d", id)
if raw, code, err = r.Get(datasourcePath, nil); err != nil {
return ds, err
raw, code, err := r.Get(datasourcePath, nil)

if err != nil {
return nil, err
}

if code != http.StatusOK {
return ds, fmt.Errorf("HTTP error %d", code)
return nil, fmt.Errorf("HTTP error %d", code)
}

ds := &DataSource{}
if err = json.Unmarshal(raw, &ds); err != nil {
return ds, fmt.Errorf("Failed to parse JSON data from %s%s", r.baseURL, datasourcePath)
return nil, fmt.Errorf("Failed to parse JSON data from %s%s", r.baseURL, datasourcePath)
}

return ds, err
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions pkg/grafana/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

func TestWriteTo(t *testing.T) {
assert := assert.New(t)

datasources := []*grafana.DataSource{
&grafana.DataSource{
Name: "test-db",
Expand All @@ -20,9 +21,11 @@ func TestWriteTo(t *testing.T) {
SecureJsonData: map[string]string{"password": "test"},
},
}

dsp := &grafana.DataSourceProvisioning{ApiVersion: 1, Datasources: datasources}
actual := new(bytes.Buffer)
dsp.WriteTo(actual)
expected, _ := ioutil.ReadFile("testdata/datasources.golden.yaml")

assert.Equal(string(expected), actual.String())
}

0 comments on commit fc98fdd

Please sign in to comment.