-
Notifications
You must be signed in to change notification settings - Fork 32
/
useragent_kv.go
198 lines (168 loc) · 5.57 KB
/
useragent_kv.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
package services
import (
"context"
"fmt"
"net/http"
"github.com/gogo/protobuf/types"
"github.com/labstack/echo"
"github.com/Juniper/asf/pkg/errutil"
"github.com/Juniper/contrail/pkg/models"
)
// UserAgent key value store operations.
const (
UserAgentKVOperationStore = "STORE"
UserAgentKVOperationRetrieve = "RETRIEVE"
UserAgentKVOperationDelete = "DELETE"
)
type userAgentKVRequest map[string]interface{}
// RESTUserAgentKV is a REST handler for UserAgentKV requests.
func (svc *ContrailService) RESTUserAgentKV(c echo.Context) error {
var data userAgentKVRequest
if err := c.Bind(&data); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid JSON format")
}
if err := data.validateKVRequest(); err != nil {
return errutil.ToHTTPError(err)
}
switch op := data["operation"]; op {
case UserAgentKVOperationStore:
return svc.storeKeyValue(c, data["key"].(string), data["value"].(string))
case UserAgentKVOperationRetrieve:
if key, ok := data["key"].(string); ok && key != "" {
return svc.retrieveValue(c, key)
}
if keys, ok := data["key"].([]string); ok && len(keys) != 0 {
return svc.retrieveValues(c, keys)
}
return svc.retrieveKVPs(c)
case UserAgentKVOperationDelete:
return svc.deleteKey(c, data["key"].(string))
}
return nil
}
func (svc *ContrailService) storeKeyValue(c echo.Context, key string, value string) error {
if _, err := svc.StoreKeyValue(c.Request().Context(), &StoreKeyValueRequest{
Key: key,
Value: value,
}); err != nil {
return errutil.ToHTTPError(err)
}
return c.NoContent(http.StatusOK)
}
func (svc *ContrailService) retrieveValue(c echo.Context, key string) error {
kv, err := svc.RetrieveValues(
c.Request().Context(),
&RetrieveValuesRequest{Keys: []string{key}},
)
if err != nil {
return errutil.ToHTTPError(err)
}
if len(kv.Values) == 0 {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("No user agent key: %v", key))
}
return c.JSON(http.StatusOK, map[string]string{"value": kv.Values[0]})
}
func (svc *ContrailService) retrieveValues(c echo.Context, keys []string) error {
response, err := svc.RetrieveValues(c.Request().Context(), &RetrieveValuesRequest{Keys: keys})
if err != nil {
return errutil.ToHTTPError(err)
}
return c.JSON(http.StatusOK, response)
}
func (svc *ContrailService) retrieveKVPs(c echo.Context) error {
response, err := svc.RetrieveKVPs(c.Request().Context(), &types.Empty{})
if err != nil {
return errutil.ToHTTPError(err)
}
return c.JSON(http.StatusOK, response)
}
func (svc *ContrailService) deleteKey(c echo.Context, key string) error {
if _, err := svc.DeleteKey(c.Request().Context(), &DeleteKeyRequest{Key: key}); err != nil {
return errutil.ToHTTPError(err)
}
return c.NoContent(http.StatusOK)
}
func (data userAgentKVRequest) validateKVRequest() error {
if _, ok := data["operation"]; !ok {
return errutil.ErrorBadRequest("Key/value store API needs 'operation' parameter")
}
if _, ok := data["key"]; !ok {
return errutil.ErrorBadRequest("Key/value store API needs 'key' parameter")
}
switch op := data["operation"]; op {
case UserAgentKVOperationStore, UserAgentKVOperationDelete:
return data.validateStoreOrDeleteOperation()
case UserAgentKVOperationRetrieve:
return data.validateRetrieveOperation()
default:
return errutil.ErrorNotFoundf("Invalid Operation %v", op)
}
}
func (data userAgentKVRequest) validateRetrieveOperation() error {
errMsg := "retrieve: 'key' must be a string or a list of strings"
switch key := data["key"].(type) {
case string:
case []interface{}:
keyStrings := make([]string, 0, len(key))
for _, k := range key {
if keyString, ok := k.(string); ok {
keyStrings = append(keyStrings, keyString)
} else {
return errutil.ErrorBadRequestf(errMsg)
}
}
data["key"] = keyStrings
default:
return errutil.ErrorBadRequestf(errMsg)
}
return nil
}
func (data userAgentKVRequest) validateStoreOrDeleteOperation() error {
if key, ok := data["key"].(string); !ok {
return errutil.ErrorBadRequestf("store/delete: 'key' must be a string")
} else if key == "" {
return errutil.ErrorBadRequestf("store/delete: 'key' must be nonempty")
}
return nil
}
// StoreKeyValue stores a value under given key.
// Updates the value if key is already present.
func (svc *ContrailService) StoreKeyValue(
ctx context.Context,
request *StoreKeyValueRequest,
) (*types.Empty, error) {
return &types.Empty{}, svc.UserAgentKVService.StoreKeyValue(ctx, request.Key, request.Value)
}
// RetrieveValues retrieves values corresponding to the given list of keys.
// The values are returned in an arbitrary order. Keys not present in the store are ignored.
func (svc *ContrailService) RetrieveValues(
ctx context.Context,
request *RetrieveValuesRequest,
) (res *RetrieveValuesResponse, err error) {
var values []string
values, err = svc.UserAgentKVService.RetrieveValues(ctx, request.Keys)
if err == nil {
res = &RetrieveValuesResponse{Values: values}
}
return res, err
}
// DeleteKey deletes the value under the given key.
// Nothing happens if the key is not present.
func (svc *ContrailService) DeleteKey(
ctx context.Context,
request *DeleteKeyRequest,
) (*types.Empty, error) {
return &types.Empty{}, svc.UserAgentKVService.DeleteKey(ctx, request.Key)
}
// RetrieveKVPs returns the entire store as a list of (key, value) pairs.
func (svc *ContrailService) RetrieveKVPs(
ctx context.Context,
request *types.Empty,
) (res *RetrieveKVPsResponse, err error) {
var kvps []*models.KeyValuePair
kvps, err = svc.UserAgentKVService.RetrieveKVPs(ctx)
if err == nil {
res = &RetrieveKVPsResponse{KeyValuePairs: kvps}
}
return res, err
}