forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.tmpl
241 lines (214 loc) · 8.23 KB
/
service.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
package services
import (
"context"
"fmt"
"net/http"
"github.com/labstack/echo"
"github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"github.com/Juniper/contrail/pkg/common"
"github.com/Juniper/contrail/pkg/services/baseservices"
"github.com/Juniper/contrail/pkg/models/basemodels"
"{{ option.PackagePath }}/pkg/models"
)
//This is needed to prevent an import error.
var _ = fmt.Println
{% for schema in schemas %}{% if schema.Type != "abstract" and schema.ID %}
type RESTUpdate{{ schema.JSONSchema.GoName }}Request struct{
{{ schema.JSONSchema.GoName }} map[string]interface{} `json:"{{ schema.Path }}"`
}
//RESTCreate{{ schema.JSONSchema.GoName }} handle a Create REST service.
func (service *ContrailService) RESTCreate{{ schema.JSONSchema.GoName }}(c echo.Context) error {
requestData := &Create{{ schema.JSONSchema.GoName }}Request{
}
if err := c.Bind(requestData); err != nil {
log.WithFields(log.Fields{
"err": err,
"resource": "{{ schema.ID }}",
}).Debug("Bind failed on create")
return echo.NewHTTPError(http.StatusBadRequest, "invalid JSON format")
}
ctx := c.Request().Context()
response, err := service.Create{{ schema.JSONSchema.GoName }}(ctx, requestData)
if err != nil {
return common.ToHTTPError(err)
}
return c.JSON(http.StatusOK, response)
}
//Create{{ schema.JSONSchema.GoName }} handle a Create API
func (service *ContrailService) Create{{ schema.JSONSchema.GoName }}(
ctx context.Context,
request *Create{{ schema.JSONSchema.GoName }}Request) (*Create{{ schema.JSONSchema.GoName }}Response, error) {
model := request.{{ schema.JSONSchema.GoName }}
if model == nil {
return nil, common.ErrorBadRequest("create body is empty")
}
auth := common.GetAuthCTX(ctx)
if auth == nil {
return nil, common.ErrorUnauthenticated
}
if model.UUID == "" {
model.UUID = uuid.NewV4().String()
}
if model.Name == "" {
if fqName := model.FQName; len(fqName) > 0 {
model.Name = fqName[len(fqName)-1]
} else {
model.Name = "default-{{ schema.TypeName }}"
}
}
{% if schema.IsConfigRootInParents %}
if model.ParentType == models.KindConfigRoot {
model.ParentType = ""
}
{% endif %}
{% if schema.Parents | length == 1 and !schema.IsConfigRootInParents %}{# There is only one parent, so "for" will extract him #}
if model.ParentType == "" { {% for key, reference in schema.Parents %}
model.ParentType = "{{ reference.LinkTo.TypeName }}"{% endfor %}
}{% endif %}
if err := service.sanitizeFQNameFor{{ schema.JSONSchema.GoName }}(ctx, request); err != nil {
return nil, err
}
{% if schema.Parents | length != 0 %}
if model.ParentUUID == "" {
// fqName must be have at least 2 elements to contain parent fqName
if l := len(model.FQName); l > 1 {
// get parentUUID by parent fqName
parentFQName := model.FQName[:l-1]
metadata, err := service.MetadataGetter.GetMetaData(ctx, "", parentFQName)
if err != nil {
return nil, common.ErrorBadRequest("please provide correct FQName or ParentUUID")
}
model.ParentUUID = metadata.UUID
}
}
{% endif %}
model.Perms2 = &models.PermType2{
OwnerAccess: basemodels.PermsRWX,
}
model.Perms2.Owner = auth.ProjectID()
if model.IDPerms == nil {
model.IDPerms = models.NewIDPerms(model.UUID)
}
if model.IDPerms.UUID == nil {
model.IDPerms.UUID = models.NewUUIDType(model.UUID)
}
err := service.TypeValidator.Validate{{ schema.JSONSchema.GoName }}(request.{{ schema.JSONSchema.GoName }})
if err != nil {
return nil, common.ErrorBadRequest(fmt.Sprintf(
"validation failed for resource with UUID %v: %v",
request.{{ schema.JSONSchema.GoName }}.UUID,
err,
))
}
return service.Next().Create{{ schema.JSONSchema.GoName }}(ctx, request)
}
func (service *ContrailService) sanitizeFQNameFor{{ schema.JSONSchema.GoName }} (
ctx context.Context,
request *Create{{ schema.JSONSchema.GoName }}Request,
) error {
model := request.{{ schema.JSONSchema.GoName }}
if len(model.FQName) != 0 {
return nil
}
{% if schema.Parents | length == 0 %}
model.FQName = []string{model.Name}
{% else %}
{% if schema.IsConfigRootInParents %}
if model.ParentUUID == "" && model.ParentType == "" {
model.FQName = []string{model.Name}
return nil
}
{% endif %}
if model.ParentUUID != "" {
// fqName is empty but parentUUID is not - get parent fqName
metadata, err := service.MetadataGetter.GetMetaData(ctx, model.ParentUUID, nil)
if err != nil {
return common.ErrorBadRequestf("Failed to generate FQName: %v", err)
}
model.FQName = append(metadata.FQName, model.Name)
return nil
}
if model.ParentType != "" {
// assign default fqName
auth := common.GetAuthCTX(ctx)
model.FQName = []string{auth.DomainID(), fmt.Sprintf("default-%s", model.ParentType), model.Name}
}
{% endif %}
return nil
}
//RESTUpdate{{ schema.JSONSchema.GoName }} handles a REST Update request.
func (service *ContrailService) RESTUpdate{{ schema.JSONSchema.GoName }}(c echo.Context) error {
id := c.Param("id")
request := &RESTUpdate{{ schema.JSONSchema.GoName }}Request{}
if err := c.Bind(request); err != nil {
log.WithFields(log.Fields{
"err": err,
"resource": "{{ schema.ID }}",
}).Debug("Bind failed on update")
return echo.NewHTTPError(http.StatusBadRequest, "invalid JSON format")
}
ctx := c.Request().Context()
model := models.InterfaceTo{{ schema.JSONSchema.GoName }}(request.{{ schema.JSONSchema.GoName }})
model.UUID = id
response, err := service.Update{{ schema.JSONSchema.GoName }}(ctx, &Update{{ schema.JSONSchema.GoName }}Request{
{{ schema.JSONSchema.GoName }}: model,
FieldMask: baseservices.MapToFieldMask(request.{{ schema.JSONSchema.GoName }}),
})
if err != nil {
return common.ToHTTPError(err)
}
return c.JSON(http.StatusOK, response)
}
//Update{{ schema.JSONSchema.GoName }} handles a Update request.
func (service *ContrailService) Update{{ schema.JSONSchema.GoName }}(
ctx context.Context,
request *Update{{ schema.JSONSchema.GoName }}Request) (*Update{{ schema.JSONSchema.GoName }}Response, error) {
model := request.{{ schema.JSONSchema.GoName }}
if model == nil {
return nil, common.ErrorBadRequest("update body is empty")
}
return service.Next().Update{{ schema.JSONSchema.GoName }}(ctx, request)
}
//RESTDelete{{ schema.JSONSchema.GoName }} delete a resource using REST service.
func (service *ContrailService) RESTDelete{{ schema.JSONSchema.GoName }}(c echo.Context) error {
id := c.Param("id")
request := &Delete{{ schema.JSONSchema.GoName }}Request{
ID: id,
}
ctx := c.Request().Context()
_, err := service.Delete{{ schema.JSONSchema.GoName }}(ctx, request)
if err != nil {
return common.ToHTTPError(err)
}
return c.NoContent(http.StatusOK)
}
//RESTGet{{ schema.JSONSchema.GoName }} a REST Get request.
func (service *ContrailService) RESTGet{{ schema.JSONSchema.GoName }}(c echo.Context) (error) {
id := c.Param("id")
request := &Get{{ schema.JSONSchema.GoName }}Request{
ID: id,
}
ctx := c.Request().Context()
response, err := service.Get{{ schema.JSONSchema.GoName }}(ctx, request)
if err != nil {
return common.ToHTTPError(err)
}
return c.JSON(http.StatusOK, response)
}
//RESTList{{ schema.JSONSchema.GoName }} handles a List REST service Request.
func (service *ContrailService) RESTList{{ schema.JSONSchema.GoName }}(c echo.Context) (error) {
var err error
spec := baseservices.GetListSpec(c)
request := &List{{ schema.JSONSchema.GoName }}Request{
Spec: spec,
}
ctx := c.Request().Context()
response, err := service.List{{ schema.JSONSchema.GoName }}(ctx, request)
if err != nil {
return common.ToHTTPError(err)
}
return c.JSON(http.StatusOK, response)
}
{% endif %}
{% endfor %}