diff --git a/authorizer/label.go b/authorizer/label.go index 658cde5ee40..52912f7ae98 100644 --- a/authorizer/label.go +++ b/authorizer/label.go @@ -133,6 +133,10 @@ func (s *LabelService) FindLabels(ctx context.Context, filter influxdb.LabelFilt // FindResourceLabels retrieves all labels belonging to the filtering resource if the authorizer on context has read access to it. // Then it filters the list down to only the labels that are authorized. func (s *LabelService) FindResourceLabels(ctx context.Context, filter influxdb.LabelMappingFilter) ([]*influxdb.Label, error) { + if err := filter.ResourceType.Valid(); err != nil { + return nil, err + } + if s.orgSvc == nil { return nil, errors.New("failed to find orgSvc") } @@ -167,9 +171,9 @@ func (s *LabelService) FindResourceLabels(ctx context.Context, filter influxdb.L return labels, nil } -// CreateLabel checks to see if the authorizer on context has read access to the new label's org. +// CreateLabel checks to see if the authorizer on context has write access to the new label's org. func (s *LabelService) CreateLabel(ctx context.Context, l *influxdb.Label) error { - if err := authorizeReadOrg(ctx, l.OrgID); err != nil { + if err := authorizeWriteOrg(ctx, l.OrgID); err != nil { return err } diff --git a/authorizer/label_test.go b/authorizer/label_test.go index 8cb8cc6753e..9ee0782af89 100644 --- a/authorizer/label_test.go +++ b/authorizer/label_test.go @@ -509,7 +509,7 @@ func TestLabelService_CreateLabel(t *testing.T) { wants wants }{ { - name: "authorized to create label", + name: "unauthorized to create label with read only permission", fields: fields{ LabelService: &mock.LabelService{ CreateLabelFn: func(ctx context.Context, l *influxdb.Label) error { @@ -527,11 +527,14 @@ func TestLabelService_CreateLabel(t *testing.T) { }, }, wants: wants{ - err: nil, + err: &influxdb.Error{ + Msg: "write:orgs/020f755c3c083000 is unauthorized", + Code: influxdb.EUnauthorized, + }, }, }, { - name: "unauthorized to create label", + name: "unauthorized to create label with incomplete write permission", fields: fields{ LabelService: &mock.LabelService{ CreateLabelFn: func(ctx context.Context, b *influxdb.Label) error { @@ -541,7 +544,7 @@ func TestLabelService_CreateLabel(t *testing.T) { }, args: args{ permission: influxdb.Permission{ - Action: "read", + Action: "write", Resource: influxdb.Resource{ Type: influxdb.LabelsResourceType, }, @@ -549,11 +552,34 @@ func TestLabelService_CreateLabel(t *testing.T) { }, wants: wants{ err: &influxdb.Error{ - Msg: "read:orgs/020f755c3c083000 is unauthorized", + Msg: "write:orgs/020f755c3c083000 is unauthorized", Code: influxdb.EUnauthorized, }, }, }, + + { + name: "authorized to create label", + fields: fields{ + LabelService: &mock.LabelService{ + CreateLabelFn: func(ctx context.Context, l *influxdb.Label) error { + return nil + }, + }, + }, + args: args{ + permission: influxdb.Permission{ + Action: "write", + Resource: influxdb.Resource{ + ID: influxdbtesting.IDPtr(orgOneInfluxID), + Type: influxdb.OrgsResourceType, + }, + }, + }, + wants: wants{ + err: nil, + }, + }, } for _, tt := range tests { diff --git a/http/api_handler.go b/http/api_handler.go index c2e9d3062be..83d8a56cc3b 100644 --- a/http/api_handler.go +++ b/http/api_handler.go @@ -127,6 +127,7 @@ func NewAPIHandler(b *APIBackend, opts ...APIHandlerOptFn) *APIHandler { bucketBackend := NewBucketBackend(b.Logger.With(zap.String("handler", "bucket")), b) bucketBackend.BucketService = authorizer.NewBucketService(b.BucketService, b.UserResourceMappingService) + bucketBackend.LabelService = authorizer.NewLabelServiceWithOrg(b.LabelService, b.OrgLookupService) h.Mount(prefixBuckets, NewBucketHandler(b.Logger, bucketBackend)) checkBackend := NewCheckBackend(b.Logger.With(zap.String("handler", "check")), b) @@ -166,6 +167,7 @@ func NewAPIHandler(b *APIBackend, opts ...APIHandlerOptFn) *APIHandler { orgBackend := NewOrgBackend(b.Logger.With(zap.String("handler", "org")), b) orgBackend.OrganizationService = authorizer.NewOrgService(b.OrganizationService) orgBackend.SecretService = authorizer.NewSecretService(b.SecretService) + orgBackend.LabelService = authorizer.NewLabelServiceWithOrg(b.LabelService, b.OrgLookupService) h.Mount(prefixOrganizations, NewOrgHandler(b.Logger, orgBackend)) scraperBackend := NewScraperBackend(b.Logger.With(zap.String("handler", "scraper")), b) diff --git a/http/bucket_service.go b/http/bucket_service.go index b57817644f2..9f454498160 100644 --- a/http/bucket_service.go +++ b/http/bucket_service.go @@ -308,7 +308,7 @@ type bucketsResponse struct { func newBucketsResponse(ctx context.Context, opts influxdb.FindOptions, f influxdb.BucketFilter, bs []*influxdb.Bucket, labelService influxdb.LabelService) *bucketsResponse { rs := make([]*bucketResponse, 0, len(bs)) for _, b := range bs { - labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: b.ID}) + labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: b.ID, ResourceType: influxdb.BucketsResourceType}) rs = append(rs, NewBucketResponse(b, labels)) } return &bucketsResponse{ @@ -405,7 +405,7 @@ func (h *BucketHandler) handleGetBucket(w http.ResponseWriter, r *http.Request) return } - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: b.ID}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: b.ID, ResourceType: influxdb.BucketsResourceType}) if err != nil { h.api.Err(w, err) return @@ -600,7 +600,8 @@ func (h *BucketHandler) handlePatchBucket(w http.ResponseWriter, r *http.Request // TODO: should move to service to encapsulate labels and what any other dependencies. Future // work for service definition labels, err := h.LabelService.FindResourceLabels(r.Context(), influxdb.LabelMappingFilter{ - ResourceID: b.ID, + ResourceID: b.ID, + ResourceType: influxdb.BucketsResourceType, }) if err != nil { h.api.Err(w, err) diff --git a/http/check_service.go b/http/check_service.go index 17155d398a3..19c0bbfaa42 100644 --- a/http/check_service.go +++ b/http/check_service.go @@ -237,7 +237,7 @@ func (h *CheckHandler) newChecksResponse(ctx context.Context, chks []influxdb.Ch Links: newPagingLinks(prefixChecks, opts, f, len(chks)), } for _, chk := range chks { - labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: chk.GetID()}) + labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: chk.GetID(), ResourceType: influxdb.ChecksResourceType}) cr, err := h.newCheckResponse(ctx, chk, labels) if err != nil { h.log.Info("Failed to retrieve task associated with check", zap.String("checkID", chk.GetID().String())) @@ -334,7 +334,7 @@ func (h *CheckHandler) handleGetCheck(w http.ResponseWriter, r *http.Request) { } h.log.Debug("Check retrieved", zap.String("check", fmt.Sprint(chk))) - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: chk.GetID()}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: chk.GetID(), ResourceType: influxdb.ChecksResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -613,7 +613,7 @@ func (h *CheckHandler) handlePutCheck(w http.ResponseWriter, r *http.Request) { return } - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: c.GetID()}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: c.GetID(), ResourceType: influxdb.ChecksResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -648,7 +648,7 @@ func (h *CheckHandler) handlePatchCheck(w http.ResponseWriter, r *http.Request) return } - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: chk.GetID()}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: chk.GetID(), ResourceType: influxdb.ChecksResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return diff --git a/http/dashboard_service.go b/http/dashboard_service.go index 859b63c0a1f..b4906e8b697 100644 --- a/http/dashboard_service.go +++ b/http/dashboard_service.go @@ -8,7 +8,7 @@ import ( "path" "github.com/influxdata/httprouter" - platform "github.com/influxdata/influxdb" + "github.com/influxdata/influxdb" "github.com/influxdata/influxdb/pkg/httpc" "go.uber.org/zap" ) @@ -16,14 +16,14 @@ import ( // DashboardBackend is all services and associated parameters required to construct // the DashboardHandler. type DashboardBackend struct { - platform.HTTPErrorHandler + influxdb.HTTPErrorHandler log *zap.Logger - DashboardService platform.DashboardService - DashboardOperationLogService platform.DashboardOperationLogService - UserResourceMappingService platform.UserResourceMappingService - LabelService platform.LabelService - UserService platform.UserService + DashboardService influxdb.DashboardService + DashboardOperationLogService influxdb.DashboardOperationLogService + UserResourceMappingService influxdb.UserResourceMappingService + LabelService influxdb.LabelService + UserService influxdb.UserService } // NewDashboardBackend creates a backend used by the dashboard handler. @@ -44,14 +44,14 @@ func NewDashboardBackend(log *zap.Logger, b *APIBackend) *DashboardBackend { type DashboardHandler struct { *httprouter.Router - platform.HTTPErrorHandler + influxdb.HTTPErrorHandler log *zap.Logger - DashboardService platform.DashboardService - DashboardOperationLogService platform.DashboardOperationLogService - UserResourceMappingService platform.UserResourceMappingService - LabelService platform.LabelService - UserService platform.UserService + DashboardService influxdb.DashboardService + DashboardOperationLogService influxdb.DashboardOperationLogService + UserResourceMappingService influxdb.UserResourceMappingService + LabelService influxdb.LabelService + UserService influxdb.UserService } const ( @@ -101,8 +101,8 @@ func NewDashboardHandler(log *zap.Logger, b *DashboardBackend) *DashboardHandler memberBackend := MemberBackend{ HTTPErrorHandler: b.HTTPErrorHandler, log: b.log.With(zap.String("handler", "member")), - ResourceType: platform.DashboardsResourceType, - UserType: platform.Member, + ResourceType: influxdb.DashboardsResourceType, + UserType: influxdb.Member, UserResourceMappingService: b.UserResourceMappingService, UserService: b.UserService, } @@ -113,8 +113,8 @@ func NewDashboardHandler(log *zap.Logger, b *DashboardBackend) *DashboardHandler ownerBackend := MemberBackend{ HTTPErrorHandler: b.HTTPErrorHandler, log: b.log.With(zap.String("handler", "member")), - ResourceType: platform.DashboardsResourceType, - UserType: platform.Owner, + ResourceType: influxdb.DashboardsResourceType, + UserType: influxdb.Owner, UserResourceMappingService: b.UserResourceMappingService, UserService: b.UserService, } @@ -126,7 +126,7 @@ func NewDashboardHandler(log *zap.Logger, b *DashboardBackend) *DashboardHandler HTTPErrorHandler: b.HTTPErrorHandler, log: b.log.With(zap.String("handler", "label")), LabelService: b.LabelService, - ResourceType: platform.DashboardsResourceType, + ResourceType: influxdb.DashboardsResourceType, } h.HandlerFunc("GET", dashboardsIDLabelsPath, newGetLabelsHandler(labelBackend)) h.HandlerFunc("POST", dashboardsIDLabelsPath, newPostLabelHandler(labelBackend)) @@ -146,25 +146,25 @@ type dashboardLinks struct { } type dashboardResponse struct { - ID platform.ID `json:"id,omitempty"` - OrganizationID platform.ID `json:"orgID,omitempty"` + ID influxdb.ID `json:"id,omitempty"` + OrganizationID influxdb.ID `json:"orgID,omitempty"` Name string `json:"name"` Description string `json:"description"` - Meta platform.DashboardMeta `json:"meta"` + Meta influxdb.DashboardMeta `json:"meta"` Cells []dashboardCellResponse `json:"cells"` - Labels []platform.Label `json:"labels"` + Labels []influxdb.Label `json:"labels"` Links dashboardLinks `json:"links"` } -func (d dashboardResponse) toPlatform() *platform.Dashboard { - var cells []*platform.Cell +func (d dashboardResponse) toinfluxdb() *influxdb.Dashboard { + var cells []*influxdb.Cell if len(d.Cells) > 0 { - cells = make([]*platform.Cell, len(d.Cells)) + cells = make([]*influxdb.Cell, len(d.Cells)) } for i := range d.Cells { - cells[i] = d.Cells[i].toPlatform() + cells[i] = d.Cells[i].toinfluxdb() } - return &platform.Dashboard{ + return &influxdb.Dashboard{ ID: d.ID, OrganizationID: d.OrganizationID, Name: d.Name, @@ -174,7 +174,7 @@ func (d dashboardResponse) toPlatform() *platform.Dashboard { } } -func newDashboardResponse(d *platform.Dashboard, labels []*platform.Label) dashboardResponse { +func newDashboardResponse(d *influxdb.Dashboard, labels []*influxdb.Label) dashboardResponse { res := dashboardResponse{ Links: dashboardLinks{ Self: fmt.Sprintf("/api/v2/dashboards/%s", d.ID), @@ -190,7 +190,7 @@ func newDashboardResponse(d *platform.Dashboard, labels []*platform.Label) dashb Name: d.Name, Description: d.Description, Meta: d.Meta, - Labels: []platform.Label{}, + Labels: []influxdb.Label{}, Cells: []dashboardCellResponse{}, } @@ -206,15 +206,15 @@ func newDashboardResponse(d *platform.Dashboard, labels []*platform.Label) dashb } type dashboardCellResponse struct { - platform.Cell - Properties platform.ViewProperties `json:"-"` + influxdb.Cell + Properties influxdb.ViewProperties `json:"-"` Name string `json:"name,omitempty"` Links map[string]string `json:"links"` } func (d *dashboardCellResponse) MarshalJSON() ([]byte, error) { r := struct { - platform.Cell + influxdb.Cell Properties json.RawMessage `json:"properties,omitempty"` Name string `json:"name,omitempty"` Links map[string]string `json:"links"` @@ -224,7 +224,7 @@ func (d *dashboardCellResponse) MarshalJSON() ([]byte, error) { } if d.Cell.View != nil { - b, err := platform.MarshalViewPropertiesJSON(d.Cell.View.Properties) + b, err := influxdb.MarshalViewPropertiesJSON(d.Cell.View.Properties) if err != nil { return nil, err } @@ -235,11 +235,11 @@ func (d *dashboardCellResponse) MarshalJSON() ([]byte, error) { return json.Marshal(r) } -func (c dashboardCellResponse) toPlatform() *platform.Cell { +func (c dashboardCellResponse) toinfluxdb() *influxdb.Cell { return &c.Cell } -func newDashboardCellResponse(dashboardID platform.ID, c *platform.Cell) dashboardCellResponse { +func newDashboardCellResponse(dashboardID influxdb.ID, c *influxdb.Cell) dashboardCellResponse { resp := dashboardCellResponse{ Cell: *c, Links: map[string]string{ @@ -260,7 +260,7 @@ type dashboardCellsResponse struct { Links map[string]string `json:"links"` } -func newDashboardCellsResponse(dashboardID platform.ID, cs []*platform.Cell) dashboardCellsResponse { +func newDashboardCellsResponse(dashboardID influxdb.ID, cs []*influxdb.Cell) dashboardCellsResponse { res := dashboardCellsResponse{ Cells: []dashboardCellResponse{}, Links: map[string]string{ @@ -280,18 +280,18 @@ type viewLinks struct { } type dashboardCellViewResponse struct { - platform.View + influxdb.View Links viewLinks `json:"links"` } func (r dashboardCellViewResponse) MarshalJSON() ([]byte, error) { - props, err := platform.MarshalViewPropertiesJSON(r.Properties) + props, err := influxdb.MarshalViewPropertiesJSON(r.Properties) if err != nil { return nil, err } return json.Marshal(struct { - platform.ViewContents + influxdb.ViewContents Links viewLinks `json:"links"` Properties json.RawMessage `json:"properties"` }{ @@ -301,7 +301,7 @@ func (r dashboardCellViewResponse) MarshalJSON() ([]byte, error) { }) } -func newDashboardCellViewResponse(dashID, cellID platform.ID, v *platform.View) dashboardCellViewResponse { +func newDashboardCellViewResponse(dashID, cellID influxdb.ID, v *influxdb.View) dashboardCellViewResponse { return dashboardCellViewResponse{ Links: viewLinks{ Self: fmt.Sprintf("/api/v2/dashboards/%s/cells/%s", dashID, cellID), @@ -315,7 +315,7 @@ type operationLogResponse struct { Logs []*operationLogEntryResponse `json:"logs"` } -func newDashboardLogResponse(id platform.ID, es []*platform.OperationLogEntry) *operationLogResponse { +func newDashboardLogResponse(id influxdb.ID, es []*influxdb.OperationLogEntry) *operationLogResponse { logs := make([]*operationLogEntryResponse, 0, len(es)) for _, e := range es { logs = append(logs, newOperationLogEntryResponse(e)) @@ -330,10 +330,10 @@ func newDashboardLogResponse(id platform.ID, es []*platform.OperationLogEntry) * type operationLogEntryResponse struct { Links map[string]string `json:"links"` - *platform.OperationLogEntry + *influxdb.OperationLogEntry } -func newOperationLogEntryResponse(e *platform.OperationLogEntry) *operationLogEntryResponse { +func newOperationLogEntryResponse(e *influxdb.OperationLogEntry) *operationLogEntryResponse { links := map[string]string{} if e.UserID.Valid() { links["user"] = fmt.Sprintf("/api/v2/users/%s", e.UserID) @@ -354,16 +354,16 @@ func (h *DashboardHandler) handleGetDashboards(w http.ResponseWriter, r *http.Re } if req.ownerID != nil { - filter := platform.UserResourceMappingFilter{ + filter := influxdb.UserResourceMappingFilter{ UserID: *req.ownerID, - UserType: platform.Owner, - ResourceType: platform.DashboardsResourceType, + UserType: influxdb.Owner, + ResourceType: influxdb.DashboardsResourceType, } mappings, _, err := h.UserResourceMappingService.FindUserResourceMappings(ctx, filter) if err != nil { - h.HandleHTTPError(ctx, &platform.Error{ - Code: platform.EInternal, + h.HandleHTTPError(ctx, &influxdb.Error{ + Code: influxdb.EInternal, Msg: "Error loading dashboard owners", Err: err, }, w) @@ -390,9 +390,9 @@ func (h *DashboardHandler) handleGetDashboards(w http.ResponseWriter, r *http.Re } type getDashboardsRequest struct { - filter platform.DashboardFilter - opts platform.FindOptions - ownerID *platform.ID + filter influxdb.DashboardFilter + opts influxdb.FindOptions + ownerID *influxdb.ID } func decodeGetDashboardsRequest(ctx context.Context, r *http.Request) (*getDashboardsRequest, error) { @@ -405,7 +405,7 @@ func decodeGetDashboardsRequest(ctx context.Context, r *http.Request) (*getDashb } req.opts = *opts - initialID := platform.InvalidID() + initialID := influxdb.InvalidID() if ids, ok := qp["id"]; ok { for _, id := range ids { i := initialID @@ -420,7 +420,7 @@ func decodeGetDashboardsRequest(ctx context.Context, r *http.Request) (*getDashb return nil, err } } else if orgID := qp.Get("orgID"); orgID != "" { - id := platform.InvalidID() + id := influxdb.InvalidID() if err := id.DecodeFromString(orgID); err != nil { return nil, err } @@ -433,19 +433,19 @@ func decodeGetDashboardsRequest(ctx context.Context, r *http.Request) (*getDashb } type getDashboardsResponse struct { - Links *platform.PagingLinks `json:"links"` + Links *influxdb.PagingLinks `json:"links"` Dashboards []dashboardResponse `json:"dashboards"` } -func (d getDashboardsResponse) toPlatform() []*platform.Dashboard { - res := make([]*platform.Dashboard, len(d.Dashboards)) +func (d getDashboardsResponse) toinfluxdb() []*influxdb.Dashboard { + res := make([]*influxdb.Dashboard, len(d.Dashboards)) for i := range d.Dashboards { - res[i] = d.Dashboards[i].toPlatform() + res[i] = d.Dashboards[i].toinfluxdb() } return res } -func newGetDashboardsResponse(ctx context.Context, dashboards []*platform.Dashboard, filter platform.DashboardFilter, opts platform.FindOptions, labelService platform.LabelService) getDashboardsResponse { +func newGetDashboardsResponse(ctx context.Context, dashboards []*influxdb.Dashboard, filter influxdb.DashboardFilter, opts influxdb.FindOptions, labelService influxdb.LabelService) getDashboardsResponse { res := getDashboardsResponse{ Links: newPagingLinks(prefixDashboards, opts, filter, len(dashboards)), Dashboards: make([]dashboardResponse, 0, len(dashboards)), @@ -453,7 +453,7 @@ func newGetDashboardsResponse(ctx context.Context, dashboards []*platform.Dashbo for _, dashboard := range dashboards { if dashboard != nil { - labels, _ := labelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: dashboard.ID}) + labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: dashboard.ID, ResourceType: influxdb.DashboardsResourceType}) res.Dashboards = append(res.Dashboards, newDashboardResponse(dashboard, labels)) } } @@ -464,7 +464,7 @@ func newGetDashboardsResponse(ctx context.Context, dashboards []*platform.Dashbo // handlePostDashboard creates a new dashboard. func (h *DashboardHandler) handlePostDashboard(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - var d platform.Dashboard + var d influxdb.Dashboard if err := json.NewDecoder(r.Body).Decode(&d); err != nil { h.HandleHTTPError(ctx, err, w) return @@ -475,7 +475,7 @@ func (h *DashboardHandler) handlePostDashboard(w http.ResponseWriter, r *http.Re return } - if err := encodeResponse(ctx, w, http.StatusCreated, newDashboardResponse(&d, []*platform.Label{})); err != nil { + if err := encodeResponse(ctx, w, http.StatusCreated, newDashboardResponse(&d, []*influxdb.Label{})); err != nil { logEncodingError(h.log, r, err) return } @@ -510,7 +510,7 @@ func (h *DashboardHandler) handleGetDashboard(w http.ResponseWriter, r *http.Req } } - labels, err := h.LabelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: dashboard.ID}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: dashboard.ID, ResourceType: influxdb.DashboardsResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -525,20 +525,20 @@ func (h *DashboardHandler) handleGetDashboard(w http.ResponseWriter, r *http.Req } type getDashboardRequest struct { - DashboardID platform.ID + DashboardID influxdb.ID } func decodeGetDashboardRequest(ctx context.Context, r *http.Request) (*getDashboardRequest, 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 } @@ -572,21 +572,21 @@ func (h *DashboardHandler) handleGetDashboardLog(w http.ResponseWriter, r *http. } type getDashboardLogRequest struct { - DashboardID platform.ID - opts platform.FindOptions + DashboardID influxdb.ID + opts influxdb.FindOptions } func decodeGetDashboardLogRequest(ctx context.Context, r *http.Request) (*getDashboardLogRequest, 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 } @@ -622,20 +622,20 @@ func (h *DashboardHandler) handleDeleteDashboard(w http.ResponseWriter, r *http. } type deleteDashboardRequest struct { - DashboardID platform.ID + DashboardID influxdb.ID } func decodeDeleteDashboardRequest(ctx context.Context, r *http.Request) (*deleteDashboardRequest, 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 } @@ -659,7 +659,7 @@ func (h *DashboardHandler) handlePatchDashboard(w http.ResponseWriter, r *http.R return } - labels, err := h.LabelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: dashboard.ID}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: dashboard.ID, ResourceType: influxdb.DashboardsResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -674,16 +674,16 @@ func (h *DashboardHandler) handlePatchDashboard(w http.ResponseWriter, r *http.R } type patchDashboardRequest struct { - DashboardID platform.ID - Upd platform.DashboardUpdate + DashboardID influxdb.ID + Upd influxdb.DashboardUpdate } func decodePatchDashboardRequest(ctx context.Context, r *http.Request) (*patchDashboardRequest, error) { req := &patchDashboardRequest{} - upd := platform.DashboardUpdate{} + upd := influxdb.DashboardUpdate{} if err := json.NewDecoder(r.Body).Decode(&upd); err != nil { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Err: err, } } @@ -692,12 +692,12 @@ func decodePatchDashboardRequest(ctx context.Context, r *http.Request) (*patchDa 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 } @@ -705,8 +705,8 @@ func decodePatchDashboardRequest(ctx context.Context, r *http.Request) (*patchDa req.DashboardID = i if err := req.Valid(); err != nil { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Err: err, } } @@ -717,8 +717,8 @@ func decodePatchDashboardRequest(ctx context.Context, r *http.Request) (*patchDa // Valid validates that the dashboard ID is non zero valued and update has expected values set. func (r *patchDashboardRequest) Valid() error { if !r.DashboardID.Valid() { - return &platform.Error{ - Code: platform.EInvalid, + return &influxdb.Error{ + Code: influxdb.EInvalid, Msg: "missing dashboard ID", } } @@ -730,9 +730,9 @@ func (r *patchDashboardRequest) Valid() error { } type postDashboardCellRequest struct { - dashboardID platform.ID - *platform.CellProperty - UsingView *platform.ID `json:"usingView"` + dashboardID influxdb.ID + *influxdb.CellProperty + UsingView *influxdb.ID `json:"usingView"` Name *string `json:"name"` } @@ -741,15 +741,15 @@ func decodePostDashboardCellRequest(ctx context.Context, r *http.Request) (*post 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", } } if err := json.NewDecoder(r.Body).Decode(req); err != nil { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Msg: "bad request json body", Err: err, } @@ -770,11 +770,11 @@ func (h *DashboardHandler) handlePostDashboardCell(w http.ResponseWriter, r *htt h.HandleHTTPError(ctx, err, w) return } - cell := new(platform.Cell) + cell := new(influxdb.Cell) - opts := new(platform.AddDashboardCellOptions) + opts := new(influxdb.AddDashboardCellOptions) if req.UsingView != nil || req.Name != nil { - opts.View = new(platform.View) + opts.View = new(influxdb.View) if req.UsingView != nil { // load the view opts.View, err = h.DashboardService.GetDashboardCellView(ctx, req.dashboardID, *req.UsingView) @@ -787,8 +787,8 @@ func (h *DashboardHandler) handlePostDashboardCell(w http.ResponseWriter, r *htt opts.View.Name = *req.Name } } else if req.CellProperty == nil { - h.HandleHTTPError(ctx, &platform.Error{ - Code: platform.EInvalid, + h.HandleHTTPError(ctx, &influxdb.Error{ + Code: influxdb.EInvalid, Msg: "req body is empty", }, w) return @@ -812,8 +812,8 @@ func (h *DashboardHandler) handlePostDashboardCell(w http.ResponseWriter, r *htt } type putDashboardCellRequest struct { - dashboardID platform.ID - cells []*platform.Cell + dashboardID influxdb.ID + cells []*influxdb.Cell } func decodePutDashboardCellRequest(ctx context.Context, r *http.Request) (*putDashboardCellRequest, error) { @@ -822,8 +822,8 @@ func decodePutDashboardCellRequest(ctx context.Context, r *http.Request) (*putDa 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", } } @@ -831,7 +831,7 @@ func decodePutDashboardCellRequest(ctx context.Context, r *http.Request) (*putDa return nil, err } - req.cells = []*platform.Cell{} + req.cells = []*influxdb.Cell{} if err := json.NewDecoder(r.Body).Decode(&req.cells); err != nil { return nil, err } @@ -862,8 +862,8 @@ func (h *DashboardHandler) handlePutDashboardCells(w http.ResponseWriter, r *htt } type deleteDashboardCellRequest struct { - dashboardID platform.ID - cellID platform.ID + dashboardID influxdb.ID + cellID influxdb.ID } func decodeDeleteDashboardCellRequest(ctx context.Context, r *http.Request) (*deleteDashboardCellRequest, error) { @@ -872,8 +872,8 @@ func decodeDeleteDashboardCellRequest(ctx context.Context, r *http.Request) (*de 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", } } @@ -883,8 +883,8 @@ func decodeDeleteDashboardCellRequest(ctx context.Context, r *http.Request) (*de cellID := params.ByName("cellID") if cellID == "" { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Msg: "url missing cellID", } } @@ -896,8 +896,8 @@ func decodeDeleteDashboardCellRequest(ctx context.Context, r *http.Request) (*de } type getDashboardCellViewRequest struct { - dashboardID platform.ID - cellID platform.ID + dashboardID influxdb.ID + cellID influxdb.ID } func decodeGetDashboardCellViewRequest(ctx context.Context, r *http.Request) (*getDashboardCellViewRequest, error) { @@ -906,7 +906,7 @@ func decodeGetDashboardCellViewRequest(ctx context.Context, r *http.Request) (*g params := httprouter.ParamsFromContext(ctx) id := params.ByName("id") if id == "" { - return nil, platform.NewError(platform.WithErrorMsg("url missing id"), platform.WithErrorCode(platform.EInvalid)) + return nil, influxdb.NewError(influxdb.WithErrorMsg("url missing id"), influxdb.WithErrorCode(influxdb.EInvalid)) } if err := req.dashboardID.DecodeFromString(id); err != nil { return nil, err @@ -914,7 +914,7 @@ func decodeGetDashboardCellViewRequest(ctx context.Context, r *http.Request) (*g cellID := params.ByName("cellID") if cellID == "" { - return nil, platform.NewError(platform.WithErrorMsg("url missing cellID"), platform.WithErrorCode(platform.EInvalid)) + return nil, influxdb.NewError(influxdb.WithErrorMsg("url missing cellID"), influxdb.WithErrorCode(influxdb.EInvalid)) } if err := req.cellID.DecodeFromString(cellID); err != nil { return nil, err @@ -946,9 +946,9 @@ func (h *DashboardHandler) handleGetDashboardCellView(w http.ResponseWriter, r * } type patchDashboardCellViewRequest struct { - dashboardID platform.ID - cellID platform.ID - upd platform.ViewUpdate + dashboardID influxdb.ID + cellID influxdb.ID + upd influxdb.ViewUpdate } func decodePatchDashboardCellViewRequest(ctx context.Context, r *http.Request) (*patchDashboardCellViewRequest, error) { @@ -957,7 +957,7 @@ func decodePatchDashboardCellViewRequest(ctx context.Context, r *http.Request) ( params := httprouter.ParamsFromContext(ctx) id := params.ByName("id") if id == "" { - return nil, platform.NewError(platform.WithErrorMsg("url missing id"), platform.WithErrorCode(platform.EInvalid)) + return nil, influxdb.NewError(influxdb.WithErrorMsg("url missing id"), influxdb.WithErrorCode(influxdb.EInvalid)) } if err := req.dashboardID.DecodeFromString(id); err != nil { return nil, err @@ -965,7 +965,7 @@ func decodePatchDashboardCellViewRequest(ctx context.Context, r *http.Request) ( cellID := params.ByName("cellID") if cellID == "" { - return nil, platform.NewError(platform.WithErrorMsg("url missing cellID"), platform.WithErrorCode(platform.EInvalid)) + return nil, influxdb.NewError(influxdb.WithErrorMsg("url missing cellID"), influxdb.WithErrorCode(influxdb.EInvalid)) } if err := req.cellID.DecodeFromString(cellID); err != nil { return nil, err @@ -1017,9 +1017,9 @@ func (h *DashboardHandler) handleDeleteDashboardCell(w http.ResponseWriter, r *h } type patchDashboardCellRequest struct { - dashboardID platform.ID - cellID platform.ID - upd platform.CellUpdate + dashboardID influxdb.ID + cellID influxdb.ID + upd influxdb.CellUpdate } func decodePatchDashboardCellRequest(ctx context.Context, r *http.Request) (*patchDashboardCellRequest, error) { @@ -1028,8 +1028,8 @@ func decodePatchDashboardCellRequest(ctx context.Context, r *http.Request) (*pat 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", } } @@ -1039,8 +1039,8 @@ func decodePatchDashboardCellRequest(ctx context.Context, r *http.Request) (*pat cellID := params.ByName("cellID") if cellID == "" { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Msg: "cannot provide empty cell id", } } @@ -1049,8 +1049,8 @@ func decodePatchDashboardCellRequest(ctx context.Context, r *http.Request) (*pat } if err := json.NewDecoder(r.Body).Decode(&req.upd); err != nil { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Err: err, } } @@ -1090,7 +1090,7 @@ type DashboardService struct { } // FindDashboardByID returns a single dashboard by ID. -func (s *DashboardService) FindDashboardByID(ctx context.Context, id platform.ID) (*platform.Dashboard, error) { +func (s *DashboardService) FindDashboardByID(ctx context.Context, id influxdb.ID) (*influxdb.Dashboard, error) { var dr dashboardResponse err := s.Client. Get(prefixDashboards, id.String()). @@ -1100,12 +1100,12 @@ func (s *DashboardService) FindDashboardByID(ctx context.Context, id platform.ID if err != nil { return nil, err } - return dr.toPlatform(), nil + return dr.toinfluxdb(), nil } // FindDashboards returns a list of dashboards that match filter and the total count of matching dashboards. // Additional options provide pagination & sorting. -func (s *DashboardService) FindDashboards(ctx context.Context, filter platform.DashboardFilter, opts platform.FindOptions) ([]*platform.Dashboard, int, error) { +func (s *DashboardService) FindDashboards(ctx context.Context, filter influxdb.DashboardFilter, opts influxdb.FindOptions) ([]*influxdb.Dashboard, int, error) { queryPairs := findOptionParams(opts) for _, id := range filter.IDs { queryPairs = append(queryPairs, [2]string{"id", id.String()}) @@ -1127,12 +1127,12 @@ func (s *DashboardService) FindDashboards(ctx context.Context, filter platform.D return nil, 0, err } - dashboards := dr.toPlatform() + dashboards := dr.toinfluxdb() return dashboards, len(dashboards), nil } // CreateDashboard creates a new dashboard and sets b.ID with the new identifier. -func (s *DashboardService) CreateDashboard(ctx context.Context, d *platform.Dashboard) error { +func (s *DashboardService) CreateDashboard(ctx context.Context, d *influxdb.Dashboard) error { return s.Client. PostJSON(d, prefixDashboards). DecodeJSON(d). @@ -1141,8 +1141,8 @@ func (s *DashboardService) CreateDashboard(ctx context.Context, d *platform.Dash // UpdateDashboard updates a single dashboard with changeset. // Returns the new dashboard state after update. -func (s *DashboardService) UpdateDashboard(ctx context.Context, id platform.ID, upd platform.DashboardUpdate) (*platform.Dashboard, error) { - var d platform.Dashboard +func (s *DashboardService) UpdateDashboard(ctx context.Context, id influxdb.ID, upd influxdb.DashboardUpdate) (*influxdb.Dashboard, error) { + var d influxdb.Dashboard err := s.Client. PatchJSON(upd, prefixDashboards, id.String()). DecodeJSON(&d). @@ -1160,14 +1160,14 @@ func (s *DashboardService) UpdateDashboard(ctx context.Context, id platform.ID, } // DeleteDashboard removes a dashboard by ID. -func (s *DashboardService) DeleteDashboard(ctx context.Context, id platform.ID) error { +func (s *DashboardService) DeleteDashboard(ctx context.Context, id influxdb.ID) error { return s.Client. Delete(dashboardIDPath(id)). Do(ctx) } // AddDashboardCell adds a cell to a dashboard. -func (s *DashboardService) AddDashboardCell(ctx context.Context, id platform.ID, c *platform.Cell, opts platform.AddDashboardCellOptions) error { +func (s *DashboardService) AddDashboardCell(ctx context.Context, id influxdb.ID, c *influxdb.Cell, opts influxdb.AddDashboardCellOptions) error { return s.Client. PostJSON(c, cellPath(id)). DecodeJSON(c). @@ -1175,21 +1175,21 @@ func (s *DashboardService) AddDashboardCell(ctx context.Context, id platform.ID, } // RemoveDashboardCell removes a dashboard. -func (s *DashboardService) RemoveDashboardCell(ctx context.Context, dashboardID, cellID platform.ID) error { +func (s *DashboardService) RemoveDashboardCell(ctx context.Context, dashboardID, cellID influxdb.ID) error { return s.Client. Delete(dashboardCellIDPath(dashboardID, cellID)). Do(ctx) } // UpdateDashboardCell replaces the dashboard cell with the provided ID. -func (s *DashboardService) UpdateDashboardCell(ctx context.Context, dashboardID, cellID platform.ID, upd platform.CellUpdate) (*platform.Cell, error) { +func (s *DashboardService) UpdateDashboardCell(ctx context.Context, dashboardID, cellID influxdb.ID, upd influxdb.CellUpdate) (*influxdb.Cell, error) { if err := upd.Valid(); err != nil { - return nil, &platform.Error{ + return nil, &influxdb.Error{ Err: err, } } - var c platform.Cell + var c influxdb.Cell err := s.Client. PatchJSON(upd, dashboardCellIDPath(dashboardID, cellID)). DecodeJSON(&c). @@ -1202,7 +1202,7 @@ func (s *DashboardService) UpdateDashboardCell(ctx context.Context, dashboardID, } // GetDashboardCellView retrieves the view for a dashboard cell. -func (s *DashboardService) GetDashboardCellView(ctx context.Context, dashboardID, cellID platform.ID) (*platform.View, error) { +func (s *DashboardService) GetDashboardCellView(ctx context.Context, dashboardID, cellID influxdb.ID) (*influxdb.View, error) { var dcv dashboardCellViewResponse err := s.Client. Get(cellViewPath(dashboardID, cellID)). @@ -1216,7 +1216,7 @@ func (s *DashboardService) GetDashboardCellView(ctx context.Context, dashboardID } // UpdateDashboardCellView updates the view for a dashboard cell. -func (s *DashboardService) UpdateDashboardCellView(ctx context.Context, dashboardID, cellID platform.ID, upd platform.ViewUpdate) (*platform.View, error) { +func (s *DashboardService) UpdateDashboardCellView(ctx context.Context, dashboardID, cellID influxdb.ID, upd influxdb.ViewUpdate) (*influxdb.View, error) { var dcv dashboardCellViewResponse err := s.Client. PatchJSON(upd, cellViewPath(dashboardID, cellID)). @@ -1229,7 +1229,7 @@ func (s *DashboardService) UpdateDashboardCellView(ctx context.Context, dashboar } // ReplaceDashboardCells replaces all cells in a dashboard -func (s *DashboardService) ReplaceDashboardCells(ctx context.Context, id platform.ID, cs []*platform.Cell) error { +func (s *DashboardService) ReplaceDashboardCells(ctx context.Context, id influxdb.ID, cs []*influxdb.Cell) error { return s.Client. PutJSON(cs, cellPath(id)). // TODO: previous implementation did not do anything with the response except validate it is valid json. @@ -1238,18 +1238,18 @@ func (s *DashboardService) ReplaceDashboardCells(ctx context.Context, id platfor Do(ctx) } -func dashboardIDPath(id platform.ID) string { +func dashboardIDPath(id influxdb.ID) string { return path.Join(prefixDashboards, id.String()) } -func cellPath(id platform.ID) string { +func cellPath(id influxdb.ID) string { return path.Join(dashboardIDPath(id), "cells") } -func cellViewPath(dashboardID, cellID platform.ID) string { +func cellViewPath(dashboardID, cellID influxdb.ID) string { return path.Join(dashboardCellIDPath(dashboardID, cellID), "view") } -func dashboardCellIDPath(id platform.ID, cellID platform.ID) string { +func dashboardCellIDPath(id influxdb.ID, cellID influxdb.ID) string { return path.Join(cellPath(id), cellID.String()) } diff --git a/http/notification_endpoint.go b/http/notification_endpoint.go index dfd729c3432..a33e70fe759 100644 --- a/http/notification_endpoint.go +++ b/http/notification_endpoint.go @@ -196,7 +196,7 @@ func newNotificationEndpointsResponse(ctx context.Context, edps []influxdb.Notif Links: newPagingLinks(prefixNotificationEndpoints, opts, f, len(edps)), } for i, edp := range edps { - labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: edp.GetID()}) + labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: edp.GetID(), ResourceType: influxdb.NotificationEndpointResourceType}) resp.NotificationEndpoints[i] = newNotificationEndpointResponse(edp, labels) } return resp @@ -253,7 +253,7 @@ func (h *NotificationEndpointHandler) handleGetNotificationEndpoint(w http.Respo } h.log.Debug("NotificationEndpoint retrieved", zap.String("notificationEndpoint", fmt.Sprint(edp))) - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: edp.GetID()}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: edp.GetID(), ResourceType: influxdb.NotificationEndpointResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -478,7 +478,7 @@ func (h *NotificationEndpointHandler) handlePutNotificationEndpoint(w http.Respo return } - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: edp.GetID()}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: edp.GetID(), ResourceType: influxdb.NotificationEndpointResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -507,7 +507,7 @@ func (h *NotificationEndpointHandler) handlePatchNotificationEndpoint(w http.Res return } - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: edp.GetID()}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: edp.GetID(), ResourceType: influxdb.NotificationEndpointResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return diff --git a/http/notification_rule.go b/http/notification_rule.go index 182c690a2c8..d01a1191c99 100644 --- a/http/notification_rule.go +++ b/http/notification_rule.go @@ -233,7 +233,7 @@ func (h *NotificationRuleHandler) newNotificationRulesResponse(ctx context.Conte Links: newPagingLinks(prefixNotificationRules, opts, f, len(nrs)), } for _, nr := range nrs { - labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: nr.GetID()}) + labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: nr.GetID(), ResourceType: influxdb.NotificationRuleResourceType}) res, err := h.newNotificationRuleResponse(ctx, nr, labels) if err != nil { continue @@ -333,7 +333,7 @@ func (h *NotificationRuleHandler) handleGetNotificationRule(w http.ResponseWrite } h.log.Debug("Notification rule retrieved", zap.String("notificationRule", fmt.Sprint(nr))) - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: nr.GetID()}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: nr.GetID(), ResourceType: influxdb.NotificationRuleResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -634,7 +634,7 @@ func (h *NotificationRuleHandler) handlePutNotificationRule(w http.ResponseWrite return } - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: nr.GetID()}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: nr.GetID(), ResourceType: influxdb.NotificationRuleResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -669,7 +669,7 @@ func (h *NotificationRuleHandler) handlePatchNotificationRule(w http.ResponseWri return } - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: nr.GetID()}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: nr.GetID(), ResourceType: influxdb.NotificationRuleResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return diff --git a/http/task_service.go b/http/task_service.go index fd7d3068849..16b8420ca90 100644 --- a/http/task_service.go +++ b/http/task_service.go @@ -319,7 +319,7 @@ func newTasksResponse(ctx context.Context, ts []*influxdb.Task, f influxdb.TaskF } for i := range ts { - labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: ts[i].ID}) + labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: ts[i].ID, ResourceType: influxdb.TasksResourceType}) rs.Tasks[i] = newTaskResponse(*ts[i], labels) } return rs @@ -640,7 +640,7 @@ func (h *TaskHandler) handleGetTask(w http.ResponseWriter, r *http.Request) { return } - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: task.ID}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: task.ID, ResourceType: influxdb.TasksResourceType}) if err != nil { err = &influxdb.Error{ Err: err, @@ -707,7 +707,7 @@ func (h *TaskHandler) handleUpdateTask(w http.ResponseWriter, r *http.Request) { return } - labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: task.ID}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: task.ID, ResourceType: influxdb.TasksResourceType}) if err != nil { err = &influxdb.Error{ Err: err, diff --git a/http/telegraf.go b/http/telegraf.go index 52deb4d981d..fe92bd08629 100644 --- a/http/telegraf.go +++ b/http/telegraf.go @@ -9,6 +9,7 @@ import ( "github.com/golang/gddo/httputil" "github.com/influxdata/httprouter" + "github.com/influxdata/influxdb" platform "github.com/influxdata/influxdb" pctx "github.com/influxdata/influxdb/context" "github.com/influxdata/influxdb/pkg/httpc" @@ -192,7 +193,7 @@ func newTelegrafResponses(ctx context.Context, tcs []*platform.TelegrafConfig, l TelegrafConfigs: make([]*telegrafResponse, len(tcs)), } for i, c := range tcs { - labels, _ := labelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: c.ID}) + labels, _ := labelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: c.ID, ResourceType: influxdb.TelegrafsResourceType}) resp.TelegrafConfigs[i] = newTelegrafResponse(c, labels) } return resp @@ -259,7 +260,7 @@ func (h *TelegrafHandler) handleGetTelegraf(w http.ResponseWriter, r *http.Reque w.WriteHeader(http.StatusOK) w.Write([]byte(tc.Config)) case "application/json": - labels, err := h.LabelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: tc.ID}) + labels, err := h.LabelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: tc.ID, ResourceType: influxdb.TelegrafsResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -373,7 +374,7 @@ func (h *TelegrafHandler) handlePutTelegraf(w http.ResponseWriter, r *http.Reque return } - labels, err := h.LabelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: tc.ID}) + labels, err := h.LabelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: tc.ID, ResourceType: influxdb.TelegrafsResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return diff --git a/http/variable_service.go b/http/variable_service.go index 66ba54ab1b2..93c1cf6b64f 100644 --- a/http/variable_service.go +++ b/http/variable_service.go @@ -7,7 +7,7 @@ import ( "net/http" "github.com/influxdata/httprouter" - platform "github.com/influxdata/influxdb" + "github.com/influxdata/influxdb" "github.com/influxdata/influxdb/pkg/httpc" "go.uber.org/zap" ) @@ -19,10 +19,10 @@ const ( // VariableBackend is all services and associated parameters required to construct // the VariableHandler. type VariableBackend struct { - platform.HTTPErrorHandler + influxdb.HTTPErrorHandler log *zap.Logger - VariableService platform.VariableService - LabelService platform.LabelService + VariableService influxdb.VariableService + LabelService influxdb.LabelService } // NewVariableBackend creates a backend used by the variable handler. @@ -39,11 +39,11 @@ func NewVariableBackend(log *zap.Logger, b *APIBackend) *VariableBackend { type VariableHandler struct { *httprouter.Router - platform.HTTPErrorHandler + influxdb.HTTPErrorHandler log *zap.Logger - VariableService platform.VariableService - LabelService platform.LabelService + VariableService influxdb.VariableService + LabelService influxdb.LabelService } // NewVariableHandler creates a new VariableHandler @@ -72,7 +72,7 @@ func NewVariableHandler(log *zap.Logger, b *VariableBackend) *VariableHandler { HTTPErrorHandler: b.HTTPErrorHandler, log: b.log.With(zap.String("handler", "label")), LabelService: b.LabelService, - ResourceType: platform.VariablesResourceType, + ResourceType: influxdb.VariablesResourceType, } h.HandlerFunc("GET", entityLabelsPath, newGetLabelsHandler(labelBackend)) h.HandlerFunc("POST", entityLabelsPath, newPostLabelHandler(labelBackend)) @@ -83,18 +83,18 @@ func NewVariableHandler(log *zap.Logger, b *VariableBackend) *VariableHandler { type getVariablesResponse struct { Variables []variableResponse `json:"variables"` - Links *platform.PagingLinks `json:"links"` + Links *influxdb.PagingLinks `json:"links"` } -func (r getVariablesResponse) ToPlatform() []*platform.Variable { - variables := make([]*platform.Variable, len(r.Variables)) +func (r getVariablesResponse) Toinfluxdb() []*influxdb.Variable { + variables := make([]*influxdb.Variable, len(r.Variables)) for i := range r.Variables { variables[i] = r.Variables[i].Variable } return variables } -func newGetVariablesResponse(ctx context.Context, variables []*platform.Variable, f platform.VariableFilter, opts platform.FindOptions, labelService platform.LabelService) getVariablesResponse { +func newGetVariablesResponse(ctx context.Context, variables []*influxdb.Variable, f influxdb.VariableFilter, opts influxdb.FindOptions, labelService influxdb.LabelService) getVariablesResponse { num := len(variables) resp := getVariablesResponse{ Variables: make([]variableResponse, 0, num), @@ -102,7 +102,7 @@ func newGetVariablesResponse(ctx context.Context, variables []*platform.Variable } for _, variable := range variables { - labels, _ := labelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: variable.ID}) + labels, _ := labelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: variable.ID, ResourceType: influxdb.VariablesResourceType}) resp.Variables = append(resp.Variables, newVariableResponse(variable, labels)) } @@ -110,8 +110,8 @@ func newGetVariablesResponse(ctx context.Context, variables []*platform.Variable } type getVariablesRequest struct { - filter platform.VariableFilter - opts platform.FindOptions + filter influxdb.VariableFilter + opts influxdb.FindOptions } func decodeGetVariablesRequest(ctx context.Context, r *http.Request) (*getVariablesRequest, error) { @@ -125,7 +125,7 @@ func decodeGetVariablesRequest(ctx context.Context, r *http.Request) (*getVariab } qp := r.URL.Query() if orgID := qp.Get("orgID"); orgID != "" { - id, err := platform.IDFromString(orgID) + id, err := influxdb.IDFromString(orgID) if err != nil { return nil, err } @@ -149,8 +149,8 @@ func (h *VariableHandler) handleGetVariables(w http.ResponseWriter, r *http.Requ variables, err := h.VariableService.FindVariables(ctx, req.filter, req.opts) if err != nil { - h.HandleHTTPError(ctx, &platform.Error{ - Code: platform.EInternal, + h.HandleHTTPError(ctx, &influxdb.Error{ + Code: influxdb.EInternal, Msg: "could not read variables", Err: err, }, w) @@ -164,19 +164,19 @@ func (h *VariableHandler) handleGetVariables(w http.ResponseWriter, r *http.Requ } } -func requestVariableID(ctx context.Context) (platform.ID, error) { +func requestVariableID(ctx context.Context) (influxdb.ID, error) { params := httprouter.ParamsFromContext(ctx) urlID := params.ByName("id") if urlID == "" { - return platform.InvalidID(), &platform.Error{ - Code: platform.EInvalid, + return influxdb.InvalidID(), &influxdb.Error{ + Code: influxdb.EInvalid, Msg: "url missing id", } } - id, err := platform.IDFromString(urlID) + id, err := influxdb.IDFromString(urlID) if err != nil { - return platform.InvalidID(), err + return influxdb.InvalidID(), err } return *id, nil @@ -196,7 +196,7 @@ func (h *VariableHandler) handleGetVariable(w http.ResponseWriter, r *http.Reque return } - labels, err := h.LabelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: variable.ID}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: variable.ID, ResourceType: influxdb.VariablesResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -216,15 +216,15 @@ type variableLinks struct { } type variableResponse struct { - *platform.Variable - Labels []platform.Label `json:"labels"` + *influxdb.Variable + Labels []influxdb.Label `json:"labels"` Links variableLinks `json:"links"` } -func newVariableResponse(m *platform.Variable, labels []*platform.Label) variableResponse { +func newVariableResponse(m *influxdb.Variable, labels []*influxdb.Label) variableResponse { res := variableResponse{ Variable: m, - Labels: []platform.Label{}, + Labels: []influxdb.Label{}, Links: variableLinks{ Self: fmt.Sprintf("/api/v2/variables/%s", m.ID), Labels: fmt.Sprintf("/api/v2/variables/%s/labels", m.ID), @@ -252,14 +252,14 @@ func (h *VariableHandler) handlePostVariable(w http.ResponseWriter, r *http.Requ return } h.log.Debug("Variable created", zap.String("var", fmt.Sprint(req.variable))) - if err := encodeResponse(ctx, w, http.StatusCreated, newVariableResponse(req.variable, []*platform.Label{})); err != nil { + if err := encodeResponse(ctx, w, http.StatusCreated, newVariableResponse(req.variable, []*influxdb.Label{})); err != nil { logEncodingError(h.log, r, err) return } } type postVariableRequest struct { - variable *platform.Variable + variable *influxdb.Variable } func (r *postVariableRequest) Valid() error { @@ -267,12 +267,12 @@ func (r *postVariableRequest) Valid() error { } func decodePostVariableRequest(r *http.Request) (*postVariableRequest, error) { - m := &platform.Variable{} + m := &influxdb.Variable{} err := json.NewDecoder(r.Body).Decode(m) if err != nil { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Msg: err.Error(), } } @@ -282,8 +282,8 @@ func decodePostVariableRequest(r *http.Request) (*postVariableRequest, error) { } if err := req.Valid(); err != nil { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Msg: err.Error(), } } @@ -305,7 +305,7 @@ func (h *VariableHandler) handlePatchVariable(w http.ResponseWriter, r *http.Req return } - labels, err := h.LabelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: variable.ID}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: variable.ID, ResourceType: influxdb.VariablesResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -319,8 +319,8 @@ func (h *VariableHandler) handlePatchVariable(w http.ResponseWriter, r *http.Req } type patchVariableRequest struct { - id platform.ID - variableUpdate *platform.VariableUpdate + id influxdb.ID + variableUpdate *influxdb.VariableUpdate } func (r *patchVariableRequest) Valid() error { @@ -328,12 +328,12 @@ func (r *patchVariableRequest) Valid() error { } func decodePatchVariableRequest(ctx context.Context, r *http.Request) (*patchVariableRequest, error) { - u := &platform.VariableUpdate{} + u := &influxdb.VariableUpdate{} err := json.NewDecoder(r.Body).Decode(u) if err != nil { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Msg: err.Error(), } } @@ -349,8 +349,8 @@ func decodePatchVariableRequest(ctx context.Context, r *http.Request) (*patchVar } if err := req.Valid(); err != nil { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Msg: err.Error(), } } @@ -372,7 +372,7 @@ func (h *VariableHandler) handlePutVariable(w http.ResponseWriter, r *http.Reque return } - labels, err := h.LabelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: req.variable.ID}) + labels, err := h.LabelService.FindResourceLabels(ctx, influxdb.LabelMappingFilter{ResourceID: req.variable.ID, ResourceType: influxdb.VariablesResourceType}) if err != nil { h.HandleHTTPError(ctx, err, w) return @@ -386,7 +386,7 @@ func (h *VariableHandler) handlePutVariable(w http.ResponseWriter, r *http.Reque } type putVariableRequest struct { - variable *platform.Variable + variable *influxdb.Variable } func (r *putVariableRequest) Valid() error { @@ -394,12 +394,12 @@ func (r *putVariableRequest) Valid() error { } func decodePutVariableRequest(ctx context.Context, r *http.Request) (*putVariableRequest, error) { - m := &platform.Variable{} + m := &influxdb.Variable{} err := json.NewDecoder(r.Body).Decode(m) if err != nil { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Err: err, } } @@ -409,8 +409,8 @@ func decodePutVariableRequest(ctx context.Context, r *http.Request) (*putVariabl } if err := req.Valid(); err != nil { - return nil, &platform.Error{ - Code: platform.EInvalid, + return nil, &influxdb.Error{ + Code: influxdb.EInvalid, Err: err, } } @@ -441,7 +441,7 @@ type VariableService struct { } // FindVariableByID finds a single variable from the store by its ID -func (s *VariableService) FindVariableByID(ctx context.Context, id platform.ID) (*platform.Variable, error) { +func (s *VariableService) FindVariableByID(ctx context.Context, id influxdb.ID) (*influxdb.Variable, error) { var mr variableResponse err := s.Client. Get(prefixVariables, id.String()). @@ -456,7 +456,7 @@ func (s *VariableService) FindVariableByID(ctx context.Context, id platform.ID) // FindVariables returns a list of variables that match filter. // Additional options provide pagination & sorting. -func (s *VariableService) FindVariables(ctx context.Context, filter platform.VariableFilter, opts ...platform.FindOptions) ([]*platform.Variable, error) { +func (s *VariableService) FindVariables(ctx context.Context, filter influxdb.VariableFilter, opts ...influxdb.FindOptions) ([]*influxdb.Variable, error) { params := findOptionParams(opts...) if filter.OrganizationID != nil { params = append(params, [2]string{"orgID", filter.OrganizationID.String()}) @@ -478,14 +478,14 @@ func (s *VariableService) FindVariables(ctx context.Context, filter platform.Var return nil, err } - return ms.ToPlatform(), nil + return ms.Toinfluxdb(), nil } -// CreateVariable creates a new variable and assigns it an platform.ID -func (s *VariableService) CreateVariable(ctx context.Context, m *platform.Variable) error { +// CreateVariable creates a new variable and assigns it an influxdb.ID +func (s *VariableService) CreateVariable(ctx context.Context, m *influxdb.Variable) error { if err := m.Valid(); err != nil { - return &platform.Error{ - Code: platform.EInvalid, + return &influxdb.Error{ + Code: influxdb.EInvalid, Err: err, } } @@ -497,8 +497,8 @@ func (s *VariableService) CreateVariable(ctx context.Context, m *platform.Variab } // UpdateVariable updates a single variable with a changeset -func (s *VariableService) UpdateVariable(ctx context.Context, id platform.ID, update *platform.VariableUpdate) (*platform.Variable, error) { - var m platform.Variable +func (s *VariableService) UpdateVariable(ctx context.Context, id influxdb.ID, update *influxdb.VariableUpdate) (*influxdb.Variable, error) { + var m influxdb.Variable err := s.Client. PatchJSON(update, prefixVariables, id.String()). DecodeJSON(&m). @@ -511,7 +511,7 @@ func (s *VariableService) UpdateVariable(ctx context.Context, id platform.ID, up } // ReplaceVariable replaces a single variable -func (s *VariableService) ReplaceVariable(ctx context.Context, variable *platform.Variable) error { +func (s *VariableService) ReplaceVariable(ctx context.Context, variable *influxdb.Variable) error { return s.Client. PutJSON(variable, prefixVariables, variable.ID.String()). DecodeJSON(variable). @@ -519,7 +519,7 @@ func (s *VariableService) ReplaceVariable(ctx context.Context, variable *platfor } // DeleteVariable removes a variable from the store -func (s *VariableService) DeleteVariable(ctx context.Context, id platform.ID) error { +func (s *VariableService) DeleteVariable(ctx context.Context, id influxdb.ID) error { return s.Client. Delete(prefixVariables, id.String()). Do(ctx)