forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbrp_mapping.go
288 lines (259 loc) · 7.88 KB
/
dbrp_mapping.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package influxdb
import (
"context"
"strconv"
"strings"
"unicode"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
)
// DBRPMappingServiceV2 provides CRUD to DBRPMappingV2s.
type DBRPMappingServiceV2 interface {
// FindBy returns the dbrp mapping for the specified ID.
// Requires orgID because every resource will be org-scoped.
FindByID(ctx context.Context, orgID, id platform.ID) (*DBRPMappingV2, error)
// FindMany returns a list of dbrp mappings that match filter and the total count of matching dbrp mappings.
FindMany(ctx context.Context, dbrp DBRPMappingFilterV2, opts ...FindOptions) ([]*DBRPMappingV2, int, error)
// Create creates a new dbrp mapping, if a different mapping exists an error is returned.
Create(ctx context.Context, dbrp *DBRPMappingV2) error
// Update a new dbrp mapping
Update(ctx context.Context, dbrp *DBRPMappingV2) error
// Delete removes a dbrp mapping.
// Deleting a mapping that does not exists is not an error.
// Requires orgID because every resource will be org-scoped.
Delete(ctx context.Context, orgID, id platform.ID) error
}
// DBRPMappingV2 represents a mapping of a database and retention policy to an organization ID and bucket ID.
type DBRPMappingV2 struct {
ID platform.ID `json:"id"`
Database string `json:"database"`
RetentionPolicy string `json:"retention_policy"`
// Default indicates if this mapping is the default for the cluster and database.
Default bool `json:"default"`
OrganizationID platform.ID `json:"orgID"`
BucketID platform.ID `json:"bucketID"`
}
// Validate reports any validation errors for the mapping.
func (m DBRPMappingV2) Validate() error {
if !validName(m.Database) {
return &errors.Error{
Code: errors.EInvalid,
Msg: "database must contain at least one character and only be letters, numbers, '_', '-', and '.'",
}
}
if !validName(m.RetentionPolicy) {
return &errors.Error{
Code: errors.EInvalid,
Msg: "retentionPolicy must contain at least one character and only be letters, numbers, '_', '-', and '.'",
}
}
if !m.OrganizationID.Valid() {
return &errors.Error{
Code: errors.EInvalid,
Msg: "organizationID is required",
}
}
if !m.BucketID.Valid() {
return &errors.Error{
Code: errors.EInvalid,
Msg: "bucketID is required",
}
}
return nil
}
// Equal checks if the two mappings are identical.
func (m *DBRPMappingV2) Equal(o *DBRPMappingV2) bool {
if m == o {
return true
}
if m == nil || o == nil {
return false
}
return m.Database == o.Database &&
m.RetentionPolicy == o.RetentionPolicy &&
m.Default == o.Default &&
m.OrganizationID.Valid() &&
o.OrganizationID.Valid() &&
m.BucketID.Valid() &&
o.BucketID.Valid() &&
o.ID.Valid() &&
m.ID == o.ID &&
m.OrganizationID == o.OrganizationID &&
m.BucketID == o.BucketID
}
// DBRPMappingFilterV2 represents a set of filters that restrict the returned results.
type DBRPMappingFilterV2 struct {
ID *platform.ID
OrgID *platform.ID
BucketID *platform.ID
Database *string
RetentionPolicy *string
Default *bool
}
func (f DBRPMappingFilterV2) String() string {
var s strings.Builder
s.WriteString("{ id:")
if f.ID != nil {
s.WriteString(f.ID.String())
} else {
s.WriteString("<nil>")
}
s.WriteString(" org_id:")
if f.ID != nil {
s.WriteString(f.OrgID.String())
} else {
s.WriteString("<nil>")
}
s.WriteString(" bucket_id:")
if f.ID != nil {
s.WriteString(f.OrgID.String())
} else {
s.WriteString("<nil>")
}
s.WriteString(" db:")
if f.Database != nil {
s.WriteString(*f.Database)
} else {
s.WriteString("<nil>")
}
s.WriteString(" rp:")
if f.RetentionPolicy != nil {
s.WriteString(*f.RetentionPolicy)
} else {
s.WriteString("<nil>")
}
s.WriteString(" default:")
if f.Default != nil {
s.WriteString(strconv.FormatBool(*f.Default))
} else {
s.WriteString("<nil>")
}
s.WriteString("}")
return s.String()
}
// DBRPMappingService provides a mapping of cluster, database and retention policy to an organization ID and bucket ID.
type DBRPMappingService interface {
// FindBy returns the dbrp mapping the for cluster, db and rp.
FindBy(ctx context.Context, cluster, db, rp string) (*DBRPMapping, error)
// Find returns the first dbrp mapping the matches the filter.
Find(ctx context.Context, filter DBRPMappingFilter) (*DBRPMapping, error)
// FindMany returns a list of dbrp mappings that match filter and the total count of matching dbrp mappings.
FindMany(ctx context.Context, filter DBRPMappingFilter, opt ...FindOptions) ([]*DBRPMapping, int, error)
// Create creates a new dbrp mapping, if a different mapping exists an error is returned.
Create(ctx context.Context, dbrpMap *DBRPMapping) error
// Delete removes a dbrp mapping.
// Deleting a mapping that does not exists is not an error.
Delete(ctx context.Context, cluster, db, rp string) error
}
// DBRPMapping represents a mapping of a cluster, database and retention policy to an organization ID and bucket ID.
type DBRPMapping struct {
Cluster string `json:"cluster"`
Database string `json:"database"`
RetentionPolicy string `json:"retention_policy"`
// Default indicates if this mapping is the default for the cluster and database.
Default bool `json:"default"`
OrganizationID platform.ID `json:"organization_id"`
BucketID platform.ID `json:"bucket_id"`
}
// Validate reports any validation errors for the mapping.
func (m DBRPMapping) Validate() error {
if !validName(m.Cluster) {
return &errors.Error{
Code: errors.EInvalid,
Msg: "cluster must contain at least one character and only be letters, numbers, '_', '-', and '.'",
}
}
if !validName(m.Database) {
return &errors.Error{
Code: errors.EInvalid,
Msg: "database must contain at least one character and only be letters, numbers, '_', '-', and '.'",
}
}
if !validName(m.RetentionPolicy) {
return &errors.Error{
Code: errors.EInvalid,
Msg: "retentionPolicy must contain at least one character and only be letters, numbers, '_', '-', and '.'",
}
}
if !m.OrganizationID.Valid() {
return &errors.Error{
Code: errors.EInvalid,
Msg: "organizationID is required",
}
}
if !m.BucketID.Valid() {
return &errors.Error{
Code: errors.EInvalid,
Msg: "bucketID is required",
}
}
return nil
}
// validName checks to see if the given name can would be valid for DB/RP name
func validName(name string) bool {
for _, r := range name {
if !unicode.IsPrint(r) {
return false
}
}
return name != "" &&
name != "." &&
name != ".." &&
!strings.ContainsAny(name, `/\`)
}
// Equal checks if the two mappings are identical.
func (m *DBRPMapping) Equal(o *DBRPMapping) bool {
if m == o {
return true
}
if m == nil || o == nil {
return false
}
return m.Cluster == o.Cluster &&
m.Database == o.Database &&
m.RetentionPolicy == o.RetentionPolicy &&
m.Default == o.Default &&
m.OrganizationID.Valid() &&
o.OrganizationID.Valid() &&
m.BucketID.Valid() &&
o.BucketID.Valid() &&
m.OrganizationID == o.OrganizationID &&
m.BucketID == o.BucketID
}
// DBRPMappingFilter represents a set of filters that restrict the returned results by cluster, database and retention policy.
type DBRPMappingFilter struct {
Cluster *string
Database *string
RetentionPolicy *string
Default *bool
}
func (f DBRPMappingFilter) String() string {
var s strings.Builder
s.WriteString("{")
s.WriteString("cluster:")
if f.Cluster != nil {
s.WriteString(*f.Cluster)
} else {
s.WriteString("<nil>")
}
s.WriteString(" db:")
if f.Database != nil {
s.WriteString(*f.Database)
} else {
s.WriteString("<nil>")
}
s.WriteString(" rp:")
if f.RetentionPolicy != nil {
s.WriteString(*f.RetentionPolicy)
} else {
s.WriteString("<nil>")
}
s.WriteString(" default:")
if f.Default != nil {
s.WriteString(strconv.FormatBool(*f.Default))
} else {
s.WriteString("<nil>")
}
s.WriteString("}")
return s.String()
}