2019-01-08 00:37:16 +00:00
|
|
|
package influxdb
|
2018-12-03 16:07:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
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-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.
|
|
|
|
FindLabelByID(ctx context.Context, id ID) (*Label, error)
|
|
|
|
|
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-01-18 19:03:36 +00:00
|
|
|
// CreateLabel maps a resource to an existing label
|
|
|
|
CreateLabelMapping(ctx context.Context, m *LabelMapping) error
|
|
|
|
|
2018-12-18 06:30:41 +00:00
|
|
|
// UpdateLabel updates a label with a changeset.
|
2019-01-18 19:03:36 +00:00
|
|
|
UpdateLabel(ctx context.Context, id 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
|
2019-01-18 19:03:36 +00:00
|
|
|
DeleteLabel(ctx context.Context, id ID) error
|
|
|
|
|
|
|
|
// 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 {
|
2019-03-19 06:58:42 +00:00
|
|
|
ID ID `json:"id,omitempty"`
|
|
|
|
OrganizationID ID `json:"orgID,omitempty"`
|
|
|
|
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 == "" {
|
2018-12-18 16:36:18 +00:00
|
|
|
return &Error{
|
|
|
|
Code: 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-03-19 08:56:55 +00:00
|
|
|
if !l.OrganizationID.Valid() {
|
2019-03-19 06:58:42 +00:00
|
|
|
return &Error{
|
|
|
|
Code: EInvalid,
|
|
|
|
Msg: "organization ID is required",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-01-24 15:44:31 +00:00
|
|
|
LabelID ID `json:"labelID"`
|
2019-03-29 15:52:06 +00:00
|
|
|
ResourceID 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() {
|
|
|
|
return &Error{
|
|
|
|
Code: EInvalid,
|
|
|
|
Msg: "label id is required",
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 19:03:36 +00:00
|
|
|
if !l.ResourceID.Valid() {
|
2018-12-18 16:36:18 +00:00
|
|
|
return &Error{
|
|
|
|
Code: 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 {
|
|
|
|
return &Error{
|
|
|
|
Code: EInvalid,
|
|
|
|
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
|
|
|
|
OrgID *ID
|
2019-01-18 19:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LabelMappingFilter represents a set of filters that restrict the returned results.
|
|
|
|
type LabelMappingFilter struct {
|
2018-12-03 16:07:08 +00:00
|
|
|
ResourceID ID
|
2019-01-23 17:38:42 +00:00
|
|
|
ResourceType
|
2018-12-03 16:07:08 +00:00
|
|
|
}
|