diff --git a/authorizer/label.go b/authorizer/label.go index 6aca920485..8aed4daa35 100644 --- a/authorizer/label.go +++ b/authorizer/label.go @@ -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 } diff --git a/authorizer/label_test.go b/authorizer/label_test.go index f8f9372c16..8b42e75cbf 100644 --- a/authorizer/label_test.go +++ b/authorizer/label_test.go @@ -55,8 +55,8 @@ func TestLabelService_FindLabelByID(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: id, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: id, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, nil }, }, @@ -81,8 +81,8 @@ func TestLabelService_FindLabelByID(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: id, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: id, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, nil }, }, @@ -144,16 +144,16 @@ func TestLabelService_FindLabels(t *testing.T) { FindLabelsFn: func(ctx context.Context, filter influxdb.LabelFilter) ([]*influxdb.Label, error) { return []*influxdb.Label{ { - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 2, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 2, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 3, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 3, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, }, nil }, @@ -170,16 +170,16 @@ func TestLabelService_FindLabels(t *testing.T) { wants: wants{ labels: []*influxdb.Label{ { - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 2, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 2, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 3, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 3, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, }, }, @@ -191,16 +191,16 @@ func TestLabelService_FindLabels(t *testing.T) { FindLabelsFn: func(ctx context.Context, filter influxdb.LabelFilter) ([]*influxdb.Label, error) { return []*influxdb.Label{ { - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 2, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 2, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 3, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 3, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, }, nil }, @@ -218,8 +218,8 @@ func TestLabelService_FindLabels(t *testing.T) { wants: wants{ labels: []*influxdb.Label{ { - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, }, }, @@ -231,16 +231,16 @@ func TestLabelService_FindLabels(t *testing.T) { FindLabelsFn: func(ctx context.Context, filter influxdb.LabelFilter) ([]*influxdb.Label, error) { return []*influxdb.Label{ { - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 2, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 2, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 3, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 3, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, }, nil }, @@ -302,14 +302,14 @@ func TestLabelService_UpdateLabel(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + 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), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, nil }, }, @@ -336,14 +336,14 @@ func TestLabelService_UpdateLabel(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + 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), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, nil }, }, @@ -406,8 +406,8 @@ func TestLabelService_DeleteLabel(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, nil }, DeleteLabelFn: func(ctx context.Context, id influxdb.ID) error { @@ -438,8 +438,8 @@ func TestLabelService_DeleteLabel(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + 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) }) } @@ -585,16 +585,16 @@ func TestLabelService_FindResourceLabels(t *testing.T) { FindResourceLabelsFn: func(ctx context.Context, f influxdb.LabelMappingFilter) ([]*influxdb.Label, error) { return []*influxdb.Label{ { - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 2, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 2, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 3, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 3, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, }, nil }, @@ -625,16 +625,16 @@ func TestLabelService_FindResourceLabels(t *testing.T) { err: nil, labels: []*influxdb.Label{ { - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 2, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 2, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 3, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 3, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, }, }, @@ -646,16 +646,16 @@ func TestLabelService_FindResourceLabels(t *testing.T) { FindResourceLabelsFn: func(ctx context.Context, f influxdb.LabelMappingFilter) ([]*influxdb.Label, error) { return []*influxdb.Label{ { - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 2, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 2, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 3, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 3, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, }, nil }, @@ -687,8 +687,8 @@ func TestLabelService_FindResourceLabels(t *testing.T) { err: nil, labels: []*influxdb.Label{ { - ID: 3, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 3, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, }, }, @@ -700,16 +700,16 @@ func TestLabelService_FindResourceLabels(t *testing.T) { FindResourceLabelsFn: func(ctx context.Context, f influxdb.LabelMappingFilter) ([]*influxdb.Label, error) { return []*influxdb.Label{ { - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 2, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 2, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 3, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 3, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, }, nil }, @@ -741,16 +741,16 @@ func TestLabelService_FindResourceLabels(t *testing.T) { FindResourceLabelsFn: func(ctx context.Context, f influxdb.LabelMappingFilter) ([]*influxdb.Label, error) { return []*influxdb.Label{ { - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 2, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 2, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, { - ID: 3, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 3, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, }, nil }, @@ -820,8 +820,8 @@ func TestLabelService_CreateLabelMapping(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, nil }, CreateLabelMappingFn: func(ctx context.Context, lm *influxdb.LabelMapping) error { @@ -861,8 +861,8 @@ func TestLabelService_CreateLabelMapping(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, nil }, CreateLabelMappingFn: func(ctx context.Context, lm *influxdb.LabelMapping) error { @@ -898,8 +898,8 @@ func TestLabelService_CreateLabelMapping(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, nil }, CreateLabelMappingFn: func(ctx context.Context, lm *influxdb.LabelMapping) error { @@ -968,8 +968,8 @@ func TestLabelService_DeleteLabelMapping(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, nil }, DeleteLabelMappingFn: func(ctx context.Context, m *influxdb.LabelMapping) error { @@ -1009,8 +1009,8 @@ func TestLabelService_DeleteLabelMapping(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, nil }, DeleteLabelMappingFn: func(ctx context.Context, m *influxdb.LabelMapping) error { @@ -1046,8 +1046,8 @@ func TestLabelService_DeleteLabelMapping(t *testing.T) { LabelService: &mock.LabelService{ FindLabelByIDFn: func(ctc context.Context, id influxdb.ID) (*influxdb.Label, error) { return &influxdb.Label{ - ID: 1, - OrganizationID: influxdbtesting.MustIDBase16(orgOneID), + ID: 1, + OrgID: influxdbtesting.MustIDBase16(orgOneID), }, nil }, DeleteLabelMappingFn: func(ctx context.Context, m *influxdb.LabelMapping) error { diff --git a/bolt/label_test.go b/bolt/label_test.go deleted file mode 100644 index 4bcace1f2d..0000000000 --- a/bolt/label_test.go +++ /dev/null @@ -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) -} diff --git a/http/label_service.go b/http/label_service.go index 4001c26859..d2871a3f57 100644 --- a/http/label_service.go +++ b/http/label_service.go @@ -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()) } diff --git a/http/label_test.go b/http/label_test.go index 578f7b4fa8..a4345b7d68 100644 --- a/http/label_test.go +++ b/http/label_test.go @@ -282,8 +282,8 @@ func TestService_handlePostLabel(t *testing.T) { }, args: args{ label: &platform.Label{ - Name: "mylabel", - OrganizationID: platformtesting.MustIDBase16("020f755c3c082008"), + Name: "mylabel", + OrgID: platformtesting.MustIDBase16("020f755c3c082008"), }, }, wants: wants{ diff --git a/http/swagger.yml b/http/swagger.yml index 4c73f86385..9b6d3d44fb 100644 --- a/http/swagger.yml +++ b/http/swagger.yml @@ -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 diff --git a/inmem/label_test.go b/inmem/label_test.go deleted file mode 100644 index 2781290d5f..0000000000 --- a/inmem/label_test.go +++ /dev/null @@ -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) -} diff --git a/kv/label.go b/kv/label.go index 82c8e4a692..4d86a2f766 100644 --- a/kv/label.go +++ b/kv/label.go @@ -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{ diff --git a/label.go b/label.go index ff8a8913ea..cc2ad1dfaf 100644 --- a/label.go +++ b/label.go @@ -47,10 +47,10 @@ 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"` - Name string `json:"name"` - Properties map[string]string `json:"properties,omitempty"` + ID ID `json:"id,omitempty"` + OrgID ID `json:"orgID,omitempty"` + Name string `json:"name"` + Properties map[string]string `json:"properties,omitempty"` } // Validate returns an error if the label is invalid. @@ -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", } } diff --git a/label_test.go b/label_test.go index 89e79e48d6..33c32ceeb8 100644 --- a/label_test.go +++ b/label_test.go @@ -47,8 +47,8 @@ func TestLabelValidate(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { m := platform.Label{ - Name: tt.fields.Name, - OrganizationID: tt.fields.OrgID, + Name: tt.fields.Name, + OrgID: tt.fields.OrgID, } if err := m.Validate(); (err != nil) != tt.wantErr { t.Errorf("Label.Validate() error = %v, wantErr %v", err, tt.wantErr) diff --git a/testing/label_service.go b/testing/label_service.go index 96daf6ac68..c99b6adf53 100644 --- a/testing/label_service.go +++ b/testing/label_service.go @@ -12,8 +12,9 @@ import ( ) const ( - labelOneID = "41a9f7288d4e2d64" - labelTwoID = "b7c5355e1134b11c" + 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 }), @@ -206,25 +207,34 @@ func FindLabels( fields: LabelFields{ Labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + Name: "Tag1", + OrgID: MustIDBase16(orgOneID), }, { - ID: MustIDBase16(labelTwoID), - Name: "Tag2", + ID: MustIDBase16(labelTwoID), + Name: "Tag2", + OrgID: MustIDBase16(orgOneID), + }, + { + ID: MustIDBase16(labelThreeID), + Name: "Tag1", + OrgID: MustIDBase16(orgTwoID), }, }, }, args: args{ filter: influxdb.LabelFilter{ - Name: "Tag1", + Name: "Tag1", + OrgID: idPtr(MustIDBase16(orgOneID)), }, }, wants: wants{ labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + Name: "Tag1", + OrgID: MustIDBase16(orgOneID), }, }, }, @@ -345,9 +355,9 @@ func UpdateLabel( fields: LabelFields{ Labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag1", }, }, }, @@ -360,9 +370,9 @@ func UpdateLabel( wants: wants{ labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - OrganizationID: MustIDBase16(orgOneID), - Name: "NotTag1", + ID: MustIDBase16(labelOneID), + OrgID: MustIDBase16(orgOneID), + Name: "NotTag1", }, }, }, @@ -372,9 +382,9 @@ func UpdateLabel( fields: LabelFields{ Labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag1", }, }, }, @@ -389,9 +399,9 @@ func UpdateLabel( wants: wants{ labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag1", Properties: map[string]string{ "color": "fff000", }, @@ -404,9 +414,9 @@ func UpdateLabel( fields: LabelFields{ Labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag1", Properties: map[string]string{ "color": "fff000", "description": "description", @@ -425,9 +435,9 @@ func UpdateLabel( wants: wants{ labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag1", Properties: map[string]string{ "color": "abc123", "description": "description", @@ -441,9 +451,9 @@ func UpdateLabel( fields: LabelFields{ Labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag1", Properties: map[string]string{ "color": "fff000", "description": "description", @@ -462,9 +472,9 @@ func UpdateLabel( wants: wants{ labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag1", Properties: map[string]string{ "color": "fff000", }, @@ -538,14 +548,14 @@ func DeleteLabel( fields: LabelFields{ Labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag1", }, { - ID: MustIDBase16(labelTwoID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag2", + ID: MustIDBase16(labelTwoID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag2", }, }, }, @@ -555,9 +565,9 @@ func DeleteLabel( wants: wants{ labels: []*influxdb.Label{ { - ID: MustIDBase16(labelTwoID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag2", + ID: MustIDBase16(labelTwoID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag2", }, }, }, @@ -567,9 +577,9 @@ func DeleteLabel( fields: LabelFields{ Labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag1", }, }, }, @@ -579,9 +589,9 @@ func DeleteLabel( wants: wants{ labels: []*influxdb.Label{ { - ID: MustIDBase16(labelOneID), - OrganizationID: MustIDBase16(orgOneID), - Name: "Tag1", + ID: MustIDBase16(labelOneID), + OrgID: MustIDBase16(orgOneID), + Name: "Tag1", }, }, err: &influxdb.Error{