-
Notifications
You must be signed in to change notification settings - Fork 309
/
registry.go
220 lines (196 loc) · 6.46 KB
/
registry.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redis
import (
"context"
"time"
"github.com/go-redis/redis/v8"
"github.com/gogo/protobuf/proto"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
ttnredis "go.thethings.network/lorawan-stack/v3/pkg/redis"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/unique"
)
var (
errInvalidFieldmask = errors.DefineInvalidArgument("invalid_fieldmask", "invalid fieldmask")
errInvalidIdentifiers = errors.DefineInvalidArgument("invalid_identifiers", "invalid identifiers")
errReadOnlyField = errors.DefineInvalidArgument("read_only_field", "read-only field `{field}`")
)
// appendImplicitWebhookGetPaths appends implicit ttnpb.ApplicationWebhook get paths to paths.
func appendImplicitWebhookGetPaths(paths ...string) []string {
return append(append(make([]string, 0, 3+len(paths)),
"created_at",
"ids",
"updated_at",
), paths...)
}
func applyWebhookFieldMask(dst, src *ttnpb.ApplicationWebhook, paths ...string) (*ttnpb.ApplicationWebhook, error) {
if dst == nil {
dst = &ttnpb.ApplicationWebhook{}
}
return dst, dst.SetFields(src, paths...)
}
// WebhookRegistry is a Redis webhook registry.
type WebhookRegistry struct {
Redis *ttnredis.Client
}
func (r *WebhookRegistry) appKey(uid string) string {
return r.Redis.Key("uid", uid)
}
func (r *WebhookRegistry) idKey(appUID, id string) string {
return r.Redis.Key("uid", appUID, id)
}
func (r *WebhookRegistry) makeIDKeyFunc(appUID string) func(id string) string {
return func(id string) string {
return r.idKey(appUID, id)
}
}
// Get implements WebhookRegistry.
func (r WebhookRegistry) Get(ctx context.Context, ids ttnpb.ApplicationWebhookIdentifiers, paths []string) (*ttnpb.ApplicationWebhook, error) {
pb := &ttnpb.ApplicationWebhook{}
if err := ttnredis.GetProto(ctx, r.Redis, r.idKey(unique.ID(ctx, ids.ApplicationIdentifiers), ids.WebhookId)).ScanProto(pb); err != nil {
return nil, err
}
return applyWebhookFieldMask(nil, pb, appendImplicitWebhookGetPaths(paths...)...)
}
// List implements WebhookRegistry.
func (r WebhookRegistry) List(ctx context.Context, ids ttnpb.ApplicationIdentifiers, paths []string) ([]*ttnpb.ApplicationWebhook, error) {
var pbs []*ttnpb.ApplicationWebhook
appUID := unique.ID(ctx, ids)
err := ttnredis.FindProtos(ctx, r.Redis, r.appKey(appUID), r.makeIDKeyFunc(appUID)).Range(func() (proto.Message, func() (bool, error)) {
pb := &ttnpb.ApplicationWebhook{}
return pb, func() (bool, error) {
pb, err := applyWebhookFieldMask(nil, pb, appendImplicitWebhookGetPaths(paths...)...)
if err != nil {
return false, err
}
pbs = append(pbs, pb)
return true, nil
}
})
if err != nil {
return nil, err
}
return pbs, nil
}
// Set implements WebhookRegistry.
func (r WebhookRegistry) Set(ctx context.Context, ids ttnpb.ApplicationWebhookIdentifiers, gets []string, f func(*ttnpb.ApplicationWebhook) (*ttnpb.ApplicationWebhook, []string, error)) (*ttnpb.ApplicationWebhook, error) {
appUID := unique.ID(ctx, ids.ApplicationIdentifiers)
ik := r.idKey(appUID, ids.WebhookId)
var pb *ttnpb.ApplicationWebhook
err := r.Redis.Watch(ctx, func(tx *redis.Tx) error {
cmd := ttnredis.GetProto(ctx, tx, ik)
stored := &ttnpb.ApplicationWebhook{}
if err := cmd.ScanProto(stored); errors.IsNotFound(err) {
stored = nil
} else if err != nil {
return err
}
gets = appendImplicitWebhookGetPaths(gets...)
var err error
if stored != nil {
pb = &ttnpb.ApplicationWebhook{}
if err := cmd.ScanProto(pb); err != nil {
return err
}
pb, err = applyWebhookFieldMask(nil, pb, gets...)
if err != nil {
return err
}
}
var sets []string
pb, sets, err = f(pb)
if err != nil {
return err
}
if stored == nil && pb == nil {
return nil
}
if pb != nil && len(sets) == 0 {
pb, err = applyWebhookFieldMask(nil, stored, gets...)
return err
}
var pipelined func(redis.Pipeliner) error
if pb == nil && len(sets) == 0 {
pipelined = func(p redis.Pipeliner) error {
p.Del(ctx, ik)
p.SRem(ctx, r.appKey(appUID), stored.WebhookId)
return nil
}
} else {
if pb == nil {
pb = &ttnpb.ApplicationWebhook{}
}
pb.UpdatedAt = time.Now().UTC()
sets = append(append(sets[:0:0], sets...),
"updated_at",
)
updated := &ttnpb.ApplicationWebhook{}
if stored == nil {
if err := ttnpb.RequireFields(sets,
"ids.application_ids",
"ids.webhook_id",
); err != nil {
return errInvalidFieldmask.WithCause(err)
}
pb.CreatedAt = pb.UpdatedAt
sets = append(sets, "created_at")
updated, err = applyWebhookFieldMask(updated, pb, sets...)
if err != nil {
return err
}
if updated.ApplicationId != ids.ApplicationId || updated.WebhookId != ids.WebhookId {
return errInvalidIdentifiers.New()
}
} else {
if ttnpb.HasAnyField(sets, "ids.application_ids.application_id") && pb.ApplicationId != stored.ApplicationId {
return errReadOnlyField.WithAttributes("field", "ids.application_ids.application_id")
}
if ttnpb.HasAnyField(sets, "ids.webhook_id") && pb.WebhookId != stored.WebhookId {
return errReadOnlyField.WithAttributes("field", "ids.webhook_id")
}
if err := cmd.ScanProto(updated); err != nil {
return err
}
updated, err = applyWebhookFieldMask(updated, pb, sets...)
if err != nil {
return err
}
}
if err := updated.ValidateFields(sets...); err != nil {
return err
}
pipelined = func(p redis.Pipeliner) error {
if _, err := ttnredis.SetProto(ctx, p, ik, updated, 0); err != nil {
return err
}
p.SAdd(ctx, r.appKey(appUID), updated.WebhookId)
return nil
}
pb, err = applyWebhookFieldMask(nil, updated, gets...)
if err != nil {
return err
}
}
_, err = tx.TxPipelined(ctx, pipelined)
if err != nil {
return err
}
return nil
}, ik)
if err != nil {
return nil, ttnredis.ConvertError(err)
}
return pb, nil
}