-
Notifications
You must be signed in to change notification settings - Fork 1
/
alert.pb.api_access.go
299 lines (280 loc) · 9.26 KB
/
alert.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Code generated by protoc-gen-goten-access
// Resource: Alert
// DO NOT EDIT!!!
package alert_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"
gotenresource "github.com/cloudwan/goten-sdk/runtime/resource"
gotenfilter "github.com/cloudwan/goten-sdk/runtime/resource/filter"
"github.com/cloudwan/goten-sdk/types/watch_type"
alert_client "github.com/cloudwan/edgelq-sdk/monitoring/client/v3/alert"
alert "github.com/cloudwan/edgelq-sdk/monitoring/resources/v3/alert"
)
var (
_ = new(context.Context)
_ = new(fmt.GoStringer)
_ = new(grpc.ClientConnInterface)
_ = codes.NotFound
_ = status.Status{}
_ = new(gotenaccess.Watcher)
_ = watch_type.WatchType_STATEFUL
_ = new(gotenresource.ListQuery)
_ = gotenfilter.Eq
)
type apiAlertAccess struct {
client alert_client.AlertServiceClient
}
func NewApiAlertAccess(client alert_client.AlertServiceClient) alert.AlertAccess {
return &apiAlertAccess{client: client}
}
func (a *apiAlertAccess) GetAlert(ctx context.Context, query *alert.GetQuery) (*alert.Alert, error) {
if !query.Reference.IsFullyQualified() {
return nil, status.Errorf(codes.InvalidArgument, "Reference %s is not fully specified", query.Reference)
}
request := &alert_client.GetAlertRequest{
Name: &query.Reference.Name,
FieldMask: query.Mask,
}
res, err := a.client.GetAlert(ctx, request)
if err != nil {
return nil, err
}
query.Reference.Resolve(res)
return res, nil
}
func (a *apiAlertAccess) BatchGetAlerts(ctx context.Context, refs []*alert.Reference, opts ...gotenresource.BatchGetOption) error {
batchGetOpts := gotenresource.MakeBatchGetOptions(opts)
asNames := make([]*alert.Name, 0, len(refs))
for _, ref := range refs {
if !ref.IsFullyQualified() {
return status.Errorf(codes.InvalidArgument, "Reference %s is not fully specified", ref)
}
asNames = append(asNames, &ref.Name)
}
request := &alert_client.BatchGetAlertsRequest{
Names: asNames,
}
fieldMask := batchGetOpts.GetFieldMask(alert.GetDescriptor())
if fieldMask != nil {
request.FieldMask = fieldMask.(*alert.Alert_FieldMask)
}
resp, err := a.client.BatchGetAlerts(ctx, request)
if err != nil {
return err
}
resultMap := make(map[alert.Name]*alert.Alert, len(refs))
for _, resolvedRes := range resp.GetAlerts() {
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 *apiAlertAccess) QueryAlerts(ctx context.Context, query *alert.ListQuery) (*alert.QueryResultSnapshot, error) {
request := &alert_client.ListAlertsRequest{
Filter: query.Filter,
FieldMask: query.Mask,
IncludePagingInfo: query.WithPagingInfo,
}
if query.Pager != nil {
request.PageSize = int32(query.Pager.Limit)
request.OrderBy = query.Pager.OrderBy
request.PageToken = query.Pager.Cursor
}
if query.Filter != nil && query.Filter.GetCondition() != nil {
request.Filter, request.Parent = getParentAndFilter(query.Filter)
}
resp, err := a.client.ListAlerts(ctx, request)
if err != nil {
return nil, err
}
return &alert.QueryResultSnapshot{
Alerts: resp.Alerts,
NextPageCursor: resp.NextPageToken,
PrevPageCursor: resp.PrevPageToken,
TotalResultsCount: resp.TotalResultsCount,
CurrentOffset: resp.CurrentOffset,
}, nil
}
func (a *apiAlertAccess) WatchAlert(ctx context.Context, query *alert.GetQuery, observerCb func(*alert.AlertChange) error) error {
if !query.Reference.IsFullyQualified() {
return status.Errorf(codes.InvalidArgument, "Reference %s is not fully specified", query.Reference)
}
request := &alert_client.WatchAlertRequest{
Name: &query.Reference.Name,
FieldMask: query.Mask,
}
changesStream, initErr := a.client.WatchAlert(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 *apiAlertAccess) WatchAlerts(ctx context.Context, query *alert.WatchQuery, observerCb func(*alert.QueryResultChange) error) error {
request := &alert_client.WatchAlertsRequest{
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
}
if query.Filter != nil && query.Filter.GetCondition() != nil {
request.Filter, request.Parent = getParentAndFilter(query.Filter)
}
changesStream, initErr := a.client.WatchAlerts(ctx, request)
if initErr != nil {
return initErr
}
for {
respChange, err := changesStream.Recv()
if err != nil {
return fmt.Errorf("watch recv error: %w", err)
}
changesWithPaging := &alert.QueryResultChange{
Changes: respChange.AlertChanges,
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 *apiAlertAccess) SaveAlert(ctx context.Context, res *alert.Alert, opts ...gotenresource.SaveOption) error {
saveOpts := gotenresource.MakeSaveOptions(opts)
previousRes := saveOpts.GetPreviousResource()
if previousRes == nil && !saveOpts.OnlyUpdate() && !saveOpts.OnlyCreate() {
var err error
previousRes, err = a.GetAlert(ctx, &alert.GetQuery{Reference: res.Name.AsReference()})
if err != nil {
if statusErr, ok := status.FromError(err); !ok || statusErr.Code() != codes.NotFound {
return err
}
}
}
var resp *alert.Alert
var err error
if saveOpts.OnlyUpdate() || previousRes != nil {
updateRequest := &alert_client.UpdateAlertRequest{
Alert: res,
}
if updateMask := saveOpts.GetUpdateMask(); updateMask != nil {
updateRequest.UpdateMask = updateMask.(*alert.Alert_FieldMask)
}
if mask, conditionalState := saveOpts.GetCAS(); mask != nil && conditionalState != nil {
updateRequest.Cas = &alert_client.UpdateAlertRequest_CAS{
ConditionalState: conditionalState.(*alert.Alert),
FieldMask: mask.(*alert.Alert_FieldMask),
}
}
resp, err = a.client.UpdateAlert(ctx, updateRequest)
if err != nil {
return err
}
} else {
createRequest := &alert_client.CreateAlertRequest{
Alert: res,
}
resp, err = a.client.CreateAlert(ctx, createRequest)
if err != nil {
return err
}
}
// Ensure object is updated - but in most shallow way possible
res.MakeDiffFieldMask(resp).Set(res, resp)
return nil
}
func (a *apiAlertAccess) DeleteAlert(ctx context.Context, ref *alert.Reference, opts ...gotenresource.DeleteOption) error {
if !ref.IsFullyQualified() {
return status.Errorf(codes.InvalidArgument, "Reference %s is not fully specified", ref)
}
request := &alert_client.DeleteAlertRequest{
Name: &ref.Name,
}
_, err := a.client.DeleteAlert(ctx, request)
return err
}
func getParentAndFilter(fullFilter *alert.Filter) (*alert.Filter, *alert.ParentName) {
var withParentExtraction func(cnd alert.FilterCondition) alert.FilterCondition
var resultParent *alert.ParentName
var resultFilter *alert.Filter
withParentExtraction = func(cnd alert.FilterCondition) alert.FilterCondition {
switch tCnd := cnd.(type) {
case *alert.FilterConditionComposite:
if tCnd.GetOperator() == gotenfilter.AND {
withoutParentCnds := make([]alert.FilterCondition, 0)
for _, subCnd := range tCnd.Conditions {
if subCndNoParent := withParentExtraction(subCnd); subCndNoParent != nil {
withoutParentCnds = append(withoutParentCnds, subCndNoParent)
}
}
if len(withoutParentCnds) == 0 {
return nil
}
if len(withoutParentCnds) == 1 {
return withoutParentCnds[0]
}
return alert.AndFilterConditions(withoutParentCnds...)
} else {
return tCnd
}
case *alert.FilterConditionCompare:
if tCnd.GetOperator() == gotenfilter.Eq && tCnd.GetRawFieldPath().String() == "name" {
nameValue := tCnd.GetRawValue().(*alert.Name)
if nameValue != nil && nameValue.ParentName.IsSpecified() {
resultParent = &nameValue.ParentName
if nameValue.IsFullyQualified() {
return tCnd
}
return nil
}
}
return tCnd
default:
return tCnd
}
}
cndWithoutParent := withParentExtraction(fullFilter.GetCondition())
if cndWithoutParent != nil {
resultFilter = &alert.Filter{FilterCondition: cndWithoutParent}
}
return resultFilter, resultParent
}
func init() {
gotenaccess.GetRegistry().RegisterApiAccessConstructor(alert.GetDescriptor(), func(cc grpc.ClientConnInterface) gotenresource.Access {
return alert.AsAnyCastAccess(NewApiAlertAccess(alert_client.NewAlertServiceClient(cc)))
})
}