Merge pull request #13315 from influxdata/filter-label-org

feat(http): filter label response by organization
pull/13351/head
Jade McGough 2019-04-12 02:07:33 -07:00 committed by GitHub
commit b5f6effda4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 267 additions and 301 deletions

View File

@ -87,7 +87,7 @@ func (s *LabelService) FindLabelByID(ctx context.Context, id influxdb.ID) (*infl
return nil, err
}
if err := authorizeReadLabel(ctx, l.OrganizationID, id); err != nil {
if err := authorizeReadLabel(ctx, l.OrgID, id); err != nil {
return nil, err
}
@ -107,7 +107,7 @@ func (s *LabelService) FindLabels(ctx context.Context, filter influxdb.LabelFilt
// https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
labels := ls[:0]
for _, l := range ls {
err := authorizeReadLabel(ctx, l.OrganizationID, l.ID)
err := authorizeReadLabel(ctx, l.OrgID, l.ID)
if err != nil &&
influxdb.ErrorCode(err) != influxdb.EUnauthorized &&
influxdb.ErrorCode(err) != influxdb.EInvalid {
@ -139,7 +139,7 @@ func (s *LabelService) FindResourceLabels(ctx context.Context, filter influxdb.L
labels := ls[:0]
for _, l := range ls {
err := authorizeReadLabel(ctx, l.OrganizationID, l.ID)
err := authorizeReadLabel(ctx, l.OrgID, l.ID)
if err != nil && influxdb.ErrorCode(err) != influxdb.EUnauthorized {
return nil, err
}
@ -156,7 +156,7 @@ func (s *LabelService) FindResourceLabels(ctx context.Context, filter influxdb.L
// CreateLabel checks to see if the authorizer on context has read access to the new label's org.
func (s *LabelService) CreateLabel(ctx context.Context, l *influxdb.Label) error {
if err := authorizeReadOrg(ctx, l.OrganizationID); err != nil {
if err := authorizeReadOrg(ctx, l.OrgID); err != nil {
return err
}
@ -170,7 +170,7 @@ func (s *LabelService) CreateLabelMapping(ctx context.Context, m *influxdb.Label
return err
}
if err := authorizeWriteLabel(ctx, l.OrganizationID, m.LabelID); err != nil {
if err := authorizeWriteLabel(ctx, l.OrgID, m.LabelID); err != nil {
return err
}
@ -188,7 +188,7 @@ func (s *LabelService) UpdateLabel(ctx context.Context, id influxdb.ID, upd infl
return nil, err
}
if err := authorizeWriteLabel(ctx, l.OrganizationID, id); err != nil {
if err := authorizeWriteLabel(ctx, l.OrgID, id); err != nil {
return nil, err
}
@ -202,7 +202,7 @@ func (s *LabelService) DeleteLabel(ctx context.Context, id influxdb.ID) error {
return err
}
if err := authorizeWriteLabel(ctx, l.OrganizationID, id); err != nil {
if err := authorizeWriteLabel(ctx, l.OrgID, id); err != nil {
return err
}
@ -216,7 +216,7 @@ func (s *LabelService) DeleteLabelMapping(ctx context.Context, m *influxdb.Label
return err
}
if err := authorizeWriteLabel(ctx, l.OrganizationID, m.LabelID); err != nil {
if err := authorizeWriteLabel(ctx, l.OrgID, m.LabelID); err != nil {
return err
}

View File

@ -56,7 +56,7 @@ func TestLabelService_FindLabelByID(t *testing.T) {
FindLabelByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: id,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
},
@ -82,7 +82,7 @@ func TestLabelService_FindLabelByID(t *testing.T) {
FindLabelByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: id,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
},
@ -145,15 +145,15 @@ func TestLabelService_FindLabels(t *testing.T) {
return []*influxdb.Label{
{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 2,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 3,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
}, nil
},
@ -171,15 +171,15 @@ func TestLabelService_FindLabels(t *testing.T) {
labels: []*influxdb.Label{
{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 2,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 3,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
},
},
@ -192,15 +192,15 @@ func TestLabelService_FindLabels(t *testing.T) {
return []*influxdb.Label{
{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 2,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 3,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
}, nil
},
@ -219,7 +219,7 @@ func TestLabelService_FindLabels(t *testing.T) {
labels: []*influxdb.Label{
{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
},
},
@ -232,15 +232,15 @@ func TestLabelService_FindLabels(t *testing.T) {
return []*influxdb.Label{
{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 2,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 3,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
}, nil
},
@ -303,13 +303,13 @@ func TestLabelService_UpdateLabel(t *testing.T) {
FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
UpdateLabelFn: func(ctx context.Context, id influxdb.ID, upd influxdb.LabelUpdate) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
},
@ -337,13 +337,13 @@ func TestLabelService_UpdateLabel(t *testing.T) {
FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
UpdateLabelFn: func(ctx context.Context, id influxdb.ID, upd influxdb.LabelUpdate) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
},
@ -407,7 +407,7 @@ func TestLabelService_DeleteLabel(t *testing.T) {
FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
DeleteLabelFn: func(ctx context.Context, id influxdb.ID) error {
@ -439,7 +439,7 @@ func TestLabelService_DeleteLabel(t *testing.T) {
FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
DeleteLabelFn: func(ctx context.Context, id influxdb.ID) error {
@ -554,7 +554,7 @@ func TestLabelService_CreateLabel(t *testing.T) {
ctx := context.Background()
ctx = influxdbcontext.SetAuthorizer(ctx, &Authorizer{[]influxdb.Permission{tt.args.permission}})
err := s.CreateLabel(ctx, &influxdb.Label{Name: "name", OrganizationID: influxdbtesting.MustIDBase16(orgOneID)})
err := s.CreateLabel(ctx, &influxdb.Label{Name: "name", OrgID: influxdbtesting.MustIDBase16(orgOneID)})
influxdbtesting.ErrorsEqual(t, err, tt.wants.err)
})
}
@ -586,15 +586,15 @@ func TestLabelService_FindResourceLabels(t *testing.T) {
return []*influxdb.Label{
{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 2,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 3,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
}, nil
},
@ -626,15 +626,15 @@ func TestLabelService_FindResourceLabels(t *testing.T) {
labels: []*influxdb.Label{
{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 2,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 3,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
},
},
@ -647,15 +647,15 @@ func TestLabelService_FindResourceLabels(t *testing.T) {
return []*influxdb.Label{
{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 2,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 3,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
}, nil
},
@ -688,7 +688,7 @@ func TestLabelService_FindResourceLabels(t *testing.T) {
labels: []*influxdb.Label{
{
ID: 3,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
},
},
@ -701,15 +701,15 @@ func TestLabelService_FindResourceLabels(t *testing.T) {
return []*influxdb.Label{
{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 2,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 3,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
}, nil
},
@ -742,15 +742,15 @@ func TestLabelService_FindResourceLabels(t *testing.T) {
return []*influxdb.Label{
{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 2,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
{
ID: 3,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
},
}, nil
},
@ -821,7 +821,7 @@ func TestLabelService_CreateLabelMapping(t *testing.T) {
FindLabelByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
CreateLabelMappingFn: func(ctx context.Context, lm *influxdb.LabelMapping) error {
@ -862,7 +862,7 @@ func TestLabelService_CreateLabelMapping(t *testing.T) {
FindLabelByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
CreateLabelMappingFn: func(ctx context.Context, lm *influxdb.LabelMapping) error {
@ -899,7 +899,7 @@ func TestLabelService_CreateLabelMapping(t *testing.T) {
FindLabelByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
CreateLabelMappingFn: func(ctx context.Context, lm *influxdb.LabelMapping) error {
@ -969,7 +969,7 @@ func TestLabelService_DeleteLabelMapping(t *testing.T) {
FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
DeleteLabelMappingFn: func(ctx context.Context, m *influxdb.LabelMapping) error {
@ -1010,7 +1010,7 @@ func TestLabelService_DeleteLabelMapping(t *testing.T) {
FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
DeleteLabelMappingFn: func(ctx context.Context, m *influxdb.LabelMapping) error {
@ -1047,7 +1047,7 @@ func TestLabelService_DeleteLabelMapping(t *testing.T) {
FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) {
return &influxdb.Label{
ID: 1,
OrganizationID: influxdbtesting.MustIDBase16(orgOneID),
OrgID: influxdbtesting.MustIDBase16(orgOneID),
}, nil
},
DeleteLabelMappingFn: func(ctx context.Context, m *influxdb.LabelMapping) error {

View File

@ -1,44 +0,0 @@
package bolt_test
import (
"context"
"testing"
platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/bolt"
platformtesting "github.com/influxdata/influxdb/testing"
)
func initLabelService(f platformtesting.LabelFields, t *testing.T) (platform.LabelService, string, func()) {
c, closeFn, err := NewTestClient()
if err != nil {
t.Fatalf("failed to create new bolt client: %v", err)
}
c.IDGenerator = f.IDGenerator
ctx := context.Background()
for _, l := range f.Labels {
if err := c.PutLabel(ctx, l); err != nil {
t.Fatalf("failed to populate labels: %v", err)
}
}
for _, m := range f.Mappings {
if err := c.PutLabelMapping(ctx, m); err != nil {
t.Fatalf("failed to populate label mappings: %v", err)
}
}
return c, bolt.OpPrefix, func() {
defer closeFn()
for _, l := range f.Labels {
if err := c.DeleteLabel(ctx, l.ID); err != nil {
t.Logf("failed to remove label: %v", err)
}
}
}
}
func TestLabelService_LabelService(t *testing.T) {
platformtesting.LabelService(initLabelService, t)
}

View File

@ -10,7 +10,7 @@ import (
"go.uber.org/zap"
platform "github.com/influxdata/influxdb"
influxdb "github.com/influxdata/influxdb"
"github.com/julienschmidt/httprouter"
)
@ -20,7 +20,7 @@ type LabelHandler struct {
Logger *zap.Logger
LabelService platform.LabelService
LabelService influxdb.LabelService
}
const (
@ -29,7 +29,7 @@ const (
)
// NewLabelHandler returns a new instance of LabelHandler
func NewLabelHandler(s platform.LabelService) *LabelHandler {
func NewLabelHandler(s influxdb.LabelService) *LabelHandler {
h := &LabelHandler{
Router: NewRouter(),
Logger: zap.NewNop(),
@ -68,19 +68,19 @@ func (h *LabelHandler) handlePostLabel(w http.ResponseWriter, r *http.Request) {
}
type postLabelRequest struct {
Label *platform.Label
Label *influxdb.Label
}
func (b postLabelRequest) Validate() error {
if b.Label.Name == "" {
return &platform.Error{
Code: platform.EInvalid,
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "label requires a name",
}
}
if !b.Label.OrganizationID.Valid() {
return &platform.Error{
Code: platform.EInvalid,
if !b.Label.OrgID.Valid() {
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "label requires a valid orgID",
}
}
@ -89,10 +89,10 @@ func (b postLabelRequest) Validate() error {
// TODO(jm): ensure that the specified org actually exists
func decodePostLabelRequest(ctx context.Context, r *http.Request) (*postLabelRequest, error) {
l := &platform.Label{}
l := &influxdb.Label{}
if err := json.NewDecoder(r.Body).Decode(l); err != nil {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "unable to decode label request",
Err: err,
}
@ -109,7 +109,13 @@ func decodePostLabelRequest(ctx context.Context, r *http.Request) (*postLabelReq
func (h *LabelHandler) handleGetLabels(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
labels, err := h.LabelService.FindLabels(ctx, platform.LabelFilter{})
req, err := decodeGetLabelsRequest(ctx, r)
if err != nil {
EncodeError(ctx, err, w)
return
}
labels, err := h.LabelService.FindLabels(ctx, req.filter)
if err != nil {
EncodeError(ctx, err, w)
return
@ -122,6 +128,25 @@ func (h *LabelHandler) handleGetLabels(w http.ResponseWriter, r *http.Request) {
}
}
type getLabelsRequest struct {
filter influxdb.LabelFilter
}
func decodeGetLabelsRequest(ctx context.Context, r *http.Request) (*getLabelsRequest, error) {
qp := r.URL.Query()
req := &getLabelsRequest{}
if orgID := qp.Get("orgID"); orgID != "" {
id, err := influxdb.IDFromString(orgID)
if err != nil {
return nil, err
}
req.filter.OrgID = id
}
return req, nil
}
// handleGetLabel is the HTTP handler for the GET /api/v2/labels/id route.
func (h *LabelHandler) handleGetLabel(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
@ -145,20 +170,20 @@ func (h *LabelHandler) handleGetLabel(w http.ResponseWriter, r *http.Request) {
}
type getLabelRequest struct {
LabelID platform.ID
LabelID influxdb.ID
}
func decodeGetLabelRequest(ctx context.Context, r *http.Request) (*getLabelRequest, error) {
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "label id is not valid",
}
}
var i platform.ID
var i influxdb.ID
if err := i.DecodeFromString(id); err != nil {
return nil, err
}
@ -188,20 +213,20 @@ func (h *LabelHandler) handleDeleteLabel(w http.ResponseWriter, r *http.Request)
}
type deleteLabelRequest struct {
LabelID platform.ID
LabelID influxdb.ID
}
func decodeDeleteLabelRequest(ctx context.Context, r *http.Request) (*deleteLabelRequest, error) {
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "url missing id",
}
}
var i platform.ID
var i influxdb.ID
if err := i.DecodeFromString(id); err != nil {
return nil, err
}
@ -235,26 +260,26 @@ func (h *LabelHandler) handlePatchLabel(w http.ResponseWriter, r *http.Request)
}
type patchLabelRequest struct {
Update platform.LabelUpdate
LabelID platform.ID
Update influxdb.LabelUpdate
LabelID influxdb.ID
}
func decodePatchLabelRequest(ctx context.Context, r *http.Request) (*patchLabelRequest, error) {
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "url missing id",
}
}
var i platform.ID
var i influxdb.ID
if err := i.DecodeFromString(id); err != nil {
return nil, err
}
upd := &platform.LabelUpdate{}
upd := &influxdb.LabelUpdate{}
if err := json.NewDecoder(r.Body).Decode(upd); err != nil {
return nil, err
}
@ -276,10 +301,10 @@ type LabelService struct {
type labelResponse struct {
Links map[string]string `json:"links"`
Label platform.Label `json:"label"`
Label influxdb.Label `json:"label"`
}
func newLabelResponse(l *platform.Label) *labelResponse {
func newLabelResponse(l *influxdb.Label) *labelResponse {
return &labelResponse{
Links: map[string]string{
"self": fmt.Sprintf("/api/v2/labels/%s", l.ID),
@ -290,10 +315,10 @@ func newLabelResponse(l *platform.Label) *labelResponse {
type labelsResponse struct {
Links map[string]string `json:"links"`
Labels []*platform.Label `json:"labels"`
Labels []*influxdb.Label `json:"labels"`
}
func newLabelsResponse(ls []*platform.Label) *labelsResponse {
func newLabelsResponse(ls []*influxdb.Label) *labelsResponse {
return &labelsResponse{
Links: map[string]string{
"self": fmt.Sprintf("/api/v2/labels"),
@ -306,8 +331,8 @@ func newLabelsResponse(ls []*platform.Label) *labelsResponse {
// label handlers.
type LabelBackend struct {
Logger *zap.Logger
LabelService platform.LabelService
ResourceType platform.ResourceType
LabelService influxdb.LabelService
ResourceType influxdb.ResourceType
}
// newGetLabelsHandler returns a handler func for a GET to /labels endpoints
@ -315,7 +340,7 @@ func newGetLabelsHandler(b *LabelBackend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
req, err := decodeGetLabelsRequest(ctx, r, b.ResourceType)
req, err := decodeGetLabelMappingsRequest(ctx, r, b.ResourceType)
if err != nil {
EncodeError(ctx, err, w)
return
@ -334,23 +359,23 @@ func newGetLabelsHandler(b *LabelBackend) http.HandlerFunc {
}
}
type getLabelsRequest struct {
filter platform.LabelMappingFilter
type getLabelMappingsRequest struct {
filter influxdb.LabelMappingFilter
}
func decodeGetLabelsRequest(ctx context.Context, r *http.Request, rt platform.ResourceType) (*getLabelsRequest, error) {
req := &getLabelsRequest{}
func decodeGetLabelMappingsRequest(ctx context.Context, r *http.Request, rt influxdb.ResourceType) (*getLabelMappingsRequest, error) {
req := &getLabelMappingsRequest{}
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "url missing id",
}
}
var i platform.ID
var i influxdb.ID
if err := i.DecodeFromString(id); err != nil {
return nil, err
}
@ -395,28 +420,28 @@ func newPostLabelHandler(b *LabelBackend) http.HandlerFunc {
}
type postLabelMappingRequest struct {
Mapping platform.LabelMapping
Mapping influxdb.LabelMapping
}
func decodePostLabelMappingRequest(ctx context.Context, r *http.Request, rt platform.ResourceType) (*postLabelMappingRequest, error) {
func decodePostLabelMappingRequest(ctx context.Context, r *http.Request, rt influxdb.ResourceType) (*postLabelMappingRequest, error) {
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "url missing id",
}
}
var rid platform.ID
var rid influxdb.ID
if err := rid.DecodeFromString(id); err != nil {
return nil, err
}
mapping := &platform.LabelMapping{}
mapping := &influxdb.LabelMapping{}
if err := json.NewDecoder(r.Body).Decode(mapping); err != nil {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "Invalid post label map request",
}
}
@ -446,7 +471,7 @@ func newDeleteLabelHandler(b *LabelBackend) http.HandlerFunc {
return
}
mapping := &platform.LabelMapping{
mapping := &influxdb.LabelMapping{
LabelID: req.LabelID,
ResourceID: req.ResourceID,
ResourceType: b.ResourceType,
@ -462,34 +487,34 @@ func newDeleteLabelHandler(b *LabelBackend) http.HandlerFunc {
}
type deleteLabelMappingRequest struct {
ResourceID platform.ID
LabelID platform.ID
ResourceID influxdb.ID
LabelID influxdb.ID
}
func decodeDeleteLabelMappingRequest(ctx context.Context, r *http.Request) (*deleteLabelMappingRequest, error) {
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "url missing resource id",
}
}
var rid platform.ID
var rid influxdb.ID
if err := rid.DecodeFromString(id); err != nil {
return nil, err
}
id = params.ByName("lid")
if id == "" {
return nil, &platform.Error{
Code: platform.EInvalid,
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "label id is missing",
}
}
var lid platform.ID
var lid influxdb.ID
if err := lid.DecodeFromString(id); err != nil {
return nil, err
}
@ -500,12 +525,12 @@ func decodeDeleteLabelMappingRequest(ctx context.Context, r *http.Request) (*del
}, nil
}
func labelIDPath(id platform.ID) string {
func labelIDPath(id influxdb.ID) string {
return path.Join(labelsPath, id.String())
}
// FindLabelByID returns a single label by ID.
func (s *LabelService) FindLabelByID(ctx context.Context, id platform.ID) (*platform.Label, error) {
func (s *LabelService) FindLabelByID(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) {
u, err := newURL(s.Addr, labelIDPath(id))
if err != nil {
return nil, err
@ -535,12 +560,12 @@ func (s *LabelService) FindLabelByID(ctx context.Context, id platform.ID) (*plat
return &lr.Label, nil
}
func (s *LabelService) FindLabels(ctx context.Context, filter platform.LabelFilter, opt ...platform.FindOptions) ([]*platform.Label, error) {
func (s *LabelService) FindLabels(ctx context.Context, filter influxdb.LabelFilter, opt ...influxdb.FindOptions) ([]*influxdb.Label, error) {
return nil, nil
}
// FindResourceLabels returns a list of labels, derived from a label mapping filter.
func (s *LabelService) FindResourceLabels(ctx context.Context, filter platform.LabelMappingFilter) ([]*platform.Label, error) {
func (s *LabelService) FindResourceLabels(ctx context.Context, filter influxdb.LabelMappingFilter) ([]*influxdb.Label, error) {
url, err := newURL(s.Addr, resourceIDPath(s.BasePath, filter.ResourceID))
if err != nil {
return nil, err
@ -574,7 +599,7 @@ func (s *LabelService) FindResourceLabels(ctx context.Context, filter platform.L
}
// CreateLabel creates a new label.
func (s *LabelService) CreateLabel(ctx context.Context, l *platform.Label) error {
func (s *LabelService) CreateLabel(ctx context.Context, l *influxdb.Label) error {
u, err := newURL(s.Addr, labelsPath)
if err != nil {
return err
@ -614,7 +639,7 @@ func (s *LabelService) CreateLabel(ctx context.Context, l *platform.Label) error
return nil
}
func (s *LabelService) CreateLabelMapping(ctx context.Context, m *platform.LabelMapping) error {
func (s *LabelService) CreateLabelMapping(ctx context.Context, m *influxdb.LabelMapping) error {
if err := m.Validate(); err != nil {
return err
}
@ -657,7 +682,7 @@ func (s *LabelService) CreateLabelMapping(ctx context.Context, m *platform.Label
}
// UpdateLabel updates a label and returns the updated label.
func (s *LabelService) UpdateLabel(ctx context.Context, id platform.ID, upd platform.LabelUpdate) (*platform.Label, error) {
func (s *LabelService) UpdateLabel(ctx context.Context, id influxdb.ID, upd influxdb.LabelUpdate) (*influxdb.Label, error) {
u, err := newURL(s.Addr, labelIDPath(id))
if err != nil {
return nil, err
@ -696,7 +721,7 @@ func (s *LabelService) UpdateLabel(ctx context.Context, id platform.ID, upd plat
}
// DeleteLabel removes a label by ID.
func (s *LabelService) DeleteLabel(ctx context.Context, id platform.ID) error {
func (s *LabelService) DeleteLabel(ctx context.Context, id influxdb.ID) error {
u, err := newURL(s.Addr, labelIDPath(id))
if err != nil {
return err
@ -718,7 +743,7 @@ func (s *LabelService) DeleteLabel(ctx context.Context, id platform.ID) error {
return CheckError(resp)
}
func (s *LabelService) DeleteLabelMapping(ctx context.Context, m *platform.LabelMapping) error {
func (s *LabelService) DeleteLabelMapping(ctx context.Context, m *influxdb.LabelMapping) error {
url, err := newURL(s.Addr, labelNamePath(s.BasePath, m.ResourceID, m.LabelID))
if err != nil {
return err
@ -740,6 +765,6 @@ func (s *LabelService) DeleteLabelMapping(ctx context.Context, m *platform.Label
return CheckError(resp)
}
func labelNamePath(basePath string, resourceID platform.ID, labelID platform.ID) string {
func labelNamePath(basePath string, resourceID influxdb.ID, labelID influxdb.ID) string {
return path.Join(basePath, resourceID.String(), "labels", labelID.String())
}

View File

@ -283,7 +283,7 @@ func TestService_handlePostLabel(t *testing.T) {
args: args{
label: &platform.Label{
Name: "mylabel",
OrganizationID: platformtesting.MustIDBase16("020f755c3c082008"),
OrgID: platformtesting.MustIDBase16("020f755c3c082008"),
},
},
wants: wants{

View File

@ -1820,6 +1820,13 @@ paths:
tags:
- Labels
summary: Create a label
parameters:
- $ref: '#/components/parameters/TraceSpan'
- in: query
name: orgID
description: return only labels that belong to the specified organization
schema:
type: string
requestBody:
description: label to create
required: true

View File

@ -1,33 +0,0 @@
package inmem
import (
"context"
"testing"
platform "github.com/influxdata/influxdb"
platformtesting "github.com/influxdata/influxdb/testing"
)
func initLabelService(f platformtesting.LabelFields, t *testing.T) (platform.LabelService, string, func()) {
s := NewService()
s.IDGenerator = f.IDGenerator
ctx := context.Background()
for _, l := range f.Labels {
if err := s.PutLabel(ctx, l); err != nil {
t.Fatalf("failed to populate labels")
}
}
for _, m := range f.Mappings {
if err := s.CreateLabelMapping(ctx, m); err != nil {
t.Fatalf("failed to populate label mappings")
}
}
return s, OpPrefix, func() {}
}
func TestLabelService(t *testing.T) {
t.Parallel()
platformtesting.LabelService(initLabelService, t)
}

View File

@ -85,7 +85,8 @@ func (s *Service) findLabelByID(ctx context.Context, tx Tx, id influxdb.ID) (*in
func filterLabelsFn(filter influxdb.LabelFilter) func(l *influxdb.Label) bool {
return func(label *influxdb.Label) bool {
return (filter.Name == "" || (filter.Name == label.Name))
return (filter.Name == "" || (filter.Name == label.Name)) &&
((filter.OrgID == nil) || (filter.OrgID != nil && *filter.OrgID == label.OrgID))
}
}
@ -286,7 +287,7 @@ func (s *Service) createLabelUserResourceMappings(ctx context.Context, tx Tx, l
ms, err := s.findUserResourceMappings(ctx, tx, influxdb.UserResourceMappingFilter{
ResourceType: influxdb.OrgsResourceType,
ResourceID: l.OrganizationID,
ResourceID: l.OrgID,
})
if err != nil {
return &influxdb.Error{

View File

@ -48,7 +48,7 @@ type LabelService interface {
// Label is a tag set on a resource, typically used for filtering on a UI.
type Label struct {
ID ID `json:"id,omitempty"`
OrganizationID ID `json:"orgID,omitempty"`
OrgID ID `json:"orgID,omitempty"`
Name string `json:"name"`
Properties map[string]string `json:"properties,omitempty"`
}
@ -62,10 +62,10 @@ func (l *Label) Validate() error {
}
}
if !l.OrganizationID.Valid() {
if !l.OrgID.Valid() {
return &Error{
Code: EInvalid,
Msg: "organization ID is required",
Msg: "orgID is required",
}
}

View File

@ -48,7 +48,7 @@ func TestLabelValidate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
m := platform.Label{
Name: tt.fields.Name,
OrganizationID: tt.fields.OrgID,
OrgID: tt.fields.OrgID,
}
if err := m.Validate(); (err != nil) != tt.wantErr {
t.Errorf("Label.Validate() error = %v, wantErr %v", err, tt.wantErr)

View File

@ -14,6 +14,7 @@ import (
const (
labelOneID = "41a9f7288d4e2d64"
labelTwoID = "b7c5355e1134b11c"
labelThreeID = "c8d6466f2245c22d"
)
var labelCmpOptions = cmp.Options{
@ -23,7 +24,7 @@ var labelCmpOptions = cmp.Options{
cmp.Transformer("Sort", func(in []*influxdb.Label) []*influxdb.Label {
out := append([]*influxdb.Label(nil), in...) // Copy input to avoid mutating it
sort.Slice(out, func(i, j int) bool {
return out[i].Name < out[j].Name
return out[i].ID.String() > out[j].ID.String()
})
return out
}),
@ -208,16 +209,24 @@ func FindLabels(
{
ID: MustIDBase16(labelOneID),
Name: "Tag1",
OrgID: MustIDBase16(orgOneID),
},
{
ID: MustIDBase16(labelTwoID),
Name: "Tag2",
OrgID: MustIDBase16(orgOneID),
},
{
ID: MustIDBase16(labelThreeID),
Name: "Tag1",
OrgID: MustIDBase16(orgTwoID),
},
},
},
args: args{
filter: influxdb.LabelFilter{
Name: "Tag1",
OrgID: idPtr(MustIDBase16(orgOneID)),
},
},
wants: wants{
@ -225,6 +234,7 @@ func FindLabels(
{
ID: MustIDBase16(labelOneID),
Name: "Tag1",
OrgID: MustIDBase16(orgOneID),
},
},
},
@ -346,7 +356,7 @@ func UpdateLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag1",
},
},
@ -361,7 +371,7 @@ func UpdateLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "NotTag1",
},
},
@ -373,7 +383,7 @@ func UpdateLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag1",
},
},
@ -390,7 +400,7 @@ func UpdateLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag1",
Properties: map[string]string{
"color": "fff000",
@ -405,7 +415,7 @@ func UpdateLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag1",
Properties: map[string]string{
"color": "fff000",
@ -426,7 +436,7 @@ func UpdateLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag1",
Properties: map[string]string{
"color": "abc123",
@ -442,7 +452,7 @@ func UpdateLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag1",
Properties: map[string]string{
"color": "fff000",
@ -463,7 +473,7 @@ func UpdateLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag1",
Properties: map[string]string{
"color": "fff000",
@ -539,12 +549,12 @@ func DeleteLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag1",
},
{
ID: MustIDBase16(labelTwoID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag2",
},
},
@ -556,7 +566,7 @@ func DeleteLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelTwoID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag2",
},
},
@ -568,7 +578,7 @@ func DeleteLabel(
Labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag1",
},
},
@ -580,7 +590,7 @@ func DeleteLabel(
labels: []*influxdb.Label{
{
ID: MustIDBase16(labelOneID),
OrganizationID: MustIDBase16(orgOneID),
OrgID: MustIDBase16(orgOneID),
Name: "Tag1",
},
},