forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_interface.tmpl
354 lines (325 loc) · 14.5 KB
/
service_interface.tmpl
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package services
import (
"context"
"github.com/gogo/protobuf/types"
"github.com/Juniper/contrail/pkg/common"
"github.com/Juniper/contrail/pkg/services/baseservices"
"{{ option.PackagePath }}/pkg/models"
)
//This is needed to prevent an import error.
var _ = types.AnyMessageName
type Service interface {
Next() Service
SetNext(Service)
WriteService
ReadService
}
type WriteService interface {
{% for schema in schemas %}{% if schema.Type != "abstract" and schema.ID %}
Create{{ schema.JSONSchema.GoName }}(context.Context, *Create{{ schema.JSONSchema.GoName }}Request) (*Create{{ schema.JSONSchema.GoName }}Response, error)
Update{{ schema.JSONSchema.GoName }}(context.Context, *Update{{ schema.JSONSchema.GoName }}Request) (*Update{{ schema.JSONSchema.GoName }}Response, error)
Delete{{ schema.JSONSchema.GoName }}(context.Context, *Delete{{ schema.JSONSchema.GoName }}Request) (*Delete{{ schema.JSONSchema.GoName }}Response, error)
{% for _, reference in schema.References %}
{% set refType = schema.JSONSchema.GoName| add:reference.GoName | add:"Ref" %}
Create{{ refType }}(context.Context, *Create{{ refType }}Request) (*Create{{ refType }}Response, error)
Delete{{ refType }}(context.Context, *Delete{{ refType }}Request) (*Delete{{ refType }}Response, error)
{% endfor %}
{% endif%}{% endfor %}
}
type ReadService interface{
{% for schema in schemas %}{% if schema.Type != "abstract" and schema.ID %}
Get{{ schema.JSONSchema.GoName }}(context.Context, *Get{{ schema.JSONSchema.GoName }}Request) (*Get{{ schema.JSONSchema.GoName }}Response, error)
List{{ schema.JSONSchema.GoName }}(context.Context, *List{{ schema.JSONSchema.GoName }}Request) (*List{{ schema.JSONSchema.GoName }}Response, error)
{% endif %}{% endfor %}
}
{% for schema in schemas %}{% if schema.Type != "abstract" and schema.ID %}
type {{ schema.JSONSchema.GoName }} = models.{{ schema.JSONSchema.GoName }}
func (service *BaseService) Create{{ schema.JSONSchema.GoName }}(ctx context.Context, request *Create{{ schema.JSONSchema.GoName }}Request) (*Create{{ schema.JSONSchema.GoName }}Response, error) {
if service.next == nil {
return &Create{{ schema.JSONSchema.GoName }}Response{
{{ schema.JSONSchema.GoName }}: request.Get{{ schema.JSONSchema.GoName }}(),
}, nil
}
return service.Next().Create{{ schema.JSONSchema.GoName }}(ctx, request)
}
func (service *BaseService) Update{{ schema.JSONSchema.GoName }}(ctx context.Context, request *Update{{ schema.JSONSchema.GoName }}Request) (*Update{{ schema.JSONSchema.GoName }}Response, error) {
if service.next == nil {
return &Update{{ schema.JSONSchema.GoName }}Response{
{{ schema.JSONSchema.GoName }}: request.Get{{ schema.JSONSchema.GoName }}(),
}, nil
}
return service.Next().Update{{ schema.JSONSchema.GoName }}(ctx, request)
}
func (service *BaseService) Delete{{ schema.JSONSchema.GoName }}(ctx context.Context, request *Delete{{ schema.JSONSchema.GoName }}Request) (*Delete{{ schema.JSONSchema.GoName }}Response, error) {
if service.next == nil {
return &Delete{{ schema.JSONSchema.GoName }}Response{
ID: request.ID,
}, nil
}
return service.Next().Delete{{ schema.JSONSchema.GoName }}(ctx, request)
}
func (service *BaseService) Get{{ schema.JSONSchema.GoName }}(ctx context.Context, request *Get{{ schema.JSONSchema.GoName }}Request) (*Get{{ schema.JSONSchema.GoName }}Response, error) {
if service.next == nil {
return nil, nil
}
return service.Next().Get{{ schema.JSONSchema.GoName }}(ctx, request)
}
func (service *BaseService) List{{ schema.JSONSchema.GoName }}(ctx context.Context, request *List{{ schema.JSONSchema.GoName }}Request) (*List{{ schema.JSONSchema.GoName }}Response, error) {
if service.next == nil {
return nil, nil
}
return service.Next().List{{ schema.JSONSchema.GoName }}(ctx, request)
}
{% for _, reference in schema.References %}
{% set refType = schema.JSONSchema.GoName| add:reference.GoName | add:"Ref" %}
func (service *BaseService) Create{{ refType }}(ctx context.Context, request *Create{{ refType }}Request) (*Create{{ refType }}Response, error) {
if service.next == nil {
return &Create{{ refType }}Response{
ID: request.ID,
{{ refType }}: request.Get{{ refType }}(),
}, nil
}
return service.Next().Create{{ refType }}(ctx, request)
}
func (service *BaseService) Delete{{ refType }}(ctx context.Context, request *Delete{{ refType }}Request) (*Delete{{ refType }}Response, error) {
if service.next == nil {
return &Delete{{ refType }}Response{
ID: request.ID,
{{ refType }}: request.Get{{ refType }}(),
}, nil
}
return service.Next().Delete{{ refType }}(ctx, request)
}
func (s *RefUpdateToUpdateService) Create{{ refType }}(ctx context.Context, request *Create{{ refType }}Request) (*Create{{ refType }}Response, error) {
refField := models.{{ schema.JSONSchema.GoName }}Field{{ reference.GoName }}Refs
if err := s.InTransactionDoer.DoInTransaction(ctx, func(ctx context.Context) error {
objResp, err := s.ReadService.Get{{ schema.JSONSchema.GoName }}(ctx, &Get{{ schema.JSONSchema.GoName }}Request{
ID: request.ID,
Fields: []string{refField},
})
if err != nil {
return err
}
obj := objResp.{{ schema.JSONSchema.GoName }}
obj.Add{{ reference.GoName }}Ref(request.{{ refType }})
_, err = s.BaseService.Update{{ schema.JSONSchema.GoName }}(ctx, &Update{{ schema.JSONSchema.GoName }}Request{
{{ schema.JSONSchema.GoName }}: obj,
FieldMask: types.FieldMask{Paths: []string{refField}},
})
return err
}); err != nil {
return nil, err
}
return &Create{{ refType }}Response{
ID: request.ID,
{{ refType }}: request.Get{{ refType }}(),
}, nil
}
func (s *RefUpdateToUpdateService) Delete{{ refType }}(ctx context.Context, request *Delete{{ refType }}Request) (*Delete{{ refType }}Response, error) {
refField := models.{{ schema.JSONSchema.GoName }}Field{{ reference.GoName }}Refs
if err := s.InTransactionDoer.DoInTransaction(ctx, func(ctx context.Context) error {
objResp, err := s.ReadService.Get{{ schema.JSONSchema.GoName }}(ctx, &Get{{ schema.JSONSchema.GoName }}Request{
ID: request.ID,
Fields: []string{refField},
})
if err != nil {
return err
}
obj := objResp.{{ schema.JSONSchema.GoName }}
obj.Remove{{ reference.GoName }}Ref(request.{{ refType }})
_, err = s.BaseService.Update{{ schema.JSONSchema.GoName }}(ctx, &Update{{ schema.JSONSchema.GoName }}Request{
{{ schema.JSONSchema.GoName }}: obj,
FieldMask: types.FieldMask{Paths: []string{refField}},
})
return err
}); err != nil {
return nil, err
}
return &Delete{{ refType }}Response{
ID: request.ID,
{{ refType }}: request.Get{{ refType }}(),
}, nil
}
{% endfor %}
func (service *EventProducerService) Create{{ schema.JSONSchema.GoName }}(ctx context.Context, request *Create{{ schema.JSONSchema.GoName }}Request) (*Create{{ schema.JSONSchema.GoName }}Response, error) {
_, err := service.Processor.Process(
ctx,
&Event{
Request: &Event_Create{{ schema.JSONSchema.GoName }}Request{
Create{{ schema.JSONSchema.GoName }}Request: request,
},
})
if err != nil {
return nil, err
}
return service.Next().Create{{ schema.JSONSchema.GoName }}(ctx, request)
}
func (service *EventProducerService) Update{{ schema.JSONSchema.GoName }}(ctx context.Context, request *Update{{ schema.JSONSchema.GoName }}Request) (*Update{{ schema.JSONSchema.GoName }}Response, error) {
_, err := service.Processor.Process(
ctx,
&Event{
Request: &Event_Update{{ schema.JSONSchema.GoName }}Request{
Update{{ schema.JSONSchema.GoName }}Request: request,
},
})
if err != nil {
return nil, err
}
return service.Next().Update{{ schema.JSONSchema.GoName }}(ctx, request)
}
func (service *EventProducerService) Delete{{ schema.JSONSchema.GoName }}(ctx context.Context, request *Delete{{ schema.JSONSchema.GoName }}Request) (*Delete{{ schema.JSONSchema.GoName }}Response, error) {
_, err := service.Processor.Process(
ctx,
&Event{
Request: &Event_Delete{{ schema.JSONSchema.GoName }}Request{
Delete{{ schema.JSONSchema.GoName }}Request: request,
},
})
if err != nil {
return nil, err
}
return service.Next().Delete{{ schema.JSONSchema.GoName }}(ctx, request)
}
{% for _, reference in schema.References %}
{% set refType = schema.JSONSchema.GoName| add:reference.GoName | add:"Ref" %}
func (service *EventProducerService) Create{{ refType }}(ctx context.Context, request *Create{{ refType }}Request) (*Create{{ refType }}Response, error) {
_, err := service.Processor.Process(
ctx,
&Event{Request: &Event_Create{{ refType }}Request{
Create{{ refType }}Request: request,
}},
)
if err != nil {
return nil, err
}
return service.Next().Create{{ refType }}(ctx, request)
}
func (service *EventProducerService) Delete{{ refType }}(ctx context.Context, request *Delete{{ refType }}Request) (*Delete{{ refType }}Response, error) {
_, err := service.Processor.Process(
ctx,
&Event{Request: &Event_Delete{{ refType }}Request{
Delete{{ refType }}Request: request,
}},
)
if err != nil {
return nil, err
}
return service.Next().Delete{{ refType }}(ctx, request)
}
{% endfor %}
func (e *Event_Create{{ schema.JSONSchema.GoName }}Request) Process(ctx context.Context, service Service) (*Event, error) {
request := e.Create{{ schema.JSONSchema.GoName }}Request
model := request.{{ schema.JSONSchema.GoName }}
uuid := model.GetUUID()
if uuid != "" {
_, err := service.Get{{ schema.JSONSchema.GoName }}(ctx,
&Get{{ schema.JSONSchema.GoName }}Request{
ID: uuid,
Fields: []string{"uuid"},
})
if err == nil {
model := request.{{ schema.JSONSchema.GoName }}
response, err := service.Update{{ schema.JSONSchema.GoName }}(ctx, &Update{{ schema.JSONSchema.GoName }}Request{
{{ schema.JSONSchema.GoName }}: model,
FieldMask: baseservices.MapToFieldMask(model.ToMap()),
})
if err != nil {
return nil, err
}
return &Event{
Request: &Event_Update{{ schema.JSONSchema.GoName }}Request{
Update{{ schema.JSONSchema.GoName }}Request: &Update{{ schema.JSONSchema.GoName }}Request{
{{ schema.JSONSchema.GoName }}: response.Get{{ schema.JSONSchema.GoName }}(),
},
},
}, nil
}
}
response, err := service.Create{{ schema.JSONSchema.GoName }}(ctx, request)
if err != nil {
return nil, err
}
return &Event{
Request: &Event_Create{{ schema.JSONSchema.GoName }}Request{
Create{{ schema.JSONSchema.GoName }}Request: &Create{{ schema.JSONSchema.GoName }}Request{
{{ schema.JSONSchema.GoName }}: response.Get{{ schema.JSONSchema.GoName }}(),
},
},
}, nil
}
func (e *Event_Update{{ schema.JSONSchema.GoName }}Request) Process(ctx context.Context, service Service) (*Event, error) {
request := e.Update{{ schema.JSONSchema.GoName }}Request
response, err := service.Update{{ schema.JSONSchema.GoName }}(ctx, request)
if err != nil {
return nil, err
}
return &Event{
Request: &Event_Update{{ schema.JSONSchema.GoName }}Request{
Update{{ schema.JSONSchema.GoName }}Request: &Update{{ schema.JSONSchema.GoName }}Request{
{{ schema.JSONSchema.GoName }}: response.Get{{ schema.JSONSchema.GoName }}(),
},
},
}, nil
}
func (e *Event_Delete{{ schema.JSONSchema.GoName }}Request) Process(ctx context.Context, service Service) (*Event, error) {
request := e.Delete{{ schema.JSONSchema.GoName }}Request
_, err := service.Delete{{ schema.JSONSchema.GoName }}(ctx, request)
if err != nil {
return nil, err
}
return &Event{
Request: &Event_Delete{{ schema.JSONSchema.GoName }}Request{
Delete{{ schema.JSONSchema.GoName }}Request: &Delete{{ schema.JSONSchema.GoName }}Request{
ID: request.ID,
},
},
}, nil
}
{% for _, reference in schema.References %}
{% set refType = schema.JSONSchema.GoName| add:reference.GoName | add:"Ref" %}
func (e *Event_Create{{ refType }}Request) Process(ctx context.Context, service Service) (*Event, error) {
request := e.Create{{ refType }}Request
if _, err := service.Create{{ refType }}(ctx, request); err != nil {
return nil, err
}
return &Event{Request: &Event_Create{{ refType }}Request{
Create{{ refType }}Request: request,
}}, nil
}
func (e *Event_Delete{{ refType }}Request) Process(ctx context.Context, service Service) (*Event, error) {
request := e.Delete{{ refType }}Request
if _, err := service.Delete{{ refType }}(ctx, request); err != nil {
return nil, err
}
return &Event{Request: &Event_Delete{{ refType }}Request{
Delete{{ refType }}Request: request,
}}, nil
}
{% endfor %}
{% endif%}{% endfor %}
//Dump dumps all service data.
func Dump(ctx context.Context, service Service) (*EventList, error) {
response := []*Event{}
{% for schema in schemas %}{% if schema.Type != "abstract" and schema.ID %}
list{{ schema.JSONSchema.GoName }}, err := service.List{{ schema.JSONSchema.GoName }}(ctx,
&List{{ schema.JSONSchema.GoName }}Request{
&baseservices.ListSpec{
Detail: true,
},
},
)
if err != nil {
return nil, common.ToHTTPError(err)
}
for _, resource := range list{{ schema.JSONSchema.GoName }}.{{ schema.JSONSchema.GoName }}s {
response = append(response, &Event{
Request: &Event_Create{{ schema.JSONSchema.GoName }}Request{
Create{{ schema.JSONSchema.GoName }}Request: &Create{{ schema.JSONSchema.GoName }}Request{
{{ schema.JSONSchema.GoName }}: resource,
}}})
}
{% endif %}{% endfor %}
return &EventList{
Events: response,
}, nil
}