influxdb/http/scraper_service.go

631 lines
17 KiB
Go
Raw Normal View History

2018-09-07 15:45:28 +00:00
package http
import (
"bytes"
"context"
"encoding/json"
2019-02-26 17:08:30 +00:00
"fmt"
2018-09-07 15:45:28 +00:00
"net/http"
"path"
"github.com/influxdata/influxdb"
2019-01-18 20:46:37 +00:00
pctx "github.com/influxdata/influxdb/context"
2018-09-07 15:45:28 +00:00
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
2018-09-07 15:45:28 +00:00
)
// ScraperBackend is all services and associated parameters required to construct
// the ScraperHandler.
type ScraperBackend struct {
influxdb.HTTPErrorHandler
Logger *zap.Logger
ScraperStorageService influxdb.ScraperTargetStoreService
BucketService influxdb.BucketService
OrganizationService influxdb.OrganizationService
UserService influxdb.UserService
UserResourceMappingService influxdb.UserResourceMappingService
LabelService influxdb.LabelService
}
// NewScraperBackend returns a new instance of ScraperBackend.
func NewScraperBackend(b *APIBackend) *ScraperBackend {
return &ScraperBackend{
HTTPErrorHandler: b.HTTPErrorHandler,
Logger: b.Logger.With(zap.String("handler", "scraper")),
ScraperStorageService: b.ScraperTargetStoreService,
BucketService: b.BucketService,
OrganizationService: b.OrganizationService,
UserService: b.UserService,
UserResourceMappingService: b.UserResourceMappingService,
LabelService: b.LabelService,
}
}
2018-09-07 15:45:28 +00:00
// ScraperHandler represents an HTTP API handler for scraper targets.
type ScraperHandler struct {
*httprouter.Router
influxdb.HTTPErrorHandler
2019-01-18 21:06:32 +00:00
Logger *zap.Logger
UserService influxdb.UserService
UserResourceMappingService influxdb.UserResourceMappingService
LabelService influxdb.LabelService
ScraperStorageService influxdb.ScraperTargetStoreService
BucketService influxdb.BucketService
OrganizationService influxdb.OrganizationService
2018-09-07 15:45:28 +00:00
}
const (
2019-01-18 21:06:32 +00:00
targetsPath = "/api/v2/scrapers"
targetsIDMembersPath = targetsPath + "/:id/members"
targetsIDMembersIDPath = targetsPath + "/:id/members/:userID"
targetsIDOwnersPath = targetsPath + "/:id/owners"
targetsIDOwnersIDPath = targetsPath + "/:id/owners/:userID"
targetsIDLabelsPath = targetsPath + "/:id/labels"
targetsIDLabelsIDPath = targetsPath + "/:id/labels/:lid"
2018-09-07 15:45:28 +00:00
)
// NewScraperHandler returns a new instance of ScraperHandler.
func NewScraperHandler(b *ScraperBackend) *ScraperHandler {
2018-09-07 15:45:28 +00:00
h := &ScraperHandler{
Router: NewRouter(b.HTTPErrorHandler),
HTTPErrorHandler: b.HTTPErrorHandler,
Logger: b.Logger,
UserService: b.UserService,
UserResourceMappingService: b.UserResourceMappingService,
LabelService: b.LabelService,
ScraperStorageService: b.ScraperStorageService,
BucketService: b.BucketService,
OrganizationService: b.OrganizationService,
2018-09-07 15:45:28 +00:00
}
2019-01-18 21:06:32 +00:00
h.HandlerFunc("POST", targetsPath, h.handlePostScraperTarget)
h.HandlerFunc("GET", targetsPath, h.handleGetScraperTargets)
h.HandlerFunc("GET", targetsPath+"/:id", h.handleGetScraperTarget)
h.HandlerFunc("PATCH", targetsPath+"/:id", h.handlePatchScraperTarget)
h.HandlerFunc("DELETE", targetsPath+"/:id", h.handleDeleteScraperTarget)
2019-02-05 19:27:49 +00:00
memberBackend := MemberBackend{
HTTPErrorHandler: b.HTTPErrorHandler,
2019-02-05 19:27:49 +00:00
Logger: b.Logger.With(zap.String("handler", "member")),
ResourceType: influxdb.ScraperResourceType,
UserType: influxdb.Member,
UserResourceMappingService: b.UserResourceMappingService,
UserService: b.UserService,
}
h.HandlerFunc("POST", targetsIDMembersPath, newPostMemberHandler(memberBackend))
h.HandlerFunc("GET", targetsIDMembersPath, newGetMembersHandler(memberBackend))
h.HandlerFunc("DELETE", targetsIDMembersIDPath, newDeleteMemberHandler(memberBackend))
ownerBackend := MemberBackend{
HTTPErrorHandler: b.HTTPErrorHandler,
2019-02-05 19:27:49 +00:00
Logger: b.Logger.With(zap.String("handler", "member")),
ResourceType: influxdb.ScraperResourceType,
UserType: influxdb.Owner,
UserResourceMappingService: b.UserResourceMappingService,
UserService: b.UserService,
}
h.HandlerFunc("POST", targetsIDOwnersPath, newPostMemberHandler(ownerBackend))
h.HandlerFunc("GET", targetsIDOwnersPath, newGetMembersHandler(ownerBackend))
h.HandlerFunc("DELETE", targetsIDOwnersIDPath, newDeleteMemberHandler(ownerBackend))
2019-01-18 21:06:32 +00:00
2019-02-05 19:27:49 +00:00
labelBackend := &LabelBackend{
HTTPErrorHandler: b.HTTPErrorHandler,
Logger: b.Logger.With(zap.String("handler", "label")),
LabelService: b.LabelService,
ResourceType: influxdb.ScraperResourceType,
2019-02-05 19:27:49 +00:00
}
h.HandlerFunc("GET", targetsIDLabelsPath, newGetLabelsHandler(labelBackend))
h.HandlerFunc("POST", targetsIDLabelsPath, newPostLabelHandler(labelBackend))
h.HandlerFunc("DELETE", targetsIDLabelsIDPath, newDeleteLabelHandler(labelBackend))
2019-01-18 21:06:32 +00:00
2018-09-07 15:45:28 +00:00
return h
}
2019-01-18 15:38:28 +00:00
// handlePostScraperTarget is HTTP handler for the POST /api/v2/scrapers route.
2018-09-07 15:45:28 +00:00
func (h *ScraperHandler) handlePostScraperTarget(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
req, err := decodeScraperTargetAddRequest(ctx, r)
if err != nil {
h.HandleHTTPError(ctx, err, w)
2018-09-07 15:45:28 +00:00
return
}
2019-01-18 20:46:37 +00:00
auth, err := pctx.GetAuthorizer(ctx)
if err != nil {
h.HandleHTTPError(ctx, err, w)
2019-01-18 20:46:37 +00:00
return
}
if err := h.ScraperStorageService.AddTarget(ctx, req, auth.GetUserID()); err != nil {
h.HandleHTTPError(ctx, err, w)
2018-09-07 15:45:28 +00:00
return
}
2019-07-09 15:16:26 +00:00
h.Logger.Debug("scraper created", zap.String("scraper", fmt.Sprint(req)))
resp, err := h.newTargetResponse(ctx, *req)
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
}
if err := encodeResponse(ctx, w, http.StatusCreated, resp); err != nil {
logEncodingError(h.Logger, r, err)
2018-09-07 15:45:28 +00:00
return
}
}
2019-01-18 15:38:28 +00:00
// handleDeleteScraperTarget is the HTTP handler for the DELETE /api/v2/scrapers/:id route.
2018-09-07 15:45:28 +00:00
func (h *ScraperHandler) handleDeleteScraperTarget(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
id, err := decodeScraperTargetIDRequest(ctx, r)
if err != nil {
h.HandleHTTPError(ctx, err, w)
2018-09-07 15:45:28 +00:00
return
}
if err := h.ScraperStorageService.RemoveTarget(ctx, *id); err != nil {
h.HandleHTTPError(ctx, err, w)
2018-09-07 15:45:28 +00:00
return
}
2019-07-09 15:16:26 +00:00
h.Logger.Debug("scraper deleted", zap.String("scraperTargetID", fmt.Sprint(id)))
2018-09-07 15:45:28 +00:00
2018-10-29 19:10:33 +00:00
w.WriteHeader(http.StatusNoContent)
2018-09-07 15:45:28 +00:00
}
2019-01-18 15:38:28 +00:00
// handlePatchScraperTarget is the HTTP handler for the PATCH /api/v2/scrapers/:id route.
2018-09-07 15:45:28 +00:00
func (h *ScraperHandler) handlePatchScraperTarget(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
update, err := decodeScraperTargetUpdateRequest(ctx, r)
if err != nil {
h.HandleHTTPError(ctx, err, w)
2018-09-07 15:45:28 +00:00
return
}
2019-01-18 20:46:37 +00:00
auth, err := pctx.GetAuthorizer(ctx)
if err != nil {
h.HandleHTTPError(ctx, err, w)
2019-01-18 20:46:37 +00:00
return
}
target, err := h.ScraperStorageService.UpdateTarget(ctx, update, auth.GetUserID())
2018-09-07 15:45:28 +00:00
if err != nil {
h.HandleHTTPError(ctx, err, w)
2018-09-07 15:45:28 +00:00
return
}
2019-07-09 15:16:26 +00:00
h.Logger.Debug("scraper updated", zap.String("scraper", fmt.Sprint(target)))
2018-09-07 15:45:28 +00:00
resp, err := h.newTargetResponse(ctx, *target)
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
}
if err := encodeResponse(ctx, w, http.StatusOK, resp); err != nil {
logEncodingError(h.Logger, r, err)
2018-09-07 15:45:28 +00:00
return
}
}
func (h *ScraperHandler) handleGetScraperTarget(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
id, err := decodeScraperTargetIDRequest(ctx, r)
if err != nil {
h.HandleHTTPError(ctx, err, w)
2018-09-07 15:45:28 +00:00
return
}
target, err := h.ScraperStorageService.GetTargetByID(ctx, *id)
if err != nil {
h.HandleHTTPError(ctx, err, w)
2018-09-07 15:45:28 +00:00
return
}
2019-07-09 15:16:26 +00:00
h.Logger.Debug("scraper retrieved", zap.String("scraper", fmt.Sprint(target)))
2018-09-07 15:45:28 +00:00
resp, err := h.newTargetResponse(ctx, *target)
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
}
if err := encodeResponse(ctx, w, http.StatusOK, resp); err != nil {
logEncodingError(h.Logger, r, err)
2018-09-07 15:45:28 +00:00
return
}
}
2019-04-12 16:45:48 +00:00
type getScraperTargetsRequest struct {
filter influxdb.ScraperTargetFilter
}
func decodeScraperTargetsRequest(ctx context.Context, r *http.Request) (*getScraperTargetsRequest, error) {
qp := r.URL.Query()
req := &getScraperTargetsRequest{}
initialID := influxdb.InvalidID()
if ids, ok := qp["id"]; ok {
req.filter.IDs = make(map[influxdb.ID]bool)
for _, id := range ids {
i := initialID
if err := i.DecodeFromString(id); err != nil {
return nil, err
}
req.filter.IDs[i] = false
}
}
if name := qp.Get("name"); name != "" {
req.filter.Name = &name
}
if orgID := qp.Get("orgID"); orgID != "" {
id := influxdb.InvalidID()
if err := id.DecodeFromString(orgID); err != nil {
return nil, err
}
req.filter.OrgID = &id
} else if org := qp.Get("org"); org != "" {
req.filter.Org = &org
}
return req, nil
}
2019-01-18 15:38:28 +00:00
// handleGetScraperTargets is the HTTP handler for the GET /api/v2/scrapers route.
2018-09-07 15:45:28 +00:00
func (h *ScraperHandler) handleGetScraperTargets(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2019-04-12 16:45:48 +00:00
req, err := decodeScraperTargetsRequest(ctx, r)
if err != nil {
h.HandleHTTPError(ctx, err, w)
2019-04-12 16:45:48 +00:00
return
}
targets, err := h.ScraperStorageService.ListTargets(ctx, req.filter)
2018-09-07 15:45:28 +00:00
if err != nil {
h.HandleHTTPError(ctx, err, w)
2018-09-07 15:45:28 +00:00
return
}
2019-07-09 15:16:26 +00:00
h.Logger.Debug("scrapers retrieved", zap.String("scrapers", fmt.Sprint(targets)))
2018-09-07 15:45:28 +00:00
resp, err := h.newListTargetsResponse(ctx, targets)
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
}
if err := encodeResponse(ctx, w, http.StatusOK, resp); err != nil {
logEncodingError(h.Logger, r, err)
2018-09-07 15:45:28 +00:00
return
}
}
func decodeScraperTargetUpdateRequest(ctx context.Context, r *http.Request) (*influxdb.ScraperTarget, error) {
update := &influxdb.ScraperTarget{}
2018-09-07 15:45:28 +00:00
if err := json.NewDecoder(r.Body).Decode(update); err != nil {
return nil, err
}
id, err := decodeScraperTargetIDRequest(ctx, r)
if err != nil {
return nil, err
}
update.ID = *id
return update, nil
}
func decodeScraperTargetAddRequest(ctx context.Context, r *http.Request) (*influxdb.ScraperTarget, error) {
req := &influxdb.ScraperTarget{}
2018-09-07 15:45:28 +00:00
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
return nil, err
}
return req, nil
}
func decodeScraperTargetIDRequest(ctx context.Context, r *http.Request) (*influxdb.ID, error) {
2018-09-07 15:45:28 +00:00
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
2019-01-10 17:39:37 +00:00
Msg: "url missing id",
}
2018-09-07 15:45:28 +00:00
}
var i influxdb.ID
2018-09-07 15:45:28 +00:00
if err := i.DecodeFromString(id); err != nil {
return nil, err
}
return &i, nil
}
// ScraperService connects to Influx via HTTP using tokens to manage scraper targets.
type ScraperService struct {
Addr string
Token string
InsecureSkipVerify bool
// OpPrefix is for update invalid ops
OpPrefix string
2018-09-07 15:45:28 +00:00
}
// ListTargets returns a list of all scraper targets.
2019-04-12 16:45:48 +00:00
func (s *ScraperService) ListTargets(ctx context.Context, filter influxdb.ScraperTargetFilter) ([]influxdb.ScraperTarget, error) {
url, err := NewURL(s.Addr, targetsPath)
2018-09-07 15:45:28 +00:00
if err != nil {
return nil, err
}
query := url.Query()
2019-04-12 16:45:48 +00:00
if filter.IDs != nil {
for id := range filter.IDs {
query.Add("id", id.String())
}
}
if filter.Name != nil {
query.Set("name", *filter.Name)
}
if filter.OrgID != nil {
query.Set("orgID", filter.OrgID.String())
}
if filter.Org != nil {
query.Set("org", *filter.Org)
}
2018-09-07 15:45:28 +00:00
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
req.URL.RawQuery = query.Encode()
SetToken(s.Token, req)
hc := NewClient(url.Scheme, s.InsecureSkipVerify)
2018-09-07 15:45:28 +00:00
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
2019-01-24 01:02:37 +00:00
if err := CheckError(resp); err != nil {
2018-09-07 15:45:28 +00:00
return nil, err
}
var targetsResp getTargetsResponse
if err := json.NewDecoder(resp.Body).Decode(&targetsResp); err != nil {
return nil, err
}
targets := make([]influxdb.ScraperTarget, len(targetsResp.Targets))
2018-09-07 15:45:28 +00:00
for k, v := range targetsResp.Targets {
targets[k] = v.ScraperTarget
}
return targets, nil
}
// UpdateTarget updates a single scraper target with changeset.
// Returns the new target state after update.
2019-01-18 20:46:37 +00:00
func (s *ScraperService) UpdateTarget(ctx context.Context, update *influxdb.ScraperTarget, userID influxdb.ID) (*influxdb.ScraperTarget, error) {
2018-10-12 02:57:09 +00:00
if !update.ID.Valid() {
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Op: s.OpPrefix + influxdb.OpUpdateTarget,
feat(kv): implemented key/value store with end-to-end integration tests * feat(kv:inmem:bolt): implement user service in a kv * refactor(kv): use consistent func receiver name * feat(kv): add initial basic auth service * refactor(passwords): move auth interface into own file * refactor(passwords): rename basic auth files to passwords * refactor(passwords): rename from BasicAuth to Passwords * refactor(kv): copy bolt user test into kv Co-authored-by: Michael Desa <mjdesa@gmail.com> * feat(kv): add inmem testing to kv store * fix(kv): remove extra user index initialization * feat(kv): attempt at making errors nice * fix(http): return not found error if filter is invalid * fix(http): s/platform/influxdb/ for user service * fix(http): s/platform/influxdb/ for user service * feat(kv): initial port of telegraf configs to kv * feat(kv): first pass at migrating bolt org service to kv * feat(kv): first pass at bucket service * feat(kv): first pass at migrating kvlog to kv package * feat(kv): add resource op logs * feat(kv): first pass at user resource mapping migration * feat(kv): add urm usage to bucket and org services * feat(kv): first pass at kv authz service * feat(kv): add cascading auth delete for users * feat(kv): first pass d authorizer.OrganizationService in kv * feat(cmd/influxd/launcher): user kv services where appropriate * fix(kv): initialize authorizations * fix(influxdb): use same buckets while slowly migrating stuff * fix(kv): make staticcheck pass * feat(kv): add dashboards to kv review: make suggestions from pr review fix: use common bucket names for bolt/kv stores * test(kv): add complete password test coverage * chore(kv): fixes for staticcheck * feat(kv): implement labels generically on kv * feat(kv): implement macro service * feat(kv): add source service * feat(kv): add session service * feat(kv): add kv secret service * refactor(kv): update telegraf and urm with error messages * feat(kv): add lookup service * feat(kv): add kv onboarding service * refactor(kv): update telegraf to avoid repetition * feat(cmd/influxd): use kv lookup service * feat(kv): add telegraf to lookup service * feat(cmd/influxd): use kv telegraf service * feat(kv): initial port of scrapers in bolt to kv * feat(kv): update scraper error messaging * feat(cmd/influxd): add kv scraper * feat(kv): add inmem backend tests * refactor(kv): copy paste errors * refactor(kv): add code to password errors * fix(testing): update error messages for incorrect passwords * feat(kv:inmem:bolt): implement user service in a kv * refactor(kv): use consistent func receiver name * refactor(kv): copy bolt user test into kv Co-authored-by: Michael Desa <mjdesa@gmail.com> * feat(kv): add inmem testing to kv store * fix(kv): remove extra user index initialization * feat(kv): attempt at making errors nice * fix(http): return not found error if filter is invalid * fix(http): s/platform/influxdb/ for user service * feat(kv): first pass at migrating bolt org service to kv * feat(kv): first pass at bucket service * feat(kv): first pass at migrating kvlog to kv package * feat(kv): add resource op logs * feat(kv): first pass at user resource mapping migration * feat(kv): add urm usage to bucket and org services * feat(kv): first pass at kv authz service * feat(kv): add cascading auth delete for users * feat(kv): first pass d authorizer.OrganizationService in kv * feat(cmd/influxd/launcher): user kv services where appropriate * feat(kv): add initial basic auth service * refactor(passwords): move auth interface into own file * refactor(passwords): rename basic auth files to passwords * fix(http): s/platform/influxdb/ for user service * fix(kv): initialize authorizations * fix(influxdb): use same buckets while slowly migrating stuff * fix(kv): make staticcheck pass * feat(kv): add dashboards to kv review: make suggestions from pr review fix: use common bucket names for bolt/kv stores * feat(kv): implement labels generically on kv * refactor(passwords): rename from BasicAuth to Passwords * test(kv): add complete password test coverage * chore(kv): fixes for staticcheck * feat(kv): implement macro service * feat(kv): add source service * feat(kv): add session service * feat(kv): initial port of telegraf configs to kv * feat(kv): initial port of scrapers in bolt to kv * feat(kv): add kv secret service * refactor(kv): update telegraf and urm with error messages * feat(kv): add lookup service * feat(kv): add kv onboarding service * refactor(kv): update telegraf to avoid repetition * feat(cmd/influxd): use kv lookup service * feat(kv): add telegraf to lookup service * feat(cmd/influxd): use kv telegraf service * feat(kv): update scraper error messaging * feat(cmd/influxd): add kv scraper * feat(kv): add inmem backend tests * refactor(kv): copy paste errors * refactor(kv): add code to password errors * fix(testing): update error messages for incorrect passwords * feat(http): initial support for flushing all key/values from kv store * feat(kv): rename macro to variable * feat(cmd/influxd/launcher): user kv services where appropriate * refactor(passwords): rename from BasicAuth to Passwords * feat(kv): implement macro service * test(ui): introduce cypress * test(ui): introduce first typescript test * test(ui/e2e): add ci job * chore: update gitignore to ignore test outputs * feat(inmem): in memory influxdb * test(e2e): adding pinger that checks if influxdb is alive * hackathon * hack * hack * hack * hack * Revert "feat(inmem): in memory influxdb" This reverts commit 30ddf032003e704643b07ce80df61c3299ea7295. * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * chore: lint ignore node_modules * hack * hack * hack * add user and flush * hack * remove unused vars * hack * hack * ci(circle): prefix e2e artifacts * change test to testid * update cypress * moar testid * fix npm warnings * remove absolte path * chore(ci): remove /home/circleci proto mkdir hack * wip: crud resources e2e * fix(inmem): use inmem kv store services * test(dashboard): add first dashboard crud tests * hack * undo hack * fix: use response from setup for orgID * chore: wip * add convenience getByTitle function * test(e2e): ui can create orgs * test(e2e): add test for org deletion and update * test(e2e): introduce task creation test * test(e2e): create and update of buckets on org view * chore: move types to declaration file * chore: use route fixture in dashboard tests * chore(ci): hack back * test(ui): update snapshots * chore: package-lock * chore: remove macros * fix: launcher rebase issues * fix: compile errors * fix: compile errors * feat(cmd/influxdb): add explicit testing, asset-path, and store flags Co-authored-by: Andrew Watkins <watts@influxdb.com> * fix(cmd/influxd): set default HTTP handler and flags Co-authored-by: Andrew Watkins <watts@influxdb.com> * build(Makefile): add run-e2e and PHONY * feat(kv:inmem:bolt): implement user service in a kv * refactor(kv): use consistent func receiver name * feat(kv): add initial basic auth service * refactor(passwords): move auth interface into own file * refactor(passwords): rename basic auth files to passwords * refactor(passwords): rename from BasicAuth to Passwords * refactor(kv): copy bolt user test into kv Co-authored-by: Michael Desa <mjdesa@gmail.com> * feat(kv): add inmem testing to kv store * fix(kv): remove extra user index initialization * feat(kv): attempt at making errors nice * fix(http): return not found error if filter is invalid * fix(http): s/platform/influxdb/ for user service * fix(http): s/platform/influxdb/ for user service * feat(kv): initial port of telegraf configs to kv * feat(kv): initial port of scrapers in bolt to kv * feat(kv): first pass at migrating bolt org service to kv * feat(kv): first pass at bucket service * feat(kv): first pass at migrating kvlog to kv package * feat(kv): add resource op logs * feat(kv): first pass at user resource mapping migration * feat(kv): add urm usage to bucket and org services * feat(kv): first pass at kv authz service * feat(kv): add cascading auth delete for users * feat(kv): first pass d authorizer.OrganizationService in kv * feat(cmd/influxd/launcher): user kv services where appropriate * fix(kv): initialize authorizations * fix(influxdb): use same buckets while slowly migrating stuff * fix(kv): make staticcheck pass * feat(kv): add dashboards to kv review: make suggestions from pr review fix: use common bucket names for bolt/kv stores * test(kv): add complete password test coverage * chore(kv): fixes for staticcheck * feat(kv): implement labels generically on kv * feat(kv): implement macro service * feat(kv): add source service * feat(kv): add session service * feat(kv): add kv secret service * refactor(kv): update telegraf and urm with error messages * feat(kv): add lookup service * feat(kv): add kv onboarding service * refactor(kv): update telegraf to avoid repetition * feat(cmd/influxd): use kv lookup service * feat(kv): add telegraf to lookup service * feat(cmd/influxd): use kv telegraf service * feat(kv): update scraper error messaging * feat(cmd/influxd): add kv scraper * feat(kv): add inmem backend tests * refactor(kv): copy paste errors * refactor(kv): add code to password errors * fix(testing): update error messages for incorrect passwords * feat(kv): rename macro to variable * refactor(kv): auth/bucket/org/user unique checks return errors now * feat(inmem): add way to get all bucket names from store * feat(inmem): Buckets to return slice of bytes rather than strings * feat(inmem): add locks around Buckets to avoid races * feat(cmd/influx): check for unauthorized error in wrapCheckSetup * chore(e2e): add video and screenshot artifcats to gitignore * docs(ci): add build instructions for e2e tests * feat(kv): add id lookup for authorized resources
2019-02-19 23:47:19 +00:00
Msg: "provided scraper target ID has invalid format",
}
2018-10-12 02:57:09 +00:00
}
url, err := NewURL(s.Addr, targetIDPath(update.ID))
2018-09-07 15:45:28 +00:00
if err != nil {
return nil, err
}
octets, err := json.Marshal(update)
if err != nil {
return nil, err
}
req, err := http.NewRequest("PATCH", url.String(), bytes.NewReader(octets))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
SetToken(s.Token, req)
hc := NewClient(url.Scheme, s.InsecureSkipVerify)
2018-09-07 15:45:28 +00:00
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
2018-09-07 15:45:28 +00:00
2019-01-24 01:02:37 +00:00
if err := CheckError(resp); err != nil {
2018-09-07 15:45:28 +00:00
return nil, err
}
var targetResp targetResponse
if err := json.NewDecoder(resp.Body).Decode(&targetResp); err != nil {
return nil, err
}
return &targetResp.ScraperTarget, nil
}
// AddTarget creates a new scraper target and sets target.ID with the new identifier.
2019-01-18 20:46:37 +00:00
func (s *ScraperService) AddTarget(ctx context.Context, target *influxdb.ScraperTarget, userID influxdb.ID) error {
url, err := NewURL(s.Addr, targetsPath)
2018-09-07 15:45:28 +00:00
if err != nil {
return err
}
2019-01-10 17:39:37 +00:00
if !target.OrgID.Valid() {
return &influxdb.Error{
Code: influxdb.EInvalid,
feat(kv): implemented key/value store with end-to-end integration tests * feat(kv:inmem:bolt): implement user service in a kv * refactor(kv): use consistent func receiver name * feat(kv): add initial basic auth service * refactor(passwords): move auth interface into own file * refactor(passwords): rename basic auth files to passwords * refactor(passwords): rename from BasicAuth to Passwords * refactor(kv): copy bolt user test into kv Co-authored-by: Michael Desa <mjdesa@gmail.com> * feat(kv): add inmem testing to kv store * fix(kv): remove extra user index initialization * feat(kv): attempt at making errors nice * fix(http): return not found error if filter is invalid * fix(http): s/platform/influxdb/ for user service * fix(http): s/platform/influxdb/ for user service * feat(kv): initial port of telegraf configs to kv * feat(kv): first pass at migrating bolt org service to kv * feat(kv): first pass at bucket service * feat(kv): first pass at migrating kvlog to kv package * feat(kv): add resource op logs * feat(kv): first pass at user resource mapping migration * feat(kv): add urm usage to bucket and org services * feat(kv): first pass at kv authz service * feat(kv): add cascading auth delete for users * feat(kv): first pass d authorizer.OrganizationService in kv * feat(cmd/influxd/launcher): user kv services where appropriate * fix(kv): initialize authorizations * fix(influxdb): use same buckets while slowly migrating stuff * fix(kv): make staticcheck pass * feat(kv): add dashboards to kv review: make suggestions from pr review fix: use common bucket names for bolt/kv stores * test(kv): add complete password test coverage * chore(kv): fixes for staticcheck * feat(kv): implement labels generically on kv * feat(kv): implement macro service * feat(kv): add source service * feat(kv): add session service * feat(kv): add kv secret service * refactor(kv): update telegraf and urm with error messages * feat(kv): add lookup service * feat(kv): add kv onboarding service * refactor(kv): update telegraf to avoid repetition * feat(cmd/influxd): use kv lookup service * feat(kv): add telegraf to lookup service * feat(cmd/influxd): use kv telegraf service * feat(kv): initial port of scrapers in bolt to kv * feat(kv): update scraper error messaging * feat(cmd/influxd): add kv scraper * feat(kv): add inmem backend tests * refactor(kv): copy paste errors * refactor(kv): add code to password errors * fix(testing): update error messages for incorrect passwords * feat(kv:inmem:bolt): implement user service in a kv * refactor(kv): use consistent func receiver name * refactor(kv): copy bolt user test into kv Co-authored-by: Michael Desa <mjdesa@gmail.com> * feat(kv): add inmem testing to kv store * fix(kv): remove extra user index initialization * feat(kv): attempt at making errors nice * fix(http): return not found error if filter is invalid * fix(http): s/platform/influxdb/ for user service * feat(kv): first pass at migrating bolt org service to kv * feat(kv): first pass at bucket service * feat(kv): first pass at migrating kvlog to kv package * feat(kv): add resource op logs * feat(kv): first pass at user resource mapping migration * feat(kv): add urm usage to bucket and org services * feat(kv): first pass at kv authz service * feat(kv): add cascading auth delete for users * feat(kv): first pass d authorizer.OrganizationService in kv * feat(cmd/influxd/launcher): user kv services where appropriate * feat(kv): add initial basic auth service * refactor(passwords): move auth interface into own file * refactor(passwords): rename basic auth files to passwords * fix(http): s/platform/influxdb/ for user service * fix(kv): initialize authorizations * fix(influxdb): use same buckets while slowly migrating stuff * fix(kv): make staticcheck pass * feat(kv): add dashboards to kv review: make suggestions from pr review fix: use common bucket names for bolt/kv stores * feat(kv): implement labels generically on kv * refactor(passwords): rename from BasicAuth to Passwords * test(kv): add complete password test coverage * chore(kv): fixes for staticcheck * feat(kv): implement macro service * feat(kv): add source service * feat(kv): add session service * feat(kv): initial port of telegraf configs to kv * feat(kv): initial port of scrapers in bolt to kv * feat(kv): add kv secret service * refactor(kv): update telegraf and urm with error messages * feat(kv): add lookup service * feat(kv): add kv onboarding service * refactor(kv): update telegraf to avoid repetition * feat(cmd/influxd): use kv lookup service * feat(kv): add telegraf to lookup service * feat(cmd/influxd): use kv telegraf service * feat(kv): update scraper error messaging * feat(cmd/influxd): add kv scraper * feat(kv): add inmem backend tests * refactor(kv): copy paste errors * refactor(kv): add code to password errors * fix(testing): update error messages for incorrect passwords * feat(http): initial support for flushing all key/values from kv store * feat(kv): rename macro to variable * feat(cmd/influxd/launcher): user kv services where appropriate * refactor(passwords): rename from BasicAuth to Passwords * feat(kv): implement macro service * test(ui): introduce cypress * test(ui): introduce first typescript test * test(ui/e2e): add ci job * chore: update gitignore to ignore test outputs * feat(inmem): in memory influxdb * test(e2e): adding pinger that checks if influxdb is alive * hackathon * hack * hack * hack * hack * Revert "feat(inmem): in memory influxdb" This reverts commit 30ddf032003e704643b07ce80df61c3299ea7295. * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * chore: lint ignore node_modules * hack * hack * hack * add user and flush * hack * remove unused vars * hack * hack * ci(circle): prefix e2e artifacts * change test to testid * update cypress * moar testid * fix npm warnings * remove absolte path * chore(ci): remove /home/circleci proto mkdir hack * wip: crud resources e2e * fix(inmem): use inmem kv store services * test(dashboard): add first dashboard crud tests * hack * undo hack * fix: use response from setup for orgID * chore: wip * add convenience getByTitle function * test(e2e): ui can create orgs * test(e2e): add test for org deletion and update * test(e2e): introduce task creation test * test(e2e): create and update of buckets on org view * chore: move types to declaration file * chore: use route fixture in dashboard tests * chore(ci): hack back * test(ui): update snapshots * chore: package-lock * chore: remove macros * fix: launcher rebase issues * fix: compile errors * fix: compile errors * feat(cmd/influxdb): add explicit testing, asset-path, and store flags Co-authored-by: Andrew Watkins <watts@influxdb.com> * fix(cmd/influxd): set default HTTP handler and flags Co-authored-by: Andrew Watkins <watts@influxdb.com> * build(Makefile): add run-e2e and PHONY * feat(kv:inmem:bolt): implement user service in a kv * refactor(kv): use consistent func receiver name * feat(kv): add initial basic auth service * refactor(passwords): move auth interface into own file * refactor(passwords): rename basic auth files to passwords * refactor(passwords): rename from BasicAuth to Passwords * refactor(kv): copy bolt user test into kv Co-authored-by: Michael Desa <mjdesa@gmail.com> * feat(kv): add inmem testing to kv store * fix(kv): remove extra user index initialization * feat(kv): attempt at making errors nice * fix(http): return not found error if filter is invalid * fix(http): s/platform/influxdb/ for user service * fix(http): s/platform/influxdb/ for user service * feat(kv): initial port of telegraf configs to kv * feat(kv): initial port of scrapers in bolt to kv * feat(kv): first pass at migrating bolt org service to kv * feat(kv): first pass at bucket service * feat(kv): first pass at migrating kvlog to kv package * feat(kv): add resource op logs * feat(kv): first pass at user resource mapping migration * feat(kv): add urm usage to bucket and org services * feat(kv): first pass at kv authz service * feat(kv): add cascading auth delete for users * feat(kv): first pass d authorizer.OrganizationService in kv * feat(cmd/influxd/launcher): user kv services where appropriate * fix(kv): initialize authorizations * fix(influxdb): use same buckets while slowly migrating stuff * fix(kv): make staticcheck pass * feat(kv): add dashboards to kv review: make suggestions from pr review fix: use common bucket names for bolt/kv stores * test(kv): add complete password test coverage * chore(kv): fixes for staticcheck * feat(kv): implement labels generically on kv * feat(kv): implement macro service * feat(kv): add source service * feat(kv): add session service * feat(kv): add kv secret service * refactor(kv): update telegraf and urm with error messages * feat(kv): add lookup service * feat(kv): add kv onboarding service * refactor(kv): update telegraf to avoid repetition * feat(cmd/influxd): use kv lookup service * feat(kv): add telegraf to lookup service * feat(cmd/influxd): use kv telegraf service * feat(kv): update scraper error messaging * feat(cmd/influxd): add kv scraper * feat(kv): add inmem backend tests * refactor(kv): copy paste errors * refactor(kv): add code to password errors * fix(testing): update error messages for incorrect passwords * feat(kv): rename macro to variable * refactor(kv): auth/bucket/org/user unique checks return errors now * feat(inmem): add way to get all bucket names from store * feat(inmem): Buckets to return slice of bytes rather than strings * feat(inmem): add locks around Buckets to avoid races * feat(cmd/influx): check for unauthorized error in wrapCheckSetup * chore(e2e): add video and screenshot artifcats to gitignore * docs(ci): add build instructions for e2e tests * feat(kv): add id lookup for authorized resources
2019-02-19 23:47:19 +00:00
Msg: "provided organization ID has invalid format",
Op: s.OpPrefix + influxdb.OpAddTarget,
2019-01-10 17:39:37 +00:00
}
}
if !target.BucketID.Valid() {
return &influxdb.Error{
Code: influxdb.EInvalid,
feat(kv): implemented key/value store with end-to-end integration tests * feat(kv:inmem:bolt): implement user service in a kv * refactor(kv): use consistent func receiver name * feat(kv): add initial basic auth service * refactor(passwords): move auth interface into own file * refactor(passwords): rename basic auth files to passwords * refactor(passwords): rename from BasicAuth to Passwords * refactor(kv): copy bolt user test into kv Co-authored-by: Michael Desa <mjdesa@gmail.com> * feat(kv): add inmem testing to kv store * fix(kv): remove extra user index initialization * feat(kv): attempt at making errors nice * fix(http): return not found error if filter is invalid * fix(http): s/platform/influxdb/ for user service * fix(http): s/platform/influxdb/ for user service * feat(kv): initial port of telegraf configs to kv * feat(kv): first pass at migrating bolt org service to kv * feat(kv): first pass at bucket service * feat(kv): first pass at migrating kvlog to kv package * feat(kv): add resource op logs * feat(kv): first pass at user resource mapping migration * feat(kv): add urm usage to bucket and org services * feat(kv): first pass at kv authz service * feat(kv): add cascading auth delete for users * feat(kv): first pass d authorizer.OrganizationService in kv * feat(cmd/influxd/launcher): user kv services where appropriate * fix(kv): initialize authorizations * fix(influxdb): use same buckets while slowly migrating stuff * fix(kv): make staticcheck pass * feat(kv): add dashboards to kv review: make suggestions from pr review fix: use common bucket names for bolt/kv stores * test(kv): add complete password test coverage * chore(kv): fixes for staticcheck * feat(kv): implement labels generically on kv * feat(kv): implement macro service * feat(kv): add source service * feat(kv): add session service * feat(kv): add kv secret service * refactor(kv): update telegraf and urm with error messages * feat(kv): add lookup service * feat(kv): add kv onboarding service * refactor(kv): update telegraf to avoid repetition * feat(cmd/influxd): use kv lookup service * feat(kv): add telegraf to lookup service * feat(cmd/influxd): use kv telegraf service * feat(kv): initial port of scrapers in bolt to kv * feat(kv): update scraper error messaging * feat(cmd/influxd): add kv scraper * feat(kv): add inmem backend tests * refactor(kv): copy paste errors * refactor(kv): add code to password errors * fix(testing): update error messages for incorrect passwords * feat(kv:inmem:bolt): implement user service in a kv * refactor(kv): use consistent func receiver name * refactor(kv): copy bolt user test into kv Co-authored-by: Michael Desa <mjdesa@gmail.com> * feat(kv): add inmem testing to kv store * fix(kv): remove extra user index initialization * feat(kv): attempt at making errors nice * fix(http): return not found error if filter is invalid * fix(http): s/platform/influxdb/ for user service * feat(kv): first pass at migrating bolt org service to kv * feat(kv): first pass at bucket service * feat(kv): first pass at migrating kvlog to kv package * feat(kv): add resource op logs * feat(kv): first pass at user resource mapping migration * feat(kv): add urm usage to bucket and org services * feat(kv): first pass at kv authz service * feat(kv): add cascading auth delete for users * feat(kv): first pass d authorizer.OrganizationService in kv * feat(cmd/influxd/launcher): user kv services where appropriate * feat(kv): add initial basic auth service * refactor(passwords): move auth interface into own file * refactor(passwords): rename basic auth files to passwords * fix(http): s/platform/influxdb/ for user service * fix(kv): initialize authorizations * fix(influxdb): use same buckets while slowly migrating stuff * fix(kv): make staticcheck pass * feat(kv): add dashboards to kv review: make suggestions from pr review fix: use common bucket names for bolt/kv stores * feat(kv): implement labels generically on kv * refactor(passwords): rename from BasicAuth to Passwords * test(kv): add complete password test coverage * chore(kv): fixes for staticcheck * feat(kv): implement macro service * feat(kv): add source service * feat(kv): add session service * feat(kv): initial port of telegraf configs to kv * feat(kv): initial port of scrapers in bolt to kv * feat(kv): add kv secret service * refactor(kv): update telegraf and urm with error messages * feat(kv): add lookup service * feat(kv): add kv onboarding service * refactor(kv): update telegraf to avoid repetition * feat(cmd/influxd): use kv lookup service * feat(kv): add telegraf to lookup service * feat(cmd/influxd): use kv telegraf service * feat(kv): update scraper error messaging * feat(cmd/influxd): add kv scraper * feat(kv): add inmem backend tests * refactor(kv): copy paste errors * refactor(kv): add code to password errors * fix(testing): update error messages for incorrect passwords * feat(http): initial support for flushing all key/values from kv store * feat(kv): rename macro to variable * feat(cmd/influxd/launcher): user kv services where appropriate * refactor(passwords): rename from BasicAuth to Passwords * feat(kv): implement macro service * test(ui): introduce cypress * test(ui): introduce first typescript test * test(ui/e2e): add ci job * chore: update gitignore to ignore test outputs * feat(inmem): in memory influxdb * test(e2e): adding pinger that checks if influxdb is alive * hackathon * hack * hack * hack * hack * Revert "feat(inmem): in memory influxdb" This reverts commit 30ddf032003e704643b07ce80df61c3299ea7295. * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * hack * chore: lint ignore node_modules * hack * hack * hack * add user and flush * hack * remove unused vars * hack * hack * ci(circle): prefix e2e artifacts * change test to testid * update cypress * moar testid * fix npm warnings * remove absolte path * chore(ci): remove /home/circleci proto mkdir hack * wip: crud resources e2e * fix(inmem): use inmem kv store services * test(dashboard): add first dashboard crud tests * hack * undo hack * fix: use response from setup for orgID * chore: wip * add convenience getByTitle function * test(e2e): ui can create orgs * test(e2e): add test for org deletion and update * test(e2e): introduce task creation test * test(e2e): create and update of buckets on org view * chore: move types to declaration file * chore: use route fixture in dashboard tests * chore(ci): hack back * test(ui): update snapshots * chore: package-lock * chore: remove macros * fix: launcher rebase issues * fix: compile errors * fix: compile errors * feat(cmd/influxdb): add explicit testing, asset-path, and store flags Co-authored-by: Andrew Watkins <watts@influxdb.com> * fix(cmd/influxd): set default HTTP handler and flags Co-authored-by: Andrew Watkins <watts@influxdb.com> * build(Makefile): add run-e2e and PHONY * feat(kv:inmem:bolt): implement user service in a kv * refactor(kv): use consistent func receiver name * feat(kv): add initial basic auth service * refactor(passwords): move auth interface into own file * refactor(passwords): rename basic auth files to passwords * refactor(passwords): rename from BasicAuth to Passwords * refactor(kv): copy bolt user test into kv Co-authored-by: Michael Desa <mjdesa@gmail.com> * feat(kv): add inmem testing to kv store * fix(kv): remove extra user index initialization * feat(kv): attempt at making errors nice * fix(http): return not found error if filter is invalid * fix(http): s/platform/influxdb/ for user service * fix(http): s/platform/influxdb/ for user service * feat(kv): initial port of telegraf configs to kv * feat(kv): initial port of scrapers in bolt to kv * feat(kv): first pass at migrating bolt org service to kv * feat(kv): first pass at bucket service * feat(kv): first pass at migrating kvlog to kv package * feat(kv): add resource op logs * feat(kv): first pass at user resource mapping migration * feat(kv): add urm usage to bucket and org services * feat(kv): first pass at kv authz service * feat(kv): add cascading auth delete for users * feat(kv): first pass d authorizer.OrganizationService in kv * feat(cmd/influxd/launcher): user kv services where appropriate * fix(kv): initialize authorizations * fix(influxdb): use same buckets while slowly migrating stuff * fix(kv): make staticcheck pass * feat(kv): add dashboards to kv review: make suggestions from pr review fix: use common bucket names for bolt/kv stores * test(kv): add complete password test coverage * chore(kv): fixes for staticcheck * feat(kv): implement labels generically on kv * feat(kv): implement macro service * feat(kv): add source service * feat(kv): add session service * feat(kv): add kv secret service * refactor(kv): update telegraf and urm with error messages * feat(kv): add lookup service * feat(kv): add kv onboarding service * refactor(kv): update telegraf to avoid repetition * feat(cmd/influxd): use kv lookup service * feat(kv): add telegraf to lookup service * feat(cmd/influxd): use kv telegraf service * feat(kv): update scraper error messaging * feat(cmd/influxd): add kv scraper * feat(kv): add inmem backend tests * refactor(kv): copy paste errors * refactor(kv): add code to password errors * fix(testing): update error messages for incorrect passwords * feat(kv): rename macro to variable * refactor(kv): auth/bucket/org/user unique checks return errors now * feat(inmem): add way to get all bucket names from store * feat(inmem): Buckets to return slice of bytes rather than strings * feat(inmem): add locks around Buckets to avoid races * feat(cmd/influx): check for unauthorized error in wrapCheckSetup * chore(e2e): add video and screenshot artifcats to gitignore * docs(ci): add build instructions for e2e tests * feat(kv): add id lookup for authorized resources
2019-02-19 23:47:19 +00:00
Msg: "provided bucket ID has invalid format",
Op: s.OpPrefix + influxdb.OpAddTarget,
2019-01-10 17:39:37 +00:00
}
}
2018-09-07 15:45:28 +00:00
octets, err := json.Marshal(target)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url.String(), bytes.NewReader(octets))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
SetToken(s.Token, req)
hc := NewClient(url.Scheme, s.InsecureSkipVerify)
2018-09-07 15:45:28 +00:00
resp, err := hc.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
2018-09-07 15:45:28 +00:00
// TODO(jsternberg): Should this check for a 201 explicitly?
2019-01-24 01:02:37 +00:00
if err := CheckError(resp); err != nil {
2018-09-07 15:45:28 +00:00
return err
}
targetResp := new(targetResponse)
if err := json.NewDecoder(resp.Body).Decode(targetResp); err != nil {
return err
}
return nil
}
// RemoveTarget removes a scraper target by ID.
func (s *ScraperService) RemoveTarget(ctx context.Context, id influxdb.ID) error {
url, err := NewURL(s.Addr, targetIDPath(id))
2018-09-07 15:45:28 +00:00
if err != nil {
return err
}
req, err := http.NewRequest("DELETE", url.String(), nil)
if err != nil {
return err
}
SetToken(s.Token, req)
hc := NewClient(url.Scheme, s.InsecureSkipVerify)
2018-09-07 15:45:28 +00:00
resp, err := hc.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
2018-10-29 19:10:33 +00:00
2019-01-24 01:02:37 +00:00
return CheckErrorStatus(http.StatusNoContent, resp)
2018-09-07 15:45:28 +00:00
}
// GetTargetByID returns a single target by ID.
func (s *ScraperService) GetTargetByID(ctx context.Context, id influxdb.ID) (*influxdb.ScraperTarget, error) {
url, err := NewURL(s.Addr, targetIDPath(id))
2018-09-07 15:45:28 +00:00
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
SetToken(s.Token, req)
hc := NewClient(url.Scheme, s.InsecureSkipVerify)
2018-09-07 15:45:28 +00:00
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
2018-09-07 15:45:28 +00:00
2019-01-24 01:02:37 +00:00
if err := CheckError(resp); err != nil {
2018-09-07 15:45:28 +00:00
return nil, err
}
var targetResp targetResponse
if err := json.NewDecoder(resp.Body).Decode(&targetResp); err != nil {
return nil, err
}
return &targetResp.ScraperTarget, nil
}
func targetIDPath(id influxdb.ID) string {
2019-01-18 21:06:32 +00:00
return path.Join(targetsPath, id.String())
2018-09-07 15:45:28 +00:00
}
type getTargetsLinks struct {
Self string `json:"self"`
}
type getTargetsResponse struct {
Links getTargetsLinks `json:"links"`
Targets []targetResponse `json:"configurations"`
2018-09-07 15:45:28 +00:00
}
type targetLinks struct {
Self string `json:"self"`
Bucket string `json:"bucket,omitempty"`
Organization string `json:"organization,omitempty"`
2019-02-26 17:08:30 +00:00
Members string `json:"members"`
Owners string `json:"owners"`
2018-09-07 15:45:28 +00:00
}
type targetResponse struct {
influxdb.ScraperTarget
2019-04-10 19:21:49 +00:00
Org string `json:"org,omitempty"`
Bucket string `json:"bucket,omitempty"`
Links targetLinks `json:"links"`
2018-09-07 15:45:28 +00:00
}
func (h *ScraperHandler) newListTargetsResponse(ctx context.Context, targets []influxdb.ScraperTarget) (getTargetsResponse, error) {
2018-09-07 15:45:28 +00:00
res := getTargetsResponse{
Links: getTargetsLinks{
2019-01-18 21:06:32 +00:00
Self: targetsPath,
2018-09-07 15:45:28 +00:00
},
Targets: make([]targetResponse, 0, len(targets)),
}
for _, target := range targets {
resp, err := h.newTargetResponse(ctx, target)
if err != nil {
return res, err
}
res.Targets = append(res.Targets, resp)
2018-09-07 15:45:28 +00:00
}
return res, nil
2018-09-07 15:45:28 +00:00
}
func (h *ScraperHandler) newTargetResponse(ctx context.Context, target influxdb.ScraperTarget) (targetResponse, error) {
res := targetResponse{
2018-09-07 15:45:28 +00:00
Links: targetLinks{
2019-02-26 17:08:30 +00:00
Self: targetIDPath(target.ID),
Members: fmt.Sprintf("/api/v2/scrapers/%s/members", target.ID),
Owners: fmt.Sprintf("/api/v2/scrapers/%s/owners", target.ID),
2018-09-07 15:45:28 +00:00
},
ScraperTarget: target,
}
bucket, err := h.BucketService.FindBucketByID(ctx, target.BucketID)
if err == nil {
res.Bucket = bucket.Name
res.BucketID = bucket.ID
res.Links.Bucket = bucketIDPath(bucket.ID)
} else {
res.BucketID = influxdb.InvalidID()
}
org, err := h.OrganizationService.FindOrganizationByID(ctx, target.OrgID)
if err == nil {
2019-04-10 19:21:49 +00:00
res.Org = org.Name
res.OrgID = org.ID
res.Links.Organization = organizationIDPath(org.ID)
} else {
res.OrgID = influxdb.InvalidID()
}
return res, nil
2018-09-07 15:45:28 +00:00
}