2019-01-08 00:37:16 +00:00
|
|
|
package influxdb
|
2018-12-03 16:07:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-30 18:10:02 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform"
|
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform/errors"
|
2018-12-03 16:07:08 +00:00
|
|
|
)
|
|
|
|
|
2018-12-07 18:25:11 +00:00
|
|
|
// ErrLabelNotFound is the error for a missing Label.
|
2019-04-11 15:17:05 +00:00
|
|
|
const ErrLabelNotFound = "label not found"
|
2018-12-07 18:25:11 +00:00
|
|
|
|
2018-12-18 07:59:04 +00:00
|
|
|
const (
|
2019-01-18 19:03:36 +00:00
|
|
|
OpFindLabels = "FindLabels"
|
|
|
|
OpFindLabelByID = "FindLabelByID"
|
|
|
|
OpFindLabelMapping = "FindLabelMapping"
|
|
|
|
OpCreateLabel = "CreateLabel"
|
|
|
|
OpCreateLabelMapping = "CreateLabelMapping"
|
|
|
|
OpUpdateLabel = "UpdateLabel"
|
|
|
|
OpDeleteLabel = "DeleteLabel"
|
|
|
|
OpDeleteLabelMapping = "DeleteLabelMapping"
|
2018-12-18 07:59:04 +00:00
|
|
|
)
|
|
|
|
|
2019-10-31 16:30:36 +00:00
|
|
|
// errors on label
|
|
|
|
var (
|
|
|
|
// ErrLabelNameisEmpty is error when org name is empty
|
2021-03-30 18:10:02 +00:00
|
|
|
ErrLabelNameisEmpty = &errors.Error{
|
|
|
|
Code: errors.EInvalid,
|
2019-10-31 16:30:36 +00:00
|
|
|
Msg: "label name is empty",
|
|
|
|
}
|
2020-04-16 16:02:50 +00:00
|
|
|
|
|
|
|
// ErrLabelExistsOnResource is used when attempting to add a label to a resource
|
|
|
|
// when that label already exists on the resource
|
2021-03-30 18:10:02 +00:00
|
|
|
ErrLabelExistsOnResource = &errors.Error{
|
|
|
|
Code: errors.EConflict,
|
2020-04-16 16:02:50 +00:00
|
|
|
Msg: "Cannot add label, label already exists on resource",
|
|
|
|
}
|
2019-10-31 16:30:36 +00:00
|
|
|
)
|
|
|
|
|
2019-01-18 19:03:36 +00:00
|
|
|
// LabelService represents a service for managing resource labels
|
2018-12-03 16:07:08 +00:00
|
|
|
type LabelService interface {
|
2019-01-18 19:03:36 +00:00
|
|
|
// FindLabelByID a single label by ID.
|
2021-03-30 18:10:02 +00:00
|
|
|
FindLabelByID(ctx context.Context, id platform.ID) (*Label, error)
|
2019-01-18 19:03:36 +00:00
|
|
|
|
2018-12-03 16:07:08 +00:00
|
|
|
// FindLabels returns a list of labels that match a filter
|
|
|
|
FindLabels(ctx context.Context, filter LabelFilter, opt ...FindOptions) ([]*Label, error)
|
|
|
|
|
2019-01-18 19:03:36 +00:00
|
|
|
// FindResourceLabels returns a list of labels that belong to a resource
|
|
|
|
FindResourceLabels(ctx context.Context, filter LabelMappingFilter) ([]*Label, error)
|
|
|
|
|
2018-12-03 16:07:08 +00:00
|
|
|
// CreateLabel creates a new label
|
|
|
|
CreateLabel(ctx context.Context, l *Label) error
|
|
|
|
|
2019-10-28 22:19:10 +00:00
|
|
|
// CreateLabelMapping maps a resource to an existing label
|
2019-01-18 19:03:36 +00:00
|
|
|
CreateLabelMapping(ctx context.Context, m *LabelMapping) error
|
|
|
|
|
2018-12-18 06:30:41 +00:00
|
|
|
// UpdateLabel updates a label with a changeset.
|
2021-03-30 18:10:02 +00:00
|
|
|
UpdateLabel(ctx context.Context, id platform.ID, upd LabelUpdate) (*Label, error)
|
2018-12-17 22:13:43 +00:00
|
|
|
|
2018-12-03 16:07:08 +00:00
|
|
|
// DeleteLabel deletes a label
|
2021-03-30 18:10:02 +00:00
|
|
|
DeleteLabel(ctx context.Context, id platform.ID) error
|
2019-01-18 19:03:36 +00:00
|
|
|
|
|
|
|
// DeleteLabelMapping deletes a label mapping
|
|
|
|
DeleteLabelMapping(ctx context.Context, m *LabelMapping) error
|
2018-12-03 16:07:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 19:03:36 +00:00
|
|
|
// Label is a tag set on a resource, typically used for filtering on a UI.
|
2018-12-03 16:07:08 +00:00
|
|
|
type Label struct {
|
2021-03-30 18:10:02 +00:00
|
|
|
ID platform.ID `json:"id,omitempty"`
|
|
|
|
OrgID platform.ID `json:"orgID,omitempty"`
|
2019-04-11 05:05:17 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Properties map[string]string `json:"properties,omitempty"`
|
2018-12-03 16:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if the label is invalid.
|
|
|
|
func (l *Label) Validate() error {
|
2019-01-18 19:03:36 +00:00
|
|
|
if l.Name == "" {
|
2021-03-30 18:10:02 +00:00
|
|
|
return &errors.Error{
|
|
|
|
Code: errors.EInvalid,
|
2019-01-18 19:03:36 +00:00
|
|
|
Msg: "label name is required",
|
2018-12-18 16:36:18 +00:00
|
|
|
}
|
2018-12-03 16:07:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 05:05:17 +00:00
|
|
|
if !l.OrgID.Valid() {
|
2021-03-30 18:10:02 +00:00
|
|
|
return &errors.Error{
|
|
|
|
Code: errors.EInvalid,
|
2019-04-11 05:05:17 +00:00
|
|
|
Msg: "orgID is required",
|
2019-03-19 06:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 19:03:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LabelMapping is used to map resource to its labels.
|
|
|
|
// It should not be shared directly over the HTTP API.
|
|
|
|
type LabelMapping struct {
|
2021-03-30 18:10:02 +00:00
|
|
|
LabelID platform.ID `json:"labelID"`
|
|
|
|
ResourceID platform.ID `json:"resourceID,omitempty"`
|
2019-01-24 15:44:31 +00:00
|
|
|
ResourceType `json:"resourceType"`
|
2019-01-18 19:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if the mapping is invalid.
|
|
|
|
func (l *LabelMapping) Validate() error {
|
2019-01-24 15:44:31 +00:00
|
|
|
if !l.LabelID.Valid() {
|
2021-03-30 18:10:02 +00:00
|
|
|
return &errors.Error{
|
|
|
|
Code: errors.EInvalid,
|
2019-01-24 15:44:31 +00:00
|
|
|
Msg: "label id is required",
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 19:03:36 +00:00
|
|
|
if !l.ResourceID.Valid() {
|
2021-03-30 18:10:02 +00:00
|
|
|
return &errors.Error{
|
|
|
|
Code: errors.EInvalid,
|
2019-01-24 15:44:31 +00:00
|
|
|
Msg: "resource id is required",
|
2018-12-18 16:36:18 +00:00
|
|
|
}
|
2018-12-03 16:07:08 +00:00
|
|
|
}
|
2019-01-22 17:54:32 +00:00
|
|
|
if err := l.ResourceType.Valid(); err != nil {
|
2021-03-30 18:10:02 +00:00
|
|
|
return &errors.Error{
|
|
|
|
Code: errors.EInvalid,
|
2019-01-22 17:54:32 +00:00
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
2018-12-03 16:07:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-18 06:30:41 +00:00
|
|
|
// LabelUpdate represents a changeset for a label.
|
2019-03-13 00:28:15 +00:00
|
|
|
// Only the properties specified are updated.
|
2018-12-18 06:30:41 +00:00
|
|
|
type LabelUpdate struct {
|
2019-03-13 00:28:15 +00:00
|
|
|
Name string `json:"name,omitempty"`
|
2018-12-20 21:52:35 +00:00
|
|
|
Properties map[string]string `json:"properties,omitempty"`
|
2018-12-18 16:56:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 19:03:36 +00:00
|
|
|
// LabelFilter represents a set of filters that restrict the returned results.
|
2018-12-03 16:07:08 +00:00
|
|
|
type LabelFilter struct {
|
2019-03-19 06:58:42 +00:00
|
|
|
Name string
|
2021-03-30 18:10:02 +00:00
|
|
|
OrgID *platform.ID
|
2019-01-18 19:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LabelMappingFilter represents a set of filters that restrict the returned results.
|
|
|
|
type LabelMappingFilter struct {
|
2021-03-30 18:10:02 +00:00
|
|
|
ResourceID platform.ID
|
2019-01-23 17:38:42 +00:00
|
|
|
ResourceType
|
2018-12-03 16:07:08 +00:00
|
|
|
}
|