forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitizer_service.tmpl
98 lines (83 loc) · 3.24 KB
/
sanitizer_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
package services
import (
"bytes"
"context"
"fmt"
"github.com/pkg/errors"
"github.com/Juniper/contrail/pkg/models/basemodels"
"github.com/Juniper/contrail/pkg/services/baseservices"
"github.com/Juniper/contrail/pkg/common"
"{{ option.PackagePath }}/pkg/models"
)
//This is needed to prevent an import error.
var _ = common.ErrorNotFound
var _ = models.Version
var _ = basemodels.MetaData{}
// SanitizerService fills up missing properties based on resources logic and metadata
// TODO: Move logic from ContrailService when validation will be a separate service
type SanitizerService struct {
BaseService
MetadataGetter baseservices.MetadataGetter
}
func (sv *SanitizerService) sanitizeRefs(ctx context.Context, refs []basemodels.Reference) error {
fqNameToRef:= make(map[string] basemodels.Reference)
var fqNameUUIDPairs []*basemodels.FQNameUUIDPair
for _, ref := range refs {
if ref.GetUUID() != "" {
continue
}
fqNameToRef[basemodels.FQNameToString(ref.GetTo())] = ref
fqNameUUIDPairs = append(fqNameUUIDPairs, &basemodels.FQNameUUIDPair{FQName: ref.GetTo()})
}
if len(fqNameUUIDPairs) == 0 {
return nil
}
metadatas, err := sv.MetadataGetter.ListMetadata(ctx, fqNameUUIDPairs)
if err != nil {
return err
}
if len(metadatas) != len(fqNameUUIDPairs) {
for _, metadata := range metadatas {
delete(fqNameToRef, basemodels.FQNameToString(metadata.FQName))
}
var missingRefs bytes.Buffer
for _, ref := range fqNameToRef {
missingRefs.WriteString(fmt.Sprintf(" {type: %v, to: %v}", ref.GetReferredKind(), ref.GetTo()))
}
return errors.Errorf("couldn't get metadatas for references:%v", missingRefs.String())
}
for _, metadata := range metadatas {
fqNameToRef[basemodels.FQNameToString(metadata.FQName)].SetUUID(metadata.UUID)
}
return nil
}
{% for schema in schemas %}{% if schema.Type != "abstract" and schema.ID %}
func (sv *SanitizerService) sanitize{{ schema.JSONSchema.GoName }}DisplayName(ctx context.Context, m *models.{{ schema.JSONSchema.GoName }}) {
if m.DisplayName == "" {
m.DisplayName = m.Name
}
}
func (sv *SanitizerService) Create{{ schema.JSONSchema.GoName }}(
ctx context.Context, request *Create{{ schema.JSONSchema.GoName }}Request,
) (*Create{{ schema.JSONSchema.GoName }}Response, error) {
m := request.Get{{ schema.JSONSchema.GoName }}()
sv.sanitize{{ schema.JSONSchema.GoName }}DisplayName(ctx, m)
{% if schema.References | length != 0 %}
if err := sv.sanitizeRefs(ctx, m.GetReferences()); err != nil {
return nil, common.ErrorBadRequestf("couldn't sanitize refs of {{ schema.JSONSchema.GoName }} %v: %v", m.GetUUID(), err)
}
{% endif %}
return sv.BaseService.Create{{ schema.JSONSchema.GoName }}(ctx, request)
}
func (sv *SanitizerService) Update{{ schema.JSONSchema.GoName }}(
ctx context.Context, request *Update{{ schema.JSONSchema.GoName }}Request,
) (*Update{{ schema.JSONSchema.GoName }}Response, error) {
{% if schema.References | length != 0 %}
m := request.Get{{ schema.JSONSchema.GoName }}()
if err := sv.sanitizeRefs(ctx, m.GetReferences()); err != nil {
return nil, common.ErrorBadRequestf("couldn't sanitize refs of {{ schema.JSONSchema.GoName }} %v: %v", m.GetUUID(), err)
}
{% endif %}
return sv.BaseService.Update{{ schema.JSONSchema.GoName }}(ctx, request)
}
{% endif %}{% endfor %}