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

Commit dd7fea8

Browse files
authored
Merge pull request #9 from medains/master
Add organization ID to client connection using header
2 parents d171908 + 36698a7 commit dd7fea8

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/url"
1212
"os"
1313
"path"
14+
"strconv"
1415

1516
"github.com/hashicorp/go-cleanhttp"
1617
)
@@ -30,6 +31,8 @@ type Config struct {
3031
BasicAuth *url.Userinfo
3132
// Client provides an optional HTTP client, otherwise a default will be used.
3233
Client *http.Client
34+
// OrgID provides an optional organization ID, ignored when using APIKey, BasicAuth defaults to last used org
35+
OrgID int64
3336
}
3437

3538
// New creates a new Grafana client.
@@ -103,6 +106,8 @@ func (c *Client) newRequest(method, requestPath string, query url.Values, body i
103106

104107
if c.config.APIKey != "" {
105108
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.config.APIKey))
109+
} else if c.config.OrgID != 0 {
110+
req.Header.Add("X-Grafana-Org-Id", strconv.FormatInt(c.config.OrgID, 10))
106111
}
107112

108113
if os.Getenv("GF_LOG") != "" {

client_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ func TestNew_tokenAuth(t *testing.T) {
3636
}
3737
}
3838

39+
func TestNew_orgID(t *testing.T) {
40+
const orgID = 456
41+
c, err := New("http://my-grafana.com", Config{OrgID: orgID})
42+
if err != nil {
43+
t.Fatalf("expected error to be nil; got: %s", err.Error())
44+
}
45+
46+
expected := "http://my-grafana.com"
47+
if c.baseURL.String() != expected {
48+
t.Errorf("expected error: %s; got: %s", expected, c.baseURL.String())
49+
}
50+
51+
if c.config.OrgID != orgID {
52+
t.Errorf("expected error: %d; got: %d", orgID, c.config.OrgID)
53+
}
54+
}
55+
3956
func TestNew_invalidURL(t *testing.T) {
4057
_, err := New("://my-grafana.com", Config{APIKey: "123"})
4158

0 commit comments

Comments
 (0)