forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvariable.go
161 lines (137 loc) · 3.68 KB
/
variable.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
package inmem
import (
"context"
"fmt"
platform "github.com/influxdata/influxdb"
)
func (s *Service) loadVariable(ctx context.Context, id platform.ID) (*platform.Variable, *platform.Error) {
r, ok := s.variableKV.Load(id.String())
if !ok {
return nil, &platform.Error{
Code: platform.ENotFound,
Msg: platform.ErrVariableNotFound,
}
}
m, ok := r.(*platform.Variable)
if !ok {
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: fmt.Sprintf("type %T is not a variable", r),
}
}
return m, nil
}
// FindVariableByID implements the platform.VariableService interface
func (s *Service) FindVariableByID(ctx context.Context, id platform.ID) (*platform.Variable, error) {
m, pe := s.loadVariable(ctx, id)
if pe != nil {
return nil, &platform.Error{
Err: pe,
Op: OpPrefix + platform.OpFindVariableByID,
}
}
return m, nil
}
func filterVariablesFn(filter platform.VariableFilter) func(m *platform.Variable) bool {
if filter.ID != nil {
return func(m *platform.Variable) bool {
return m.ID == *filter.ID
}
}
if filter.OrganizationID != nil {
return func(m *platform.Variable) bool {
return m.OrganizationID == *filter.OrganizationID
}
}
return func(m *platform.Variable) bool { return true }
}
// FindVariables implements the platform.VariableService interface
func (s *Service) FindVariables(ctx context.Context, filter platform.VariableFilter, opt ...platform.FindOptions) ([]*platform.Variable, error) {
op := OpPrefix + platform.OpFindVariables
var variables []*platform.Variable
if filter.ID != nil {
m, err := s.FindVariableByID(ctx, *filter.ID)
if err != nil && platform.ErrorCode(err) != platform.ENotFound {
return variables, &platform.Error{
Err: err,
Op: op,
}
}
if m == nil {
return variables, nil
}
return []*platform.Variable{m}, nil
}
filterFn := filterVariablesFn(filter)
s.variableKV.Range(func(k, v interface{}) bool {
variable, ok := v.(*platform.Variable)
if !ok {
return false
}
if filterFn(variable) {
variables = append(variables, variable)
}
return true
})
return variables, nil
}
// CreateVariable implements the platform.VariableService interface
func (s *Service) CreateVariable(ctx context.Context, m *platform.Variable) error {
op := OpPrefix + platform.OpCreateVariable
m.ID = s.IDGenerator.ID()
err := s.ReplaceVariable(ctx, m)
now := s.Now()
m.CreatedAt = now
m.UpdatedAt = now
if err != nil {
return &platform.Error{
Op: op,
Err: err,
}
}
return nil
}
// UpdateVariable implements the platform.VariableService interface
func (s *Service) UpdateVariable(ctx context.Context, id platform.ID, update *platform.VariableUpdate) (*platform.Variable, error) {
op := OpPrefix + platform.OpUpdateVariable
variable, err := s.FindVariableByID(ctx, id)
if err != nil {
return nil, &platform.Error{
Op: op,
Err: err,
}
}
now := s.Now()
variable.UpdatedAt = now
if err := update.Apply(variable); err != nil {
return nil, &platform.Error{
Op: op,
Err: err,
}
}
if err := s.ReplaceVariable(ctx, variable); err != nil {
return nil, &platform.Error{
Op: op,
Err: err,
}
}
return variable, nil
}
// DeleteVariable implements the platform.VariableService interface
func (s *Service) DeleteVariable(ctx context.Context, id platform.ID) error {
op := OpPrefix + platform.OpDeleteVariable
_, err := s.FindVariableByID(ctx, id)
if err != nil {
return &platform.Error{
Op: op,
Err: err,
}
}
s.variableKV.Delete(id.String())
return nil
}
// ReplaceVariable stores a Variable in the key value store
func (s *Service) ReplaceVariable(ctx context.Context, m *platform.Variable) error {
s.variableKV.Store(m.ID.String(), m)
return nil
}