mirror of https://github.com/grafana/grafana
parent
118e2a6364
commit
c973241435
@ -0,0 +1,61 @@ |
||||
package mqe |
||||
|
||||
import ( |
||||
"context" |
||||
"encoding/json" |
||||
"fmt" |
||||
"io/ioutil" |
||||
"net/http" |
||||
"net/url" |
||||
"path" |
||||
|
||||
"golang.org/x/net/context/ctxhttp" |
||||
|
||||
"github.com/grafana/grafana/pkg/log" |
||||
"github.com/grafana/grafana/pkg/tsdb" |
||||
) |
||||
|
||||
type TokenClient struct { |
||||
tlog log.Logger |
||||
} |
||||
|
||||
func NewTokenClient() *TokenClient { |
||||
return &TokenClient{ |
||||
tlog: log.New("tsdb.mqe.tokenclient"), |
||||
} |
||||
} |
||||
|
||||
func (client *TokenClient) GetTokenData(ctx context.Context, datasource *tsdb.DataSourceInfo) (*TokenResponse, error) { |
||||
u, _ := url.Parse(datasource.Url) |
||||
u.Path = path.Join(u.Path, "token") |
||||
|
||||
req, err := http.NewRequest(http.MethodGet, u.String(), nil) |
||||
if err != nil { |
||||
client.tlog.Info("Failed to create request", "error", err) |
||||
} |
||||
|
||||
res, err := ctxhttp.Do(ctx, HttpClient, req) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
body, err := ioutil.ReadAll(res.Body) |
||||
defer res.Body.Close() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
if res.StatusCode/100 != 2 { |
||||
client.tlog.Info("Request failed", "status", res.Status, "body", string(body)) |
||||
return nil, fmt.Errorf("Request failed status: %v", res.Status) |
||||
} |
||||
|
||||
var result *TokenResponse |
||||
err = json.Unmarshal(body, &result) |
||||
if err != nil { |
||||
client.tlog.Info("Failed to unmarshal graphite response", "error", err, "status", res.Status, "body", string(body)) |
||||
return nil, err |
||||
} |
||||
|
||||
return result, nil |
||||
} |
@ -0,0 +1,28 @@ |
||||
package mqe |
||||
|
||||
import ( |
||||
"context" |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson" |
||||
"github.com/grafana/grafana/pkg/tsdb" |
||||
. "github.com/smartystreets/goconvey/convey" |
||||
) |
||||
|
||||
func TestTokenClient(t *testing.T) { |
||||
SkipConvey("Token client", t, func() { |
||||
dsInfo := &tsdb.DataSourceInfo{ |
||||
JsonData: simplejson.New(), |
||||
Url: "", |
||||
} |
||||
|
||||
client := NewTokenClient() |
||||
|
||||
body, err := client.GetTokenData(context.TODO(), dsInfo) |
||||
|
||||
So(err, ShouldBeNil) |
||||
So(len(body.Body.Functions), ShouldBeGreaterThan, 1) |
||||
So(len(body.Body.Metrics), ShouldBeGreaterThan, 1) |
||||
So(body.Success, ShouldBeTrue) |
||||
}) |
||||
} |
@ -1,7 +1,26 @@ |
||||
package mqe |
||||
|
||||
import ( |
||||
"github.com/grafana/grafana/pkg/tsdb" |
||||
) |
||||
|
||||
type MQEQuery struct { |
||||
Metrics []string |
||||
Hosts []string |
||||
Apps []string |
||||
} |
||||
|
||||
func (q *MQEQuery) Build(queryContext *tsdb.QueryContext) string { |
||||
return "" |
||||
} |
||||
|
||||
type TokenBody struct { |
||||
Functions []string |
||||
Metrics []string |
||||
//tagset
|
||||
} |
||||
|
||||
type TokenResponse struct { |
||||
Success bool |
||||
Body TokenBody |
||||
} |
||||
|
Loading…
Reference in new issue