diff --git a/client.go b/client.go index b83a2165..c31c2b56 100644 --- a/client.go +++ b/client.go @@ -11,6 +11,7 @@ import ( "net/url" "os" "path" + "strconv" "github.com/hashicorp/go-cleanhttp" ) @@ -30,6 +31,8 @@ type Config struct { BasicAuth *url.Userinfo // Client provides an optional HTTP client, otherwise a default will be used. Client *http.Client + // OrgID provides an optional organization ID, ignored when using APIKey, BasicAuth defaults to last used org + OrgID int64 } // New creates a new Grafana client. @@ -103,6 +106,8 @@ func (c *Client) newRequest(method, requestPath string, query url.Values, body i if c.config.APIKey != "" { req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.config.APIKey)) + } else if c.config.OrgID != 0 { + req.Header.Add("X-Grafana-Org-Id", strconv.FormatInt(c.config.OrgID, 10)) } if os.Getenv("GF_LOG") != "" { diff --git a/client_test.go b/client_test.go index 6b6f04a1..e8d8198c 100644 --- a/client_test.go +++ b/client_test.go @@ -36,6 +36,23 @@ func TestNew_tokenAuth(t *testing.T) { } } +func TestNew_orgID(t *testing.T) { + const orgID = 456 + c, err := New("http://my-grafana.com", Config{OrgID: orgID}) + if err != nil { + t.Fatalf("expected error to be nil; got: %s", err.Error()) + } + + expected := "http://my-grafana.com" + if c.baseURL.String() != expected { + t.Errorf("expected error: %s; got: %s", expected, c.baseURL.String()) + } + + if c.config.OrgID != orgID { + t.Errorf("expected error: %d; got: %d", orgID, c.config.OrgID) + } +} + func TestNew_invalidURL(t *testing.T) { _, err := New("://my-grafana.com", Config{APIKey: "123"})