From 844c0a59c2426e31804255537652e390080246b1 Mon Sep 17 00:00:00 2001 From: medains Date: Tue, 20 Oct 2020 11:42:07 +0100 Subject: [PATCH 1/3] Add organization ID to client connection using header. 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 --- client.go | 4 ++++ client_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/client.go b/client.go index b83a2165..fa40114c 100644 --- a/client.go +++ b/client.go @@ -30,6 +30,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 string } // New creates a new Grafana client. @@ -103,6 +105,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 != "" { + req.Header.Add("X-Grafana-Org-Id", c.config.OrgID) } if os.Getenv("GF_LOG") != "" { diff --git a/client_test.go b/client_test.go index 6b6f04a1..e183d506 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: %s; got: %s", orgID, c.config.OrgID) + } +} + func TestNew_invalidURL(t *testing.T) { _, err := New("://my-grafana.com", Config{APIKey: "123"}) From ab37cb661799e26f6dccdb6d00985724ca009de6 Mon Sep 17 00:00:00 2001 From: medains Date: Wed, 28 Oct 2020 09:34:29 +0000 Subject: [PATCH 2/3] Change OrgID from string to int64 --- client.go | 7 ++++--- client_test.go | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index fa40114c..c31c2b56 100644 --- a/client.go +++ b/client.go @@ -11,6 +11,7 @@ import ( "net/url" "os" "path" + "strconv" "github.com/hashicorp/go-cleanhttp" ) @@ -31,7 +32,7 @@ type Config struct { // 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 string + OrgID int64 } // New creates a new Grafana client. @@ -105,8 +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 != "" { - req.Header.Add("X-Grafana-Org-Id", c.config.OrgID) + } 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 e183d506..18349b55 100644 --- a/client_test.go +++ b/client_test.go @@ -37,7 +37,7 @@ func TestNew_tokenAuth(t *testing.T) { } func TestNew_orgId(t *testing.T) { - const orgID = "456" + 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()) @@ -49,7 +49,7 @@ func TestNew_orgId(t *testing.T) { } if c.config.OrgID != orgID { - t.Errorf("expected error: %s; got: %s", orgID, c.config.OrgID) + t.Errorf("expected error: %d; got: %d", orgID, c.config.OrgID) } } From 36698a7d87e3d064b08fcee83efc41950c1ae8d5 Mon Sep 17 00:00:00 2001 From: Arve Knudsen Date: Wed, 28 Oct 2020 10:55:11 +0100 Subject: [PATCH 3/3] Update client_test.go --- client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client_test.go b/client_test.go index 18349b55..e8d8198c 100644 --- a/client_test.go +++ b/client_test.go @@ -36,7 +36,7 @@ func TestNew_tokenAuth(t *testing.T) { } } -func TestNew_orgId(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 {