github.com/cloudwan/edgelq-sdk@v1.15.4/monitoring/access/v3/alerting_policy/alerting_policy.pb.api_access.go (about) 1 // Code generated by protoc-gen-goten-access 2 // Resource: AlertingPolicy 3 // DO NOT EDIT!!! 4 5 package alerting_policy_access 6 7 import ( 8 "context" 9 10 "google.golang.org/grpc" 11 "google.golang.org/grpc/codes" 12 "google.golang.org/grpc/metadata" 13 "google.golang.org/grpc/status" 14 15 gotenaccess "github.com/cloudwan/goten-sdk/runtime/access" 16 gotenresource "github.com/cloudwan/goten-sdk/runtime/resource" 17 gotenfilter "github.com/cloudwan/goten-sdk/runtime/resource/filter" 18 "github.com/cloudwan/goten-sdk/types/watch_type" 19 20 alerting_policy_client "github.com/cloudwan/edgelq-sdk/monitoring/client/v3/alerting_policy" 21 alerting_policy "github.com/cloudwan/edgelq-sdk/monitoring/resources/v3/alerting_policy" 22 ) 23 24 var ( 25 _ = new(context.Context) 26 27 _ = metadata.MD{} 28 _ = new(grpc.ClientConnInterface) 29 _ = codes.NotFound 30 _ = status.Status{} 31 32 _ = new(gotenaccess.Watcher) 33 _ = watch_type.WatchType_STATEFUL 34 _ = new(gotenresource.ListQuery) 35 _ = gotenfilter.Eq 36 ) 37 38 type apiAlertingPolicyAccess struct { 39 client alerting_policy_client.AlertingPolicyServiceClient 40 } 41 42 func NewApiAlertingPolicyAccess(client alerting_policy_client.AlertingPolicyServiceClient) alerting_policy.AlertingPolicyAccess { 43 return &apiAlertingPolicyAccess{client: client} 44 } 45 46 func (a *apiAlertingPolicyAccess) GetAlertingPolicy(ctx context.Context, query *alerting_policy.GetQuery, opts ...gotenresource.GetOption) (*alerting_policy.AlertingPolicy, error) { 47 getOpts := gotenresource.MakeGetOptions(opts) 48 callHeaders := metadata.MD{} 49 if getOpts.GetSkipCache() { 50 callHeaders["cache-control"] = []string{"no-cache"} 51 } 52 callOpts := []grpc.CallOption{} 53 if len(callHeaders) > 0 { 54 callOpts = append(callOpts, grpc.Header(&callHeaders)) 55 } 56 if !query.Reference.IsFullyQualified() { 57 return nil, status.Errorf(codes.InvalidArgument, "Reference %s is not fully specified", query.Reference) 58 } 59 request := &alerting_policy_client.GetAlertingPolicyRequest{ 60 Name: &query.Reference.Name, 61 FieldMask: query.Mask, 62 } 63 res, err := a.client.GetAlertingPolicy(ctx, request, callOpts...) 64 if err != nil { 65 return nil, err 66 } 67 query.Reference.Resolve(res) 68 return res, nil 69 } 70 71 func (a *apiAlertingPolicyAccess) BatchGetAlertingPolicies(ctx context.Context, refs []*alerting_policy.Reference, opts ...gotenresource.BatchGetOption) error { 72 batchGetOpts := gotenresource.MakeBatchGetOptions(opts) 73 callHeaders := metadata.MD{} 74 if batchGetOpts.GetSkipCache() { 75 callHeaders["cache-control"] = []string{"no-cache"} 76 } 77 callOpts := []grpc.CallOption{} 78 if len(callHeaders) > 0 { 79 callOpts = append(callOpts, grpc.Header(&callHeaders)) 80 } 81 asNames := make([]*alerting_policy.Name, 0, len(refs)) 82 for _, ref := range refs { 83 if !ref.IsFullyQualified() { 84 return status.Errorf(codes.InvalidArgument, "Reference %s is not fully specified", ref) 85 } 86 asNames = append(asNames, &ref.Name) 87 } 88 request := &alerting_policy_client.BatchGetAlertingPoliciesRequest{ 89 Names: asNames, 90 } 91 fieldMask := batchGetOpts.GetFieldMask(alerting_policy.GetDescriptor()) 92 if fieldMask != nil { 93 request.FieldMask = fieldMask.(*alerting_policy.AlertingPolicy_FieldMask) 94 } 95 resp, err := a.client.BatchGetAlertingPolicies(ctx, request, callOpts...) 96 if err != nil { 97 return err 98 } 99 resultMap := make(map[alerting_policy.Name]*alerting_policy.AlertingPolicy, len(refs)) 100 for _, resolvedRes := range resp.GetAlertingPolicies() { 101 resultMap[*resolvedRes.GetName()] = resolvedRes 102 } 103 for _, ref := range refs { 104 resolvedRes := resultMap[ref.Name] 105 if resolvedRes != nil { 106 ref.Resolve(resolvedRes) 107 } 108 } 109 if batchGetOpts.MustResolveAll() && len(resp.GetMissing()) > 0 { 110 return status.Errorf(codes.NotFound, "Number of references not found: %d", len(resp.GetMissing())) 111 } 112 return nil 113 } 114 115 func (a *apiAlertingPolicyAccess) QueryAlertingPolicies(ctx context.Context, query *alerting_policy.ListQuery, opts ...gotenresource.QueryOption) (*alerting_policy.QueryResultSnapshot, error) { 116 qOpts := gotenresource.MakeQueryOptions(opts) 117 callHeaders := metadata.MD{} 118 if qOpts.GetSkipCache() { 119 callHeaders["cache-control"] = []string{"no-cache"} 120 } 121 callOpts := []grpc.CallOption{} 122 if len(callHeaders) > 0 { 123 callOpts = append(callOpts, grpc.Header(&callHeaders)) 124 } 125 request := &alerting_policy_client.ListAlertingPoliciesRequest{ 126 Filter: query.Filter, 127 FieldMask: query.Mask, 128 IncludePagingInfo: query.WithPagingInfo, 129 } 130 if query.Pager != nil { 131 request.PageSize = int32(query.Pager.Limit) 132 request.OrderBy = query.Pager.OrderBy 133 request.PageToken = query.Pager.Cursor 134 } 135 if query.Filter != nil && query.Filter.GetCondition() != nil { 136 request.Filter, request.Parent = getParentAndFilter(query.Filter) 137 } 138 resp, err := a.client.ListAlertingPolicies(ctx, request) 139 if err != nil { 140 return nil, err 141 } 142 return &alerting_policy.QueryResultSnapshot{ 143 AlertingPolicies: resp.AlertingPolicies, 144 NextPageCursor: resp.NextPageToken, 145 PrevPageCursor: resp.PrevPageToken, 146 TotalResultsCount: resp.TotalResultsCount, 147 CurrentOffset: resp.CurrentOffset, 148 }, nil 149 } 150 151 func (a *apiAlertingPolicyAccess) SearchAlertingPolicies(ctx context.Context, query *alerting_policy.SearchQuery, opts ...gotenresource.QueryOption) (*alerting_policy.QueryResultSnapshot, error) { 152 qOpts := gotenresource.MakeQueryOptions(opts) 153 callHeaders := metadata.MD{} 154 if qOpts.GetSkipCache() { 155 callHeaders["cache-control"] = []string{"no-cache"} 156 } 157 callOpts := []grpc.CallOption{} 158 if len(callHeaders) > 0 { 159 callOpts = append(callOpts, grpc.Header(&callHeaders)) 160 } 161 request := &alerting_policy_client.SearchAlertingPoliciesRequest{ 162 Phrase: query.Phrase, 163 Filter: query.Filter, 164 FieldMask: query.Mask, 165 } 166 if query.Pager != nil { 167 request.PageSize = int32(query.Pager.Limit) 168 request.OrderBy = query.Pager.OrderBy 169 request.PageToken = query.Pager.Cursor 170 } 171 if query.Filter != nil && query.Filter.GetCondition() != nil { 172 request.Filter, request.Parent = getParentAndFilter(query.Filter) 173 } 174 resp, err := a.client.SearchAlertingPolicies(ctx, request, callOpts...) 175 if err != nil { 176 return nil, err 177 } 178 return &alerting_policy.QueryResultSnapshot{ 179 AlertingPolicies: resp.AlertingPolicies, 180 NextPageCursor: resp.NextPageToken, 181 PrevPageCursor: resp.PrevPageToken, 182 CurrentOffset: resp.CurrentOffset, 183 TotalResultsCount: resp.TotalResultsCount, 184 }, nil 185 } 186 187 func (a *apiAlertingPolicyAccess) WatchAlertingPolicy(ctx context.Context, query *alerting_policy.GetQuery, observerCb func(*alerting_policy.AlertingPolicyChange) error) error { 188 if !query.Reference.IsFullyQualified() { 189 return status.Errorf(codes.InvalidArgument, "Reference %s is not fully specified", query.Reference) 190 } 191 request := &alerting_policy_client.WatchAlertingPolicyRequest{ 192 Name: &query.Reference.Name, 193 FieldMask: query.Mask, 194 } 195 ctx, cancel := context.WithCancel(ctx) 196 defer cancel() 197 198 changesStream, initErr := a.client.WatchAlertingPolicy(ctx, request) 199 if initErr != nil { 200 return initErr 201 } 202 for { 203 resp, err := changesStream.Recv() 204 if err != nil { 205 return status.Errorf(status.Code(err), "watch recv error: %s", err) 206 } 207 change := resp.GetChange() 208 if err := observerCb(change); err != nil { 209 return err 210 } 211 } 212 } 213 214 func (a *apiAlertingPolicyAccess) WatchAlertingPolicies(ctx context.Context, query *alerting_policy.WatchQuery, observerCb func(*alerting_policy.QueryResultChange) error) error { 215 request := &alerting_policy_client.WatchAlertingPoliciesRequest{ 216 Filter: query.Filter, 217 FieldMask: query.Mask, 218 MaxChunkSize: int32(query.ChunkSize), 219 Type: query.WatchType, 220 ResumeToken: query.ResumeToken, 221 StartingTime: query.StartingTime, 222 } 223 if query.Pager != nil { 224 request.OrderBy = query.Pager.OrderBy 225 request.PageSize = int32(query.Pager.Limit) 226 request.PageToken = query.Pager.Cursor 227 } 228 if query.Filter != nil && query.Filter.GetCondition() != nil { 229 request.Filter, request.Parent = getParentAndFilter(query.Filter) 230 } 231 ctx, cancel := context.WithCancel(ctx) 232 defer cancel() 233 234 changesStream, initErr := a.client.WatchAlertingPolicies(ctx, request) 235 if initErr != nil { 236 return initErr 237 } 238 for { 239 respChange, err := changesStream.Recv() 240 if err != nil { 241 return status.Errorf(status.Code(err), "watch recv error: %s", err) 242 } 243 changesWithPaging := &alerting_policy.QueryResultChange{ 244 Changes: respChange.AlertingPolicyChanges, 245 IsCurrent: respChange.IsCurrent, 246 IsHardReset: respChange.IsHardReset, 247 IsSoftReset: respChange.IsSoftReset, 248 ResumeToken: respChange.ResumeToken, 249 SnapshotSize: respChange.SnapshotSize, 250 } 251 if respChange.PageTokenChange != nil { 252 changesWithPaging.PrevPageCursor = respChange.PageTokenChange.PrevPageToken 253 changesWithPaging.NextPageCursor = respChange.PageTokenChange.NextPageToken 254 } 255 if err := observerCb(changesWithPaging); err != nil { 256 return err 257 } 258 } 259 } 260 261 func (a *apiAlertingPolicyAccess) SaveAlertingPolicy(ctx context.Context, res *alerting_policy.AlertingPolicy, opts ...gotenresource.SaveOption) error { 262 saveOpts := gotenresource.MakeSaveOptions(opts) 263 var resp *alerting_policy.AlertingPolicy 264 var err error 265 if !saveOpts.OnlyCreate() { 266 updateRequest := &alerting_policy_client.UpdateAlertingPolicyRequest{ 267 AlertingPolicy: res, 268 AllowMissing: !saveOpts.OnlyUpdate(), 269 } 270 if updateMask := saveOpts.GetUpdateMask(); updateMask != nil { 271 updateRequest.UpdateMask = updateMask.(*alerting_policy.AlertingPolicy_FieldMask) 272 } 273 if mask, conditionalState := saveOpts.GetCAS(); mask != nil && conditionalState != nil { 274 updateRequest.Cas = &alerting_policy_client.UpdateAlertingPolicyRequest_CAS{ 275 ConditionalState: conditionalState.(*alerting_policy.AlertingPolicy), 276 FieldMask: mask.(*alerting_policy.AlertingPolicy_FieldMask), 277 } 278 } 279 resp, err = a.client.UpdateAlertingPolicy(ctx, updateRequest) 280 if err != nil { 281 return err 282 } 283 } else { 284 createRequest := &alerting_policy_client.CreateAlertingPolicyRequest{ 285 AlertingPolicy: res, 286 } 287 resp, err = a.client.CreateAlertingPolicy(ctx, createRequest) 288 if err != nil { 289 return err 290 } 291 } 292 // Ensure object is updated - but in most shallow way possible 293 res.MakeDiffFieldMask(resp).Set(res, resp) 294 return nil 295 } 296 297 func (a *apiAlertingPolicyAccess) DeleteAlertingPolicy(ctx context.Context, ref *alerting_policy.Reference, opts ...gotenresource.DeleteOption) error { 298 delOpts := gotenresource.MakeDeleteOptions(opts) 299 if !ref.IsFullyQualified() { 300 return status.Errorf(codes.InvalidArgument, "Reference %s is not fully specified", ref) 301 } 302 request := &alerting_policy_client.DeleteAlertingPolicyRequest{ 303 Name: &ref.Name, 304 AllowMissing: delOpts.AllowMissing(), 305 } 306 _, err := a.client.DeleteAlertingPolicy(ctx, request) 307 return err 308 } 309 func getParentAndFilter(fullFilter *alerting_policy.Filter) (*alerting_policy.Filter, *alerting_policy.ParentName) { 310 var withParentExtraction func(cnd alerting_policy.FilterCondition) alerting_policy.FilterCondition 311 var resultParent *alerting_policy.ParentName 312 var resultFilter *alerting_policy.Filter 313 withParentExtraction = func(cnd alerting_policy.FilterCondition) alerting_policy.FilterCondition { 314 switch tCnd := cnd.(type) { 315 case *alerting_policy.FilterConditionComposite: 316 if tCnd.GetOperator() == gotenfilter.AND { 317 withoutParentCnds := make([]alerting_policy.FilterCondition, 0) 318 for _, subCnd := range tCnd.Conditions { 319 if subCndNoParent := withParentExtraction(subCnd); subCndNoParent != nil { 320 withoutParentCnds = append(withoutParentCnds, subCndNoParent) 321 } 322 } 323 if len(withoutParentCnds) == 0 { 324 return nil 325 } 326 if len(withoutParentCnds) == 1 { 327 return withoutParentCnds[0] 328 } 329 return alerting_policy.AndFilterConditions(withoutParentCnds...) 330 } else { 331 return tCnd 332 } 333 case *alerting_policy.FilterConditionCompare: 334 if tCnd.GetOperator() == gotenfilter.Eq && tCnd.GetRawFieldPath().String() == "name" { 335 nameValue := tCnd.GetRawValue().(*alerting_policy.Name) 336 if nameValue != nil && nameValue.ParentName.IsSpecified() { 337 resultParent = &nameValue.ParentName 338 if nameValue.IsFullyQualified() { 339 return tCnd 340 } 341 return nil 342 } 343 } 344 return tCnd 345 default: 346 return tCnd 347 } 348 } 349 cndWithoutParent := withParentExtraction(fullFilter.GetCondition()) 350 if cndWithoutParent != nil { 351 resultFilter = &alerting_policy.Filter{FilterCondition: cndWithoutParent} 352 } 353 return resultFilter, resultParent 354 } 355 356 func GetApiAccessBuilder() *gotenaccess.ApiAccessBuilder { 357 return gotenaccess.GetRegistry().FindApiAccessBuilder(alerting_policy.GetDescriptor()) 358 } 359 360 func init() { 361 gotenaccess.GetRegistry().RegisterApiAccessConstructor(alerting_policy.GetDescriptor(), func(cc grpc.ClientConnInterface) gotenresource.Access { 362 return alerting_policy.AsAnyCastAccess(NewApiAlertingPolicyAccess(alerting_policy_client.NewAlertingPolicyServiceClient(cc))) 363 }) 364 }