forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.tmpl
91 lines (80 loc) · 3.88 KB
/
client.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
package client
import (
"context"
"github.com/Juniper/contrail/pkg/services"
"github.com/Juniper/contrail/pkg/models"
)
{% for schema in schemas %}{% if schema.Type != "abstract" and schema.ID %}
func (h *HTTP) Create{{ schema.JSONSchema.GoName }}(ctx context.Context, request *services.Create{{ schema.JSONSchema.GoName }}Request) (*services.Create{{ schema.JSONSchema.GoName }}Response, error) {
response := &services.Create{{ schema.JSONSchema.GoName }}Response{}
_, err := h.Create(ctx, "{{ schema.Prefix }}{{ schema.PluralPath }}", request, response)
return response, err
}
func (h *HTTP) Update{{ schema.JSONSchema.GoName }}(ctx context.Context, request *services.Update{{ schema.JSONSchema.GoName }}Request) (*services.Update{{ schema.JSONSchema.GoName }}Response, error) {
response := &services.Update{{ schema.JSONSchema.GoName }}Response{}
_, err := h.Update(ctx, "{{ schema.Prefix }}{{ schema.Path }}/" + request.{{ schema.JSONSchema.GoName }}.UUID,
request, &response)
return response, err
}
func (h *HTTP) Delete{{ schema.JSONSchema.GoName }}(ctx context.Context, request *services.Delete{{ schema.JSONSchema.GoName }}Request) (*services.Delete{{ schema.JSONSchema.GoName }}Response, error) {
response := &services.Delete{{ schema.JSONSchema.GoName }}Response{}
_, err := h.Delete(ctx, "{{ schema.Prefix }}{{ schema.Path }}/" + request.ID, nil)
return response, err
}
func (h *HTTP) Get{{ schema.JSONSchema.GoName }}(ctx context.Context, request *services.Get{{ schema.JSONSchema.GoName }}Request) (*services.Get{{ schema.JSONSchema.GoName }}Response, error) {
response := &services.Get{{ schema.JSONSchema.GoName }}Response{}
_, err := h.Read(ctx, "{{ schema.Prefix }}{{ schema.Path }}/" + request.ID, response)
return response, err
}
func (h *HTTP) List{{ schema.JSONSchema.GoName }}(ctx context.Context, request *services.List{{ schema.JSONSchema.GoName }}Request) (*services.List{{ schema.JSONSchema.GoName }}Response, error) {
//TODO(nati) support encoding list spec for query param.
response := &services.List{{ schema.JSONSchema.GoName }}Response{}
_, err := h.Read(ctx, "{{ schema.Prefix }}{{ schema.PluralPath }}", response)
return response, err
}
{% for _, reference in schema.References %}
{% set refType = schema.JSONSchema.GoName| add:reference.GoName | add:"Ref" %}
func (h *HTTP) Create{{ refType }}(
ctx context.Context, request *services.Create{{ refType }}Request,
) (*services.Create{{ refType }}Response, error) {
data := getRefUpdateFromRequest{{ refType }} (request, services.RefOperationAdd)
_, err := h.RefUpdate(ctx, data, nil)
if err != nil {
return nil, err
}
return (*services.Create{{ refType }}Response)(request), nil
}
func (h *HTTP) Delete{{ refType }}(
ctx context.Context, request *services.Delete{{ refType }}Request,
) (*services.Delete{{ refType }}Response, error) {
data := getRefUpdateFromRequest{{ refType }} ((*services.Create{{ refType }}Request)(request), services.RefOperationDelete)
_, err := h.RefUpdate(ctx, data, nil)
if err != nil {
return nil, err
}
return (*services.Delete{{ refType }}Response)(request), nil
}
func getRefUpdateFromRequest{{ refType }} (request *services.Create{{ refType }}Request, operation services.RefOperation) interface{}{
id := request.GetID()
reference := request.Get{{ refType }}()
data := struct{
services.RefUpdate
{% if reference.RefType %}
Attr *models.{{reference.RefType}} `json:"attr"`
{% endif %}
}{
services.RefUpdate{
Operation: operation,
Type: "{{ schema.Path }}",
UUID: id,
RefType: "{{ reference.LinkTo.Path }}",
RefUUID: reference.GetUUID(),
},
{% if reference.RefType %}
reference.GetAttr(),
{% endif %}
}
return data
}
{% endfor %}
{% endif%}{% endfor %}