fix(http): filtering telegraf configs only by organization ID

Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
pull/10616/head
Leonardo Di Donato 2019-01-09 22:01:37 +01:00 committed by Leonardo Di Donato
parent b44ced3607
commit 3d0a42cbbc
2 changed files with 55 additions and 8 deletions

View File

@ -194,10 +194,9 @@ func (h *TelegrafHandler) handleGetTelegraf(w http.ResponseWriter, r *http.Reque
func decodeTelegrafConfigFilter(ctx context.Context, r *http.Request) (*platform.TelegrafConfigFilter, error) { func decodeTelegrafConfigFilter(ctx context.Context, r *http.Request) (*platform.TelegrafConfigFilter, error) {
f := &platform.TelegrafConfigFilter{} f := &platform.TelegrafConfigFilter{}
urm, err := decodeUserResourceMappingFilter(ctx, r) urm, err := decodeUserResourceMappingFilter(ctx, r)
if err == nil { if err != nil {
return f, err f.UserResourceMappingFilter = *urm
} }
f.UserResourceMappingFilter = *urm
q := r.URL.Query() q := r.URL.Query()
if orgIDStr := q.Get("orgID"); orgIDStr != "" { if orgIDStr := q.Get("orgID"); orgIDStr != "" {

View File

@ -3,6 +3,7 @@ package http
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -28,6 +29,57 @@ func TestTelegrafHandler_handleGetTelegrafs(t *testing.T) {
r *http.Request r *http.Request
wants wants wants wants
}{ }{
{
name: "get telegraf configs by organization id",
r: httptest.NewRequest("GET", "http://any.url/api/v2/telegrafs?orgID=0000000000000002", nil),
svc: &mock.TelegrafConfigStore{
FindTelegrafConfigsF: func(ctx context.Context, filter platform.TelegrafConfigFilter, opt ...platform.FindOptions) ([]*platform.TelegrafConfig, int, error) {
if filter.OrganizationID != nil && *filter.OrganizationID == platform.ID(2) {
return []*platform.TelegrafConfig{
&platform.TelegrafConfig{
ID: platform.ID(1),
OrganizationID: platform.ID(2),
Name: "tc1",
Plugins: []platform.TelegrafPlugin{
{
Config: &inputs.CPUStats{},
},
},
},
}, 1, nil
}
return []*platform.TelegrafConfig{}, 0, fmt.Errorf("not found")
},
},
wants: wants{
statusCode: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: `
{
"configurations":[
{
"id":"0000000000000001",
"organizationID":"0000000000000002",
"name":"tc1",
"agent":{
"collectionInterval":0
},
"plugins":[
{
"name":"cpu",
"type":"input",
"comment":"",
"config":{
}
}
]
}
]
}`,
},
},
{ {
name: "return CPU plugin for telegraf", name: "return CPU plugin for telegraf",
r: httptest.NewRequest("GET", "http://any.url/api/v2/telegrafs", nil), r: httptest.NewRequest("GET", "http://any.url/api/v2/telegrafs", nil),
@ -102,12 +154,8 @@ func TestTelegrafHandler_handleGetTelegrafs(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
logger := zaptest.NewLogger(t)
mapping := mock.NewUserResourceMappingService()
labels := mock.NewLabelService()
users := mock.NewUserService()
w := httptest.NewRecorder() w := httptest.NewRecorder()
h := NewTelegrafHandler(logger, mapping, labels, tt.svc, users) h := NewTelegrafHandler(zaptest.NewLogger(t), mock.NewUserResourceMappingService(), mock.NewLabelService(), tt.svc, mock.NewUserService())
h.ServeHTTP(w, tt.r) h.ServeHTTP(w, tt.r)
res := w.Result() res := w.Result()