forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetcdserviceif.tmpl
91 lines (73 loc) · 3.58 KB
/
etcdserviceif.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
// nolint
package etcd
import (
"context"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"{{ option.PackagePath }}/pkg/services"
)
{% for schema in schemas %}{% if schema.Type != "abstract" and schema.ID %}
func (service *NotifierService) Create{{ schema.JSONSchema.GoName }}(ctx context.Context, request *services.Create{{ schema.JSONSchema.GoName }}Request) (*services.Create{{ schema.JSONSchema.GoName }}Response, error) {
response, err := service.BaseService.Create{{ schema.JSONSchema.GoName }}(ctx, request)
if err != nil {
log.WithError(err).WithField("resource", "{{ schema.ID }}").Debug("Create {{ schema.JSONSchema.GoName }} failed")
return nil, err
}
obj := response.Get{{ schema.JSONSchema.GoName }}()
key, jsonStr := service.EtcdNotifierMarshal("{{ schema.ID }}", obj.UUID, obj)
if key == "" {
return nil, errors.New("etcd object key is empty")
}
err = service.Client.Put(ctx, key, jsonStr)
if err != nil {
log.WithError(err).WithField("resource", "{{ schema.ID }}").Debug(
"Create {{ schema.JSONSchema.GoName }}[%s]: Failed to Write to ETCD", obj.UUID)
return nil, err
}
log.WithField("resource", "{{ schema.ID }}").Debugf(
"Create {{ schema.JSONSchema.GoName }}[%s]: Write to ETCD successful", obj.UUID)
return response, nil
}
func (service *NotifierService) Update{{ schema.JSONSchema.GoName }}(ctx context.Context, request *services.Update{{ schema.JSONSchema.GoName }}Request) (*services.Update{{ schema.JSONSchema.GoName }}Response, error) {
response, err := service.BaseService.Update{{ schema.JSONSchema.GoName }}(ctx, request)
if err != nil {
log.WithError(err).WithField("resource", "{{ schema.ID }}").Debug("Update {{ schema.JSONSchema.GoName }} failed")
return nil, err
}
obj := response.Get{{ schema.JSONSchema.GoName }}()
key, jsonStr := service.EtcdNotifierMarshal("{{ schema.ID }}", obj.UUID, obj)
if key == "" {
return nil, errors.New("etcd object key is empty")
}
err = service.Client.Put(ctx, key, jsonStr)
if err != nil {
log.WithError(err).WithField("resource", "{{ schema.ID }}").Debug(
"Update {{ schema.JSONSchema.GoName }}[%s]: Failed to Write to ETCD", obj.UUID)
return nil, err
}
log.WithError(err).WithField("resource", "{{ schema.ID }}").Debugf(
"Update {{ schema.JSONSchema.GoName }}[%s]: Write to ETCD successful", obj.UUID)
return response, nil
}
func (service *NotifierService) Delete{{ schema.JSONSchema.GoName }}(ctx context.Context, request *services.Delete{{ schema.JSONSchema.GoName }}Request) (*services.Delete{{ schema.JSONSchema.GoName }}Response, error) {
response, err := service.BaseService.Delete{{ schema.JSONSchema.GoName }}(ctx, request)
if err != nil {
log.WithError(err).WithField("resource", "{{ schema.ID }}").Debug("Delete {{ schema.JSONSchema.GoName }} failed")
return nil, err
}
UUID := response.GetID()
key, _ := service.EtcdNotifierMarshal("{{ schema.ID }}", UUID, nil)
if key == "" {
return nil, errors.New("etcd object key is empty")
}
err = service.Client.Delete(ctx, key)
if err != nil {
log.WithError(err).WithField("resource", "{{ schema.ID }}").Debug(
"Delete {{ schema.JSONSchema.GoName }}[%s]: Failed to Write to ETCD", UUID)
return nil, err
}
log.WithField("resource", "{{ schema.ID }}").Debugf(
"Delete {{ schema.JSONSchema.GoName }}[%s]: Write to ETCD successful", UUID)
return response, nil
}
{% endif%}{% endfor %}