1// Copyright 2020 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 gapic-generator. 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/transport" 31 monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres" 32 monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" 33 "google.golang.org/grpc" 34 "google.golang.org/grpc/codes" 35 "google.golang.org/grpc/metadata" 36) 37 38// GroupCallOptions contains the retry settings for each method of GroupClient. 39type GroupCallOptions struct { 40 ListGroups []gax.CallOption 41 GetGroup []gax.CallOption 42 CreateGroup []gax.CallOption 43 UpdateGroup []gax.CallOption 44 DeleteGroup []gax.CallOption 45 ListGroupMembers []gax.CallOption 46} 47 48func defaultGroupClientOptions() []option.ClientOption { 49 return []option.ClientOption{ 50 option.WithEndpoint("monitoring.googleapis.com:443"), 51 option.WithScopes(DefaultAuthScopes()...), 52 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 53 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 54 } 55} 56 57func defaultGroupCallOptions() *GroupCallOptions { 58 retry := map[[2]string][]gax.CallOption{ 59 {"default", "idempotent"}: { 60 gax.WithRetry(func() gax.Retryer { 61 return gax.OnCodes([]codes.Code{ 62 codes.DeadlineExceeded, 63 codes.Unavailable, 64 }, gax.Backoff{ 65 Initial: 100 * time.Millisecond, 66 Max: 60000 * time.Millisecond, 67 Multiplier: 1.3, 68 }) 69 }), 70 }, 71 } 72 return &GroupCallOptions{ 73 ListGroups: retry[[2]string{"default", "idempotent"}], 74 GetGroup: retry[[2]string{"default", "idempotent"}], 75 CreateGroup: retry[[2]string{"default", "non_idempotent"}], 76 UpdateGroup: retry[[2]string{"default", "idempotent"}], 77 DeleteGroup: retry[[2]string{"default", "idempotent"}], 78 ListGroupMembers: retry[[2]string{"default", "idempotent"}], 79 } 80} 81 82// GroupClient is a client for interacting with Stackdriver Monitoring API. 83// 84// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 85type GroupClient struct { 86 // The connection to the service. 87 conn *grpc.ClientConn 88 89 // The gRPC API client. 90 groupClient monitoringpb.GroupServiceClient 91 92 // The call options for this service. 93 CallOptions *GroupCallOptions 94 95 // The x-goog-* metadata to be sent with each request. 96 xGoogMetadata metadata.MD 97} 98 99// NewGroupClient creates a new group service client. 100// 101// The Group API lets you inspect and manage your 102// groups (at #google.monitoring.v3.Group). 103// 104// A group is a named filter that is used to identify 105// a collection of monitored resources. Groups are typically used to 106// mirror the physical and/or logical topology of the environment. 107// Because group membership is computed dynamically, monitored 108// resources that are started in the future are automatically placed 109// in matching groups. By using a group to name monitored resources in, 110// for example, an alert policy, the target of that alert policy is 111// updated automatically as monitored resources are added and removed 112// from the infrastructure. 113func NewGroupClient(ctx context.Context, opts ...option.ClientOption) (*GroupClient, error) { 114 conn, err := transport.DialGRPC(ctx, append(defaultGroupClientOptions(), opts...)...) 115 if err != nil { 116 return nil, err 117 } 118 c := &GroupClient{ 119 conn: conn, 120 CallOptions: defaultGroupCallOptions(), 121 122 groupClient: monitoringpb.NewGroupServiceClient(conn), 123 } 124 c.setGoogleClientInfo() 125 return c, nil 126} 127 128// Connection returns the client's connection to the API service. 129func (c *GroupClient) Connection() *grpc.ClientConn { 130 return c.conn 131} 132 133// Close closes the connection to the API service. The user should invoke this when 134// the client is no longer required. 135func (c *GroupClient) Close() error { 136 return c.conn.Close() 137} 138 139// setGoogleClientInfo sets the name and version of the application in 140// the `x-goog-api-client` header passed on each request. Intended for 141// use by Google-written clients. 142func (c *GroupClient) setGoogleClientInfo(keyval ...string) { 143 kv := append([]string{"gl-go", versionGo()}, keyval...) 144 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 145 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 146} 147 148// ListGroups lists the existing groups. 149func (c *GroupClient) ListGroups(ctx context.Context, req *monitoringpb.ListGroupsRequest, opts ...gax.CallOption) *GroupIterator { 150 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 151 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 152 opts = append(c.CallOptions.ListGroups[0:len(c.CallOptions.ListGroups):len(c.CallOptions.ListGroups)], opts...) 153 it := &GroupIterator{} 154 req = proto.Clone(req).(*monitoringpb.ListGroupsRequest) 155 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Group, string, error) { 156 var resp *monitoringpb.ListGroupsResponse 157 req.PageToken = pageToken 158 if pageSize > math.MaxInt32 { 159 req.PageSize = math.MaxInt32 160 } else { 161 req.PageSize = int32(pageSize) 162 } 163 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 164 var err error 165 resp, err = c.groupClient.ListGroups(ctx, req, settings.GRPC...) 166 return err 167 }, opts...) 168 if err != nil { 169 return nil, "", err 170 } 171 return resp.Group, resp.NextPageToken, nil 172 } 173 fetch := func(pageSize int, pageToken string) (string, error) { 174 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 175 if err != nil { 176 return "", err 177 } 178 it.items = append(it.items, items...) 179 return nextPageToken, nil 180 } 181 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 182 it.pageInfo.MaxSize = int(req.PageSize) 183 it.pageInfo.Token = req.PageToken 184 return it 185} 186 187// GetGroup gets a single group. 188func (c *GroupClient) GetGroup(ctx context.Context, req *monitoringpb.GetGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) { 189 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 190 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 191 opts = append(c.CallOptions.GetGroup[0:len(c.CallOptions.GetGroup):len(c.CallOptions.GetGroup)], opts...) 192 var resp *monitoringpb.Group 193 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 194 var err error 195 resp, err = c.groupClient.GetGroup(ctx, req, settings.GRPC...) 196 return err 197 }, opts...) 198 if err != nil { 199 return nil, err 200 } 201 return resp, nil 202} 203 204// CreateGroup creates a new group. 205func (c *GroupClient) CreateGroup(ctx context.Context, req *monitoringpb.CreateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) { 206 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 207 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 208 opts = append(c.CallOptions.CreateGroup[0:len(c.CallOptions.CreateGroup):len(c.CallOptions.CreateGroup)], opts...) 209 var resp *monitoringpb.Group 210 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 211 var err error 212 resp, err = c.groupClient.CreateGroup(ctx, req, settings.GRPC...) 213 return err 214 }, opts...) 215 if err != nil { 216 return nil, err 217 } 218 return resp, nil 219} 220 221// UpdateGroup updates an existing group. 222// You can change any group attributes except name. 223func (c *GroupClient) UpdateGroup(ctx context.Context, req *monitoringpb.UpdateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) { 224 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "group.name", url.QueryEscape(req.GetGroup().GetName()))) 225 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 226 opts = append(c.CallOptions.UpdateGroup[0:len(c.CallOptions.UpdateGroup):len(c.CallOptions.UpdateGroup)], opts...) 227 var resp *monitoringpb.Group 228 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 229 var err error 230 resp, err = c.groupClient.UpdateGroup(ctx, req, settings.GRPC...) 231 return err 232 }, opts...) 233 if err != nil { 234 return nil, err 235 } 236 return resp, nil 237} 238 239// DeleteGroup deletes an existing group. 240func (c *GroupClient) DeleteGroup(ctx context.Context, req *monitoringpb.DeleteGroupRequest, opts ...gax.CallOption) error { 241 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 242 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 243 opts = append(c.CallOptions.DeleteGroup[0:len(c.CallOptions.DeleteGroup):len(c.CallOptions.DeleteGroup)], opts...) 244 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 245 var err error 246 _, err = c.groupClient.DeleteGroup(ctx, req, settings.GRPC...) 247 return err 248 }, opts...) 249 return err 250} 251 252// ListGroupMembers lists the monitored resources that are members of a group. 253func (c *GroupClient) ListGroupMembers(ctx context.Context, req *monitoringpb.ListGroupMembersRequest, opts ...gax.CallOption) *MonitoredResourceIterator { 254 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 255 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 256 opts = append(c.CallOptions.ListGroupMembers[0:len(c.CallOptions.ListGroupMembers):len(c.CallOptions.ListGroupMembers)], opts...) 257 it := &MonitoredResourceIterator{} 258 req = proto.Clone(req).(*monitoringpb.ListGroupMembersRequest) 259 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResource, string, error) { 260 var resp *monitoringpb.ListGroupMembersResponse 261 req.PageToken = pageToken 262 if pageSize > math.MaxInt32 { 263 req.PageSize = math.MaxInt32 264 } else { 265 req.PageSize = int32(pageSize) 266 } 267 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 268 var err error 269 resp, err = c.groupClient.ListGroupMembers(ctx, req, settings.GRPC...) 270 return err 271 }, opts...) 272 if err != nil { 273 return nil, "", err 274 } 275 return resp.Members, resp.NextPageToken, nil 276 } 277 fetch := func(pageSize int, pageToken string) (string, error) { 278 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 279 if err != nil { 280 return "", err 281 } 282 it.items = append(it.items, items...) 283 return nextPageToken, nil 284 } 285 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 286 it.pageInfo.MaxSize = int(req.PageSize) 287 it.pageInfo.Token = req.PageToken 288 return it 289} 290 291// GroupIterator manages a stream of *monitoringpb.Group. 292type GroupIterator struct { 293 items []*monitoringpb.Group 294 pageInfo *iterator.PageInfo 295 nextFunc func() error 296 297 // InternalFetch is for use by the Google Cloud Libraries only. 298 // It is not part of the stable interface of this package. 299 // 300 // InternalFetch returns results from a single call to the underlying RPC. 301 // The number of results is no greater than pageSize. 302 // If there are no more results, nextPageToken is empty and err is nil. 303 InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.Group, nextPageToken string, err error) 304} 305 306// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 307func (it *GroupIterator) PageInfo() *iterator.PageInfo { 308 return it.pageInfo 309} 310 311// Next returns the next result. Its second return value is iterator.Done if there are no more 312// results. Once Next returns Done, all subsequent calls will return Done. 313func (it *GroupIterator) Next() (*monitoringpb.Group, error) { 314 var item *monitoringpb.Group 315 if err := it.nextFunc(); err != nil { 316 return item, err 317 } 318 item = it.items[0] 319 it.items = it.items[1:] 320 return item, nil 321} 322 323func (it *GroupIterator) bufLen() int { 324 return len(it.items) 325} 326 327func (it *GroupIterator) takeBuf() interface{} { 328 b := it.items 329 it.items = nil 330 return b 331} 332 333// MonitoredResourceIterator manages a stream of *monitoredrespb.MonitoredResource. 334type MonitoredResourceIterator struct { 335 items []*monitoredrespb.MonitoredResource 336 pageInfo *iterator.PageInfo 337 nextFunc func() error 338 339 // InternalFetch is for use by the Google Cloud Libraries only. 340 // It is not part of the stable interface of this package. 341 // 342 // InternalFetch returns results from a single call to the underlying RPC. 343 // The number of results is no greater than pageSize. 344 // If there are no more results, nextPageToken is empty and err is nil. 345 InternalFetch func(pageSize int, pageToken string) (results []*monitoredrespb.MonitoredResource, nextPageToken string, err error) 346} 347 348// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 349func (it *MonitoredResourceIterator) PageInfo() *iterator.PageInfo { 350 return it.pageInfo 351} 352 353// Next returns the next result. Its second return value is iterator.Done if there are no more 354// results. Once Next returns Done, all subsequent calls will return Done. 355func (it *MonitoredResourceIterator) Next() (*monitoredrespb.MonitoredResource, error) { 356 var item *monitoredrespb.MonitoredResource 357 if err := it.nextFunc(); err != nil { 358 return item, err 359 } 360 item = it.items[0] 361 it.items = it.items[1:] 362 return item, nil 363} 364 365func (it *MonitoredResourceIterator) bufLen() int { 366 return len(it.items) 367} 368 369func (it *MonitoredResourceIterator) takeBuf() interface{} { 370 b := it.items 371 it.items = nil 372 return b 373} 374