Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
Add organization ID to client connection using header.
Browse files Browse the repository at this point in the history
This seems like the right place to do so - the resource API calls that
require an organization ID that are not-quite RESTful also would need
authentication with a user that is an administrator for the
organization.  So putting it in at this level works out, and also avoids
adding orgID parameters all over the place.

Co-authored-by: hansnqyr <[email protected]>
  • Loading branch information
medains and hansnqyr committed Aug 5, 2020
1 parent 249aba6 commit ebb560a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
type Client struct {
key string
baseURL url.URL
orgID string
*http.Client
}

//New creates a new grafana client
//auth can be in user:pass format, or it can be an api key
func New(auth, baseURL string) (*Client, error) {
func New(auth, baseURL string, orgID int) (*Client, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
Expand All @@ -40,6 +41,7 @@ func New(auth, baseURL string) (*Client, error) {
return &Client{
key,
*u,
fmt.Sprintf("%d", orgID),
cleanhttp.DefaultClient(),
}, nil
}
Expand Down Expand Up @@ -88,6 +90,9 @@ func (c *Client) newRequest(method, requestPath string, query url.Values, body i
if err != nil {
return req, err
}

req.Header.Add("X-Grafana-Org-Id", c.orgID)

if c.key != "" {
req.Header.Add("Authorization", c.key)
}
Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestNew_basicAuth(t *testing.T) {
c, err := New("user:pass", "http://my-grafana.com")
c, err := New("user:pass", "http://my-grafana.com", 1)
if err != nil {
t.Errorf("expected error to be nil; got: %s", err.Error())
}
Expand All @@ -20,7 +20,7 @@ func TestNew_basicAuth(t *testing.T) {
}

func TestNew_tokenAuth(t *testing.T) {
c, err := New("123", "http://my-grafana.com")
c, err := New("123", "http://my-grafana.com", 1)
if err != nil {
t.Errorf("expected error to be nil; got: %s", err.Error())
}
Expand All @@ -37,7 +37,7 @@ func TestNew_tokenAuth(t *testing.T) {
}

func TestNew_invalidURL(t *testing.T) {
_, err := New("123", "://my-grafana.com")
_, err := New("123", "://my-grafana.com", 1)

expected := "parse \"://my-grafana.com\": missing protocol scheme"
if err.Error() != expected {
Expand Down
2 changes: 1 addition & 1 deletion mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ func gapiTestTools(code int, body string) (*mockServer, *Client) {
Host: "my-grafana.com",
}

client := &Client{"my-key", url, httpClient}
client := &Client{"my-key", url, "1", httpClient}
return mock, client
}

0 comments on commit ebb560a

Please sign in to comment.