2020-05-21 18:30:19 +00:00
|
|
|
package label
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path"
|
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform"
|
|
|
|
|
2020-05-21 18:30:19 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/pkg/httpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ influxdb.LabelService = (*LabelClientService)(nil)
|
|
|
|
|
|
|
|
type LabelClientService struct {
|
|
|
|
Client *httpc.Client
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
func labelIDPath(id platform.ID) string {
|
2020-05-21 18:30:19 +00:00
|
|
|
return path.Join(prefixLabels, id.String())
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
func resourceIDPath(resourceType influxdb.ResourceType, resourceID platform.ID, p string) string {
|
2020-05-21 18:30:19 +00:00
|
|
|
return path.Join("/api/v2/", string(resourceType), resourceID.String(), p)
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
func resourceIDMappingPath(resourceType influxdb.ResourceType, resourceID platform.ID, p string, labelID platform.ID) string {
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
return path.Join("/api/v2/", string(resourceType), resourceID.String(), p, labelID.String())
|
|
|
|
}
|
|
|
|
|
2020-05-21 18:30:19 +00:00
|
|
|
// CreateLabel creates a new label.
|
|
|
|
func (s *LabelClientService) CreateLabel(ctx context.Context, l *influxdb.Label) error {
|
|
|
|
var lr labelResponse
|
|
|
|
err := s.Client.
|
|
|
|
PostJSON(l, prefixLabels).
|
|
|
|
DecodeJSON(&lr).
|
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*l = lr.Label
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindLabelByID returns a single label by ID.
|
2021-03-30 18:10:02 +00:00
|
|
|
func (s *LabelClientService) FindLabelByID(ctx context.Context, id platform.ID) (*influxdb.Label, error) {
|
2020-05-21 18:30:19 +00:00
|
|
|
var lr labelResponse
|
|
|
|
err := s.Client.
|
|
|
|
Get(labelIDPath(id)).
|
|
|
|
DecodeJSON(&lr).
|
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &lr.Label, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindLabels is a client for the find labels response from the server.
|
|
|
|
func (s *LabelClientService) FindLabels(ctx context.Context, filter influxdb.LabelFilter, opt ...influxdb.FindOptions) ([]*influxdb.Label, error) {
|
|
|
|
params := influxdb.FindOptionParams(opt...)
|
|
|
|
if filter.OrgID != nil {
|
|
|
|
params = append(params, [2]string{"orgID", filter.OrgID.String()})
|
|
|
|
}
|
|
|
|
if filter.Name != "" {
|
|
|
|
params = append(params, [2]string{"name", filter.Name})
|
|
|
|
}
|
|
|
|
|
|
|
|
var lr labelsResponse
|
|
|
|
err := s.Client.
|
|
|
|
Get(prefixLabels).
|
|
|
|
QueryParams(params...).
|
|
|
|
DecodeJSON(&lr).
|
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return lr.Labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindResourceLabels returns a list of labels, derived from a label mapping filter.
|
|
|
|
func (s *LabelClientService) FindResourceLabels(ctx context.Context, filter influxdb.LabelMappingFilter) ([]*influxdb.Label, error) {
|
|
|
|
if err := filter.Valid(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var r labelsResponse
|
|
|
|
err := s.Client.
|
|
|
|
Get(resourceIDPath(filter.ResourceType, filter.ResourceID, "labels")).
|
|
|
|
DecodeJSON(&r).
|
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r.Labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateLabel updates a label and returns the updated label.
|
2021-03-30 18:10:02 +00:00
|
|
|
func (s *LabelClientService) UpdateLabel(ctx context.Context, id platform.ID, upd influxdb.LabelUpdate) (*influxdb.Label, error) {
|
2020-05-21 18:30:19 +00:00
|
|
|
var lr labelResponse
|
|
|
|
err := s.Client.
|
|
|
|
PatchJSON(upd, labelIDPath(id)).
|
|
|
|
DecodeJSON(&lr).
|
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &lr.Label, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteLabel removes a label by ID.
|
2021-03-30 18:10:02 +00:00
|
|
|
func (s *LabelClientService) DeleteLabel(ctx context.Context, id platform.ID) error {
|
2020-05-21 18:30:19 +00:00
|
|
|
return s.Client.
|
|
|
|
Delete(labelIDPath(id)).
|
|
|
|
Do(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ******* Label Mappings ******* //
|
|
|
|
|
2020-11-11 18:54:21 +00:00
|
|
|
// CreateLabelMapping will create a label mapping
|
2020-05-21 18:30:19 +00:00
|
|
|
func (s *LabelClientService) CreateLabelMapping(ctx context.Context, m *influxdb.LabelMapping) error {
|
|
|
|
if err := m.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
urlPath := resourceIDPath(m.ResourceType, m.ResourceID, "labels")
|
|
|
|
return s.Client.
|
|
|
|
PostJSON(m, urlPath).
|
|
|
|
DecodeJSON(m).
|
|
|
|
Do(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LabelClientService) DeleteLabelMapping(ctx context.Context, m *influxdb.LabelMapping) error {
|
|
|
|
if err := m.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Client.
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
Delete(resourceIDMappingPath(m.ResourceType, m.ResourceID, "labels", m.LabelID)).
|
2020-05-21 18:30:19 +00:00
|
|
|
Do(ctx)
|
|
|
|
}
|