-
Notifications
You must be signed in to change notification settings - Fork 7
/
local.go
185 lines (144 loc) · 5.38 KB
/
local.go
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
package client
import (
"context"
"github.com/apibrew/apibrew/pkg/errors"
"github.com/apibrew/apibrew/pkg/model"
"github.com/apibrew/apibrew/pkg/service"
"github.com/apibrew/apibrew/pkg/stub"
log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/structpb"
)
type localClient struct {
container service.Container
}
func (l localClient) Watch(ctx context.Context, request *stub.WatchRequest) (stub.Watch_WatchClient, error) {
//TODO implement me
panic("implement me")
}
func (l localClient) LoadRecord(ctx context.Context, namespace string, resource string, properties map[string]*structpb.Value, params service.RecordLoadParams) (*model.Record, error) {
//TODO implement me
panic("implement me")
}
func (l localClient) DeleteRecord(ctx context.Context, namespace string, name string, record *model.Record) error {
//TODO implement me
panic("implement me")
}
func (l localClient) CreateResource(ctx context.Context, resource *model.Resource, migration bool, force bool) error {
//TODO implement me
panic("implement me")
}
func (l localClient) UpdateResource(ctx context.Context, resource *model.Resource, migration bool, force bool) error {
//TODO implement me
panic("implement me")
}
func (l localClient) ListenRecords(ctx context.Context, namespace string, resource string, consumer func(records []*model.Record)) error {
//TODO implement me
panic("implement me")
}
func (l localClient) NewRemoteExtension(host string, remoteHost string) Extension {
//TODO implement me
panic("implement me")
}
func (l localClient) NewPollExtension() Extension {
//TODO implement me
panic("implement me")
}
func (l localClient) PollEvents(ctx context.Context, key string) (<-chan *model.Event, error) {
panic("implement me")
}
func (l localClient) WriteEvent(ctx context.Context, key string, event *model.Event) error {
panic("implement me")
}
func (l localClient) DeleteResource(ctx context.Context, id string, doMigration bool, forceMigration bool) error {
//TODO implement me
panic("implement me")
}
func (l localClient) ListRecords(ctx context.Context, params service.RecordListParams) ([]*model.Record, uint32, error) {
return l.container.GetRecordService().List(ctx, params)
}
func (l localClient) GetResourceByName(ctx context.Context, namespace string, getType string) (*model.Resource, error) {
return l.container.GetResourceService().GetResourceByName(ctx, namespace, getType)
}
func (l localClient) ReadRecordStream(ctx context.Context, params service.RecordListParams, recordsChan chan *model.Record) error {
panic("Unsupported")
}
func (l localClient) AuthenticateWithToken(token string) {
panic("Unsupported")
}
func (l localClient) AuthenticateWithUsernameAndPassword(username string, password string) error {
panic("Unsupported")
}
func (l localClient) UpdateTokenFromContext(ctx context.Context) {
panic("Unsupported")
}
func (l localClient) ApplyRecord(ctx context.Context, namespace string, resource string, record *model.Record) (*model.Record, error) {
resp, err := l.container.GetRecordService().Apply(ctx, service.RecordUpdateParams{
Namespace: namespace,
Resource: resource,
Records: []*model.Record{record},
})
if err != nil {
return nil, err
}
return resp[0], err
}
func (l localClient) CreateRecord(ctx context.Context, namespace string, resource string, record *model.Record) (*model.Record, error) {
resp, err := l.container.GetRecordService().Create(ctx, service.RecordCreateParams{
Namespace: namespace,
Resource: resource,
Records: []*model.Record{record},
})
if err != nil {
return nil, err
}
return resp[0], err
}
func (l localClient) UpdateRecord(ctx context.Context, namespace string, resource string, record *model.Record) (*model.Record, error) {
resp, err := l.container.GetRecordService().Update(ctx, service.RecordUpdateParams{
Namespace: namespace,
Resource: resource,
Records: []*model.Record{record},
})
if err != nil {
return nil, err
}
return resp[0], err
}
func (l localClient) GetRecord(ctx context.Context, namespace string, resource string, id string) (*model.Record, error) {
return l.container.GetRecordService().Get(ctx, service.RecordGetParams{
Namespace: namespace,
Resource: resource,
Id: id,
})
}
func (l localClient) FindRecords(ctx context.Context, params service.RecordListParams) ([]*model.Record, uint32, error) {
return l.container.GetRecordService().List(ctx, params)
}
func (l localClient) ListResources(ctx context.Context) ([]*model.Resource, error) {
return l.container.GetResourceService().List(ctx)
}
func (l localClient) ApplyResource(ctx context.Context, resource *model.Resource, doMigration, forceMigration bool) error {
resp, err := l.container.GetResourceService().GetResourceByName(ctx, resource.Namespace, resource.Name)
if !errors.ResourceNotFoundError.Is(err) && err != nil {
return err
}
if errors.ResourceNotFoundError.Is(err) || resp == nil { // create
_, err = l.container.GetResourceService().Create(ctx, resource, doMigration, forceMigration)
if err != nil {
return err
}
log.Info("Resource created: " + resource.Name)
return nil
} else {
resource.Id = resp.Id
err = l.container.GetResourceService().Update(ctx, resource, doMigration, forceMigration)
if err != nil {
return err
}
log.Info("Resource updated: " + resource.Name)
return nil
}
}
func NewLocalClient(container service.Container) Client {
return &localClient{container: container}
}