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 monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" 32 "google.golang.org/grpc" 33 "google.golang.org/grpc/codes" 34 "google.golang.org/grpc/metadata" 35) 36 37// ServiceMonitoringCallOptions contains the retry settings for each method of ServiceMonitoringClient. 38type ServiceMonitoringCallOptions struct { 39 CreateService []gax.CallOption 40 GetService []gax.CallOption 41 ListServices []gax.CallOption 42 UpdateService []gax.CallOption 43 DeleteService []gax.CallOption 44 CreateServiceLevelObjective []gax.CallOption 45 GetServiceLevelObjective []gax.CallOption 46 ListServiceLevelObjectives []gax.CallOption 47 UpdateServiceLevelObjective []gax.CallOption 48 DeleteServiceLevelObjective []gax.CallOption 49} 50 51func defaultServiceMonitoringClientOptions() []option.ClientOption { 52 return []option.ClientOption{ 53 option.WithEndpoint("monitoring.googleapis.com:443"), 54 option.WithScopes(DefaultAuthScopes()...), 55 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 56 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 57 } 58} 59 60func defaultServiceMonitoringCallOptions() *ServiceMonitoringCallOptions { 61 retry := map[[2]string][]gax.CallOption{ 62 {"default", "idempotent"}: { 63 gax.WithRetry(func() gax.Retryer { 64 return gax.OnCodes([]codes.Code{ 65 codes.DeadlineExceeded, 66 codes.Unavailable, 67 }, gax.Backoff{ 68 Initial: 100 * time.Millisecond, 69 Max: 60000 * time.Millisecond, 70 Multiplier: 1.3, 71 }) 72 }), 73 }, 74 } 75 return &ServiceMonitoringCallOptions{ 76 CreateService: retry[[2]string{"default", "non_idempotent"}], 77 GetService: retry[[2]string{"default", "idempotent"}], 78 ListServices: retry[[2]string{"default", "idempotent"}], 79 UpdateService: retry[[2]string{"default", "non_idempotent"}], 80 DeleteService: retry[[2]string{"default", "idempotent"}], 81 CreateServiceLevelObjective: retry[[2]string{"default", "non_idempotent"}], 82 GetServiceLevelObjective: retry[[2]string{"default", "idempotent"}], 83 ListServiceLevelObjectives: retry[[2]string{"default", "idempotent"}], 84 UpdateServiceLevelObjective: retry[[2]string{"default", "non_idempotent"}], 85 DeleteServiceLevelObjective: retry[[2]string{"default", "idempotent"}], 86 } 87} 88 89// ServiceMonitoringClient is a client for interacting with Stackdriver Monitoring API. 90// 91// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 92type ServiceMonitoringClient struct { 93 // The connection to the service. 94 conn *grpc.ClientConn 95 96 // The gRPC API client. 97 serviceMonitoringClient monitoringpb.ServiceMonitoringServiceClient 98 99 // The call options for this service. 100 CallOptions *ServiceMonitoringCallOptions 101 102 // The x-goog-* metadata to be sent with each request. 103 xGoogMetadata metadata.MD 104} 105 106// NewServiceMonitoringClient creates a new service monitoring service client. 107// 108// The Stackdriver Monitoring Service-Oriented Monitoring API has endpoints for 109// managing and querying aspects of a workspace's services. These include the 110// Service's monitored resources, its Service-Level Objectives, and a taxonomy 111// of categorized Health Metrics. 112func NewServiceMonitoringClient(ctx context.Context, opts ...option.ClientOption) (*ServiceMonitoringClient, error) { 113 conn, err := transport.DialGRPC(ctx, append(defaultServiceMonitoringClientOptions(), opts...)...) 114 if err != nil { 115 return nil, err 116 } 117 c := &ServiceMonitoringClient{ 118 conn: conn, 119 CallOptions: defaultServiceMonitoringCallOptions(), 120 121 serviceMonitoringClient: monitoringpb.NewServiceMonitoringServiceClient(conn), 122 } 123 c.setGoogleClientInfo() 124 return c, nil 125} 126 127// Connection returns the client's connection to the API service. 128func (c *ServiceMonitoringClient) Connection() *grpc.ClientConn { 129 return c.conn 130} 131 132// Close closes the connection to the API service. The user should invoke this when 133// the client is no longer required. 134func (c *ServiceMonitoringClient) Close() error { 135 return c.conn.Close() 136} 137 138// setGoogleClientInfo sets the name and version of the application in 139// the `x-goog-api-client` header passed on each request. Intended for 140// use by Google-written clients. 141func (c *ServiceMonitoringClient) setGoogleClientInfo(keyval ...string) { 142 kv := append([]string{"gl-go", versionGo()}, keyval...) 143 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 144 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 145} 146 147// CreateService create a Service. 148func (c *ServiceMonitoringClient) CreateService(ctx context.Context, req *monitoringpb.CreateServiceRequest, opts ...gax.CallOption) (*monitoringpb.Service, error) { 149 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 150 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 151 opts = append(c.CallOptions.CreateService[0:len(c.CallOptions.CreateService):len(c.CallOptions.CreateService)], opts...) 152 var resp *monitoringpb.Service 153 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 154 var err error 155 resp, err = c.serviceMonitoringClient.CreateService(ctx, req, settings.GRPC...) 156 return err 157 }, opts...) 158 if err != nil { 159 return nil, err 160 } 161 return resp, nil 162} 163 164// GetService get the named Service. 165func (c *ServiceMonitoringClient) GetService(ctx context.Context, req *monitoringpb.GetServiceRequest, opts ...gax.CallOption) (*monitoringpb.Service, error) { 166 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 167 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 168 opts = append(c.CallOptions.GetService[0:len(c.CallOptions.GetService):len(c.CallOptions.GetService)], opts...) 169 var resp *monitoringpb.Service 170 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 171 var err error 172 resp, err = c.serviceMonitoringClient.GetService(ctx, req, settings.GRPC...) 173 return err 174 }, opts...) 175 if err != nil { 176 return nil, err 177 } 178 return resp, nil 179} 180 181// ListServices list Services for this workspace. 182func (c *ServiceMonitoringClient) ListServices(ctx context.Context, req *monitoringpb.ListServicesRequest, opts ...gax.CallOption) *ServiceIterator { 183 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 184 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 185 opts = append(c.CallOptions.ListServices[0:len(c.CallOptions.ListServices):len(c.CallOptions.ListServices)], opts...) 186 it := &ServiceIterator{} 187 req = proto.Clone(req).(*monitoringpb.ListServicesRequest) 188 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Service, string, error) { 189 var resp *monitoringpb.ListServicesResponse 190 req.PageToken = pageToken 191 if pageSize > math.MaxInt32 { 192 req.PageSize = math.MaxInt32 193 } else { 194 req.PageSize = int32(pageSize) 195 } 196 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 197 var err error 198 resp, err = c.serviceMonitoringClient.ListServices(ctx, req, settings.GRPC...) 199 return err 200 }, opts...) 201 if err != nil { 202 return nil, "", err 203 } 204 return resp.Services, resp.NextPageToken, nil 205 } 206 fetch := func(pageSize int, pageToken string) (string, error) { 207 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 208 if err != nil { 209 return "", err 210 } 211 it.items = append(it.items, items...) 212 return nextPageToken, nil 213 } 214 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 215 it.pageInfo.MaxSize = int(req.PageSize) 216 it.pageInfo.Token = req.PageToken 217 return it 218} 219 220// UpdateService update this Service. 221func (c *ServiceMonitoringClient) UpdateService(ctx context.Context, req *monitoringpb.UpdateServiceRequest, opts ...gax.CallOption) (*monitoringpb.Service, error) { 222 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "service.name", url.QueryEscape(req.GetService().GetName()))) 223 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 224 opts = append(c.CallOptions.UpdateService[0:len(c.CallOptions.UpdateService):len(c.CallOptions.UpdateService)], opts...) 225 var resp *monitoringpb.Service 226 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 227 var err error 228 resp, err = c.serviceMonitoringClient.UpdateService(ctx, req, settings.GRPC...) 229 return err 230 }, opts...) 231 if err != nil { 232 return nil, err 233 } 234 return resp, nil 235} 236 237// DeleteService soft delete this Service. 238func (c *ServiceMonitoringClient) DeleteService(ctx context.Context, req *monitoringpb.DeleteServiceRequest, opts ...gax.CallOption) error { 239 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 240 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 241 opts = append(c.CallOptions.DeleteService[0:len(c.CallOptions.DeleteService):len(c.CallOptions.DeleteService)], opts...) 242 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 243 var err error 244 _, err = c.serviceMonitoringClient.DeleteService(ctx, req, settings.GRPC...) 245 return err 246 }, opts...) 247 return err 248} 249 250// CreateServiceLevelObjective create a ServiceLevelObjective for the given Service. 251func (c *ServiceMonitoringClient) CreateServiceLevelObjective(ctx context.Context, req *monitoringpb.CreateServiceLevelObjectiveRequest, opts ...gax.CallOption) (*monitoringpb.ServiceLevelObjective, error) { 252 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 253 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 254 opts = append(c.CallOptions.CreateServiceLevelObjective[0:len(c.CallOptions.CreateServiceLevelObjective):len(c.CallOptions.CreateServiceLevelObjective)], opts...) 255 var resp *monitoringpb.ServiceLevelObjective 256 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 257 var err error 258 resp, err = c.serviceMonitoringClient.CreateServiceLevelObjective(ctx, req, settings.GRPC...) 259 return err 260 }, opts...) 261 if err != nil { 262 return nil, err 263 } 264 return resp, nil 265} 266 267// GetServiceLevelObjective get a ServiceLevelObjective by name. 268func (c *ServiceMonitoringClient) GetServiceLevelObjective(ctx context.Context, req *monitoringpb.GetServiceLevelObjectiveRequest, opts ...gax.CallOption) (*monitoringpb.ServiceLevelObjective, error) { 269 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 270 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 271 opts = append(c.CallOptions.GetServiceLevelObjective[0:len(c.CallOptions.GetServiceLevelObjective):len(c.CallOptions.GetServiceLevelObjective)], opts...) 272 var resp *monitoringpb.ServiceLevelObjective 273 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 274 var err error 275 resp, err = c.serviceMonitoringClient.GetServiceLevelObjective(ctx, req, settings.GRPC...) 276 return err 277 }, opts...) 278 if err != nil { 279 return nil, err 280 } 281 return resp, nil 282} 283 284// ListServiceLevelObjectives list the ServiceLevelObjectives for the given Service. 285func (c *ServiceMonitoringClient) ListServiceLevelObjectives(ctx context.Context, req *monitoringpb.ListServiceLevelObjectivesRequest, opts ...gax.CallOption) *ServiceLevelObjectiveIterator { 286 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 287 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 288 opts = append(c.CallOptions.ListServiceLevelObjectives[0:len(c.CallOptions.ListServiceLevelObjectives):len(c.CallOptions.ListServiceLevelObjectives)], opts...) 289 it := &ServiceLevelObjectiveIterator{} 290 req = proto.Clone(req).(*monitoringpb.ListServiceLevelObjectivesRequest) 291 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.ServiceLevelObjective, string, error) { 292 var resp *monitoringpb.ListServiceLevelObjectivesResponse 293 req.PageToken = pageToken 294 if pageSize > math.MaxInt32 { 295 req.PageSize = math.MaxInt32 296 } else { 297 req.PageSize = int32(pageSize) 298 } 299 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 300 var err error 301 resp, err = c.serviceMonitoringClient.ListServiceLevelObjectives(ctx, req, settings.GRPC...) 302 return err 303 }, opts...) 304 if err != nil { 305 return nil, "", err 306 } 307 return resp.ServiceLevelObjectives, resp.NextPageToken, nil 308 } 309 fetch := func(pageSize int, pageToken string) (string, error) { 310 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 311 if err != nil { 312 return "", err 313 } 314 it.items = append(it.items, items...) 315 return nextPageToken, nil 316 } 317 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 318 it.pageInfo.MaxSize = int(req.PageSize) 319 it.pageInfo.Token = req.PageToken 320 return it 321} 322 323// UpdateServiceLevelObjective update the given ServiceLevelObjective. 324func (c *ServiceMonitoringClient) UpdateServiceLevelObjective(ctx context.Context, req *monitoringpb.UpdateServiceLevelObjectiveRequest, opts ...gax.CallOption) (*monitoringpb.ServiceLevelObjective, error) { 325 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "service_level_objective.name", url.QueryEscape(req.GetServiceLevelObjective().GetName()))) 326 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 327 opts = append(c.CallOptions.UpdateServiceLevelObjective[0:len(c.CallOptions.UpdateServiceLevelObjective):len(c.CallOptions.UpdateServiceLevelObjective)], opts...) 328 var resp *monitoringpb.ServiceLevelObjective 329 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 330 var err error 331 resp, err = c.serviceMonitoringClient.UpdateServiceLevelObjective(ctx, req, settings.GRPC...) 332 return err 333 }, opts...) 334 if err != nil { 335 return nil, err 336 } 337 return resp, nil 338} 339 340// DeleteServiceLevelObjective delete the given ServiceLevelObjective. 341func (c *ServiceMonitoringClient) DeleteServiceLevelObjective(ctx context.Context, req *monitoringpb.DeleteServiceLevelObjectiveRequest, opts ...gax.CallOption) error { 342 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 343 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 344 opts = append(c.CallOptions.DeleteServiceLevelObjective[0:len(c.CallOptions.DeleteServiceLevelObjective):len(c.CallOptions.DeleteServiceLevelObjective)], opts...) 345 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 346 var err error 347 _, err = c.serviceMonitoringClient.DeleteServiceLevelObjective(ctx, req, settings.GRPC...) 348 return err 349 }, opts...) 350 return err 351} 352 353// ServiceIterator manages a stream of *monitoringpb.Service. 354type ServiceIterator struct { 355 items []*monitoringpb.Service 356 pageInfo *iterator.PageInfo 357 nextFunc func() error 358 359 // InternalFetch is for use by the Google Cloud Libraries only. 360 // It is not part of the stable interface of this package. 361 // 362 // InternalFetch returns results from a single call to the underlying RPC. 363 // The number of results is no greater than pageSize. 364 // If there are no more results, nextPageToken is empty and err is nil. 365 InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.Service, nextPageToken string, err error) 366} 367 368// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 369func (it *ServiceIterator) PageInfo() *iterator.PageInfo { 370 return it.pageInfo 371} 372 373// Next returns the next result. Its second return value is iterator.Done if there are no more 374// results. Once Next returns Done, all subsequent calls will return Done. 375func (it *ServiceIterator) Next() (*monitoringpb.Service, error) { 376 var item *monitoringpb.Service 377 if err := it.nextFunc(); err != nil { 378 return item, err 379 } 380 item = it.items[0] 381 it.items = it.items[1:] 382 return item, nil 383} 384 385func (it *ServiceIterator) bufLen() int { 386 return len(it.items) 387} 388 389func (it *ServiceIterator) takeBuf() interface{} { 390 b := it.items 391 it.items = nil 392 return b 393} 394 395// ServiceLevelObjectiveIterator manages a stream of *monitoringpb.ServiceLevelObjective. 396type ServiceLevelObjectiveIterator struct { 397 items []*monitoringpb.ServiceLevelObjective 398 pageInfo *iterator.PageInfo 399 nextFunc func() error 400 401 // InternalFetch is for use by the Google Cloud Libraries only. 402 // It is not part of the stable interface of this package. 403 // 404 // InternalFetch returns results from a single call to the underlying RPC. 405 // The number of results is no greater than pageSize. 406 // If there are no more results, nextPageToken is empty and err is nil. 407 InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.ServiceLevelObjective, nextPageToken string, err error) 408} 409 410// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 411func (it *ServiceLevelObjectiveIterator) PageInfo() *iterator.PageInfo { 412 return it.pageInfo 413} 414 415// Next returns the next result. Its second return value is iterator.Done if there are no more 416// results. Once Next returns Done, all subsequent calls will return Done. 417func (it *ServiceLevelObjectiveIterator) Next() (*monitoringpb.ServiceLevelObjective, error) { 418 var item *monitoringpb.ServiceLevelObjective 419 if err := it.nextFunc(); err != nil { 420 return item, err 421 } 422 item = it.items[0] 423 it.items = it.items[1:] 424 return item, nil 425} 426 427func (it *ServiceLevelObjectiveIterator) bufLen() int { 428 return len(it.items) 429} 430 431func (it *ServiceLevelObjectiveIterator) takeBuf() interface{} { 432 b := it.items 433 it.items = nil 434 return b 435} 436