1// Copyright 2021 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Code generated by protoc-gen-go_gapic. DO NOT EDIT. 16 17package monitoring 18 19import ( 20 "context" 21 "fmt" 22 "math" 23 "net/url" 24 "time" 25 26 "github.com/golang/protobuf/proto" 27 gax "github.com/googleapis/gax-go/v2" 28 "google.golang.org/api/iterator" 29 "google.golang.org/api/option" 30 "google.golang.org/api/option/internaloption" 31 gtransport "google.golang.org/api/transport/grpc" 32 monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres" 33 monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" 34 "google.golang.org/grpc" 35 "google.golang.org/grpc/codes" 36 "google.golang.org/grpc/metadata" 37) 38 39var newGroupClientHook clientHook 40 41// GroupCallOptions contains the retry settings for each method of GroupClient. 42type GroupCallOptions struct { 43 ListGroups []gax.CallOption 44 GetGroup []gax.CallOption 45 CreateGroup []gax.CallOption 46 UpdateGroup []gax.CallOption 47 DeleteGroup []gax.CallOption 48 ListGroupMembers []gax.CallOption 49} 50 51func defaultGroupClientOptions() []option.ClientOption { 52 return []option.ClientOption{ 53 internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"), 54 internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"), 55 internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"), 56 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 57 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 58 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 59 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 60 } 61} 62 63func defaultGroupCallOptions() *GroupCallOptions { 64 return &GroupCallOptions{ 65 ListGroups: []gax.CallOption{ 66 gax.WithRetry(func() gax.Retryer { 67 return gax.OnCodes([]codes.Code{ 68 codes.DeadlineExceeded, 69 codes.Unavailable, 70 }, gax.Backoff{ 71 Initial: 100 * time.Millisecond, 72 Max: 30000 * time.Millisecond, 73 Multiplier: 1.30, 74 }) 75 }), 76 }, 77 GetGroup: []gax.CallOption{ 78 gax.WithRetry(func() gax.Retryer { 79 return gax.OnCodes([]codes.Code{ 80 codes.DeadlineExceeded, 81 codes.Unavailable, 82 }, gax.Backoff{ 83 Initial: 100 * time.Millisecond, 84 Max: 30000 * time.Millisecond, 85 Multiplier: 1.30, 86 }) 87 }), 88 }, 89 CreateGroup: []gax.CallOption{}, 90 UpdateGroup: []gax.CallOption{ 91 gax.WithRetry(func() gax.Retryer { 92 return gax.OnCodes([]codes.Code{ 93 codes.DeadlineExceeded, 94 codes.Unavailable, 95 }, gax.Backoff{ 96 Initial: 100 * time.Millisecond, 97 Max: 30000 * time.Millisecond, 98 Multiplier: 1.30, 99 }) 100 }), 101 }, 102 DeleteGroup: []gax.CallOption{ 103 gax.WithRetry(func() gax.Retryer { 104 return gax.OnCodes([]codes.Code{ 105 codes.DeadlineExceeded, 106 codes.Unavailable, 107 }, gax.Backoff{ 108 Initial: 100 * time.Millisecond, 109 Max: 30000 * time.Millisecond, 110 Multiplier: 1.30, 111 }) 112 }), 113 }, 114 ListGroupMembers: []gax.CallOption{ 115 gax.WithRetry(func() gax.Retryer { 116 return gax.OnCodes([]codes.Code{ 117 codes.DeadlineExceeded, 118 codes.Unavailable, 119 }, gax.Backoff{ 120 Initial: 100 * time.Millisecond, 121 Max: 30000 * time.Millisecond, 122 Multiplier: 1.30, 123 }) 124 }), 125 }, 126 } 127} 128 129// GroupClient is a client for interacting with Cloud Monitoring API. 130// 131// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 132type GroupClient struct { 133 // Connection pool of gRPC connections to the service. 134 connPool gtransport.ConnPool 135 136 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 137 disableDeadlines bool 138 139 // The gRPC API client. 140 groupClient monitoringpb.GroupServiceClient 141 142 // The call options for this service. 143 CallOptions *GroupCallOptions 144 145 // The x-goog-* metadata to be sent with each request. 146 xGoogMetadata metadata.MD 147} 148 149// NewGroupClient creates a new group service client. 150// 151// The Group API lets you inspect and manage your 152// groups (at #google.monitoring.v3.Group). 153// 154// A group is a named filter that is used to identify 155// a collection of monitored resources. Groups are typically used to 156// mirror the physical and/or logical topology of the environment. 157// Because group membership is computed dynamically, monitored 158// resources that are started in the future are automatically placed 159// in matching groups. By using a group to name monitored resources in, 160// for example, an alert policy, the target of that alert policy is 161// updated automatically as monitored resources are added and removed 162// from the infrastructure. 163func NewGroupClient(ctx context.Context, opts ...option.ClientOption) (*GroupClient, error) { 164 clientOpts := defaultGroupClientOptions() 165 166 if newGroupClientHook != nil { 167 hookOpts, err := newGroupClientHook(ctx, clientHookParams{}) 168 if err != nil { 169 return nil, err 170 } 171 clientOpts = append(clientOpts, hookOpts...) 172 } 173 174 disableDeadlines, err := checkDisableDeadlines() 175 if err != nil { 176 return nil, err 177 } 178 179 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 180 if err != nil { 181 return nil, err 182 } 183 c := &GroupClient{ 184 connPool: connPool, 185 disableDeadlines: disableDeadlines, 186 CallOptions: defaultGroupCallOptions(), 187 188 groupClient: monitoringpb.NewGroupServiceClient(connPool), 189 } 190 c.setGoogleClientInfo() 191 192 return c, nil 193} 194 195// Connection returns a connection to the API service. 196// 197// Deprecated. 198func (c *GroupClient) Connection() *grpc.ClientConn { 199 return c.connPool.Conn() 200} 201 202// Close closes the connection to the API service. The user should invoke this when 203// the client is no longer required. 204func (c *GroupClient) Close() error { 205 return c.connPool.Close() 206} 207 208// setGoogleClientInfo sets the name and version of the application in 209// the `x-goog-api-client` header passed on each request. Intended for 210// use by Google-written clients. 211func (c *GroupClient) setGoogleClientInfo(keyval ...string) { 212 kv := append([]string{"gl-go", versionGo()}, keyval...) 213 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 214 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 215} 216 217// ListGroups lists the existing groups. 218func (c *GroupClient) ListGroups(ctx context.Context, req *monitoringpb.ListGroupsRequest, opts ...gax.CallOption) *GroupIterator { 219 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 220 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 221 opts = append(c.CallOptions.ListGroups[0:len(c.CallOptions.ListGroups):len(c.CallOptions.ListGroups)], opts...) 222 it := &GroupIterator{} 223 req = proto.Clone(req).(*monitoringpb.ListGroupsRequest) 224 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Group, string, error) { 225 var resp *monitoringpb.ListGroupsResponse 226 req.PageToken = pageToken 227 if pageSize > math.MaxInt32 { 228 req.PageSize = math.MaxInt32 229 } else { 230 req.PageSize = int32(pageSize) 231 } 232 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 233 var err error 234 resp, err = c.groupClient.ListGroups(ctx, req, settings.GRPC...) 235 return err 236 }, opts...) 237 if err != nil { 238 return nil, "", err 239 } 240 241 it.Response = resp 242 return resp.GetGroup(), resp.GetNextPageToken(), nil 243 } 244 fetch := func(pageSize int, pageToken string) (string, error) { 245 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 246 if err != nil { 247 return "", err 248 } 249 it.items = append(it.items, items...) 250 return nextPageToken, nil 251 } 252 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 253 it.pageInfo.MaxSize = int(req.GetPageSize()) 254 it.pageInfo.Token = req.GetPageToken() 255 return it 256} 257 258// GetGroup gets a single group. 259func (c *GroupClient) GetGroup(ctx context.Context, req *monitoringpb.GetGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) { 260 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 261 cctx, cancel := context.WithTimeout(ctx, 30000*time.Millisecond) 262 defer cancel() 263 ctx = cctx 264 } 265 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 266 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 267 opts = append(c.CallOptions.GetGroup[0:len(c.CallOptions.GetGroup):len(c.CallOptions.GetGroup)], opts...) 268 var resp *monitoringpb.Group 269 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 270 var err error 271 resp, err = c.groupClient.GetGroup(ctx, req, settings.GRPC...) 272 return err 273 }, opts...) 274 if err != nil { 275 return nil, err 276 } 277 return resp, nil 278} 279 280// CreateGroup creates a new group. 281func (c *GroupClient) CreateGroup(ctx context.Context, req *monitoringpb.CreateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) { 282 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 283 cctx, cancel := context.WithTimeout(ctx, 30000*time.Millisecond) 284 defer cancel() 285 ctx = cctx 286 } 287 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 288 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 289 opts = append(c.CallOptions.CreateGroup[0:len(c.CallOptions.CreateGroup):len(c.CallOptions.CreateGroup)], opts...) 290 var resp *monitoringpb.Group 291 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 292 var err error 293 resp, err = c.groupClient.CreateGroup(ctx, req, settings.GRPC...) 294 return err 295 }, opts...) 296 if err != nil { 297 return nil, err 298 } 299 return resp, nil 300} 301 302// UpdateGroup updates an existing group. 303// You can change any group attributes except name. 304func (c *GroupClient) UpdateGroup(ctx context.Context, req *monitoringpb.UpdateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) { 305 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 306 cctx, cancel := context.WithTimeout(ctx, 30000*time.Millisecond) 307 defer cancel() 308 ctx = cctx 309 } 310 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "group.name", url.QueryEscape(req.GetGroup().GetName()))) 311 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 312 opts = append(c.CallOptions.UpdateGroup[0:len(c.CallOptions.UpdateGroup):len(c.CallOptions.UpdateGroup)], opts...) 313 var resp *monitoringpb.Group 314 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 315 var err error 316 resp, err = c.groupClient.UpdateGroup(ctx, req, settings.GRPC...) 317 return err 318 }, opts...) 319 if err != nil { 320 return nil, err 321 } 322 return resp, nil 323} 324 325// DeleteGroup deletes an existing group. 326func (c *GroupClient) DeleteGroup(ctx context.Context, req *monitoringpb.DeleteGroupRequest, opts ...gax.CallOption) error { 327 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 328 cctx, cancel := context.WithTimeout(ctx, 30000*time.Millisecond) 329 defer cancel() 330 ctx = cctx 331 } 332 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 333 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 334 opts = append(c.CallOptions.DeleteGroup[0:len(c.CallOptions.DeleteGroup):len(c.CallOptions.DeleteGroup)], opts...) 335 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 336 var err error 337 _, err = c.groupClient.DeleteGroup(ctx, req, settings.GRPC...) 338 return err 339 }, opts...) 340 return err 341} 342 343// ListGroupMembers lists the monitored resources that are members of a group. 344func (c *GroupClient) ListGroupMembers(ctx context.Context, req *monitoringpb.ListGroupMembersRequest, opts ...gax.CallOption) *MonitoredResourceIterator { 345 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 346 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 347 opts = append(c.CallOptions.ListGroupMembers[0:len(c.CallOptions.ListGroupMembers):len(c.CallOptions.ListGroupMembers)], opts...) 348 it := &MonitoredResourceIterator{} 349 req = proto.Clone(req).(*monitoringpb.ListGroupMembersRequest) 350 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResource, string, error) { 351 var resp *monitoringpb.ListGroupMembersResponse 352 req.PageToken = pageToken 353 if pageSize > math.MaxInt32 { 354 req.PageSize = math.MaxInt32 355 } else { 356 req.PageSize = int32(pageSize) 357 } 358 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 359 var err error 360 resp, err = c.groupClient.ListGroupMembers(ctx, req, settings.GRPC...) 361 return err 362 }, opts...) 363 if err != nil { 364 return nil, "", err 365 } 366 367 it.Response = resp 368 return resp.GetMembers(), resp.GetNextPageToken(), nil 369 } 370 fetch := func(pageSize int, pageToken string) (string, error) { 371 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 372 if err != nil { 373 return "", err 374 } 375 it.items = append(it.items, items...) 376 return nextPageToken, nil 377 } 378 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 379 it.pageInfo.MaxSize = int(req.GetPageSize()) 380 it.pageInfo.Token = req.GetPageToken() 381 return it 382} 383 384// GroupIterator manages a stream of *monitoringpb.Group. 385type GroupIterator struct { 386 items []*monitoringpb.Group 387 pageInfo *iterator.PageInfo 388 nextFunc func() error 389 390 // Response is the raw response for the current page. 391 // It must be cast to the RPC response type. 392 // Calling Next() or InternalFetch() updates this value. 393 Response interface{} 394 395 // InternalFetch is for use by the Google Cloud Libraries only. 396 // It is not part of the stable interface of this package. 397 // 398 // InternalFetch returns results from a single call to the underlying RPC. 399 // The number of results is no greater than pageSize. 400 // If there are no more results, nextPageToken is empty and err is nil. 401 InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.Group, nextPageToken string, err error) 402} 403 404// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 405func (it *GroupIterator) PageInfo() *iterator.PageInfo { 406 return it.pageInfo 407} 408 409// Next returns the next result. Its second return value is iterator.Done if there are no more 410// results. Once Next returns Done, all subsequent calls will return Done. 411func (it *GroupIterator) Next() (*monitoringpb.Group, error) { 412 var item *monitoringpb.Group 413 if err := it.nextFunc(); err != nil { 414 return item, err 415 } 416 item = it.items[0] 417 it.items = it.items[1:] 418 return item, nil 419} 420 421func (it *GroupIterator) bufLen() int { 422 return len(it.items) 423} 424 425func (it *GroupIterator) takeBuf() interface{} { 426 b := it.items 427 it.items = nil 428 return b 429} 430 431// MonitoredResourceIterator manages a stream of *monitoredrespb.MonitoredResource. 432type MonitoredResourceIterator struct { 433 items []*monitoredrespb.MonitoredResource 434 pageInfo *iterator.PageInfo 435 nextFunc func() error 436 437 // Response is the raw response for the current page. 438 // It must be cast to the RPC response type. 439 // Calling Next() or InternalFetch() updates this value. 440 Response interface{} 441 442 // InternalFetch is for use by the Google Cloud Libraries only. 443 // It is not part of the stable interface of this package. 444 // 445 // InternalFetch returns results from a single call to the underlying RPC. 446 // The number of results is no greater than pageSize. 447 // If there are no more results, nextPageToken is empty and err is nil. 448 InternalFetch func(pageSize int, pageToken string) (results []*monitoredrespb.MonitoredResource, nextPageToken string, err error) 449} 450 451// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 452func (it *MonitoredResourceIterator) PageInfo() *iterator.PageInfo { 453 return it.pageInfo 454} 455 456// Next returns the next result. Its second return value is iterator.Done if there are no more 457// results. Once Next returns Done, all subsequent calls will return Done. 458func (it *MonitoredResourceIterator) Next() (*monitoredrespb.MonitoredResource, error) { 459 var item *monitoredrespb.MonitoredResource 460 if err := it.nextFunc(); err != nil { 461 return item, err 462 } 463 item = it.items[0] 464 it.items = it.items[1:] 465 return item, nil 466} 467 468func (it *MonitoredResourceIterator) bufLen() int { 469 return len(it.items) 470} 471 472func (it *MonitoredResourceIterator) takeBuf() interface{} { 473 b := it.items 474 it.items = nil 475 return b 476} 477