-
Notifications
You must be signed in to change notification settings - Fork 1
/
device.pb.api_access.go
220 lines (200 loc) · 6.4 KB
/
device.pb.api_access.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
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
// Code generated by protoc-gen-goten-access
// Resource: Device
// DO NOT EDIT!!!
package device_access
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
gotenaccess "github.com/cloudwan/goten-sdk/runtime/access"
"github.com/cloudwan/goten-sdk/runtime/api/watch_type"
gotenresource "github.com/cloudwan/goten-sdk/runtime/resource"
device_client "github.com/cloudwan/edgelq-sdk/devices/client/v1alpha2/device"
device "github.com/cloudwan/edgelq-sdk/devices/resources/v1alpha2/device"
)
var (
_ = context.Context(nil)
_ = fmt.GoStringer(nil)
_ = grpc.ClientConnInterface(nil)
_ = codes.NotFound
_ = status.Status{}
_ = gotenaccess.Watcher(nil)
_ = watch_type.WatchType_STATEFUL
_ = gotenresource.ListQuery(nil)
)
type apiDeviceAccess struct {
client device_client.DeviceServiceClient
}
func NewApiDeviceAccess(client device_client.DeviceServiceClient) device.DeviceAccess {
return &apiDeviceAccess{client: client}
}
func (a *apiDeviceAccess) GetDevice(ctx context.Context, query *device.GetQuery) (*device.Device, error) {
request := &device_client.GetDeviceRequest{
Name: query.Reference,
FieldMask: query.Mask,
}
res, err := a.client.GetDevice(ctx, request)
if err != nil {
return nil, err
}
query.Reference.Resolve(res)
return res, nil
}
func (a *apiDeviceAccess) BatchGetDevices(ctx context.Context, refs []*device.Reference, opts ...gotenresource.BatchGetOption) error {
batchGetOpts := gotenresource.MakeBatchGetOptions(opts)
request := &device_client.BatchGetDevicesRequest{
Names: refs,
}
resp, err := a.client.BatchGetDevices(ctx, request)
if err != nil {
return err
}
resultMap := make(map[device.Name]*device.Device, len(refs))
for _, resolvedRes := range resp.GetDevices() {
resultMap[*resolvedRes.GetName()] = resolvedRes
}
for _, ref := range refs {
resolvedRes := resultMap[ref.Name]
if resolvedRes != nil {
ref.Resolve(resolvedRes)
}
}
if batchGetOpts.MustResolveAll() && len(resp.GetMissing()) > 0 {
return status.Errorf(codes.NotFound, "Number of references not found: %d", len(resp.GetMissing()))
}
return nil
}
func (a *apiDeviceAccess) QueryDevices(ctx context.Context, query *device.ListQuery) (*device.QueryResultSnapshot, error) {
request := &device_client.ListDevicesRequest{
Filter: query.Filter,
FieldMask: query.Mask,
}
if query.Pager != nil {
request.PageSize = int32(query.Pager.Limit)
request.OrderBy = query.Pager.OrderBy
request.PageToken = query.Pager.Cursor
}
resp, err := a.client.ListDevices(ctx, request)
if err != nil {
return nil, err
}
return &device.QueryResultSnapshot{
Devices: resp.Devices,
NextPageCursor: resp.NextPageToken,
PrevPageCursor: resp.PrevPageToken,
}, nil
}
func (a *apiDeviceAccess) WatchDevice(ctx context.Context, query *device.GetQuery, observerCb func(*device.DeviceChange) error) error {
request := &device_client.WatchDeviceRequest{
Name: query.Reference,
FieldMask: query.Mask,
}
changesStream, initErr := a.client.WatchDevice(ctx, request)
if initErr != nil {
return initErr
}
for {
resp, err := changesStream.Recv()
if err != nil {
return fmt.Errorf("watch recv error: %w", err)
}
change := resp.GetChange()
if err := observerCb(change); err != nil {
return err
}
}
}
func (a *apiDeviceAccess) WatchDevices(ctx context.Context, query *device.WatchQuery, observerCb func(*device.QueryResultChange) error) error {
request := &device_client.WatchDevicesRequest{
Filter: query.Filter,
FieldMask: query.Mask,
MaxChunkSize: int32(query.ChunkSize),
Type: query.WatchType,
ResumeToken: query.ResumeToken,
}
if query.Pager != nil {
request.OrderBy = query.Pager.OrderBy
request.PageSize = int32(query.Pager.Limit)
request.PageToken = query.Pager.Cursor
}
changesStream, initErr := a.client.WatchDevices(ctx, request)
if initErr != nil {
return initErr
}
for {
respChange, err := changesStream.Recv()
if err != nil {
return fmt.Errorf("watch recv error: %w", err)
}
changesWithPaging := &device.QueryResultChange{
Changes: respChange.DeviceChanges,
IsCurrent: respChange.IsCurrent,
IsHardReset: respChange.IsHardReset,
IsSoftReset: respChange.IsSoftReset,
ResumeToken: respChange.ResumeToken,
SnapshotSize: respChange.SnapshotSize,
}
if respChange.PageTokenChange != nil {
changesWithPaging.PrevPageCursor = respChange.PageTokenChange.PrevPageToken
changesWithPaging.NextPageCursor = respChange.PageTokenChange.NextPageToken
}
if err := observerCb(changesWithPaging); err != nil {
return err
}
}
}
func (a *apiDeviceAccess) SaveDevice(ctx context.Context, res *device.Device, opts ...gotenresource.SaveOption) error {
saveOpts := gotenresource.MakeSaveOptions(opts)
previousRes := saveOpts.GetPreviousResource()
if previousRes == nil && !saveOpts.OnlyUpdate() && !saveOpts.OnlyCreate() {
var err error
previousRes, err = a.GetDevice(ctx, &device.GetQuery{Reference: res.Name.AsReference()})
if err != nil {
if statusErr, ok := status.FromError(err); !ok || statusErr.Code() != codes.NotFound {
return err
}
}
}
if saveOpts.OnlyUpdate() || previousRes != nil {
updateRequest := &device_client.UpdateDeviceRequest{
Device: res,
}
if updateMask := saveOpts.GetUpdateMask(); updateMask != nil {
updateRequest.UpdateMask = updateMask.(*device.Device_FieldMask)
}
if mask, conditionalState := saveOpts.GetCAS(); mask != nil && conditionalState != nil {
updateRequest.Cas = &device_client.UpdateDeviceRequest_CAS{
ConditionalState: conditionalState.(*device.Device),
FieldMask: mask.(*device.Device_FieldMask),
}
}
_, err := a.client.UpdateDevice(ctx, updateRequest)
if err != nil {
return err
}
return nil
} else {
createRequest := &device_client.CreateDeviceRequest{
Device: res,
}
_, err := a.client.CreateDevice(ctx, createRequest)
if err != nil {
return err
}
return nil
}
}
func (a *apiDeviceAccess) DeleteDevice(ctx context.Context, ref *device.Reference, opts ...gotenresource.DeleteOption) error {
request := &device_client.DeleteDeviceRequest{
Name: ref,
}
_, err := a.client.DeleteDevice(ctx, request)
return err
}
func init() {
gotenaccess.GetRegistry().RegisterApiAccessConstructor(device.GetDescriptor(), func(cc grpc.ClientConnInterface) gotenresource.Access {
return device.AsAnyCastAccess(NewApiDeviceAccess(device_client.NewDeviceServiceClient(cc)))
})
}