Skip to content

Commit

Permalink
Merge pull request #11763 from influxdata/fix/label-auth
Browse files Browse the repository at this point in the history
Pass resource type to generic resource handler for labels
  • Loading branch information
desa authored Feb 8, 2019
2 parents 9859025 + f271d0a commit 9aeca07
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 174 deletions.
169 changes: 85 additions & 84 deletions http/bucket_service.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions http/dashboard_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func NewDashboardHandler(b *DashboardBackend) *DashboardHandler {
labelBackend := &LabelBackend{
Logger: b.Logger.With(zap.String("handler", "label")),
LabelService: b.LabelService,
ResourceType: platform.DashboardsResourceType,
}
h.HandlerFunc("GET", dashboardsIDLabelsPath, newGetLabelsHandler(labelBackend))
h.HandlerFunc("POST", dashboardsIDLabelsPath, newPostLabelHandler(labelBackend))
Expand Down
101 changes: 100 additions & 1 deletion http/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"context"
"encoding/json"
"fmt"
"go.uber.org/zap"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"

"go.uber.org/zap"

platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/inmem"
"github.com/influxdata/influxdb/mock"
Expand Down Expand Up @@ -1287,3 +1288,101 @@ func TestDashboardService(t *testing.T) {
t.Parallel()
platformtesting.DashboardService(initDashboardService, t)
}

func TestService_handlePostDashboardLabel(t *testing.T) {
type fields struct {
LabelService platform.LabelService
}
type args struct {
labelMapping *platform.LabelMapping
dashboardID platform.ID
}
type wants struct {
statusCode int
contentType string
body string
}

tests := []struct {
name string
fields fields
args args
wants wants
}{
{
name: "add label to dashboard",
fields: fields{
LabelService: &mock.LabelService{
FindLabelByIDFn: func(ctx context.Context, id platform.ID) (*platform.Label, error) {
return &platform.Label{
ID: 1,
Name: "label",
Properties: map[string]string{
"color": "fff000",
},
}, nil
},
CreateLabelMappingFn: func(ctx context.Context, m *platform.LabelMapping) error { return nil },
},
},
args: args{
labelMapping: &platform.LabelMapping{
ResourceID: 100,
LabelID: 1,
},
dashboardID: 100,
},
wants: wants{
statusCode: http.StatusCreated,
contentType: "application/json; charset=utf-8",
body: `
{
"label": {
"id": "0000000000000001",
"name": "label",
"properties": {
"color": "fff000"
}
},
"links": {
"self": "/api/v2/labels/0000000000000001"
}
}
`,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dashboardBackend := NewMockDashboardBackend()
dashboardBackend.LabelService = tt.fields.LabelService
h := NewDashboardHandler(dashboardBackend)

b, err := json.Marshal(tt.args.labelMapping)
if err != nil {
t.Fatalf("failed to unmarshal label mapping: %v", err)
}

url := fmt.Sprintf("http://localhost:9999/api/v2/dashboards/%s/labels", tt.args.dashboardID)
r := httptest.NewRequest("POST", url, bytes.NewReader(b))
w := httptest.NewRecorder()

h.ServeHTTP(w, r)

res := w.Result()
content := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body)

if res.StatusCode != tt.wants.statusCode {
t.Errorf("got %v, want %v", res.StatusCode, tt.wants.statusCode)
}
if tt.wants.contentType != "" && content != tt.wants.contentType {
t.Errorf("got %v, want %v", content, tt.wants.contentType)
}
if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq {
t.Errorf("Diff\n%s", diff)
}
})
}
}
19 changes: 12 additions & 7 deletions http/label_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"context"
"encoding/json"
"fmt"
"go.uber.org/zap"
"net/http"
"path"

"go.uber.org/zap"

platform "github.com/influxdata/influxdb"
"github.com/julienschmidt/httprouter"
)
Expand Down Expand Up @@ -299,14 +300,15 @@ func newLabelsResponse(ls []*platform.Label) *labelsResponse {
type LabelBackend struct {
Logger *zap.Logger
LabelService platform.LabelService
ResourceType platform.ResourceType
}

// newGetLabelsHandler returns a handler func for a GET to /labels endpoints
func newGetLabelsHandler(b *LabelBackend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

req, err := decodeGetLabelsRequest(ctx, r)
req, err := decodeGetLabelsRequest(ctx, r, b.ResourceType)
if err != nil {
EncodeError(ctx, err, w)
return
Expand All @@ -329,7 +331,7 @@ type getLabelsRequest struct {
filter platform.LabelMappingFilter
}

func decodeGetLabelsRequest(ctx context.Context, r *http.Request) (*getLabelsRequest, error) {
func decodeGetLabelsRequest(ctx context.Context, r *http.Request, rt platform.ResourceType) (*getLabelsRequest, error) {
req := &getLabelsRequest{}

params := httprouter.ParamsFromContext(ctx)
Expand All @@ -346,6 +348,7 @@ func decodeGetLabelsRequest(ctx context.Context, r *http.Request) (*getLabelsReq
return nil, err
}
req.filter.ResourceID = i
req.filter.ResourceType = rt

return req, nil
}
Expand All @@ -355,7 +358,7 @@ func newPostLabelHandler(b *LabelBackend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

req, err := decodePostLabelMappingRequest(ctx, r)
req, err := decodePostLabelMappingRequest(ctx, r, b.ResourceType)
if err != nil {
EncodeError(ctx, err, w)
return
Expand Down Expand Up @@ -388,7 +391,7 @@ type postLabelMappingRequest struct {
Mapping platform.LabelMapping
}

func decodePostLabelMappingRequest(ctx context.Context, r *http.Request) (*postLabelMappingRequest, error) {
func decodePostLabelMappingRequest(ctx context.Context, r *http.Request, rt platform.ResourceType) (*postLabelMappingRequest, error) {
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
Expand All @@ -409,6 +412,7 @@ func decodePostLabelMappingRequest(ctx context.Context, r *http.Request) (*postL
}

mapping.ResourceID = rid
mapping.ResourceType = rt

if err := mapping.Validate(); err != nil {
return nil, err
Expand Down Expand Up @@ -457,8 +461,9 @@ func newDeleteLabelHandler(b *LabelBackend) http.HandlerFunc {
}

mapping := &platform.LabelMapping{
LabelID: req.LabelID,
ResourceID: req.ResourceID,
LabelID: req.LabelID,
ResourceID: req.ResourceID,
ResourceType: b.ResourceType,
}

if err := b.LabelService.DeleteLabelMapping(ctx, mapping); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion http/onboarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (s *SetupService) Generate(ctx context.Context, or *platform.OnboardingRequ
return nil, err
}

bkt, err := oResp.Bucket.toPlatform()
bkt, err := oResp.Bucket.toInfluxDB()
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 9aeca07

Please sign in to comment.