forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
143 lines (119 loc) · 3.92 KB
/
label.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package influxdb
import (
"context"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
)
// ErrLabelNotFound is the error for a missing Label.
const ErrLabelNotFound = "label not found"
const (
OpFindLabels = "FindLabels"
OpFindLabelByID = "FindLabelByID"
OpFindLabelMapping = "FindLabelMapping"
OpCreateLabel = "CreateLabel"
OpCreateLabelMapping = "CreateLabelMapping"
OpUpdateLabel = "UpdateLabel"
OpDeleteLabel = "DeleteLabel"
OpDeleteLabelMapping = "DeleteLabelMapping"
)
// errors on label
var (
// ErrLabelNameisEmpty is error when org name is empty
ErrLabelNameisEmpty = &errors.Error{
Code: errors.EInvalid,
Msg: "label name is empty",
}
// ErrLabelExistsOnResource is used when attempting to add a label to a resource
// when that label already exists on the resource
ErrLabelExistsOnResource = &errors.Error{
Code: errors.EConflict,
Msg: "Cannot add label, label already exists on resource",
}
)
// LabelService represents a service for managing resource labels
type LabelService interface {
// FindLabelByID a single label by ID.
FindLabelByID(ctx context.Context, id platform.ID) (*Label, error)
// FindLabels returns a list of labels that match a filter
FindLabels(ctx context.Context, filter LabelFilter, opt ...FindOptions) ([]*Label, error)
// FindResourceLabels returns a list of labels that belong to a resource
FindResourceLabels(ctx context.Context, filter LabelMappingFilter) ([]*Label, error)
// CreateLabel creates a new label
CreateLabel(ctx context.Context, l *Label) error
// CreateLabelMapping maps a resource to an existing label
CreateLabelMapping(ctx context.Context, m *LabelMapping) error
// UpdateLabel updates a label with a changeset.
UpdateLabel(ctx context.Context, id platform.ID, upd LabelUpdate) (*Label, error)
// DeleteLabel deletes a label
DeleteLabel(ctx context.Context, id platform.ID) error
// DeleteLabelMapping deletes a label mapping
DeleteLabelMapping(ctx context.Context, m *LabelMapping) error
}
// Label is a tag set on a resource, typically used for filtering on a UI.
type Label struct {
ID platform.ID `json:"id,omitempty"`
OrgID platform.ID `json:"orgID,omitempty"`
Name string `json:"name"`
Properties map[string]string `json:"properties,omitempty"`
}
// Validate returns an error if the label is invalid.
func (l *Label) Validate() error {
if l.Name == "" {
return &errors.Error{
Code: errors.EInvalid,
Msg: "label name is required",
}
}
if !l.OrgID.Valid() {
return &errors.Error{
Code: errors.EInvalid,
Msg: "orgID is required",
}
}
return nil
}
// LabelMapping is used to map resource to its labels.
// It should not be shared directly over the HTTP API.
type LabelMapping struct {
LabelID platform.ID `json:"labelID"`
ResourceID platform.ID `json:"resourceID,omitempty"`
ResourceType `json:"resourceType"`
}
// Validate returns an error if the mapping is invalid.
func (l *LabelMapping) Validate() error {
if !l.LabelID.Valid() {
return &errors.Error{
Code: errors.EInvalid,
Msg: "label id is required",
}
}
if !l.ResourceID.Valid() {
return &errors.Error{
Code: errors.EInvalid,
Msg: "resource id is required",
}
}
if err := l.ResourceType.Valid(); err != nil {
return &errors.Error{
Code: errors.EInvalid,
Err: err,
}
}
return nil
}
// LabelUpdate represents a changeset for a label.
// Only the properties specified are updated.
type LabelUpdate struct {
Name string `json:"name,omitempty"`
Properties map[string]string `json:"properties,omitempty"`
}
// LabelFilter represents a set of filters that restrict the returned results.
type LabelFilter struct {
Name string
OrgID *platform.ID
}
// LabelMappingFilter represents a set of filters that restrict the returned results.
type LabelMappingFilter struct {
ResourceID platform.ID
ResourceType
}