/
prop_collection_update.go
275 lines (235 loc) · 7.32 KB
/
prop_collection_update.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
package services
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
"github.com/Juniper/asf/pkg/apiserver"
"github.com/Juniper/asf/pkg/errutil"
"github.com/Juniper/asf/pkg/services"
"github.com/Juniper/contrail/pkg/models"
"github.com/gogo/protobuf/types"
"github.com/labstack/echo"
"github.com/pkg/errors"
)
// PropCollectionUpdatePath is the path and the name of the prop-collection-update endpoint.
const PropCollectionUpdatePath = "prop-collection-update"
// PropCollectionUpdatePlugin provides prop-collection-update HTTP endpoint and GRPC service.
type PropCollectionUpdatePlugin struct {
Service Service
InTransactionDoer services.InTransactionDoer
MetadataGetter services.MetadataGetter
}
// RegisterHTTPAPI registers the prop-collection-update endpoint.
func (p *PropCollectionUpdatePlugin) RegisterHTTPAPI(r apiserver.HTTPRouter) {
r.POST(PropCollectionUpdatePath, p.RESTPropCollectionUpdate)
}
// RegisterGRPCAPI registers the prop-collection-update GRPC service.
func (p *PropCollectionUpdatePlugin) RegisterGRPCAPI(r apiserver.GRPCRouter) {
r.RegisterService(&_PropCollectionUpdate_serviceDesc, p)
}
// RESTPropCollectionUpdate handles a prop-collection-update request.
func (p *PropCollectionUpdatePlugin) RESTPropCollectionUpdate(c echo.Context) error {
var data restPropCollectionUpdateRequest
if err := c.Bind(&data); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid JSON format: %v", err))
}
if err := data.validate(); err != nil {
return errutil.ToHTTPError(err)
}
if err := p.InTransactionDoer.DoInTransaction(c.Request().Context(), func(ctx context.Context) error {
obj, objType, err := GetObjectAndType(ctx, data.UUID, p.MetadataGetter, p.Service)
if err != nil {
return err
}
r, err := data.toPropCollectionUpdateRequest(obj)
if err != nil {
return errutil.ErrorBadRequestf("error resolving request: %v", err)
}
updatableObj, ok := obj.(applyPropCollectionUpdater)
if !ok {
return errors.Errorf("%T does not support prop-collection-update", obj)
}
return p.updatePropCollection(ctx, &r, updatableObj, objType)
}); err != nil {
return errutil.ToHTTPError(err)
}
return c.NoContent(http.StatusOK)
}
type restPropCollectionUpdateRequest struct {
PropCollectionUpdateRequest
Updates []struct {
Field string `json:"field"`
Operation string `json:"operation"`
Position *string `json:"position"`
Value json.RawMessage `json:"value"`
} `json:"updates"`
}
func (p *restPropCollectionUpdateRequest) validate() error {
if p.UUID == "" {
return errutil.ErrorBadRequest("prop-collection-update needs object UUID")
}
return nil
}
func (p *restPropCollectionUpdateRequest) toPropCollectionUpdateRequest(
obj interface{},
) (PropCollectionUpdateRequest, error) {
for _, u := range p.Updates {
c := PropCollectionChange{
Field: u.Field,
Operation: u.Operation,
}
if pos := u.Position; pos != nil {
if i, err := strconv.ParseInt(*pos, 10, 64); err == nil {
c.Position = &PropCollectionChange_PositionInt{
PositionInt: int32(i),
}
} else {
c.Position = &PropCollectionChange_PositionString{
PositionString: *pos,
}
}
}
if len(u.Value) > 0 {
item, err := newCollectionItem(obj, u.Field)
if err != nil {
return PropCollectionUpdateRequest{}, err
}
err = json.Unmarshal(u.Value, item)
if err != nil {
return PropCollectionUpdateRequest{}, err
}
c.SetValue(item)
}
p.PropCollectionUpdateRequest.Updates = append(p.PropCollectionUpdateRequest.Updates, &c)
}
return p.PropCollectionUpdateRequest, nil
}
func newCollectionItem(obj interface{}, field string) (interface{}, error) {
objType := reflect.TypeOf(obj)
objType = indirect(objType)
if objType.Kind() != reflect.Struct {
return nil, errutil.ErrorBadRequest("obj must be a struct")
}
wrapperField, ok := fieldByTag(objType, "json", field)
if !ok {
return nil, errutil.ErrorBadRequestf("obj has no field with json tag: %s", field)
}
wrapperType := indirect(wrapperField.Type)
if wrapperType.Kind() != reflect.Struct {
return nil, errutil.ErrorBadRequestf("field '%s' must be a struct or struct pointer type", field)
}
innerField := wrapperType.Field(0)
if !isPropCollectionType(innerField.Type) {
return nil, errutil.ErrorBadRequestf("provided field '%s' is not valid collection type", field)
}
itemType := innerField.Type.Elem()
return reflect.New(indirect(itemType)).Interface(), nil
}
func fieldByTag(t reflect.Type, key, value string) (reflect.StructField, bool) {
if t == nil {
return reflect.StructField{}, false
}
t = indirect(t)
if t.Kind() != reflect.Struct {
return reflect.StructField{}, false
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if tag, ok := field.Tag.Lookup(key); ok {
tagName := strings.SplitN(tag, ",", 2)[0]
if tagName == value {
return field, true
}
}
}
return reflect.StructField{}, false
}
func indirect(t reflect.Type) reflect.Type {
for t != nil && t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t
}
func isPropCollectionType(t reflect.Type) bool {
if t == nil {
return false
}
k := t.Kind()
return k == reflect.Map || k == reflect.Slice
}
// PropCollectionUpdate handles a prop-collection-update grpc request.
func (p *PropCollectionUpdatePlugin) PropCollectionUpdate(
ctx context.Context, request *PropCollectionUpdateRequest,
) (*types.Empty, error) {
err := p.InTransactionDoer.DoInTransaction(ctx, func(ctx context.Context) error {
obj, objType, err := GetObjectAndType(ctx, request.UUID, p.MetadataGetter, p.Service)
if err != nil {
return err
}
updatableObj, ok := obj.(applyPropCollectionUpdater)
if !ok {
return errors.Errorf("%T does not support prop-collection-update", obj)
}
return p.updatePropCollection(ctx, request, updatableObj, objType)
})
return &types.Empty{}, err
}
func (p *PropCollectionUpdatePlugin) updatePropCollection(
ctx context.Context,
request *PropCollectionUpdateRequest,
obj applyPropCollectionUpdater,
objType string,
) error {
updateMap, err := createUpdateMap(obj, request.Updates)
if err != nil {
return errutil.ErrorBadRequest(err.Error())
}
e, err := NewEvent(EventOption{
Data: updateMap,
Kind: objType,
UUID: request.UUID,
Operation: services.OperationUpdate,
})
if err != nil {
return err
}
_, err = e.Process(ctx, p.Service)
return err
}
func createUpdateMap(
object applyPropCollectionUpdater, updates []*PropCollectionChange,
) (map[string]interface{}, error) {
updateMap := map[string]interface{}{}
for _, update := range updates {
updated, err := object.ApplyPropCollectionUpdate(&models.PropCollectionUpdate{
Field: update.Field,
Operation: update.Operation,
Value: update.ValueAsInterface(),
Position: getPosition(update.Position),
})
if err != nil {
return nil, err
}
for key, value := range updated {
updateMap[key] = value
}
}
return updateMap, nil
}
type applyPropCollectionUpdater interface {
ApplyPropCollectionUpdate(*models.PropCollectionUpdate) (updated map[string]interface{}, err error)
}
func getPosition(pos isPropCollectionChange_Position) interface{} {
switch p := pos.(type) {
case *PropCollectionChange_PositionInt:
return p.PositionInt
case *PropCollectionChange_PositionString:
return p.PositionString
default:
return nil
}
}