forked from google/trillian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quota_server.go
240 lines (217 loc) · 7.02 KB
/
quota_server.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
// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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 quotaapi provides a Quota admin server implementation.
package quotaapi
import (
"context"
"github.com/coreos/etcd/clientv3"
"github.com/golang/glog"
"github.com/golang/protobuf/ptypes/empty"
"github.com/google/trillian/quota/etcd/quotapb"
"github.com/google/trillian/quota/etcd/storage"
"github.com/google/trillian/quota/etcd/storagepb"
"google.golang.org/genproto/protobuf/field_mask"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Server is a quotapb.QuotaServer implementation backed by etcd.
type Server struct {
qs *storage.QuotaStorage
}
// NewServer returns a new Server instance backed by client.
func NewServer(client *clientv3.Client) *Server {
return &Server{qs: &storage.QuotaStorage{Client: client}}
}
// CreateConfig implements quotapb.QuotaServer.CreateConfig.
func (s *Server) CreateConfig(ctx context.Context, req *quotapb.CreateConfigRequest) (*quotapb.Config, error) {
if req.Config == nil {
return nil, status.Errorf(codes.InvalidArgument, "config is required")
}
if err := validateName(req.Name); err != nil {
return nil, err
}
req.Config.Name = req.Name
var alreadyExists bool
updated, err := s.qs.UpdateConfigs(ctx, false /* reset */, func(cfgs *storagepb.Configs) {
if _, alreadyExists = findByName(req.Name, cfgs); alreadyExists {
return
}
cfgs.Configs = append(cfgs.Configs, convertToStorage(req.Config))
})
switch {
case alreadyExists:
return nil, status.Errorf(codes.AlreadyExists, "%q already exists", req.Name)
case err != nil:
return nil, err
}
return s.getConfig(ctx, req.Name, updated, codes.Internal)
}
// DeleteConfig implements quotapb.QuotaServer.DeleteConfig.
func (s *Server) DeleteConfig(ctx context.Context, req *quotapb.DeleteConfigRequest) (*empty.Empty, error) {
if err := validateName(req.Name); err != nil {
return nil, err
}
notFound := false
_, err := s.qs.UpdateConfigs(ctx, false /* reset */, func(cfgs *storagepb.Configs) {
for i, cfg := range cfgs.Configs {
if cfg.Name == req.Name {
cfgs.Configs = append(cfgs.Configs[:i], cfgs.Configs[i+1:]...)
return
}
}
notFound = true
})
if notFound {
return nil, status.Errorf(codes.NotFound, "%q not found", req.Name)
}
return &empty.Empty{}, err
}
// GetConfig implements quotapb.QuotaServer.GetConfig.
func (s *Server) GetConfig(ctx context.Context, req *quotapb.GetConfigRequest) (*quotapb.Config, error) {
if err := validateName(req.Name); err != nil {
return nil, err
}
cfgs, err := s.qs.Configs(ctx)
if err != nil {
return nil, err
}
return s.getConfig(ctx, req.Name, cfgs, codes.NotFound)
}
// getConfig finds the Config named "name" on "cfgs", converts it to API and returns it.
// If the config cannot be found an error with code "code" is returned.
func (s *Server) getConfig(ctx context.Context, name string, cfgs *storagepb.Configs, code codes.Code) (*quotapb.Config, error) {
storedCfg, ok := findByName(name, cfgs)
if !ok {
return nil, status.Errorf(code, "%q not found", name)
}
cfg := convertToAPI(storedCfg)
tokens, err := s.qs.Peek(ctx, []string{cfg.Name})
if err == nil {
cfg.CurrentTokens = tokens[cfg.Name]
} else {
glog.Warningf("Unexpected error peeking token count for %q: %v", cfg.Name, err)
}
return cfg, nil
}
// ListConfigs implements quotapb.QuotaServer.ListConfigs.
func (s *Server) ListConfigs(ctx context.Context, req *quotapb.ListConfigsRequest) (*quotapb.ListConfigsResponse, error) {
nfs := make([]nameFilter, 0, len(req.Names))
for _, name := range req.Names {
nf, err := newNameFilter(name)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
nfs = append(nfs, nf)
}
cfgs, err := s.qs.Configs(ctx)
if err != nil {
return nil, err
}
resp := "apb.ListConfigsResponse{}
for _, cfg := range cfgs.Configs {
if listMatches(nfs, cfg) {
resp.Configs = append(resp.Configs, listView(req.View, cfg))
}
}
// Peek token counts for FULL view
if req.View == quotapb.ListConfigsRequest_FULL {
names := make([]string, 0, len(resp.Configs))
for _, cfg := range resp.Configs {
names = append(names, cfg.Name)
}
tokens, err := s.qs.Peek(ctx, names)
if err == nil {
for _, cfg := range resp.Configs {
cfg.CurrentTokens = tokens[cfg.Name]
}
} else {
glog.Infof("Error peeking token counts for %v: %v", names, err)
}
}
return resp, nil
}
func listMatches(nfs []nameFilter, cfg *storagepb.Config) bool {
if len(nfs) == 0 {
return true // Match all
}
for _, nf := range nfs {
if nf.matches(cfg.Name) {
return true
}
}
return false
}
func listView(view quotapb.ListConfigsRequest_ListView, src *storagepb.Config) *quotapb.Config {
switch view {
case quotapb.ListConfigsRequest_BASIC:
return "apb.Config{Name: src.Name}
default:
return convertToAPI(src)
}
}
// UpdateConfig implements quotapb.QuotaServer.UpdateConfig.
func (s *Server) UpdateConfig(ctx context.Context, req *quotapb.UpdateConfigRequest) (*quotapb.Config, error) {
cfg, mask := req.Config, req.UpdateMask
hasConfig := cfg != nil
hasMask := mask != nil && len(mask.Paths) > 0
switch {
case req.ResetQuota && !hasConfig && !hasMask:
// For convenience, reset-only requests are allowed.
cfg = "apb.Config{}
mask = &field_mask.FieldMask{}
case !hasConfig:
return nil, status.Errorf(codes.InvalidArgument, "config must be specified")
case !hasMask:
return nil, status.Errorf(codes.InvalidArgument, "update_mask must be specified")
}
if err := validateName(req.Name); err != nil {
return nil, err
}
if err := validateMask(mask); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid update_mask: %v", err)
}
var notFound bool
updated, err := s.qs.UpdateConfigs(ctx, req.ResetQuota, func(cfgs *storagepb.Configs) {
existingCfg, ok := findByName(req.Name, cfgs)
if !ok {
notFound = true
return
}
applyMask(cfg, existingCfg, mask)
})
switch {
case notFound:
return nil, status.Errorf(codes.NotFound, "%q not found", req.Name)
case err != nil:
return nil, err
}
return s.getConfig(ctx, req.Name, updated, codes.Internal)
}
func findByName(name string, cfgs *storagepb.Configs) (*storagepb.Config, bool) {
for _, cfg := range cfgs.Configs {
if cfg.Name == name {
return cfg, true
}
}
return nil, false
}
func validateName(name string) error {
switch {
case name == "":
return status.Errorf(codes.InvalidArgument, "name is required")
case !storage.IsNameValid(name):
return status.Errorf(codes.InvalidArgument, "invalid name: %q", name)
}
return nil
}