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 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	gtransport "google.golang.org/api/transport/grpc"
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.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
55		option.WithScopes(DefaultAuthScopes()...),
56		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
57			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
58	}
59}
60
61func defaultServiceMonitoringCallOptions() *ServiceMonitoringCallOptions {
62	return &ServiceMonitoringCallOptions{
63		CreateService: []gax.CallOption{},
64		GetService: []gax.CallOption{
65			gax.WithRetry(func() gax.Retryer {
66				return gax.OnCodes([]codes.Code{
67					codes.DeadlineExceeded,
68					codes.Unavailable,
69				}, gax.Backoff{
70					Initial:    100 * time.Millisecond,
71					Max:        30000 * time.Millisecond,
72					Multiplier: 1.30,
73				})
74			}),
75		},
76		ListServices: []gax.CallOption{
77			gax.WithRetry(func() gax.Retryer {
78				return gax.OnCodes([]codes.Code{
79					codes.DeadlineExceeded,
80					codes.Unavailable,
81				}, gax.Backoff{
82					Initial:    100 * time.Millisecond,
83					Max:        30000 * time.Millisecond,
84					Multiplier: 1.30,
85				})
86			}),
87		},
88		UpdateService: []gax.CallOption{},
89		DeleteService: []gax.CallOption{
90			gax.WithRetry(func() gax.Retryer {
91				return gax.OnCodes([]codes.Code{
92					codes.DeadlineExceeded,
93					codes.Unavailable,
94				}, gax.Backoff{
95					Initial:    100 * time.Millisecond,
96					Max:        30000 * time.Millisecond,
97					Multiplier: 1.30,
98				})
99			}),
100		},
101		CreateServiceLevelObjective: []gax.CallOption{},
102		GetServiceLevelObjective: []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		ListServiceLevelObjectives: []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		UpdateServiceLevelObjective: []gax.CallOption{},
127		DeleteServiceLevelObjective: []gax.CallOption{
128			gax.WithRetry(func() gax.Retryer {
129				return gax.OnCodes([]codes.Code{
130					codes.DeadlineExceeded,
131					codes.Unavailable,
132				}, gax.Backoff{
133					Initial:    100 * time.Millisecond,
134					Max:        30000 * time.Millisecond,
135					Multiplier: 1.30,
136				})
137			}),
138		},
139	}
140}
141
142// ServiceMonitoringClient is a client for interacting with Cloud Monitoring API.
143//
144// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
145type ServiceMonitoringClient struct {
146	// Connection pool of gRPC connections to the service.
147	connPool gtransport.ConnPool
148
149	// The gRPC API client.
150	serviceMonitoringClient monitoringpb.ServiceMonitoringServiceClient
151
152	// The call options for this service.
153	CallOptions *ServiceMonitoringCallOptions
154
155	// The x-goog-* metadata to be sent with each request.
156	xGoogMetadata metadata.MD
157}
158
159// NewServiceMonitoringClient creates a new service monitoring service client.
160//
161// The Cloud Monitoring Service-Oriented Monitoring API has endpoints for
162// managing and querying aspects of a workspace’s services. These include the
163// Service's monitored resources, its Service-Level Objectives, and a taxonomy
164// of categorized Health Metrics.
165func NewServiceMonitoringClient(ctx context.Context, opts ...option.ClientOption) (*ServiceMonitoringClient, error) {
166	connPool, err := gtransport.DialPool(ctx, append(defaultServiceMonitoringClientOptions(), opts...)...)
167	if err != nil {
168		return nil, err
169	}
170	c := &ServiceMonitoringClient{
171		connPool:    connPool,
172		CallOptions: defaultServiceMonitoringCallOptions(),
173
174		serviceMonitoringClient: monitoringpb.NewServiceMonitoringServiceClient(connPool),
175	}
176	c.setGoogleClientInfo()
177
178	return c, nil
179}
180
181// Connection returns a connection to the API service.
182//
183// Deprecated.
184func (c *ServiceMonitoringClient) Connection() *grpc.ClientConn {
185	return c.connPool.Conn()
186}
187
188// Close closes the connection to the API service. The user should invoke this when
189// the client is no longer required.
190func (c *ServiceMonitoringClient) Close() error {
191	return c.connPool.Close()
192}
193
194// setGoogleClientInfo sets the name and version of the application in
195// the `x-goog-api-client` header passed on each request. Intended for
196// use by Google-written clients.
197func (c *ServiceMonitoringClient) setGoogleClientInfo(keyval ...string) {
198	kv := append([]string{"gl-go", versionGo()}, keyval...)
199	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
200	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
201}
202
203// CreateService create a Service.
204func (c *ServiceMonitoringClient) CreateService(ctx context.Context, req *monitoringpb.CreateServiceRequest, opts ...gax.CallOption) (*monitoringpb.Service, error) {
205	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
206	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
207	opts = append(c.CallOptions.CreateService[0:len(c.CallOptions.CreateService):len(c.CallOptions.CreateService)], opts...)
208	var resp *monitoringpb.Service
209	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
210		var err error
211		resp, err = c.serviceMonitoringClient.CreateService(ctx, req, settings.GRPC...)
212		return err
213	}, opts...)
214	if err != nil {
215		return nil, err
216	}
217	return resp, nil
218}
219
220// GetService get the named Service.
221func (c *ServiceMonitoringClient) GetService(ctx context.Context, req *monitoringpb.GetServiceRequest, opts ...gax.CallOption) (*monitoringpb.Service, error) {
222	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
223	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
224	opts = append(c.CallOptions.GetService[0:len(c.CallOptions.GetService):len(c.CallOptions.GetService)], 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.GetService(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// ListServices list Services for this workspace.
238func (c *ServiceMonitoringClient) ListServices(ctx context.Context, req *monitoringpb.ListServicesRequest, opts ...gax.CallOption) *ServiceIterator {
239	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
240	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
241	opts = append(c.CallOptions.ListServices[0:len(c.CallOptions.ListServices):len(c.CallOptions.ListServices)], opts...)
242	it := &ServiceIterator{}
243	req = proto.Clone(req).(*monitoringpb.ListServicesRequest)
244	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Service, string, error) {
245		var resp *monitoringpb.ListServicesResponse
246		req.PageToken = pageToken
247		if pageSize > math.MaxInt32 {
248			req.PageSize = math.MaxInt32
249		} else {
250			req.PageSize = int32(pageSize)
251		}
252		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
253			var err error
254			resp, err = c.serviceMonitoringClient.ListServices(ctx, req, settings.GRPC...)
255			return err
256		}, opts...)
257		if err != nil {
258			return nil, "", err
259		}
260
261		it.Response = resp
262		return resp.Services, resp.NextPageToken, nil
263	}
264	fetch := func(pageSize int, pageToken string) (string, error) {
265		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
266		if err != nil {
267			return "", err
268		}
269		it.items = append(it.items, items...)
270		return nextPageToken, nil
271	}
272	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
273	it.pageInfo.MaxSize = int(req.PageSize)
274	it.pageInfo.Token = req.PageToken
275	return it
276}
277
278// UpdateService update this Service.
279func (c *ServiceMonitoringClient) UpdateService(ctx context.Context, req *monitoringpb.UpdateServiceRequest, opts ...gax.CallOption) (*monitoringpb.Service, error) {
280	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "service.name", url.QueryEscape(req.GetService().GetName())))
281	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
282	opts = append(c.CallOptions.UpdateService[0:len(c.CallOptions.UpdateService):len(c.CallOptions.UpdateService)], opts...)
283	var resp *monitoringpb.Service
284	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
285		var err error
286		resp, err = c.serviceMonitoringClient.UpdateService(ctx, req, settings.GRPC...)
287		return err
288	}, opts...)
289	if err != nil {
290		return nil, err
291	}
292	return resp, nil
293}
294
295// DeleteService soft delete this Service.
296func (c *ServiceMonitoringClient) DeleteService(ctx context.Context, req *monitoringpb.DeleteServiceRequest, opts ...gax.CallOption) error {
297	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
298	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
299	opts = append(c.CallOptions.DeleteService[0:len(c.CallOptions.DeleteService):len(c.CallOptions.DeleteService)], opts...)
300	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
301		var err error
302		_, err = c.serviceMonitoringClient.DeleteService(ctx, req, settings.GRPC...)
303		return err
304	}, opts...)
305	return err
306}
307
308// CreateServiceLevelObjective create a ServiceLevelObjective for the given Service.
309func (c *ServiceMonitoringClient) CreateServiceLevelObjective(ctx context.Context, req *monitoringpb.CreateServiceLevelObjectiveRequest, opts ...gax.CallOption) (*monitoringpb.ServiceLevelObjective, error) {
310	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
311	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
312	opts = append(c.CallOptions.CreateServiceLevelObjective[0:len(c.CallOptions.CreateServiceLevelObjective):len(c.CallOptions.CreateServiceLevelObjective)], opts...)
313	var resp *monitoringpb.ServiceLevelObjective
314	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
315		var err error
316		resp, err = c.serviceMonitoringClient.CreateServiceLevelObjective(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// GetServiceLevelObjective get a ServiceLevelObjective by name.
326func (c *ServiceMonitoringClient) GetServiceLevelObjective(ctx context.Context, req *monitoringpb.GetServiceLevelObjectiveRequest, opts ...gax.CallOption) (*monitoringpb.ServiceLevelObjective, error) {
327	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
328	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
329	opts = append(c.CallOptions.GetServiceLevelObjective[0:len(c.CallOptions.GetServiceLevelObjective):len(c.CallOptions.GetServiceLevelObjective)], opts...)
330	var resp *monitoringpb.ServiceLevelObjective
331	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
332		var err error
333		resp, err = c.serviceMonitoringClient.GetServiceLevelObjective(ctx, req, settings.GRPC...)
334		return err
335	}, opts...)
336	if err != nil {
337		return nil, err
338	}
339	return resp, nil
340}
341
342// ListServiceLevelObjectives list the ServiceLevelObjectives for the given Service.
343func (c *ServiceMonitoringClient) ListServiceLevelObjectives(ctx context.Context, req *monitoringpb.ListServiceLevelObjectivesRequest, opts ...gax.CallOption) *ServiceLevelObjectiveIterator {
344	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
345	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
346	opts = append(c.CallOptions.ListServiceLevelObjectives[0:len(c.CallOptions.ListServiceLevelObjectives):len(c.CallOptions.ListServiceLevelObjectives)], opts...)
347	it := &ServiceLevelObjectiveIterator{}
348	req = proto.Clone(req).(*monitoringpb.ListServiceLevelObjectivesRequest)
349	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.ServiceLevelObjective, string, error) {
350		var resp *monitoringpb.ListServiceLevelObjectivesResponse
351		req.PageToken = pageToken
352		if pageSize > math.MaxInt32 {
353			req.PageSize = math.MaxInt32
354		} else {
355			req.PageSize = int32(pageSize)
356		}
357		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
358			var err error
359			resp, err = c.serviceMonitoringClient.ListServiceLevelObjectives(ctx, req, settings.GRPC...)
360			return err
361		}, opts...)
362		if err != nil {
363			return nil, "", err
364		}
365
366		it.Response = resp
367		return resp.ServiceLevelObjectives, resp.NextPageToken, nil
368	}
369	fetch := func(pageSize int, pageToken string) (string, error) {
370		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
371		if err != nil {
372			return "", err
373		}
374		it.items = append(it.items, items...)
375		return nextPageToken, nil
376	}
377	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
378	it.pageInfo.MaxSize = int(req.PageSize)
379	it.pageInfo.Token = req.PageToken
380	return it
381}
382
383// UpdateServiceLevelObjective update the given ServiceLevelObjective.
384func (c *ServiceMonitoringClient) UpdateServiceLevelObjective(ctx context.Context, req *monitoringpb.UpdateServiceLevelObjectiveRequest, opts ...gax.CallOption) (*monitoringpb.ServiceLevelObjective, error) {
385	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "service_level_objective.name", url.QueryEscape(req.GetServiceLevelObjective().GetName())))
386	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
387	opts = append(c.CallOptions.UpdateServiceLevelObjective[0:len(c.CallOptions.UpdateServiceLevelObjective):len(c.CallOptions.UpdateServiceLevelObjective)], opts...)
388	var resp *monitoringpb.ServiceLevelObjective
389	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
390		var err error
391		resp, err = c.serviceMonitoringClient.UpdateServiceLevelObjective(ctx, req, settings.GRPC...)
392		return err
393	}, opts...)
394	if err != nil {
395		return nil, err
396	}
397	return resp, nil
398}
399
400// DeleteServiceLevelObjective delete the given ServiceLevelObjective.
401func (c *ServiceMonitoringClient) DeleteServiceLevelObjective(ctx context.Context, req *monitoringpb.DeleteServiceLevelObjectiveRequest, opts ...gax.CallOption) error {
402	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
403	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
404	opts = append(c.CallOptions.DeleteServiceLevelObjective[0:len(c.CallOptions.DeleteServiceLevelObjective):len(c.CallOptions.DeleteServiceLevelObjective)], opts...)
405	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
406		var err error
407		_, err = c.serviceMonitoringClient.DeleteServiceLevelObjective(ctx, req, settings.GRPC...)
408		return err
409	}, opts...)
410	return err
411}
412
413// ServiceIterator manages a stream of *monitoringpb.Service.
414type ServiceIterator struct {
415	items    []*monitoringpb.Service
416	pageInfo *iterator.PageInfo
417	nextFunc func() error
418
419	// Response is the raw response for the current page.
420	// It must be cast to the RPC response type.
421	// Calling Next() or InternalFetch() updates this value.
422	Response interface{}
423
424	// InternalFetch is for use by the Google Cloud Libraries only.
425	// It is not part of the stable interface of this package.
426	//
427	// InternalFetch returns results from a single call to the underlying RPC.
428	// The number of results is no greater than pageSize.
429	// If there are no more results, nextPageToken is empty and err is nil.
430	InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.Service, nextPageToken string, err error)
431}
432
433// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
434func (it *ServiceIterator) PageInfo() *iterator.PageInfo {
435	return it.pageInfo
436}
437
438// Next returns the next result. Its second return value is iterator.Done if there are no more
439// results. Once Next returns Done, all subsequent calls will return Done.
440func (it *ServiceIterator) Next() (*monitoringpb.Service, error) {
441	var item *monitoringpb.Service
442	if err := it.nextFunc(); err != nil {
443		return item, err
444	}
445	item = it.items[0]
446	it.items = it.items[1:]
447	return item, nil
448}
449
450func (it *ServiceIterator) bufLen() int {
451	return len(it.items)
452}
453
454func (it *ServiceIterator) takeBuf() interface{} {
455	b := it.items
456	it.items = nil
457	return b
458}
459
460// ServiceLevelObjectiveIterator manages a stream of *monitoringpb.ServiceLevelObjective.
461type ServiceLevelObjectiveIterator struct {
462	items    []*monitoringpb.ServiceLevelObjective
463	pageInfo *iterator.PageInfo
464	nextFunc func() error
465
466	// Response is the raw response for the current page.
467	// It must be cast to the RPC response type.
468	// Calling Next() or InternalFetch() updates this value.
469	Response interface{}
470
471	// InternalFetch is for use by the Google Cloud Libraries only.
472	// It is not part of the stable interface of this package.
473	//
474	// InternalFetch returns results from a single call to the underlying RPC.
475	// The number of results is no greater than pageSize.
476	// If there are no more results, nextPageToken is empty and err is nil.
477	InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.ServiceLevelObjective, nextPageToken string, err error)
478}
479
480// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
481func (it *ServiceLevelObjectiveIterator) PageInfo() *iterator.PageInfo {
482	return it.pageInfo
483}
484
485// Next returns the next result. Its second return value is iterator.Done if there are no more
486// results. Once Next returns Done, all subsequent calls will return Done.
487func (it *ServiceLevelObjectiveIterator) Next() (*monitoringpb.ServiceLevelObjective, error) {
488	var item *monitoringpb.ServiceLevelObjective
489	if err := it.nextFunc(); err != nil {
490		return item, err
491	}
492	item = it.items[0]
493	it.items = it.items[1:]
494	return item, nil
495}
496
497func (it *ServiceLevelObjectiveIterator) bufLen() int {
498	return len(it.items)
499}
500
501func (it *ServiceLevelObjectiveIterator) takeBuf() interface{} {
502	b := it.items
503	it.items = nil
504	return b
505}
506